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>
conds_conds_triggers_LDADD = \
$(COMMON_TEST_LDADD) \
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
dist_check_SCRIPTS += conds/test-conds.sh conds/test.py
int main(int argc, const char *argv[])
{
int main(int argc, const char *argv[])
{
- cond_main(argc, argv, triggers, sizeof(triggers) / sizeof(*triggers));
+ condMain(argc, argv, triggers);
* Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
*/
* 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 <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"
#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:
case COND_TRIGGER_FUNC_TYPE_BASIC:
break;
case COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT:
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();
}
}
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();
- trigger_obj["cond-id"] = trigger.cond_id;
+ triggerObj["cond-id"] = trigger.cond_id;
/* Name starts with condition ID */
std::string name = trigger.cond_id;
/* Name starts with condition ID */
std::string name = trigger.cond_id;
name += trigger.suffix;
}
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) {
{
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]);
} 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]);
#include <babeltrace2/babeltrace.h>
#include "cpp-common/bt2/self-component-port.hpp"
#include <babeltrace2/babeltrace.h>
#include "cpp-common/bt2/self-component-port.hpp"
+#include "cpp-common/bt2s/span.hpp"
enum cond_trigger_func_type
{
enum cond_trigger_func_type
{
-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 */
#endif /* TESTS_LIB_CONDS_UTILS_H */