Sort includes in C++ files
[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
SM
7#include <iostream>
8
9#include <assert.h>
10#include <glib.h>
5d7e8359 11#include <stdio.h>
c802cacb 12#include <stdlib.h>
5d7e8359 13#include <string.h>
c802cacb 14
5d7e8359 15#include <babeltrace2/babeltrace.h>
5d7e8359
PP
16
17#include "common/assert.h"
4b4f3ca6 18#include "cpp-common/nlohmann/json.hpp"
c802cacb 19
366250bb 20#include "../utils/run-in.hpp"
c802cacb 21#include "utils.hpp"
5d7e8359 22
486428e8 23static void run_trigger(const struct cond_trigger *trigger)
5d7e8359 24{
486428e8
SM
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:
366250bb 30 runInCompClsInit(trigger->func.run_in_comp_cls_init);
486428e8
SM
31 break;
32 default:
33 abort();
34 }
5d7e8359
PP
35}
36
486428e8 37static void list_triggers(const struct cond_trigger triggers[], size_t trigger_count)
5d7e8359 38{
4b4f3ca6 39 nlohmann::json trigger_array = nlohmann::json::array();
5d7e8359 40
4b4f3ca6
SM
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];
5d7e8359 44
486428e8 45 /* Condition ID */
4b4f3ca6 46 trigger_obj["cond-id"] = trigger.cond_id;
5d7e8359 47
486428e8 48 /* Name starts with condition ID */
4b4f3ca6 49 std::string name = trigger.cond_id;
5d7e8359 50
4b4f3ca6
SM
51 if (trigger.suffix) {
52 name += '-';
53 name += trigger.suffix;
486428e8 54 }
5d7e8359 55
4b4f3ca6
SM
56 trigger_obj["name"] = std::move(name);
57 trigger_array.push_back(std::move(trigger_obj));
486428e8 58 }
5d7e8359 59
4b4f3ca6
SM
60 auto str = trigger_array.dump();
61 std::cout << str;
62 std::flush(std::cout);
5d7e8359
PP
63}
64
486428e8
SM
65void cond_main(int argc, const char *argv[], const struct cond_trigger triggers[],
66 size_t trigger_count)
5d7e8359 67{
486428e8
SM
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 }
5d7e8359 80}
This page took 0.038025 seconds and 4 git commands to generate.