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"
16 #include "borrowed-obj.hpp"
17 #include "common-iter.hpp"
18 #include "internal/shared-obj.hpp"
22 class ConstIndexFieldPathItem;
24 enum class FieldPathItemType
26 INDEX = BT_FIELD_PATH_ITEM_TYPE_INDEX,
27 CURRENT_ARRAY_ELEMENT = BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT,
28 CURRENT_OPTION_CONTENT = BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT,
31 class ConstFieldPathItem : public BorrowedObj<const bt_field_path_item>
34 explicit ConstFieldPathItem(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
38 ConstFieldPathItem(const ConstFieldPathItem& fpItem) noexcept : _ThisBorrowedObj {fpItem}
42 ConstFieldPathItem& operator=(const ConstFieldPathItem& fpItem) noexcept
44 _ThisBorrowedObj::operator=(fpItem);
48 FieldPathItemType type() const noexcept
50 return static_cast<FieldPathItemType>(this->_libType());
53 bool isIndex() const noexcept
55 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_INDEX;
58 bool isCurrentArrayElement() const noexcept
60 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT;
63 bool isCurrentOptionContent() const noexcept
65 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT;
68 ConstIndexFieldPathItem asIndex() const noexcept;
71 bt_field_path_item_type _libType() const noexcept
73 return bt_field_path_item_get_type(this->libObjPtr());
77 class ConstIndexFieldPathItem final : public ConstFieldPathItem
80 explicit ConstIndexFieldPathItem(const _LibObjPtr libObjPtr) noexcept :
81 ConstFieldPathItem {libObjPtr}
83 BT_ASSERT_DBG(this->isIndex());
86 ConstIndexFieldPathItem(const ConstIndexFieldPathItem& fpItem) noexcept :
87 ConstFieldPathItem {fpItem}
91 ConstIndexFieldPathItem& operator=(const ConstIndexFieldPathItem& fpItem) noexcept
93 ConstFieldPathItem::operator=(fpItem);
97 std::uint64_t index() const noexcept
99 return bt_field_path_item_index_get_index(this->libObjPtr());
103 inline ConstIndexFieldPathItem ConstFieldPathItem::asIndex() const noexcept
105 BT_ASSERT_DBG(this->isIndex());
106 return ConstIndexFieldPathItem {this->libObjPtr()};
111 struct FieldPathRefFuncs final
113 static void get(const bt_field_path * const libObjPtr)
115 bt_field_path_get_ref(libObjPtr);
118 static void put(const bt_field_path * const libObjPtr)
120 bt_field_path_put_ref(libObjPtr);
124 } /* namespace internal */
126 class ConstFieldPath final : public BorrowedObj<const bt_field_path>
130 internal::SharedObj<ConstFieldPath, const bt_field_path, internal::FieldPathRefFuncs>;
132 using Iterator = CommonIterator<ConstFieldPath, ConstFieldPathItem>;
136 PACKET_CONTEXT = BT_FIELD_PATH_SCOPE_PACKET_CONTEXT,
137 EVENT_COMMON_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT,
138 EVENT_SPECIFIC_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT,
139 EVENT_PAYLOAD = BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD,
142 explicit ConstFieldPath(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
146 ConstFieldPath(const ConstFieldPath& fieldPath) noexcept : _ThisBorrowedObj {fieldPath}
150 ConstFieldPath& operator=(const ConstFieldPath& fieldPath) noexcept
152 _ThisBorrowedObj::operator=(fieldPath);
156 Scope rootScope() const noexcept
158 return static_cast<Scope>(bt_field_path_get_root_scope(this->libObjPtr()));
161 std::uint64_t size() const noexcept
163 return bt_field_path_get_item_count(this->libObjPtr());
166 ConstFieldPathItem operator[](const std::uint64_t index) const noexcept
168 return ConstFieldPathItem {
169 bt_field_path_borrow_item_by_index_const(this->libObjPtr(), index)};
172 Iterator begin() const noexcept
174 return Iterator {*this, 0};
177 Iterator end() const noexcept
179 return Iterator {*this, this->size()};
182 Shared shared() const noexcept
184 return Shared::createWithRef(*this);
188 } /* namespace bt2 */
190 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP */