From 40ed8b0148b4b1d02dc1fd4f8fa4f615f40a4f59 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 17 Dec 2020 13:13:10 -0500 Subject: [PATCH] Add C++ interface for the libbabeltrace2 `bt_field_path` API This patch adds C++ wrappers for Babeltrace 2 field path objects. The new available classes are: * `bt2::ConstFieldPathItem` * `bt2::ConstIndexFieldPathItem` * `bt2::ConstFieldPath` Those new classes follow the approach of other wrappers in `src/cpp-common/bt2`. You can't create a field path: it's what some field class methods (not part of `src/cpp-common/bt2` yet) return. Signed-off-by: Philippe Proulx Change-Id: I3414ac96ff2d305669ae24ef13aa8e88dad88df5 Reviewed-on: https://review.lttng.org/c/babeltrace/+/4611 --- src/cpp-common/bt2/field-path.hpp | 174 ++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 src/cpp-common/bt2/field-path.hpp diff --git a/src/cpp-common/bt2/field-path.hpp b/src/cpp-common/bt2/field-path.hpp new file mode 100644 index 00000000..b91d9113 --- /dev/null +++ b/src/cpp-common/bt2/field-path.hpp @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2020 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP +#define BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP + +#include +#include + +#include "common/assert.h" +#include "internal/borrowed-obj.hpp" + +namespace bt2 { + +class ConstIndexFieldPathItem; + +enum class FieldPathItemType +{ + INDEX = BT_FIELD_PATH_ITEM_TYPE_INDEX, + CURRENT_ARRAY_ELEMENT = BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT, + CURRENT_OPTION_CONTENT = BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT, +}; + +class ConstFieldPathItem : public internal::BorrowedObj +{ +public: + explicit ConstFieldPathItem(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr} + { + } + + ConstFieldPathItem(const ConstFieldPathItem& fpItem) noexcept : _ThisBorrowedObj {fpItem} + { + } + + ConstFieldPathItem& operator=(const ConstFieldPathItem& fpItem) noexcept + { + _ThisBorrowedObj::operator=(fpItem); + return *this; + } + + FieldPathItemType type() const noexcept + { + return static_cast(this->_libType()); + } + + bool isIndex() const noexcept + { + return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_INDEX; + } + + bool isCurrentArrayElement() const noexcept + { + return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT; + } + + bool isCurrentOptionContent() const noexcept + { + return this->_libType() == BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT; + } + + ConstIndexFieldPathItem asIndex() const noexcept; + +private: + bt_field_path_item_type _libType() const noexcept + { + return bt_field_path_item_get_type(this->_libObjPtr()); + } +}; + +class ConstIndexFieldPathItem final : public ConstFieldPathItem +{ +public: + explicit ConstIndexFieldPathItem(const _LibObjPtr libObjPtr) noexcept : + ConstFieldPathItem {libObjPtr} + { + BT_ASSERT_DBG(this->isIndex()); + } + + ConstIndexFieldPathItem(const ConstIndexFieldPathItem& fpItem) noexcept : + ConstFieldPathItem {fpItem} + { + } + + ConstIndexFieldPathItem& operator=(const ConstIndexFieldPathItem& fpItem) noexcept + { + ConstFieldPathItem::operator=(fpItem); + return *this; + } + + std::uint64_t index() const noexcept + { + return bt_field_path_item_index_get_index(this->_libObjPtr()); + } +}; + +inline ConstIndexFieldPathItem ConstFieldPathItem::asIndex() const noexcept +{ + BT_ASSERT_DBG(this->isIndex()); + return ConstIndexFieldPathItem {this->_libObjPtr()}; +} + +namespace internal { + +struct FieldPathRefFuncs final +{ + static void get(const bt_field_path * const libObjPtr) + { + bt_field_path_get_ref(libObjPtr); + } + + static void put(const bt_field_path * const libObjPtr) + { + bt_field_path_put_ref(libObjPtr); + } +}; + +} // namespace internal + +class ConstFieldPath final : public internal::BorrowedObj +{ +public: + using Shared = + internal::SharedObj; + + enum class Scope + { + PACKET_CONTEXT = BT_FIELD_PATH_SCOPE_PACKET_CONTEXT, + EVENT_COMMON_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT, + EVENT_SPECIFIC_CONTEXT = BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT, + EVENT_PAYLOAD = BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD, + }; + + explicit ConstFieldPath(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr} + { + } + + ConstFieldPath(const ConstFieldPath& clkSnapshot) noexcept : _ThisBorrowedObj {clkSnapshot} + { + } + + ConstFieldPath& operator=(const ConstFieldPath& clkSnapshot) noexcept + { + _ThisBorrowedObj::operator=(clkSnapshot); + return *this; + } + + Scope rootScope() const noexcept + { + return static_cast(bt_field_path_get_root_scope(this->_libObjPtr())); + } + + std::uint64_t size() const noexcept + { + return bt_field_path_get_item_count(this->_libObjPtr()); + } + + ConstFieldPathItem operator[](const std::uint64_t index) const noexcept + { + return ConstFieldPathItem { + bt_field_path_borrow_item_by_index_const(this->_libObjPtr(), index)}; + } + + Shared shared() const noexcept + { + return Shared {*this}; + } +}; + +} // namespace bt2 + +#endif // BABELTRACE_CPP_COMMON_BT2_FIELD_PATH_HPP -- 2.34.1