cpp-common/bt2: add C++ plugin bindings
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 16 Feb 2024 19:33:07 +0000 (14:33 -0500)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 29 Feb 2024 23:46:23 +0000 (18:46 -0500)
Add C++ bindings around `bt_plugin`.  There is only the const version
(`ConstPlugin`), because there is no need for a non-const plugin.

Get the scalar properties of a plugin using the `name`, `description`,
`author`, `license` and `path` methods.

Get the version of a plugin using the `version` method.

Get the component classes provided by the plugin using the
`sourceComponentClasses`, `filterComponentClasses` and
`sinkComponentClasses` methods.  These methods return proxy objects that
can be iterated on.  They also defined an `operator[]` method to access
component classes by name.

Change-Id: I5121c7c54a8058e5ca116618472ec8bb63f3a825
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11822
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
src/Makefile.am
src/cpp-common/bt2/plugin.hpp [new file with mode: 0644]

index 235dab4eb729d1f9d3f8aaae17e1301d9c50b815..ca87e544aafea078e423c00f66e798de65226057 100644 (file)
@@ -137,6 +137,7 @@ cpp_common_libcpp_common_la_SOURCES = \
        cpp-common/bt2/plugin-dev.hpp \
        cpp-common/bt2/plugin-load.hpp \
        cpp-common/bt2/plugin-set.hpp \
+       cpp-common/bt2/plugin.hpp \
        cpp-common/bt2/private-query-executor.hpp \
        cpp-common/bt2/query-executor.hpp \
        cpp-common/bt2/raw-value-proxy.hpp \
