From cb0369458c84a18946db78af09d403a43d5df26e Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Wed, 14 Feb 2024 11:09:27 -0500 Subject: [PATCH] cpp-common/bt2: add C++ component class bindings Add C++ bindings for component classes. Similar to how C++ bindings for components work, the `CommonComponentClass` type is implicitly constructible from the specialized component class types, so that it's possible to pass a `SourceComponentClass` to something expecting a `ComponentClass`, for instance. Get a component class' name, description or help with the methods of the same name. Change-Id: Ia6301f4b37f4f86036f6e09cc2aaf49d7028c6fc Signed-off-by: Simon Marchi Signed-off-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/11803 --- src/Makefile.am | 1 + src/cpp-common/bt2/component-class.hpp | 446 +++++++++++++++++++++++++ 2 files changed, 447 insertions(+) create mode 100644 src/cpp-common/bt2/component-class.hpp diff --git a/src/Makefile.am b/src/Makefile.am index 1a9ac61d..b226abee 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -118,6 +118,7 @@ cpp_common_libcpp_common_la_SOURCES = \ cpp-common/bt2/clock-class.hpp \ cpp-common/bt2/clock-snapshot.hpp \ cpp-common/bt2/component-class-dev.hpp \ + cpp-common/bt2/component-class.hpp \ cpp-common/bt2/component-port.hpp \ cpp-common/bt2/exc.hpp \ cpp-common/bt2/field-class.hpp \ diff --git a/src/cpp-common/bt2/component-class.hpp b/src/cpp-common/bt2/component-class.hpp new file mode 100644 index 00000000..51f7f470 --- /dev/null +++ b/src/cpp-common/bt2/component-class.hpp @@ -0,0 +1,446 @@ +/* + * Copyright (c) 2024 EfficiOS, Inc. + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2_COMPONENT_CLASS_HPP +#define BABELTRACE_CPP_COMMON_BT2_COMPONENT_CLASS_HPP + +#include + +#include "cpp-common/bt2c/c-string-view.hpp" + +#include "borrowed-object.hpp" +#include "shared-object.hpp" + +namespace bt2 { +namespace internal { + +struct ComponentClassRefFuncs final +{ + static void get(const bt_component_class * const libObjPtr) noexcept + { + bt_component_class_get_ref(libObjPtr); + } + + static void put(const bt_component_class * const libObjPtr) noexcept + { + bt_component_class_put_ref(libObjPtr); + } +}; + +} /* namespace internal */ + +template +class CommonSourceComponentClass; + +template +class CommonFilterComponentClass; + +template +class CommonSinkComponentClass; + +template +class CommonComponentClass : public BorrowedObject +{ +private: + using _ThisBorrowedObject = BorrowedObject; + +public: + using typename _ThisBorrowedObject::LibObjPtr; + using Shared = SharedObject; + + explicit CommonComponentClass(const LibObjPtr libObjPtr) noexcept : + _ThisBorrowedObject {libObjPtr} + { + } + + template + CommonComponentClass(const CommonComponentClass compCls) noexcept : + _ThisBorrowedObject {compCls} + { + } + + template + CommonComponentClass operator=(const CommonComponentClass compCls) noexcept + { + _ThisBorrowedObject::operator=(compCls); + return *this; + } + + /* Not `explicit` to make them behave like copy constructors */ + CommonComponentClass( + const CommonSourceComponentClass other) noexcept; + CommonComponentClass( + const CommonSourceComponentClass other) noexcept; + CommonComponentClass( + const CommonFilterComponentClass other) noexcept; + CommonComponentClass( + const CommonFilterComponentClass other) noexcept; + CommonComponentClass( + const CommonSinkComponentClass other) noexcept; + CommonComponentClass(const CommonSinkComponentClass other) noexcept; + + CommonComponentClass + operator=(CommonSourceComponentClass other) noexcept; + CommonComponentClass + operator=(CommonSourceComponentClass other) noexcept; + CommonComponentClass + operator=(CommonFilterComponentClass other) noexcept; + CommonComponentClass + operator=(CommonFilterComponentClass other) noexcept; + CommonComponentClass + operator=(CommonSinkComponentClass other) noexcept; + CommonComponentClass + operator=(CommonSinkComponentClass other) noexcept; + + bool isSource() const noexcept + { + return static_cast(bt_component_class_is_source(this->libObjPtr())); + } + + bool isFilter() const noexcept + { + return static_cast(bt_component_class_is_filter(this->libObjPtr())); + } + + bool isSink() const noexcept + { + return static_cast(bt_component_class_is_sink(this->libObjPtr())); + } + + bt2c::CStringView name() const noexcept + { + return bt_component_class_get_name(this->libObjPtr()); + } + + bt2c::CStringView description() const noexcept + { + return bt_component_class_get_description(this->libObjPtr()); + } + + bt2c::CStringView help() const noexcept + { + return bt_component_class_get_help(this->libObjPtr()); + } +}; + +using ComponentClass = CommonComponentClass; +using ConstComponentClass = CommonComponentClass; + +namespace internal { + +struct SourceComponentClassRefFuncs final +{ + static void get(const bt_component_class_source * const libObjPtr) noexcept + { + bt_component_class_source_get_ref(libObjPtr); + } + + static void put(const bt_component_class_source * const libObjPtr) noexcept + { + bt_component_class_source_put_ref(libObjPtr); + } +}; + +} /* namespace internal */ + +template +class CommonSourceComponentClass final : public BorrowedObject +{ +private: + using _ThisBorrowedObject = BorrowedObject; + +public: + using typename _ThisBorrowedObject::LibObjPtr; + using Shared = + SharedObject; + + CommonSourceComponentClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} + { + } + + template + CommonSourceComponentClass(const CommonSourceComponentClass compCls) noexcept : + _ThisBorrowedObject {compCls} + { + } + + template + CommonSourceComponentClass + operator=(const CommonSourceComponentClass compCls) noexcept + { + _ThisBorrowedObject::operator=(compCls); + return *this; + } + + bt2c::CStringView name() const noexcept + { + return this->_constComponentClass().name(); + } + + bt2c::CStringView description() const noexcept + { + return this->_constComponentClass().description(); + } + + bt2c::CStringView help() const noexcept + { + return this->_constComponentClass().help(); + } + +private: + ConstComponentClass _constComponentClass() const noexcept + { + return ConstComponentClass { + bt_component_class_source_as_component_class_const(this->libObjPtr())}; + } +}; + +template +CommonComponentClass::CommonComponentClass( + const CommonSourceComponentClass other) noexcept : + _ThisBorrowedObject {bt_component_class_source_as_component_class_const(other.libObjPtr())} +{ +} + +template +CommonComponentClass::CommonComponentClass( + const CommonSourceComponentClass other) noexcept : + _ThisBorrowedObject {bt_component_class_source_as_component_class(other.libObjPtr())} +{ +} + +template +CommonComponentClass CommonComponentClass::operator=( + const CommonSourceComponentClass other) noexcept +{ + BorrowedObject::operator=(ConstComponentClass { + bt_component_class_source_as_component_class_const(other.libObjPtr())}); + return *this; +} + +template +CommonComponentClass CommonComponentClass::operator=( + const CommonSourceComponentClass other) noexcept +{ + BorrowedObject::operator=( + ComponentClass {bt_component_class_source_as_component_class(other.libObjPtr())}); + return *this; +} + +using SourceComponentClass = CommonSourceComponentClass; +using ConstSourceComponentClass = CommonSourceComponentClass; + +namespace internal { + +struct FilterComponentClassRefFuncs final +{ + static void get(const bt_component_class_filter * const libObjPtr) noexcept + { + bt_component_class_filter_get_ref(libObjPtr); + } + + static void put(const bt_component_class_filter * const libObjPtr) noexcept + { + bt_component_class_filter_put_ref(libObjPtr); + } +}; + +} /* namespace internal */ + +template +class CommonFilterComponentClass final : public BorrowedObject +{ +private: + using _ThisBorrowedObject = BorrowedObject; + +public: + using typename _ThisBorrowedObject::LibObjPtr; + using Shared = + SharedObject; + + CommonFilterComponentClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} + { + } + + template + CommonFilterComponentClass(const CommonFilterComponentClass compCls) noexcept : + _ThisBorrowedObject {compCls} + { + } + + template + CommonFilterComponentClass + operator=(const CommonFilterComponentClass compCls) noexcept + { + _ThisBorrowedObject::operator=(compCls); + return *this; + } + + bt2c::CStringView name() const noexcept + { + return this->_constComponentClass().name(); + } + + bt2c::CStringView description() const noexcept + { + return this->_constComponentClass().description(); + } + + bt2c::CStringView help() const noexcept + { + return this->_constComponentClass().help(); + } + +private: + ConstComponentClass _constComponentClass() const noexcept + { + return ConstComponentClass { + bt_component_class_filter_as_component_class_const(this->libObjPtr())}; + } +}; + +template +CommonComponentClass::CommonComponentClass( + const CommonFilterComponentClass other) noexcept : + _ThisBorrowedObject {bt_component_class_filter_as_component_class_const(other.libObjPtr())} +{ +} + +template +CommonComponentClass::CommonComponentClass( + const CommonFilterComponentClass other) noexcept : + _ThisBorrowedObject {bt_component_class_filter_as_component_class(other.libObjPtr())} +{ +} + +template +CommonComponentClass CommonComponentClass::operator=( + const CommonFilterComponentClass other) noexcept +{ + BorrowedObject::operator=(ConstComponentClass { + bt_component_class_filter_as_component_class_const(other.libObjPtr())}); + return *this; +} + +template +CommonComponentClass CommonComponentClass::operator=( + const CommonFilterComponentClass other) noexcept +{ + BorrowedObject::operator=( + ComponentClass {bt_component_class_filter_as_component_class(other.libObjPtr())}); + return *this; +} + +using FilterComponentClass = CommonFilterComponentClass; +using ConstFilterComponentClass = CommonFilterComponentClass; + +namespace internal { + +struct SinkComponentClassRefFuncs final +{ + static void get(const bt_component_class_sink * const libObjPtr) noexcept + { + bt_component_class_sink_get_ref(libObjPtr); + } + + static void put(const bt_component_class_sink * const libObjPtr) noexcept + { + bt_component_class_sink_put_ref(libObjPtr); + } +}; + +} /* namespace internal */ + +template +class CommonSinkComponentClass final : public BorrowedObject +{ +private: + using _ThisBorrowedObject = BorrowedObject; + +public: + using typename _ThisBorrowedObject::LibObjPtr; + using Shared = + SharedObject; + + CommonSinkComponentClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} + { + } + + template + CommonSinkComponentClass(const CommonSinkComponentClass compCls) noexcept : + _ThisBorrowedObject {compCls} + { + } + + template + CommonSinkComponentClass + operator=(const CommonSinkComponentClass compCls) noexcept + { + _ThisBorrowedObject::operator=(compCls); + return *this; + } + + bt2c::CStringView name() const noexcept + { + return this->_constComponentClass().name(); + } + + bt2c::CStringView description() const noexcept + { + return this->_constComponentClass().description(); + } + + bt2c::CStringView help() const noexcept + { + return this->_constComponentClass().help(); + } + +private: + ConstComponentClass _constComponentClass() const noexcept + { + return ConstComponentClass { + bt_component_class_sink_as_component_class_const(this->libObjPtr())}; + } +}; + +template +CommonComponentClass::CommonComponentClass( + const CommonSinkComponentClass other) noexcept : + _ThisBorrowedObject {bt_component_class_sink_as_component_class_const(other.libObjPtr())} +{ +} + +template +CommonComponentClass::CommonComponentClass( + const CommonSinkComponentClass other) noexcept : + _ThisBorrowedObject {bt_component_class_sink_as_component_class(other.libObjPtr())} +{ +} + +template +CommonComponentClass CommonComponentClass::operator=( + const CommonSinkComponentClass other) noexcept +{ + BorrowedObject::operator=( + ConstComponentClass {bt_component_class_sink_as_component_class_const(other.libObjPtr())}); + return *this; +} + +template +CommonComponentClass CommonComponentClass::operator=( + const CommonSinkComponentClass other) noexcept +{ + BorrowedObject::operator=( + ComponentClass {bt_component_class_sink_as_component_class(other.libObjPtr())}); + return *this; +} + +using SinkComponentClass = CommonSinkComponentClass; +using ConstSinkComponentClass = CommonSinkComponentClass; + +} /* namespace bt2 */ + +#endif /* BABELTRACE_CPP_COMMON_BT2_COMPONENT_CLASS_HPP */ -- 2.34.1