From: Simon Marchi Date: Thu, 15 Feb 2024 03:12:39 +0000 (-0500) Subject: cpp-common/bt2: `CompClsBridge`: pass init method data to user component constructors X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=e66071f3c17583e8964d2f09845858720d9a5478 cpp-common/bt2: `CompClsBridge`: pass init method data to user component constructors Make `CompClsBridge::init` pass the initialization method data (received from the lib as a `void *`) down to the user component's constructor. For convenience, user component classes can specify the type of the data they expect to receive as a template parameter, sparing them of doing a static_cast. This template parameter can be omitted, in which case it defaults to `void`. Change-Id: I6b64ea6d75d535d7f2f3b8d559bd2698c186f0ca Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/11787 Reviewed-by: Philippe Proulx Tested-by: jenkins --- diff --git a/src/cpp-common/bt2/plugin-dev.hpp b/src/cpp-common/bt2/plugin-dev.hpp index 2f255e42..162858ba 100644 --- a/src/cpp-common/bt2/plugin-dev.hpp +++ b/src/cpp-common/bt2/plugin-dev.hpp @@ -64,12 +64,14 @@ public: static bt_component_class_initialize_method_status init(const _LibSelfCompPtr libSelfCompPtr, typename LibTypesT::SelfCompCfg *, const bt_value * const libParamsPtr, - void *) noexcept + void * const initData) noexcept { const auto selfComp = wrap(libSelfCompPtr); try { - const auto comp = new UserCompClsT {selfComp, wrap(libParamsPtr).asMap()}; + const auto comp = + new UserCompClsT {selfComp, wrap(libParamsPtr).asMap(), + static_cast(initData)}; selfComp.data(*comp); } catch (const std::bad_alloc&) { @@ -458,13 +460,16 @@ class UserMessageIterator; * See the specific `bt2::UserSourceComponent`, * `bt2::UserFilterComponent`, and `bt2::UserSinkComponent`. */ -template +template class UserComponent { /* Give a related message iterator access to this logger */ template friend class UserMessageIterator; +public: + using InitData = InitDataT; + protected: explicit UserComponent(const SelfCompT selfComp, const std::string& logTag) : _mLogger {selfComp, fmt::format("{}/[{}]", logTag, selfComp.name())}, _mSelfComp {selfComp} @@ -501,16 +506,20 @@ private: /* * Base class of a user source component `UserComponentT` (CRTP). * - * `UserComponentT::UserComponentT()` must accept a - * `bt2::SelfSourceComponent` parameter, which it needs to forward to - * bt2::UserSourceComponent::UserSourceComponent(), and a - * `bt2::ConstValue` parameter (initialization parameters). + * UserComponentT::UserComponentT() must accept, in this order: + * + * 1. A `bt2::SelfSourceComponent` parameter, which it needs to forward + * to bt2::UserSourceComponent::UserSourceComponent(). + * + * 2. A `bt2::ConstValue` parameter (the initialization parameters). + * + * 3. An `InitDataT *` parameter (the initialization method data). * * `UserMessageIteratorT`, the message iterator class to use, must inherit * `UserMessageIterator`. */ -template -class UserSourceComponent : public UserComponent +template +class UserSourceComponent : public UserComponent { static_assert(std::is_base_of, UserMessageIteratorT>::value, @@ -523,7 +532,7 @@ protected: using _OutputPorts = SelfSourceComponent::OutputPorts; explicit UserSourceComponent(const SelfSourceComponent selfComp, const std::string& logTag) : - UserComponent {selfComp, logTag} + UserComponent {selfComp, logTag} { } @@ -588,16 +597,20 @@ protected: /* * Base class of a user filter component `UserComponentT` (CRTP). * - * `UserComponentT::UserComponentT()` must accept a - * `bt2::SelfFilterComponent` parameter, which it needs to forward to - * bt2::UserFilterComponent::UserFilterComponent(), and a - * `bt2::ConstValue` parameter (initialization parameters). + * UserComponentT::UserComponentT() must accept, in this order: + * + * 1. A `bt2::SelfFilterComponent` parameter, which it needs to forward + * to bt2::UserFilterComponent::UserFilterComponent(). + * + * 2. A `bt2::ConstValue` parameter (the initialization parameters). + * + * 3. An `InitDataT *` parameter (the initialization method data). * * `UserMessageIteratorT`, the message iterator class to use, must inherit * `UserMessageIterator`. */ -template -class UserFilterComponent : public UserComponent +template +class UserFilterComponent : public UserComponent { static_assert(std::is_base_of, UserMessageIteratorT>::value, @@ -611,7 +624,7 @@ protected: using _OutputPorts = SelfFilterComponent::OutputPorts; explicit UserFilterComponent(const SelfFilterComponent selfComp, const std::string& logTag) : - UserComponent {selfComp, logTag} + UserComponent {selfComp, logTag} { } @@ -703,10 +716,14 @@ protected: /* * Base class of a user sink component `UserComponentT` (CRTP). * - * `UserComponentT::UserComponentT()` must accept a - * `bt2::SelfSinkComponent` parameter, which it needs to forward to - * bt2::UserSinkComponent::UserSinkComponent(), and a `bt2::ConstValue` - * parameter (initialization parameters). + * UserComponentT::UserComponentT() must accept, in this order: + * + * 1. A `bt2::SelfSinkComponent` parameter, which it needs to forward + * to bt2::UserSinkComponent::UserSinkComponent(). + * + * 2. A `bt2::ConstValue` parameter (the initialization parameters). + * + * 3. An `InitDataT *` parameter (the initialization method data). * * `UserComponentT` must implement: * @@ -715,14 +732,14 @@ protected: * This method returns `true` if the sink component still needs to * consume, or `false` if it's finished. */ -template -class UserSinkComponent : public UserComponent +template +class UserSinkComponent : public UserComponent { protected: using _InputPorts = SelfSinkComponent::InputPorts; explicit UserSinkComponent(const SelfSinkComponent selfComp, const std::string& logTag) : - UserComponent {selfComp, logTag} + UserComponent {selfComp, logTag} { } diff --git a/src/plugins/utils/muxer/comp.cpp b/src/plugins/utils/muxer/comp.cpp index ba44729a..ea2df4d0 100644 --- a/src/plugins/utils/muxer/comp.cpp +++ b/src/plugins/utils/muxer/comp.cpp @@ -10,7 +10,7 @@ namespace bt2mux { -Comp::Comp(const bt2::SelfFilterComponent selfComp, const bt2::ConstMapValue params) : +Comp::Comp(const bt2::SelfFilterComponent selfComp, const bt2::ConstMapValue params, void *) : bt2::UserFilterComponent {selfComp, "PLUGIN/FLT.UTILS.MUXER"} { BT_CPPLOGI_STR("Initializing component."); diff --git a/src/plugins/utils/muxer/comp.hpp b/src/plugins/utils/muxer/comp.hpp index 4572d1d4..5fe50c23 100644 --- a/src/plugins/utils/muxer/comp.hpp +++ b/src/plugins/utils/muxer/comp.hpp @@ -22,7 +22,7 @@ class Comp final : public bt2::UserFilterComponent friend bt2::UserFilterComponent; public: - explicit Comp(bt2::SelfFilterComponent selfComp, bt2::ConstMapValue params); + explicit Comp(bt2::SelfFilterComponent selfComp, bt2::ConstMapValue params, void *); private: void _inputPortConnected(bt2::SelfComponentInputPort selfPort, bt2::ConstOutputPort otherPort);