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