| 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 | |
| 16 | #include "borrowed-object-iterator.hpp" |
| 17 | #include "borrowed-object.hpp" |
| 18 | #include "shared-object.hpp" |
| 19 | |
| 20 | namespace bt2 { |
| 21 | |
| 22 | class ConstIndexFieldPathItem; |
| 23 | |
| 24 | enum 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 | |
| 31 | class ConstFieldPathItem : public BorrowedObject<const bt_field_path_item> |
| 32 | { |
| 33 | public: |
| 34 | explicit ConstFieldPathItem(const LibObjPtr libObjPtr) noexcept : |
| 35 | _ThisBorrowedObject {libObjPtr} |
| 36 | { |
| 37 | } |
| 38 | |
| 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 | |
| 61 | private: |
| 62 | bt_field_path_item_type _libType() const noexcept |
| 63 | { |
| 64 | return bt_field_path_item_get_type(this->libObjPtr()); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | class ConstIndexFieldPathItem final : public ConstFieldPathItem |
| 69 | { |
| 70 | public: |
| 71 | explicit ConstIndexFieldPathItem(const LibObjPtr libObjPtr) noexcept : |
| 72 | ConstFieldPathItem {libObjPtr} |
| 73 | { |
| 74 | BT_ASSERT_DBG(this->isIndex()); |
| 75 | } |
| 76 | |
| 77 | std::uint64_t index() const noexcept |
| 78 | { |
| 79 | return bt_field_path_item_index_get_index(this->libObjPtr()); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | inline ConstIndexFieldPathItem ConstFieldPathItem::asIndex() const noexcept |
| 84 | { |
| 85 | return ConstIndexFieldPathItem {this->libObjPtr()}; |
| 86 | } |
| 87 | |
| 88 | namespace internal { |
| 89 | |
| 90 | struct FieldPathRefFuncs final |
| 91 | { |
| 92 | static void get(const bt_field_path * const libObjPtr) noexcept |
| 93 | { |
| 94 | bt_field_path_get_ref(libObjPtr); |
| 95 | } |
| 96 | |
| 97 | static void put(const bt_field_path * const libObjPtr) noexcept |
| 98 | { |
| 99 | bt_field_path_put_ref(libObjPtr); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | } /* namespace internal */ |
| 104 | |
| 105 | class ConstFieldPath final : public BorrowedObject<const bt_field_path> |
| 106 | { |
| 107 | public: |
| 108 | using Shared = SharedObject<ConstFieldPath, const bt_field_path, internal::FieldPathRefFuncs>; |
| 109 | using Iterator = BorrowedObjectIterator<ConstFieldPath>; |
| 110 | |
| 111 | enum class Scope |
| 112 | { |
| 113 | PACKET_CONTEXT = BT_FIELD_PATH_SCOPE_PACKET_CONTEXT, |
| 114 | EVENT_COMMON_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT, |
| 115 | EVENT_SPECIFIC_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT, |
| 116 | EVENT_PAYLOAD = BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD, |
| 117 | }; |
| 118 | |
| 119 | explicit ConstFieldPath(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | Scope rootScope() const noexcept |
| 124 | { |
| 125 | return static_cast<Scope>(bt_field_path_get_root_scope(this->libObjPtr())); |
| 126 | } |
| 127 | |
| 128 | std::uint64_t length() const noexcept |
| 129 | { |
| 130 | return bt_field_path_get_item_count(this->libObjPtr()); |
| 131 | } |
| 132 | |
| 133 | ConstFieldPathItem operator[](const std::uint64_t index) const noexcept |
| 134 | { |
| 135 | return ConstFieldPathItem { |
| 136 | bt_field_path_borrow_item_by_index_const(this->libObjPtr(), index)}; |
| 137 | } |
| 138 | |
| 139 | Iterator begin() const noexcept |
| 140 | { |
| 141 | return Iterator {*this, 0}; |
| 142 | } |
| 143 | |
| 144 | Iterator end() const noexcept |
| 145 | { |
| 146 | return Iterator {*this, this->length()}; |
| 147 | } |
| 148 | |
| 149 | Shared shared() const noexcept |
| 150 | { |
| 151 | return Shared::createWithRef(*this); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | } /* namespace bt2 */ |
| 156 | |
| 157 | #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP */ |