From c52879364e32e2c342e6df6231c7b44efe43173f Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Fri, 8 Dec 2023 18:50:58 +0000 Subject: [PATCH] src/cpp-common/bt2: add field location support This patch adds the `bt2::ConstFieldLocation` wrapper class as well as bt2::TraceClass::createFieldLocation(). I didn't implement a `bt2::CommonFieldLocation` template class because a field location is immutable anyway, so we only need the const version. bt2::TraceClass::createFieldLocation() returns a shared const field location. bt2::TraceClass::createFieldLocation() accepts a span of strings (either `const char * const` or `const std::string`) to make the interface a little nicer than a raw array of `const char *` and a matching size. Creating a field location is a slow-path operation anyway. Also adding bt2::wrap() accepting `const bt_field_location *`. Signed-off-by: Philippe Proulx Change-Id: I160052c5a787114b52062aa87908c94a2af40f32 Reviewed-on: https://review.lttng.org/c/babeltrace/+/8016 Reviewed-on: https://review.lttng.org/c/babeltrace/+/12699 --- src/Makefile.am | 1 + src/cpp-common/bt2/field-location.hpp | 80 +++++++++++++++++++++++++++ src/cpp-common/bt2/trace-ir.hpp | 35 ++++++++++++ src/cpp-common/bt2/wrap.hpp | 6 ++ 4 files changed, 122 insertions(+) create mode 100644 src/cpp-common/bt2/field-location.hpp diff --git a/src/Makefile.am b/src/Makefile.am index 1cbcaf7f..075e6695 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -132,6 +132,7 @@ cpp_common_libcpp_common_la_SOURCES = \ cpp-common/bt2/field-class.hpp \ cpp-common/bt2/field-path.hpp \ cpp-common/bt2/field.hpp \ + cpp-common/bt2/field-location.hpp \ cpp-common/bt2/integer-range-set.hpp \ cpp-common/bt2/integer-range.hpp \ cpp-common/bt2/internal/comp-cls-bridge.hpp \ diff --git a/src/cpp-common/bt2/field-location.hpp b/src/cpp-common/bt2/field-location.hpp new file mode 100644 index 00000000..be1d1950 --- /dev/null +++ b/src/cpp-common/bt2/field-location.hpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2022 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2_FIELD_LOCATION_HPP +#define BABELTRACE_CPP_COMMON_BT2_FIELD_LOCATION_HPP + +#include + +#include + +#include "common/assert.h" +#include "cpp-common/bt2c/c-string-view.hpp" + +#include "borrowed-object.hpp" +#include "shared-object.hpp" + +namespace bt2 { +namespace internal { + +struct FieldLocationRefFuncs final +{ + static void get(const bt_field_location * const libObjPtr) + { + bt_field_location_get_ref(libObjPtr); + } + + static void put(const bt_field_location * const libObjPtr) + { + bt_field_location_put_ref(libObjPtr); + } +}; + +} /* namespace internal */ + +class ConstFieldLocation : public BorrowedObject +{ +public: + using Shared = + SharedObject; + + enum class Scope + { + PacketContext = BT_FIELD_LOCATION_SCOPE_PACKET_CONTEXT, + CommonEventContext = BT_FIELD_LOCATION_SCOPE_EVENT_COMMON_CONTEXT, + SpecificEventContext = BT_FIELD_LOCATION_SCOPE_EVENT_SPECIFIC_CONTEXT, + EventPayload = BT_FIELD_LOCATION_SCOPE_EVENT_PAYLOAD, + }; + + explicit ConstFieldLocation(const LibObjPtr libObjPtr) noexcept : + _ThisBorrowedObject {libObjPtr} + { + } + + Scope rootScope() const noexcept + { + return static_cast(bt_field_location_get_root_scope(this->libObjPtr())); + } + + std::uint64_t size() const noexcept + { + return bt_field_location_get_item_count(this->libObjPtr()); + } + + bt2c::CStringView operator[](const std::uint64_t index) const noexcept + { + return bt_field_location_get_item_by_index(this->libObjPtr(), index); + } + + Shared shared() const noexcept + { + return Shared::createWithRef(*this); + } +}; + +} /* namespace bt2 */ + +#endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_LOCATION_HPP */ diff --git a/src/cpp-common/bt2/trace-ir.hpp b/src/cpp-common/bt2/trace-ir.hpp index 02d1e548..8972bd22 100644 --- a/src/cpp-common/bt2/trace-ir.hpp +++ b/src/cpp-common/bt2/trace-ir.hpp @@ -9,16 +9,20 @@ #include #include +#include #include #include "common/macros.h" #include "cpp-common/bt2c/c-string-view.hpp" +#include "cpp-common/bt2c/call.hpp" #include "cpp-common/bt2s/optional.hpp" +#include "cpp-common/bt2s/span.hpp" #include "borrowed-object.hpp" #include "clock-class.hpp" #include "field-class.hpp" +#include "field-location.hpp" #include "field.hpp" #include "internal/utils.hpp" #include "optional-borrowed-object.hpp" @@ -1671,6 +1675,37 @@ public: return Trace::Shared::createWithoutRef(libObjPtr); } + ConstFieldLocation::Shared createFieldLocation(const ConstFieldLocation::Scope scope, + const bt2s::span items) const + { + static_assert(!std::is_const::value, "Not available with `bt2::ConstTraceClass`."); + + const auto libObjPtr = + bt_field_location_create(this->libObjPtr(), static_cast(scope), + items.data(), items.size()); + + internal::validateCreatedObjPtr(libObjPtr); + return ConstFieldLocation::Shared::createWithoutRef(libObjPtr); + } + + ConstFieldLocation::Shared createFieldLocation(const ConstFieldLocation::Scope scope, + const bt2s::span items) const + { + static_assert(!std::is_const::value, "Not available with `bt2::ConstTraceClass`."); + + const auto ptrItems = bt2c::call([items] { + std::vector v; + + for (auto& item : items) { + v.push_back(item.data()); + } + + return v; + }); + + return this->createFieldLocation(scope, ptrItems); + } + StreamClass::Shared createStreamClass() const { static_assert(!std::is_const::value, "Not available with `bt2::ConstTraceClass`."); diff --git a/src/cpp-common/bt2/wrap.hpp b/src/cpp-common/bt2/wrap.hpp index c3b177cc..ef192b89 100644 --- a/src/cpp-common/bt2/wrap.hpp +++ b/src/cpp-common/bt2/wrap.hpp @@ -14,6 +14,7 @@ #include "component-port.hpp" #include "error.hpp" #include "field-class.hpp" +#include "field-location.hpp" #include "field.hpp" #include "graph.hpp" #include "integer-range-set.hpp" @@ -138,6 +139,11 @@ inline ConstFieldPathItem wrap(const bt_field_path_item * const libObjPtr) noexc return ConstFieldPathItem {libObjPtr}; } +inline ConstFieldLocation wrap(const bt_field_location * const libObjPtr) noexcept +{ + return ConstFieldLocation {libObjPtr}; +} + inline ConstFieldPath wrap(const bt_field_path * const libObjPtr) noexcept { return ConstFieldPath {libObjPtr}; -- 2.34.1