tests/lib/conds: change `CreateClockCls` type to `std::function`
authorSimon Marchi <simon.marchi@efficios.com>
Thu, 2 May 2024 15:25:33 +0000 (11:25 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 9 Oct 2024 02:56:57 +0000 (22:56 -0400)
The `CreateClockCls` callable type is currently a raw function pointer.
An upcoming patch will want to pass lambdas with captures, so change
`CreateClockCls` to be `std::function` instead.  Update the
`CreateClockCls` parameters to be non-const where we want to move the
value, and const-ref otherwise.

Remove the `noClockClass` function, and use an empty `CreateClockCls` to
mean "do not create a clock class".  I couldn't find how to do the
equivalent of the `createClockCls == noClockClass` with an
`std::function`, but using an empty `std::function` is clear enough (we
could have done it with a nullptr before too).

Change-Id: Ie30e42b68b5ffad566458933869e78da8ca01614
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12781
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
tests/lib/conds/clk-cls-compat-postconds-triggers.cpp

index 916b921b0c66e3c695cecdb6c2b3575cc7d1367b..0c7675b4cfa7fabbde50a38f1700b12c4151a467 100644 (file)
@@ -25,14 +25,13 @@ public:
         MsgIterInactivity,
     };
 
-    using CreateClockCls = bt2::ClockClass::Shared (*)(bt2::SelfComponent);
+    using CreateClockCls = std::function<bt2::ClockClass::Shared(bt2::SelfComponent)>;
 
-    explicit ClockClsCompatRunIn(const MsgType msgType1, const CreateClockCls createClockCls1,
-                                 const MsgType msgType2,
-                                 const CreateClockCls createClockCls2) noexcept :
+    explicit ClockClsCompatRunIn(const MsgType msgType1, CreateClockCls createClockCls1,
+                                 const MsgType msgType2, CreateClockCls createClockCls2) noexcept :
         _mMsgType1 {msgType1},
-        _mMsgType2 {msgType2}, _mCreateClockCls1 {createClockCls1},
-        _mCreateClockCls2 {createClockCls2}
+        _mMsgType2 {msgType2}, _mCreateClockCls1 {std::move(createClockCls1)},
+        _mCreateClockCls2 {std::move(createClockCls2)}
     {
     }
 
@@ -52,10 +51,11 @@ public:
 private:
     static bt2::Message::Shared _createOneMsg(const bt2::SelfMessageIterator self,
                                               const MsgType msgType,
-                                              const CreateClockCls createClockCls,
+                                              const CreateClockCls& createClockCls,
                                               const bt2::Trace trace)
     {
-        const auto clockCls = createClockCls(self.component());
+        const auto clockCls =
+            createClockCls ? createClockCls(self.component()) : bt2::ClockClass::Shared {};
 
         switch (msgType) {
         case MsgType::StreamBeg:
@@ -95,11 +95,6 @@ __attribute__((used)) const char *format_as(const ClockClsCompatRunIn::MsgType m
     bt_common_abort();
 }
 
-bt2::ClockClass::Shared noClockClass(bt2::SelfComponent) noexcept
-{
-    return bt2::ClockClass::Shared {};
-}
-
 const bt2c::Uuid uuidA {"f00aaf65-ebec-4eeb-85b2-fc255cf1aa8a"};
 const bt2c::Uuid uuidB {"03482981-a77b-4d7b-94c4-592bf9e91785"};
 
@@ -114,8 +109,8 @@ const bt2c::Uuid uuidB {"03482981-a77b-4d7b-94c4-592bf9e91785"};
 void addClkClsCompatTriggers(CondTriggers& triggers)
 {
     const auto addValidCases = [&triggers](
-                                   const ClockClsCompatRunIn::CreateClockCls createClockCls1,
-                                   const ClockClsCompatRunIn::CreateClockCls createClockCls2,
+                                   const ClockClsCompatRunIn::CreateClockCls& createClockCls1,
+                                   const ClockClsCompatRunIn::CreateClockCls& createClockCls2,
                                    const char * const condId) {
         /*
          * Add triggers for all possible combinations of message types.
@@ -129,9 +124,8 @@ void addClkClsCompatTriggers(CondTriggers& triggers)
         };
 
         const auto isInvalidCase = [](const ClockClsCompatRunIn::MsgType msgType,
-                                      const ClockClsCompatRunIn::CreateClockCls createClockCls) {
-            return msgType == ClockClsCompatRunIn::MsgType::MsgIterInactivity &&
-                   createClockCls == noClockClass;
+                                      const ClockClsCompatRunIn::CreateClockCls& createClockCls) {
+            return msgType == ClockClsCompatRunIn::MsgType::MsgIterInactivity && !createClockCls;
         };
 
         for (const auto msgType1 : msgTypes) {
@@ -152,7 +146,7 @@ void addClkClsCompatTriggers(CondTriggers& triggers)
     };
 
     addValidCases(
-        noClockClass,
+        {},
         [](const bt2::SelfComponent self) {
             return self.createClockClass();
         },
@@ -162,7 +156,7 @@ void addClkClsCompatTriggers(CondTriggers& triggers)
         [](const bt2::SelfComponent self) {
             return self.createClockClass();
         },
-        noClockClass,
+        {},
         "message-iterator-class-next-method:stream-class-has-clock-class-with-unix-epoch-origin");
 
     addValidCases(
@@ -184,7 +178,7 @@ void addClkClsCompatTriggers(CondTriggers& triggers)
             clockCls->originIsUnixEpoch(false).uuid(uuidA);
             return clockCls;
         },
-        noClockClass, "message-iterator-class-next-method:stream-class-has-clock-class-with-uuid");
+        {}, "message-iterator-class-next-method:stream-class-has-clock-class-with-uuid");
 
     addValidCases(
         [](const bt2::SelfComponent self) {
@@ -235,7 +229,7 @@ void addClkClsCompatTriggers(CondTriggers& triggers)
             clockCls->originIsUnixEpoch(false);
             return clockCls;
         },
-        noClockClass, "message-iterator-class-next-method:stream-class-has-clock-class");
+        {}, "message-iterator-class-next-method:stream-class-has-clock-class");
 
     addValidCases(
         [](const bt2::SelfComponent self) {
This page took 0.025882 seconds and 4 git commands to generate.