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