cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[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
12 #include <babeltrace2/babeltrace.h>
13
14 #include "common/assert.h"
15 #include "cpp-common/vendor/wise-enum/wise_enum.h"
16
17 #include "borrowed-object-iterator.hpp"
18 #include "borrowed-object.hpp"
19 #include "shared-object.hpp"
20
21 namespace bt2 {
22
23 class ConstIndexFieldPathItem;
24
25 enum class FieldPathItemType
26 {
27 Index = BT_FIELD_PATH_ITEM_TYPE_INDEX,
28 CurrentArrayElement = BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT,
29 CurrentOptionContent = BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT,
30 };
31
32 class ConstFieldPathItem : public BorrowedObject<const bt_field_path_item>
33 {
34 public:
35 explicit ConstFieldPathItem(const LibObjPtr libObjPtr) noexcept :
36 _ThisBorrowedObject {libObjPtr}
37 {
38 }
39
40 FieldPathItemType type() const noexcept
41 {
42 return static_cast<FieldPathItemType>(this->_libType());
43 }
44
45 bool isIndex() const noexcept
46 {
47 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_INDEX;
48 }
49
50 bool isCurrentArrayElement() const noexcept
51 {
52 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT;
53 }
54
55 bool isCurrentOptionContent() const noexcept
56 {
57 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT;
58 }
59
60 ConstIndexFieldPathItem asIndex() const noexcept;
61
62 private:
63 bt_field_path_item_type _libType() const noexcept
64 {
65 return bt_field_path_item_get_type(this->libObjPtr());
66 }
67 };
68
69 class ConstIndexFieldPathItem final : public ConstFieldPathItem
70 {
71 public:
72 explicit ConstIndexFieldPathItem(const LibObjPtr libObjPtr) noexcept :
73 ConstFieldPathItem {libObjPtr}
74 {
75 BT_ASSERT_DBG(this->isIndex());
76 }
77
78 std::uint64_t index() const noexcept
79 {
80 return bt_field_path_item_index_get_index(this->libObjPtr());
81 }
82 };
83
84 inline ConstIndexFieldPathItem ConstFieldPathItem::asIndex() const noexcept
85 {
86 return ConstIndexFieldPathItem {this->libObjPtr()};
87 }
88
89 namespace internal {
90
91 struct FieldPathRefFuncs final
92 {
93 static void get(const bt_field_path * const libObjPtr) noexcept
94 {
95 bt_field_path_get_ref(libObjPtr);
96 }
97
98 static void put(const bt_field_path * const libObjPtr) noexcept
99 {
100 bt_field_path_put_ref(libObjPtr);
101 }
102 };
103
104 } /* namespace internal */
105
106 /* clang-format off */
107
108 WISE_ENUM_CLASS(FieldPathScope,
109 (PacketContext, BT_FIELD_PATH_SCOPE_PACKET_CONTEXT),
110 (EventCommonContext, BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT),
111 (EventSpecificContext, BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT),
112 (EventPayload, BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD))
113
114 /* clang-format on */
115
116 class ConstFieldPath final : public BorrowedObject<const bt_field_path>
117 {
118 public:
119 using Shared = SharedObject<ConstFieldPath, const bt_field_path, internal::FieldPathRefFuncs>;
120 using Iterator = BorrowedObjectIterator<ConstFieldPath>;
121
122 explicit ConstFieldPath(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
123 {
124 }
125
126 FieldPathScope rootScope() const noexcept
127 {
128 return static_cast<FieldPathScope>(bt_field_path_get_root_scope(this->libObjPtr()));
129 }
130
131 std::uint64_t length() const noexcept
132 {
133 return bt_field_path_get_item_count(this->libObjPtr());
134 }
135
136 ConstFieldPathItem operator[](const std::uint64_t index) const noexcept
137 {
138 return ConstFieldPathItem {
139 bt_field_path_borrow_item_by_index_const(this->libObjPtr(), index)};
140 }
141
142 Iterator begin() const noexcept
143 {
144 return Iterator {*this, 0};
145 }
146
147 Iterator end() const noexcept
148 {
149 return Iterator {*this, this->length()};
150 }
151
152 Shared shared() const noexcept
153 {
154 return Shared::createWithRef(*this);
155 }
156 };
157
158 } /* namespace bt2 */
159
160 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP */
This page took 0.033081 seconds and 4 git commands to generate.