cpp-common/bt2: `CompClsBridge`: pass init method data to user component constructors
authorSimon Marchi <simon.marchi@efficios.com>
Thu, 15 Feb 2024 03:12:39 +0000 (22:12 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Mon, 19 Feb 2024 18:10:15 +0000 (13:10 -0500)
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 <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11787
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
src/cpp-common/bt2/plugin-dev.hpp
src/plugins/utils/muxer/comp.cpp
src/plugins/utils/muxer/comp.hpp

index 2f255e42af9f54d845c8503a58d5206e8b9d06b8..162858ba533dba0ca0b1e75bddb5ea14d6d5ff0a 100644 (file)
@@ -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<typename UserCompClsT::InitData *>(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 <typename SelfCompT>
+template <typename SelfCompT, typename InitDataT>
 class UserComponent
 {
     /* Give a related message iterator access to this logger */
     template <typename, typename>
     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 <typename UserComponentT, typename UserMessageIteratorT>
-class UserSourceComponent : public UserComponent<SelfSourceComponent>
+template <typename UserComponentT, typename UserMessageIteratorT, typename InitDataT = void>
+class UserSourceComponent : public UserComponent<SelfSourceComponent, InitDataT>
 {
     static_assert(std::is_base_of<UserMessageIterator<UserMessageIteratorT, UserComponentT>,
                                   UserMessageIteratorT>::value,
@@ -523,7 +532,7 @@ protected:
     using _OutputPorts = SelfSourceComponent::OutputPorts;
 
     explicit UserSourceComponent(const SelfSourceComponent selfComp, const std::string& logTag) :
-        UserComponent<SelfSourceComponent> {selfComp, logTag}
+        UserComponent<SelfSourceComponent, InitDataT> {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 <typename UserComponentT, typename UserMessageIteratorT>
-class UserFilterComponent : public UserComponent<SelfFilterComponent>
+template <typename UserComponentT, typename UserMessageIteratorT, typename InitDataT = void>
+class UserFilterComponent : public UserComponent<SelfFilterComponent, InitDataT>
 {
     static_assert(std::is_base_of<UserMessageIterator<UserMessageIteratorT, UserComponentT>,
                                   UserMessageIteratorT>::value,
@@ -611,7 +624,7 @@ protected:
     using _OutputPorts = SelfFilterComponent::OutputPorts;
 
     explicit UserFilterComponent(const SelfFilterComponent selfComp, const std::string& logTag) :
-        UserComponent<SelfFilterComponent> {selfComp, logTag}
+        UserComponent<SelfFilterComponent, InitDataT> {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 <typename UserComponentT>
-class UserSinkComponent : public UserComponent<SelfSinkComponent>
+template <typename UserComponentT, typename InitDataT = void>
+class UserSinkComponent : public UserComponent<SelfSinkComponent, InitDataT>
 {
 protected:
     using _InputPorts = SelfSinkComponent::InputPorts;
 
     explicit UserSinkComponent(const SelfSinkComponent selfComp, const std::string& logTag) :
-        UserComponent<SelfSinkComponent> {selfComp, logTag}
+        UserComponent<SelfSinkComponent, InitDataT> {selfComp, logTag}
     {
     }
 
index ba44729a9fbd0ad840b58a96788131437794639a..ea2df4d03136316bd8df66ee8e034ab5547d26f8 100644 (file)
@@ -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<Comp, MsgIter> {selfComp, "PLUGIN/FLT.UTILS.MUXER"}
 {
     BT_CPPLOGI_STR("Initializing component.");
index 4572d1d43039087397c39fae83fd5bd188de6d20..5fe50c235e2bb2721c71760eddff9d65162a6a46 100644 (file)
@@ -22,7 +22,7 @@ class Comp final : public bt2::UserFilterComponent<Comp, MsgIter>
     friend bt2::UserFilterComponent<Comp, MsgIter>;
 
 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);
This page took 0.026704 seconds and 4 git commands to generate.