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