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