cpp-common/bt2: make `bt2::BorrowedObject::LibObj` public
[babeltrace.git] / src / cpp-common / bt2 / field-path.hpp
CommitLineData
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"
c802cacb 15
56862ee2 16#include "borrowed-object-iterator.hpp"
0d218157 17#include "borrowed-object.hpp"
7f5cdaf0 18#include "shared-object.hpp"
40ed8b01
PP
19
20namespace bt2 {
21
22class ConstIndexFieldPathItem;
23
24enum class FieldPathItemType
25{
26 INDEX = BT_FIELD_PATH_ITEM_TYPE_INDEX,
27 CURRENT_ARRAY_ELEMENT = BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT,
28 CURRENT_OPTION_CONTENT = BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT,
29};
30
0d218157 31class ConstFieldPathItem : public BorrowedObject<const bt_field_path_item>
40ed8b01
PP
32{
33public:
d246c457 34 explicit ConstFieldPathItem(const LibObjPtr libObjPtr) noexcept :
0d218157 35 _ThisBorrowedObject {libObjPtr}
40ed8b01
PP
36 {
37 }
38
40ed8b01
PP
39 FieldPathItemType type() const noexcept
40 {
41 return static_cast<FieldPathItemType>(this->_libType());
42 }
43
44 bool isIndex() const noexcept
45 {
46 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_INDEX;
47 }
48
49 bool isCurrentArrayElement() const noexcept
50 {
51 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT;
52 }
53
54 bool isCurrentOptionContent() const noexcept
55 {
56 return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT;
57 }
58
59 ConstIndexFieldPathItem asIndex() const noexcept;
60
61private:
62 bt_field_path_item_type _libType() const noexcept
63 {
341a67c4 64 return bt_field_path_item_get_type(this->libObjPtr());
40ed8b01
PP
65 }
66};
67
68class ConstIndexFieldPathItem final : public ConstFieldPathItem
69{
70public:
d246c457 71 explicit ConstIndexFieldPathItem(const LibObjPtr libObjPtr) noexcept :
40ed8b01
PP
72 ConstFieldPathItem {libObjPtr}
73 {
74 BT_ASSERT_DBG(this->isIndex());
75 }
76
40ed8b01
PP
77 std::uint64_t index() const noexcept
78 {
341a67c4 79 return bt_field_path_item_index_get_index(this->libObjPtr());
40ed8b01
PP
80 }
81};
82
83inline ConstIndexFieldPathItem ConstFieldPathItem::asIndex() const noexcept
84{
85 BT_ASSERT_DBG(this->isIndex());
341a67c4 86 return ConstIndexFieldPathItem {this->libObjPtr()};
40ed8b01
PP
87}
88
89namespace internal {
90
91struct 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
0d218157 106class ConstFieldPath final : public BorrowedObject<const bt_field_path>
40ed8b01
PP
107{
108public:
7f5cdaf0 109 using Shared = SharedObject<ConstFieldPath, const bt_field_path, internal::FieldPathRefFuncs>;
56862ee2 110 using Iterator = BorrowedObjectIterator<ConstFieldPath>;
b3a04931 111
40ed8b01
PP
112 enum class Scope
113 {
114 PACKET_CONTEXT = BT_FIELD_PATH_SCOPE_PACKET_CONTEXT,
115 EVENT_COMMON_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT,
116 EVENT_SPECIFIC_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT,
117 EVENT_PAYLOAD = BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD,
118 };
119
d246c457 120 explicit ConstFieldPath(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
40ed8b01
PP
121 {
122 }
123
40ed8b01
PP
124 Scope rootScope() const noexcept
125 {
341a67c4 126 return static_cast<Scope>(bt_field_path_get_root_scope(this->libObjPtr()));
40ed8b01
PP
127 }
128
c0b73c63 129 std::uint64_t length() const noexcept
40ed8b01 130 {
341a67c4 131 return bt_field_path_get_item_count(this->libObjPtr());
40ed8b01
PP
132 }
133
134 ConstFieldPathItem operator[](const std::uint64_t index) const noexcept
135 {
136 return ConstFieldPathItem {
341a67c4 137 bt_field_path_borrow_item_by_index_const(this->libObjPtr(), index)};
40ed8b01
PP
138 }
139
b3a04931
FD
140 Iterator begin() const noexcept
141 {
142 return Iterator {*this, 0};
143 }
144
145 Iterator end() const noexcept
146 {
c0b73c63 147 return Iterator {*this, this->length()};
b3a04931
FD
148 }
149
40ed8b01
PP
150 Shared shared() const noexcept
151 {
c9c0b6e2 152 return Shared::createWithRef(*this);
40ed8b01
PP
153 }
154};
155
b5f55e9f 156} /* namespace bt2 */
40ed8b01 157
b5f55e9f 158#endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP */
This page took 0.045998 seconds and 4 git commands to generate.