tests/lib: C++ify `conds/utils.cpp` a bit
[babeltrace.git] / tests / lib / conds / utils.cpp
1 /*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include <babeltrace2/babeltrace.h>
11
12 #include "common/assert.h"
13 #include "cpp-common/vendor/fmt/core.h"
14 #include "cpp-common/vendor/nlohmann/json.hpp"
15
16 #include "../utils/run-in.hpp"
17 #include "utils.hpp"
18
19 namespace {
20
21 void runTrigger(const cond_trigger& trigger) noexcept
22 {
23 switch (trigger.func_type) {
24 case COND_TRIGGER_FUNC_TYPE_BASIC:
25 trigger.func.basic();
26 break;
27 case COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT:
28 runInCompClsInit(trigger.func.run_in_comp_cls_init);
29 break;
30 default:
31 abort();
32 }
33 }
34
35 void listTriggers(const bt2s::span<const cond_trigger> triggers) noexcept
36 {
37 auto triggerArray = nlohmann::json::array();
38
39 for (auto& trigger : triggers) {
40 auto triggerObj = nlohmann::json::object();
41
42 /* Condition ID */
43 triggerObj["cond-id"] = trigger.cond_id;
44
45 /* Name starts with condition ID */
46 std::string name = trigger.cond_id;
47
48 if (trigger.suffix) {
49 name += '-';
50 name += trigger.suffix;
51 }
52
53 triggerObj["name"] = std::move(name);
54 triggerArray.push_back(std::move(triggerObj));
55 }
56
57 fmt::println("{}", triggerArray.dump());
58 }
59
60 } /* namespace */
61
62 void condMain(const int argc, const char ** const argv,
63 const bt2s::span<const cond_trigger> triggers) noexcept
64 {
65 BT_ASSERT(argc >= 2);
66
67 if (strcmp(argv[1], "list") == 0) {
68 listTriggers(triggers);
69 } else if (strcmp(argv[1], "run") == 0) {
70 int index;
71
72 BT_ASSERT(argc >= 3);
73 index = atoi(argv[2]);
74 BT_ASSERT(index >= 0 && index < triggers.size());
75 runTrigger(triggers[index]);
76 }
77 }
This page took 0.033287 seconds and 4 git commands to generate.