cpp-common: Expose BorrowedObj::libObjPtr() as public method
[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 #include "internal/shared-obj.hpp"
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 {
70 return bt_field_path_item_get_type(this->libObjPtr());
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 {
96 return bt_field_path_item_index_get_index(this->libObjPtr());
97 }
98 };
99
100 inline ConstIndexFieldPathItem ConstFieldPathItem::asIndex() const noexcept
101 {
102 BT_ASSERT_DBG(this->isIndex());
103 return ConstIndexFieldPathItem {this->libObjPtr()};
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
121 } /* namespace internal */
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
141 ConstFieldPath(const ConstFieldPath& fieldPath) noexcept : _ThisBorrowedObj {fieldPath}
142 {
143 }
144
145 ConstFieldPath& operator=(const ConstFieldPath& fieldPath) noexcept
146 {
147 _ThisBorrowedObj::operator=(fieldPath);
148 return *this;
149 }
150
151 Scope rootScope() const noexcept
152 {
153 return static_cast<Scope>(bt_field_path_get_root_scope(this->libObjPtr()));
154 }
155
156 std::uint64_t size() const noexcept
157 {
158 return bt_field_path_get_item_count(this->libObjPtr());
159 }
160
161 ConstFieldPathItem operator[](const std::uint64_t index) const noexcept
162 {
163 return ConstFieldPathItem {
164 bt_field_path_borrow_item_by_index_const(this->libObjPtr(), index)};
165 }
166
167 Shared shared() const noexcept
168 {
169 return Shared {*this};
170 }
171 };
172
173 } /* namespace bt2 */
174
175 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP */
This page took 0.03442 seconds and 5 git commands to generate.