diff --git a/src/cpp-common/bt2/plugin.hpp b/src/cpp-common/bt2/plugin.hpp
new file mode 100644 (file)
index 0000000..36d6b6b
--- /dev/null
@@ -0,0 +1,251 @@
+/*
+ * Copyright (c) 2024 EfficiOS, Inc.
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2_PLUGIN_HPP
+#define BABELTRACE_CPP_COMMON_BT2_PLUGIN_HPP
+
+#include <cstdint>
+
+#include <babeltrace2/babeltrace.h>
+
+#include "cpp-common/bt2c/c-string-view.hpp"
+#include "cpp-common/bt2s/optional.hpp"
+
+#include "borrowed-object-iterator.hpp"
+#include "borrowed-object.hpp"
+#include "component-class.hpp"
+#include "shared-object.hpp"
+
+namespace bt2 {
+
+template <typename PluginSpecCompClsT>
+class ConstPluginComponentClasses final : public BorrowedObject<const bt_plugin>
+{
+public:
+    using Iterator = BorrowedObjectIterator<ConstPluginComponentClasses<PluginSpecCompClsT>>;
+
+    explicit ConstPluginComponentClasses(const LibObjPtr libPluginPtr) noexcept :
+        _ThisBorrowedObject {libPluginPtr}
+    {
+    }
+
+    std::uint64_t length() const noexcept
+    {
+        return PluginSpecCompClsT::getCompClsCount(this->libObjPtr());
+    }
+
+    Iterator begin() const noexcept
+    {
+        return Iterator {*this, 0};
+    }
+
+    Iterator end() const noexcept
+    {
+        return Iterator {*this, this->length()};
+    }
+
+    typename PluginSpecCompClsT::CompCls operator[](const std::uint64_t index) const noexcept
+    {
+        return PluginSpecCompClsT::borrowCompClsByIndex(this->libObjPtr(), index);
+    }
+
+    OptionalBorrowedObject<typename PluginSpecCompClsT::CompCls>
+    operator[](const bt2c::CStringView name) const noexcept
+    {
+        return PluginSpecCompClsT::borrowCompClsByName(this->libObjPtr(), name);
+    }
+};
+
+namespace internal {
+
+struct PluginRefFuncs final
+{
+    static void get(const bt_plugin * const libObjPtr) noexcept
+    {
+        bt_plugin_get_ref(libObjPtr);
+    }
+
+    static void put(const bt_plugin * const libObjPtr) noexcept
+    {
+        bt_plugin_put_ref(libObjPtr);
+    }
+};
+
+struct PluginSourceCompClsFuncs final
+{
+    using CompCls = ConstSourceComponentClass;
+
+    static std::uint64_t getCompClsCount(const bt_plugin * const plugin) noexcept
+    {
+        return bt_plugin_get_source_component_class_count(plugin);
+    }
+
+    static const bt_component_class_source *borrowCompClsByIndex(const bt_plugin * const plugin,
+                                                                 const std::uint64_t index) noexcept
+    {
+        return bt_plugin_borrow_source_component_class_by_index_const(plugin, index);
+    }
+
+    static const bt_component_class_source *borrowCompClsByName(const bt_plugin * const plugin,
+                                                                const char *name) noexcept
+    {
+        return bt_plugin_borrow_source_component_class_by_name_const(plugin, name);
+    }
+};
+
+struct PluginFilterCompClsFuncs final
+{
+    using CompCls = ConstFilterComponentClass;
+
+    static std::uint64_t getCompClsCount(const bt_plugin * const plugin) noexcept
+    {
+        return bt_plugin_get_filter_component_class_count(plugin);
+    }
+
+    static const bt_component_class_filter *borrowCompClsByIndex(const bt_plugin * const plugin,
+                                                                 const std::uint64_t index) noexcept
+    {
+        return bt_plugin_borrow_filter_component_class_by_index_const(plugin, index);
+    }
+
+    static const bt_component_class_filter *borrowCompClsByName(const bt_plugin * const plugin,
+                                                                const char *name) noexcept
+    {
+        return bt_plugin_borrow_filter_component_class_by_name_const(plugin, name);
+    }
+};
+
+struct PluginSinkCompClsFuncs final
+{
+    using CompCls = ConstSinkComponentClass;
+
+    static std::uint64_t getCompClsCount(const bt_plugin * const plugin) noexcept
+    {
+        return bt_plugin_get_sink_component_class_count(plugin);
+    }
+
+    static const bt_component_class_sink *borrowCompClsByIndex(const bt_plugin * const plugin,
+                                                               const std::uint64_t index) noexcept
+    {
+        return bt_plugin_borrow_sink_component_class_by_index_const(plugin, index);
+    }
+
+    static const bt_component_class_sink *borrowCompClsByName(const bt_plugin * const plugin,
+                                                              const char *name) noexcept
+    {
+        return bt_plugin_borrow_sink_component_class_by_name_const(plugin, name);
+    }
+};
+
+} /* namespace internal */
+
+class ConstPlugin final : public BorrowedObject<const bt_plugin>
+{
+public:
+    using SourceComponementClasses =
+        ConstPluginComponentClasses<internal::PluginSourceCompClsFuncs>;
+    using FilterComponementClasses =
+        ConstPluginComponentClasses<internal::PluginFilterCompClsFuncs>;
+    using SinkComponementClasses = ConstPluginComponentClasses<internal::PluginSinkCompClsFuncs>;
+
+    class Version final
+    {
+    public:
+        explicit Version(const unsigned int major, const unsigned int minor,
+                         const unsigned int patch, const bt2c::CStringView extra) noexcept :
+            _mMajor {major},
+            _mMinor {minor}, _mPatch {patch}, _mExtra {extra}
+        {
+        }
+
+        unsigned int major() const noexcept
+        {
+            return _mMajor;
+        }
+
+        unsigned int minor() const noexcept
+        {
+            return _mMinor;
+        }
+
+        unsigned int patch() const noexcept
+        {
+            return _mPatch;
+        }
+
+        bt2c::CStringView extra() const noexcept
+        {
+            return _mExtra;
+        }
+
+    private:
+        unsigned int _mMajor, _mMinor, _mPatch;
+        bt2c::CStringView _mExtra;
+    };
+
+    using Shared = SharedObject<ConstPlugin, const bt_plugin, internal::PluginRefFuncs>;
+
+    explicit ConstPlugin(const LibObjPtr plugin) : _ThisBorrowedObject {plugin}
+    {
+    }
+
+    bt2c::CStringView name() const noexcept
+    {
+        return bt_plugin_get_name(this->libObjPtr());
+    }
+
+    bt2c::CStringView description() const noexcept
+    {
+        return bt_plugin_get_description(this->libObjPtr());
+    }
+
+    bt2c::CStringView author() const noexcept
+    {
+        return bt_plugin_get_author(this->libObjPtr());
+    }
+
+    bt2c::CStringView license() const noexcept
+    {
+        return bt_plugin_get_license(this->libObjPtr());
+    }
+
+    bt2c::CStringView path() const noexcept
+    {
+        return bt_plugin_get_path(this->libObjPtr());
+    }
+
+    bt2s::optional<Version> version() const noexcept
+    {
+        unsigned int major, minor, patch;
+        const char *extra;
+
+        if (bt_plugin_get_version(this->libObjPtr(), &major, &minor, &patch, &extra) ==
+            BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
+            return bt2s::nullopt;
+        }
+
+        return Version {major, minor, patch, extra};
+    }
+
+    SourceComponementClasses sourceComponentClasses() const noexcept
+    {
+        return SourceComponementClasses {this->libObjPtr()};
+    }
+
+    FilterComponementClasses filterComponentClasses() const noexcept
+    {
+        return FilterComponementClasses {this->libObjPtr()};
+    }
+
+    SinkComponementClasses sinkComponentClasses() const noexcept
+    {
+        return SinkComponementClasses {this->libObjPtr()};
+    }
+};
+
+} /* namespace bt2 */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2_PLUGIN_HPP */
This page took 0.027201 seconds and 4 git commands to generate.