cpp-common: move `nlohmann/json.hpp` to `vendor`
[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 <iostream>
8
9 #include <glib.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include <babeltrace2/babeltrace.h>
15
16 #include "common/assert.h"
17 #include "cpp-common/vendor/nlohmann/json.hpp"
18
19 #include "../utils/run-in.hpp"
20 #include "utils.hpp"
21
22 static void run_trigger(const struct cond_trigger *trigger)
23 {
24 switch (trigger->func_type) {
25 case COND_TRIGGER_FUNC_TYPE_BASIC:
26 trigger->func.basic();
27 break;
28 case COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT:
29 runInCompClsInit(trigger->func.run_in_comp_cls_init);
30 break;
31 default:
32 abort();
33 }
34 }
35
36 static void list_triggers(const struct cond_trigger triggers[], size_t trigger_count)
37 {
38 nlohmann::json trigger_array = nlohmann::json::array();
39
40 for (size_t i = 0; i < trigger_count; i++) {
41 nlohmann::json trigger_obj = nlohmann::json::object();
42 const cond_trigger& trigger = triggers[i];
43
44 /* Condition ID */
45 trigger_obj["cond-id"] = trigger.cond_id;
46
47 /* Name starts with condition ID */
48 std::string name = trigger.cond_id;
49
50 if (trigger.suffix) {
51 name += '-';
52 name += trigger.suffix;
53 }
54
55 trigger_obj["name"] = std::move(name);
56 trigger_array.push_back(std::move(trigger_obj));
57 }
58
59 auto str = trigger_array.dump();
60 std::cout << str;
61 std::flush(std::cout);
62 }
63
64 void cond_main(int argc, const char *argv[], const struct cond_trigger triggers[],
65 size_t trigger_count)
66 {
67 BT_ASSERT(argc >= 2);
68
69 if (strcmp(argv[1], "list") == 0) {
70 list_triggers(triggers, trigger_count);
71 } else if (strcmp(argv[1], "run") == 0) {
72 int index;
73
74 BT_ASSERT(argc >= 3);
75 index = atoi(argv[2]);
76 BT_ASSERT(index >= 0 && index < trigger_count);
77 run_trigger(&triggers[index]);
78 }
79 }
This page took 0.031825 seconds and 4 git commands to generate.