0a0244802d87c6571c0d6cb60a248bb0c5571bfe
[babeltrace.git] / src / cpp-common / bt2 / field-path.hpp
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"
14 #include "common-iter.hpp"
15 #include "internal/borrowed-obj.hpp"
16 #include "internal/shared-obj.hpp"
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 {
71 return bt_field_path_item_get_type(this->libObjPtr());
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 {
97 return bt_field_path_item_index_get_index(this->libObjPtr());
98 }
99 };
100
101 inline ConstIndexFieldPathItem ConstFieldPathItem::asIndex() const noexcept
102 {
103 BT_ASSERT_DBG(this->isIndex());
104 return ConstIndexFieldPathItem {this->libObjPtr()};
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
122 } /* namespace internal */
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
130 using Iterator = CommonIterator<ConstFieldPath, ConstFieldPathItem>;
131
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
144 ConstFieldPath(const ConstFieldPath& fieldPath) noexcept : _ThisBorrowedObj {fieldPath}
145 {
146 }
147
148 ConstFieldPath& operator=(const ConstFieldPath& fieldPath) noexcept
149 {
150 _ThisBorrowedObj::operator=(fieldPath);
151 return *this;
152 }
153
154 Scope rootScope() const noexcept
155 {
156 return static_cast<Scope>(bt_field_path_get_root_scope(this->libObjPtr()));
157 }
158
159 std::uint64_t size() const noexcept
160 {
161 return bt_field_path_get_item_count(this->libObjPtr());
162 }
163
164 ConstFieldPathItem operator[](const std::uint64_t index) const noexcept
165 {
166 return ConstFieldPathItem {
167 bt_field_path_borrow_item_by_index_const(this->libObjPtr(), index)};
168 }
169
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
180 Shared shared() const noexcept
181 {
182 return Shared::createWithRef(*this);
183 }
184 };
185
186 } /* namespace bt2 */
187
188 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP */
This page took 0.032942 seconds and 3 git commands to generate.