tests/lib: C++ify run-in and condition trigger code
[babeltrace.git] / tests / lib / conds / utils.cpp
index 4df4340c3565624552302948ed9f2fd9dd542fbf..28f0ba40748eecf0000e2811817eea603ac08546 100644 (file)
 #include "cpp-common/vendor/fmt/core.h"
 #include "cpp-common/vendor/nlohmann/json.hpp"
 
-#include "../utils/run-in.hpp"
 #include "utils.hpp"
 
-namespace {
-
-void runTrigger(const cond_trigger& trigger) noexcept
+CondTrigger::CondTrigger(const Type type, const std::string& condId,
+                         const bt2s::optional<std::string>& nameSuffix) noexcept :
+    _mType {type},
+    _mCondId {fmt::format("{}:{}", type == Type::PRE ? "pre" : "post", condId)},
+    _mName {
+        fmt::format("{}{}{}", condId, nameSuffix ? "-" : "", nameSuffix ? nameSuffix->data() : "")}
 {
-    switch (trigger.func_type) {
-    case COND_TRIGGER_FUNC_TYPE_BASIC:
-        trigger.func.basic();
-        break;
-    case COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT:
-        runInCompClsInit(trigger.func.run_in_comp_cls_init);
-        break;
-    default:
-        abort();
-    }
 }
 
-void listTriggers(const bt2s::span<const cond_trigger> triggers) noexcept
+SimpleCondTrigger::SimpleCondTrigger(std::function<void()> func, const Type type,
+                                     const std::string& condId,
+                                     const bt2s::optional<std::string>& nameSuffix) :
+    CondTrigger {type, condId, nameSuffix},
+    _mFunc {std::move(func)}
 {
-    auto triggerArray = nlohmann::json::array();
-
-    for (auto& trigger : triggers) {
-        auto triggerObj = nlohmann::json::object();
-
-        /* Condition ID */
-        triggerObj["cond-id"] = trigger.cond_id;
+}
 
-        /* Name starts with condition ID */
-        std::string name = trigger.cond_id;
+namespace {
 
-        if (trigger.suffix) {
-            name += '-';
-            name += trigger.suffix;
-        }
+void listCondTriggers(const CondTriggers condTriggers) noexcept
+{
+    auto condTriggerArray = nlohmann::json::array();
 
-        triggerObj["name"] = std::move(name);
-        triggerArray.push_back(std::move(triggerObj));
+    for (const auto condTrigger : condTriggers) {
+        condTriggerArray.push_back(nlohmann::json {
+            {"cond-id", condTrigger->condId()},
+            {"name", condTrigger->name()},
+        });
     }
 
-    fmt::println("{}", triggerArray.dump());
+    fmt::println("{}", condTriggerArray.dump());
 }
 
 } /* namespace */
 
-void condMain(const int argc, const char ** const argv,
-              const bt2s::span<const cond_trigger> triggers) noexcept
+void condMain(const int argc, const char ** const argv, const CondTriggers condTriggers) noexcept
 {
     BT_ASSERT(argc >= 2);
 
     if (strcmp(argv[1], "list") == 0) {
-        listTriggers(triggers);
+        listCondTriggers(condTriggers);
     } else if (strcmp(argv[1], "run") == 0) {
-        int index;
-
         BT_ASSERT(argc >= 3);
-        index = atoi(argv[2]);
-        BT_ASSERT(index >= 0 && index < triggers.size());
-        runTrigger(triggers[index]);
+
+        const auto index = atoi(argv[2]);
+
+        BT_ASSERT(index >= 0 && index < condTriggers.size());
+        (*condTriggers[index])();
     }
 }
This page took 0.024306 seconds and 4 git commands to generate.