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> | |
c802cacb | 11 | |
40ed8b01 PP |
12 | #include <babeltrace2/babeltrace.h> |
13 | ||
14 | #include "common/assert.h" | |
f0b83426 | 15 | #include "cpp-common/vendor/wise-enum/wise_enum.h" |
c802cacb | 16 | |
56862ee2 | 17 | #include "borrowed-object-iterator.hpp" |
0d218157 | 18 | #include "borrowed-object.hpp" |
7f5cdaf0 | 19 | #include "shared-object.hpp" |
40ed8b01 PP |
20 | |
21 | namespace bt2 { | |
22 | ||
23 | class ConstIndexFieldPathItem; | |
24 | ||
25 | enum class FieldPathItemType | |
26 | { | |
1c5ea5eb SM |
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, | |
40ed8b01 PP |
30 | }; |
31 | ||
0d218157 | 32 | class ConstFieldPathItem : public BorrowedObject<const bt_field_path_item> |
40ed8b01 PP |
33 | { |
34 | public: | |
d246c457 | 35 | explicit ConstFieldPathItem(const LibObjPtr libObjPtr) noexcept : |
0d218157 | 36 | _ThisBorrowedObject {libObjPtr} |
40ed8b01 PP |
37 | { |
38 | } | |
39 | ||
40ed8b01 PP |
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 | { | |
341a67c4 | 65 | return bt_field_path_item_get_type(this->libObjPtr()); |
40ed8b01 PP |
66 | } |
67 | }; | |
68 | ||
69 | class ConstIndexFieldPathItem final : public ConstFieldPathItem | |
70 | { | |
71 | public: | |
d246c457 | 72 | explicit ConstIndexFieldPathItem(const LibObjPtr libObjPtr) noexcept : |
40ed8b01 PP |
73 | ConstFieldPathItem {libObjPtr} |
74 | { | |
75 | BT_ASSERT_DBG(this->isIndex()); | |
76 | } | |
77 | ||
40ed8b01 PP |
78 | std::uint64_t index() const noexcept |
79 | { | |
341a67c4 | 80 | return bt_field_path_item_index_get_index(this->libObjPtr()); |
40ed8b01 PP |
81 | } |
82 | }; | |
83 | ||
84 | inline ConstIndexFieldPathItem ConstFieldPathItem::asIndex() const noexcept | |
85 | { | |
341a67c4 | 86 | return ConstIndexFieldPathItem {this->libObjPtr()}; |
40ed8b01 PP |
87 | } |
88 | ||
89 | namespace internal { | |
90 | ||
91 | struct FieldPathRefFuncs final | |
92 | { | |
c677c492 | 93 | static void get(const bt_field_path * const libObjPtr) noexcept |
40ed8b01 PP |
94 | { |
95 | bt_field_path_get_ref(libObjPtr); | |
96 | } | |
97 | ||
c677c492 | 98 | static void put(const bt_field_path * const libObjPtr) noexcept |
40ed8b01 PP |
99 | { |
100 | bt_field_path_put_ref(libObjPtr); | |
101 | } | |
102 | }; | |
103 | ||
b5f55e9f | 104 | } /* namespace internal */ |
40ed8b01 | 105 | |
f0b83426 SM |
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 */ | |
33f20928 | 115 | |
0d218157 | 116 | class ConstFieldPath final : public BorrowedObject<const bt_field_path> |
40ed8b01 PP |
117 | { |
118 | public: | |
7f5cdaf0 | 119 | using Shared = SharedObject<ConstFieldPath, const bt_field_path, internal::FieldPathRefFuncs>; |
56862ee2 | 120 | using Iterator = BorrowedObjectIterator<ConstFieldPath>; |
b3a04931 | 121 | |
d246c457 | 122 | explicit ConstFieldPath(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} |
40ed8b01 PP |
123 | { |
124 | } | |
125 | ||
33f20928 | 126 | FieldPathScope rootScope() const noexcept |
40ed8b01 | 127 | { |
33f20928 | 128 | return static_cast<FieldPathScope>(bt_field_path_get_root_scope(this->libObjPtr())); |
40ed8b01 PP |
129 | } |
130 | ||
c0b73c63 | 131 | std::uint64_t length() const noexcept |
40ed8b01 | 132 | { |
341a67c4 | 133 | return bt_field_path_get_item_count(this->libObjPtr()); |
40ed8b01 PP |
134 | } |
135 | ||
136 | ConstFieldPathItem operator[](const std::uint64_t index) const noexcept | |
137 | { | |
138 | return ConstFieldPathItem { | |
341a67c4 | 139 | bt_field_path_borrow_item_by_index_const(this->libObjPtr(), index)}; |
40ed8b01 PP |
140 | } |
141 | ||
b3a04931 FD |
142 | Iterator begin() const noexcept |
143 | { | |
144 | return Iterator {*this, 0}; | |
145 | } | |
146 | ||
147 | Iterator end() const noexcept | |
148 | { | |
c0b73c63 | 149 | return Iterator {*this, this->length()}; |
b3a04931 FD |
150 | } |
151 | ||
40ed8b01 PP |
152 | Shared shared() const noexcept |
153 | { | |
c9c0b6e2 | 154 | return Shared::createWithRef(*this); |
40ed8b01 PP |
155 | } |
156 | }; | |
157 | ||
b5f55e9f | 158 | } /* namespace bt2 */ |
40ed8b01 | 159 | |
b5f55e9f | 160 | #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP */ |