tests/lib: C++ify `conds/utils.cpp` a bit
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 12 Feb 2024 22:07:35 +0000 (17:07 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Mon, 19 Feb 2024 18:10:15 +0000 (13:10 -0500)
Change `utils.cpp` to fit a bit more our C++ style.

 - use an anonymous namespace
 - use camel-case for functions
 - use `bt2s::span` for the list of triggers
 - declare functions as `noexcept`

I didn't update the contents of `utils.hpp`, because I expect this to
change in some following patches, while I expect `utils.cpp` to stay
pretty much as-is.

Change-Id: I9dc01a22b3e9ceb2d90cd35c9f6ca93d1afe248f
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11794
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
tests/lib/Makefile.am
tests/lib/conds/conds-triggers.cpp
tests/lib/conds/utils.cpp
tests/lib/conds/utils.hpp

index 3f3273b384cee9e35d41aa430ff722b19c97fbc0..3eed8c8c2b1bfe23805bfe051cbde888f74182b3 100644 (file)
@@ -128,7 +128,8 @@ conds_conds_triggers_SOURCES = \
 
 conds_conds_triggers_LDADD = \
        $(COMMON_TEST_LDADD) \
-       $(top_builddir)/src/lib/libbabeltrace2.la
+       $(top_builddir)/src/lib/libbabeltrace2.la \
+       $(top_builddir)/src/cpp-common/vendor/fmt/libfmt.la
 
 dist_check_SCRIPTS += conds/test-conds.sh conds/test.py
 
index 79aa5e6ca3679c550402a34bf55c2206b19c6030..343c2be64fc9644760c3853939f6493870576201 100644 (file)
@@ -50,6 +50,6 @@ const cond_trigger triggers[] = {
 
 int main(int argc, const char *argv[])
 {
-    cond_main(argc, argv, triggers, sizeof(triggers) / sizeof(*triggers));
+    condMain(argc, argv, triggers);
     return 0;
 }
index 7a70e72f4a01a372d7367a6608e700f2cf077416..4df4340c3565624552302948ed9f2fd9dd542fbf 100644 (file)
@@ -4,45 +4,43 @@
  * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
  */
 
-#include <iostream>
-
-#include <glib.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <babeltrace2/babeltrace.h>
 
 #include "common/assert.h"
+#include "cpp-common/vendor/fmt/core.h"
 #include "cpp-common/vendor/nlohmann/json.hpp"
 
 #include "../utils/run-in.hpp"
 #include "utils.hpp"
 
-static void run_trigger(const struct cond_trigger *trigger)
+namespace {
+
+void runTrigger(const cond_trigger& trigger) noexcept
 {
-    switch (trigger->func_type) {
+    switch (trigger.func_type) {
     case COND_TRIGGER_FUNC_TYPE_BASIC:
-        trigger->func.basic();
+        trigger.func.basic();
         break;
     case COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT:
-        runInCompClsInit(trigger->func.run_in_comp_cls_init);
+        runInCompClsInit(trigger.func.run_in_comp_cls_init);
         break;
     default:
         abort();
     }
 }
 
-static void list_triggers(const struct cond_trigger triggers[], size_t trigger_count)
+void listTriggers(const bt2s::span<const cond_trigger> triggers) noexcept
 {
-    nlohmann::json trigger_array = nlohmann::json::array();
+    auto triggerArray = nlohmann::json::array();
 
-    for (size_t i = 0; i < trigger_count; i++) {
-        nlohmann::json trigger_obj = nlohmann::json::object();
-        const cond_trigger& trigger = triggers[i];
+    for (auto& trigger : triggers) {
+        auto triggerObj = nlohmann::json::object();
 
         /* Condition ID */
-        trigger_obj["cond-id"] = trigger.cond_id;
+        triggerObj["cond-id"] = trigger.cond_id;
 
         /* Name starts with condition ID */
         std::string name = trigger.cond_id;
@@ -52,28 +50,28 @@ static void list_triggers(const struct cond_trigger triggers[], size_t trigger_c
             name += trigger.suffix;
         }
 
-        trigger_obj["name"] = std::move(name);
-        trigger_array.push_back(std::move(trigger_obj));
+        triggerObj["name"] = std::move(name);
+        triggerArray.push_back(std::move(triggerObj));
     }
 
-    auto str = trigger_array.dump();
-    std::cout << str;
-    std::flush(std::cout);
+    fmt::println("{}", triggerArray.dump());
 }
 
-void cond_main(int argc, const char *argv[], const struct cond_trigger triggers[],
-               size_t trigger_count)
+} /* namespace */
+
+void condMain(const int argc, const char ** const argv,
+              const bt2s::span<const cond_trigger> triggers) noexcept
 {
     BT_ASSERT(argc >= 2);
 
     if (strcmp(argv[1], "list") == 0) {
-        list_triggers(triggers, trigger_count);
+        listTriggers(triggers);
     } else if (strcmp(argv[1], "run") == 0) {
         int index;
 
         BT_ASSERT(argc >= 3);
         index = atoi(argv[2]);
-        BT_ASSERT(index >= 0 && index < trigger_count);
-        run_trigger(&triggers[index]);
+        BT_ASSERT(index >= 0 && index < triggers.size());
+        runTrigger(triggers[index]);
     }
 }
index 6ba7401fc66a0f94e167eabf7557e4fdd72190c6..53676d8c01e09828b9c57e3b019ef4deffa339f0 100644 (file)
@@ -10,6 +10,7 @@
 #include <babeltrace2/babeltrace.h>
 
 #include "cpp-common/bt2/self-component-port.hpp"
+#include "cpp-common/bt2s/span.hpp"
 
 enum cond_trigger_func_type
 {
@@ -71,7 +72,6 @@ struct cond_trigger
         }                                                                                          \
     }
 
-void cond_main(int argc, const char *argv[], const struct cond_trigger triggers[],
-               size_t trigger_count);
+void condMain(int argc, const char **argv, bt2s::span<const cond_trigger> triggers) noexcept;
 
 #endif /* TESTS_LIB_CONDS_UTILS_H */
This page took 0.027413 seconds and 4 git commands to generate.