src/cpp-common/bt2: add field location support
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 8 Dec 2023 18:50:58 +0000 (18:50 +0000)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 4 Sep 2024 19:05:14 +0000 (15:05 -0400)
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 <eeppeliteloop@gmail.com>
Change-Id: I160052c5a787114b52062aa87908c94a2af40f32
Reviewed-on: https://review.lttng.org/c/babeltrace/+/8016
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12699

src/Makefile.am
src/cpp-common/bt2/field-location.hpp [new file with mode: 0644]
src/cpp-common/bt2/trace-ir.hpp
src/cpp-common/bt2/wrap.hpp

index 1cbcaf7f6d74b836a669816ddb495a5df301ae04..075e6695019f2e350be3c8d5a4dd3456d1fb3a5b 100644 (file)
@@ -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 (file)
index 0000000..be1d195
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2_FIELD_LOCATION_HPP
+#define BABELTRACE_CPP_COMMON_BT2_FIELD_LOCATION_HPP
+
+#include <cstdint>
+
+#include <babeltrace2/babeltrace.h>
+
+#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<const bt_field_location>
+{
+public:
+    using Shared =
+        SharedObject<ConstFieldLocation, const bt_field_location, internal::FieldLocationRefFuncs>;
+
+    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<Scope>(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 */
index 02d1e548ae0a2b6f4aed50d19ee949e29a8f697a..8972bd2247068e70a6fcda2cd7c1e9e378db263e 100644 (file)
@@ -9,16 +9,20 @@
 
 #include <cstdint>
 #include <type_traits>
+#include <vector>
 
 #include <babeltrace2/babeltrace.h>
 
 #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<const char * const> items) const
+    {
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstTraceClass`.");
+
+        const auto libObjPtr =
+            bt_field_location_create(this->libObjPtr(), static_cast<bt_field_location_scope>(scope),
+                                     items.data(), items.size());
+
+        internal::validateCreatedObjPtr(libObjPtr);
+        return ConstFieldLocation::Shared::createWithoutRef(libObjPtr);
+    }
+
+    ConstFieldLocation::Shared createFieldLocation(const ConstFieldLocation::Scope scope,
+                                                   const bt2s::span<const std::string> items) const
+    {
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstTraceClass`.");
+
+        const auto ptrItems = bt2c::call([items] {
+            std::vector<const char *> 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<LibObjT>::value, "Not available with `bt2::ConstTraceClass`.");
index c3b177cc089ba4640173f50e66dd2a8a19fa7504..ef192b8904466c721ef064b2eddb5d001429b016 100644 (file)
@@ -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};
This page took 0.027247 seconds and 4 git commands to generate.