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
11 #include <babeltrace2/babeltrace.h>
13 #include "common/assert.h"
14 #include "common-iter.hpp"
15 #include "internal/borrowed-obj.hpp"
16 #include "internal/shared-obj.hpp"
20 class ConstIndexFieldPathItem;
22 enum class FieldPathItemType
24 INDEX = BT_FIELD_PATH_ITEM_TYPE_INDEX,
25 CURRENT_ARRAY_ELEMENT = BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT,
26 CURRENT_OPTION_CONTENT = BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT,
29 class ConstFieldPathItem : public internal::BorrowedObj<const bt_field_path_item>
32 explicit ConstFieldPathItem(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
36 ConstFieldPathItem(const ConstFieldPathItem& fpItem) noexcept : _ThisBorrowedObj {fpItem}
40 ConstFieldPathItem& operator=(const ConstFieldPathItem& fpItem) noexcept
42 _ThisBorrowedObj::operator=(fpItem);
46 FieldPathItemType type() const noexcept
48 return static_cast<FieldPathItemType>(this->_libType());
51 bool isIndex() const noexcept
53 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_INDEX;
56 bool isCurrentArrayElement() const noexcept
58 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT;
61 bool isCurrentOptionContent() const noexcept
63 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT;
66 ConstIndexFieldPathItem asIndex() const noexcept;
69 bt_field_path_item_type _libType() const noexcept
71 return bt_field_path_item_get_type(this->libObjPtr());
75 class ConstIndexFieldPathItem final : public ConstFieldPathItem
78 explicit ConstIndexFieldPathItem(const _LibObjPtr libObjPtr) noexcept :
79 ConstFieldPathItem {libObjPtr}
81 BT_ASSERT_DBG(this->isIndex());
84 ConstIndexFieldPathItem(const ConstIndexFieldPathItem& fpItem) noexcept :
85 ConstFieldPathItem {fpItem}
89 ConstIndexFieldPathItem& operator=(const ConstIndexFieldPathItem& fpItem) noexcept
91 ConstFieldPathItem::operator=(fpItem);
95 std::uint64_t index() const noexcept
97 return bt_field_path_item_index_get_index(this->libObjPtr());
101 inline ConstIndexFieldPathItem ConstFieldPathItem::asIndex() const noexcept
103 BT_ASSERT_DBG(this->isIndex());
104 return ConstIndexFieldPathItem {this->libObjPtr()};
109 struct FieldPathRefFuncs final
111 static void get(const bt_field_path * const libObjPtr)
113 bt_field_path_get_ref(libObjPtr);
116 static void put(const bt_field_path * const libObjPtr)
118 bt_field_path_put_ref(libObjPtr);
122 } /* namespace internal */
124 class ConstFieldPath final : public internal::BorrowedObj<const bt_field_path>
128 internal::SharedObj<ConstFieldPath, const bt_field_path, internal::FieldPathRefFuncs>;
130 using Iterator = CommonIterator<ConstFieldPath, ConstFieldPathItem>;
134 PACKET_CONTEXT = BT_FIELD_PATH_SCOPE_PACKET_CONTEXT,
135 EVENT_COMMON_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT,
136 EVENT_SPECIFIC_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT,
137 EVENT_PAYLOAD = BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD,
140 explicit ConstFieldPath(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
144 ConstFieldPath(const ConstFieldPath& fieldPath) noexcept : _ThisBorrowedObj {fieldPath}
148 ConstFieldPath& operator=(const ConstFieldPath& fieldPath) noexcept
150 _ThisBorrowedObj::operator=(fieldPath);
154 Scope rootScope() const noexcept
156 return static_cast<Scope>(bt_field_path_get_root_scope(this->libObjPtr()));
159 std::uint64_t size() const noexcept
161 return bt_field_path_get_item_count(this->libObjPtr());
164 ConstFieldPathItem operator[](const std::uint64_t index) const noexcept
166 return ConstFieldPathItem {
167 bt_field_path_borrow_item_by_index_const(this->libObjPtr(), index)};
170 Iterator begin() const noexcept
172 return Iterator {*this, 0};
175 Iterator end() const noexcept
177 return Iterator {*this, this->size()};
180 Shared shared() const noexcept
182 return Shared::createWithRef(*this);
186 } /* namespace bt2 */
188 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP */