2 * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP
12 #include <babeltrace2/babeltrace.h>
14 #include "common/assert.h"
15 #include "cpp-common/vendor/wise-enum/wise_enum.h"
17 #include "borrowed-object-iterator.hpp"
18 #include "borrowed-object.hpp"
19 #include "shared-object.hpp"
23 class ConstIndexFieldPathItem;
25 enum class FieldPathItemType
27 Index = BT_FIELD_PATH_ITEM_TYPE_INDEX,
28 CurrentArrayElement = BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT,
29 CurrentOptionContent = BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT,
32 class ConstFieldPathItem : public BorrowedObject<const bt_field_path_item>
35 explicit ConstFieldPathItem(const LibObjPtr libObjPtr) noexcept :
36 _ThisBorrowedObject {libObjPtr}
40 FieldPathItemType type() const noexcept
42 return static_cast<FieldPathItemType>(this->_libType());
45 bool isIndex() const noexcept
47 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_INDEX;
50 bool isCurrentArrayElement() const noexcept
52 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT;
55 bool isCurrentOptionContent() const noexcept
57 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT;
60 ConstIndexFieldPathItem asIndex() const noexcept;
63 bt_field_path_item_type _libType() const noexcept
65 return bt_field_path_item_get_type(this->libObjPtr());
69 class ConstIndexFieldPathItem final : public ConstFieldPathItem
72 explicit ConstIndexFieldPathItem(const LibObjPtr libObjPtr) noexcept :
73 ConstFieldPathItem {libObjPtr}
75 BT_ASSERT_DBG(this->isIndex());
78 std::uint64_t index() const noexcept
80 return bt_field_path_item_index_get_index(this->libObjPtr());
84 inline ConstIndexFieldPathItem ConstFieldPathItem::asIndex() const noexcept
86 return ConstIndexFieldPathItem {this->libObjPtr()};
91 struct FieldPathRefFuncs final
93 static void get(const bt_field_path * const libObjPtr) noexcept
95 bt_field_path_get_ref(libObjPtr);
98 static void put(const bt_field_path * const libObjPtr) noexcept
100 bt_field_path_put_ref(libObjPtr);
104 } /* namespace internal */
106 /* clang-format off */
108 WISE_ENUM_CLASS(FieldPathScope,
109 (PacketContext, BT_FIELD_PATH_SCOPE_PACKET_CONTEXT),
110 (EventCommonContext, BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT),
111 (EventSpecificContext, BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT),
112 (EventPayload, BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD))
114 /* clang-format on */
116 class ConstFieldPath final : public BorrowedObject<const bt_field_path>
119 using Shared = SharedObject<ConstFieldPath, const bt_field_path, internal::FieldPathRefFuncs>;
120 using Iterator = BorrowedObjectIterator<ConstFieldPath>;
122 explicit ConstFieldPath(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
126 FieldPathScope rootScope() const noexcept
128 return static_cast<FieldPathScope>(bt_field_path_get_root_scope(this->libObjPtr()));
131 std::uint64_t length() const noexcept
133 return bt_field_path_get_item_count(this->libObjPtr());
136 ConstFieldPathItem operator[](const std::uint64_t index) const noexcept
138 return ConstFieldPathItem {
139 bt_field_path_borrow_item_by_index_const(this->libObjPtr(), index)};
142 Iterator begin() const noexcept
144 return Iterator {*this, 0};
147 Iterator end() const noexcept
149 return Iterator {*this, this->length()};
152 Shared shared() const noexcept
154 return Shared::createWithRef(*this);
158 } /* namespace bt2 */
160 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP */