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-object-iterator.hpp"
17 #include "borrowed-object.hpp"
18 #include "shared-object.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 BorrowedObject<const bt_field_path_item>
34 explicit ConstFieldPathItem(const LibObjPtr libObjPtr) noexcept :
35 _ThisBorrowedObject {libObjPtr}
39 FieldPathItemType type() const noexcept
41 return static_cast<FieldPathItemType>(this->_libType());
44 bool isIndex() const noexcept
46 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_INDEX;
49 bool isCurrentArrayElement() const noexcept
51 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT;
54 bool isCurrentOptionContent() const noexcept
56 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT;
59 ConstIndexFieldPathItem asIndex() const noexcept;
62 bt_field_path_item_type _libType() const noexcept
64 return bt_field_path_item_get_type(this->libObjPtr());
68 class ConstIndexFieldPathItem final : public ConstFieldPathItem
71 explicit ConstIndexFieldPathItem(const LibObjPtr libObjPtr) noexcept :
72 ConstFieldPathItem {libObjPtr}
74 BT_ASSERT_DBG(this->isIndex());
77 std::uint64_t index() const noexcept
79 return bt_field_path_item_index_get_index(this->libObjPtr());
83 inline ConstIndexFieldPathItem ConstFieldPathItem::asIndex() const noexcept
85 BT_ASSERT_DBG(this->isIndex());
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 class ConstFieldPath final : public BorrowedObject<const bt_field_path>
109 using Shared = SharedObject<ConstFieldPath, const bt_field_path, internal::FieldPathRefFuncs>;
110 using Iterator = BorrowedObjectIterator<ConstFieldPath>;
114 PACKET_CONTEXT = BT_FIELD_PATH_SCOPE_PACKET_CONTEXT,
115 EVENT_COMMON_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT,
116 EVENT_SPECIFIC_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT,
117 EVENT_PAYLOAD = BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD,
120 explicit ConstFieldPath(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
124 Scope rootScope() const noexcept
126 return static_cast<Scope>(bt_field_path_get_root_scope(this->libObjPtr()));
129 std::uint64_t length() const noexcept
131 return bt_field_path_get_item_count(this->libObjPtr());
134 ConstFieldPathItem operator[](const std::uint64_t index) const noexcept
136 return ConstFieldPathItem {
137 bt_field_path_borrow_item_by_index_const(this->libObjPtr(), index)};
140 Iterator begin() const noexcept
142 return Iterator {*this, 0};
145 Iterator end() const noexcept
147 return Iterator {*this, this->length()};
150 Shared shared() const noexcept
152 return Shared::createWithRef(*this);
156 } /* namespace bt2 */
158 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP */