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