Commit | Line | Data |
---|---|---|
40ed8b01 PP |
1 | /* |
2 | * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP | |
9 | ||
10 | #include <cstdint> | |
c802cacb | 11 | |
40ed8b01 PP |
12 | #include <babeltrace2/babeltrace.h> |
13 | ||
14 | #include "common/assert.h" | |
c802cacb | 15 | |
56862ee2 | 16 | #include "borrowed-object-iterator.hpp" |
0d218157 | 17 | #include "borrowed-object.hpp" |
7f5cdaf0 | 18 | #include "shared-object.hpp" |
40ed8b01 PP |
19 | |
20 | namespace bt2 { | |
21 | ||
22 | class ConstIndexFieldPathItem; | |
23 | ||
24 | enum class FieldPathItemType | |
25 | { | |
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, | |
29 | }; | |
30 | ||
0d218157 | 31 | class ConstFieldPathItem : public BorrowedObject<const bt_field_path_item> |
40ed8b01 PP |
32 | { |
33 | public: | |
0d218157 PP |
34 | explicit ConstFieldPathItem(const _LibObjPtr libObjPtr) noexcept : |
35 | _ThisBorrowedObject {libObjPtr} | |
40ed8b01 PP |
36 | { |
37 | } | |
38 | ||
0d218157 | 39 | ConstFieldPathItem(const ConstFieldPathItem& fpItem) noexcept : _ThisBorrowedObject {fpItem} |
40ed8b01 PP |
40 | { |
41 | } | |
42 | ||
43 | ConstFieldPathItem& operator=(const ConstFieldPathItem& fpItem) noexcept | |
44 | { | |
0d218157 | 45 | _ThisBorrowedObject::operator=(fpItem); |
40ed8b01 PP |
46 | return *this; |
47 | } | |
48 | ||
49 | FieldPathItemType type() const noexcept | |
50 | { | |
51 | return static_cast<FieldPathItemType>(this->_libType()); | |
52 | } | |
53 | ||
54 | bool isIndex() const noexcept | |
55 | { | |
56 | return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_INDEX; | |
57 | } | |
58 | ||
59 | bool isCurrentArrayElement() const noexcept | |
60 | { | |
61 | return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT; | |
62 | } | |
63 | ||
64 | bool isCurrentOptionContent() const noexcept | |
65 | { | |
66 | return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT; | |
67 | } | |
68 | ||
69 | ConstIndexFieldPathItem asIndex() const noexcept; | |
70 | ||
71 | private: | |
72 | bt_field_path_item_type _libType() const noexcept | |
73 | { | |
341a67c4 | 74 | return bt_field_path_item_get_type(this->libObjPtr()); |
40ed8b01 PP |
75 | } |
76 | }; | |
77 | ||
78 | class ConstIndexFieldPathItem final : public ConstFieldPathItem | |
79 | { | |
80 | public: | |
81 | explicit ConstIndexFieldPathItem(const _LibObjPtr libObjPtr) noexcept : | |
82 | ConstFieldPathItem {libObjPtr} | |
83 | { | |
84 | BT_ASSERT_DBG(this->isIndex()); | |
85 | } | |
86 | ||
87 | ConstIndexFieldPathItem(const ConstIndexFieldPathItem& fpItem) noexcept : | |
88 | ConstFieldPathItem {fpItem} | |
89 | { | |
90 | } | |
91 | ||
92 | ConstIndexFieldPathItem& operator=(const ConstIndexFieldPathItem& fpItem) noexcept | |
93 | { | |
94 | ConstFieldPathItem::operator=(fpItem); | |
95 | return *this; | |
96 | } | |
97 | ||
98 | std::uint64_t index() const noexcept | |
99 | { | |
341a67c4 | 100 | return bt_field_path_item_index_get_index(this->libObjPtr()); |
40ed8b01 PP |
101 | } |
102 | }; | |
103 | ||
104 | inline ConstIndexFieldPathItem ConstFieldPathItem::asIndex() const noexcept | |
105 | { | |
106 | BT_ASSERT_DBG(this->isIndex()); | |
341a67c4 | 107 | return ConstIndexFieldPathItem {this->libObjPtr()}; |
40ed8b01 PP |
108 | } |
109 | ||
110 | namespace internal { | |
111 | ||
112 | struct FieldPathRefFuncs final | |
113 | { | |
c677c492 | 114 | static void get(const bt_field_path * const libObjPtr) noexcept |
40ed8b01 PP |
115 | { |
116 | bt_field_path_get_ref(libObjPtr); | |
117 | } | |
118 | ||
c677c492 | 119 | static void put(const bt_field_path * const libObjPtr) noexcept |
40ed8b01 PP |
120 | { |
121 | bt_field_path_put_ref(libObjPtr); | |
122 | } | |
123 | }; | |
124 | ||
b5f55e9f | 125 | } /* namespace internal */ |
40ed8b01 | 126 | |
0d218157 | 127 | class ConstFieldPath final : public BorrowedObject<const bt_field_path> |
40ed8b01 PP |
128 | { |
129 | public: | |
7f5cdaf0 | 130 | using Shared = SharedObject<ConstFieldPath, const bt_field_path, internal::FieldPathRefFuncs>; |
56862ee2 | 131 | using Iterator = BorrowedObjectIterator<ConstFieldPath>; |
b3a04931 | 132 | |
40ed8b01 PP |
133 | enum class Scope |
134 | { | |
135 | PACKET_CONTEXT = BT_FIELD_PATH_SCOPE_PACKET_CONTEXT, | |
136 | EVENT_COMMON_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT, | |
137 | EVENT_SPECIFIC_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT, | |
138 | EVENT_PAYLOAD = BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD, | |
139 | }; | |
140 | ||
0d218157 | 141 | explicit ConstFieldPath(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} |
40ed8b01 PP |
142 | { |
143 | } | |
144 | ||
0d218157 | 145 | ConstFieldPath(const ConstFieldPath& fieldPath) noexcept : _ThisBorrowedObject {fieldPath} |
40ed8b01 PP |
146 | { |
147 | } | |
148 | ||
151c78ca | 149 | ConstFieldPath& operator=(const ConstFieldPath& fieldPath) noexcept |
40ed8b01 | 150 | { |
0d218157 | 151 | _ThisBorrowedObject::operator=(fieldPath); |
40ed8b01 PP |
152 | return *this; |
153 | } | |
154 | ||
155 | Scope rootScope() const noexcept | |
156 | { | |
341a67c4 | 157 | return static_cast<Scope>(bt_field_path_get_root_scope(this->libObjPtr())); |
40ed8b01 PP |
158 | } |
159 | ||
c0b73c63 | 160 | std::uint64_t length() const noexcept |
40ed8b01 | 161 | { |
341a67c4 | 162 | return bt_field_path_get_item_count(this->libObjPtr()); |
40ed8b01 PP |
163 | } |
164 | ||
165 | ConstFieldPathItem operator[](const std::uint64_t index) const noexcept | |
166 | { | |
167 | return ConstFieldPathItem { | |
341a67c4 | 168 | bt_field_path_borrow_item_by_index_const(this->libObjPtr(), index)}; |
40ed8b01 PP |
169 | } |
170 | ||
b3a04931 FD |
171 | Iterator begin() const noexcept |
172 | { | |
173 | return Iterator {*this, 0}; | |
174 | } | |
175 | ||
176 | Iterator end() const noexcept | |
177 | { | |
c0b73c63 | 178 | return Iterator {*this, this->length()}; |
b3a04931 FD |
179 | } |
180 | ||
40ed8b01 PP |
181 | Shared shared() const noexcept |
182 | { | |
c9c0b6e2 | 183 | return Shared::createWithRef(*this); |
40ed8b01 PP |
184 | } |
185 | }; | |
186 | ||
b5f55e9f | 187 | } /* namespace bt2 */ |
40ed8b01 | 188 | |
b5f55e9f | 189 | #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP */ |