tests: re-format conds 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
7#include <stdlib.h>
8#include <stdio.h>
9#include <string.h>
10#include <assert.h>
11#include <babeltrace2/babeltrace.h>
12#include <glib.h>
13
14#include "common/assert.h"
053db960 15#include "utils.hpp"
5d7e8359 16
486428e8 17typedef void (*run_in_comp_cls_init_func)(bt_self_component *self_comp, void *user_data);
5d7e8359 18
486428e8
SM
19struct comp_cls_init_method_data
20{
21 run_in_comp_cls_init_func func;
22 void *user_data;
5d7e8359
PP
23};
24
486428e8
SM
25static bt_component_class_initialize_method_status
26comp_cls_init(bt_self_component_source *self_comp, bt_self_component_source_configuration *conf,
27 const bt_value *params, void *init_method_data)
5d7e8359 28{
486428e8 29 comp_cls_init_method_data *data = static_cast<comp_cls_init_method_data *>(init_method_data);
5d7e8359 30
486428e8
SM
31 /* Call user function which is expected to abort */
32 data->func(bt_self_component_source_as_self_component(self_comp), data->user_data);
5d7e8359 33
486428e8
SM
34 /* Never reached! */
35 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
5d7e8359
PP
36}
37
486428e8
SM
38static bt_message_iterator_class_next_method_status
39msg_iter_cls_next(bt_self_message_iterator *self_msg_iter, bt_message_array_const msgs,
40 uint64_t capacity, uint64_t *count)
5d7e8359 41{
486428e8
SM
42 /* Not used */
43 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
5d7e8359
PP
44}
45
486428e8 46static void run_in_comp_cls_init(run_in_comp_cls_init_func func, void *user_data)
5d7e8359 47{
486428e8
SM
48 bt_message_iterator_class *msg_iter_cls;
49 bt_component_class_source *comp_cls;
50 bt_component_class_set_method_status set_method_status;
51 bt_graph *graph;
52 struct comp_cls_init_method_data init_method_data = {
53 .func = func,
54 .user_data = user_data,
55 };
56
57 /* Create component class */
58 msg_iter_cls = bt_message_iterator_class_create(msg_iter_cls_next);
59 BT_ASSERT(msg_iter_cls);
60 comp_cls = bt_component_class_source_create("yo", msg_iter_cls);
61 BT_ASSERT(comp_cls);
62 set_method_status = bt_component_class_source_set_initialize_method(comp_cls, comp_cls_init);
63 BT_ASSERT(set_method_status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
64
65 /* Create graph */
66 graph = bt_graph_create(0);
67 BT_ASSERT(graph);
68
69 /*
5d7e8359
PP
70 * Add source component: this calls the initialization method,
71 * calling `func`.
72 */
486428e8
SM
73 (void) bt_graph_add_source_component_with_initialize_method_data(
74 graph, comp_cls, "whatever", NULL, &init_method_data, BT_LOGGING_LEVEL_NONE, NULL);
5d7e8359 75
486428e8 76 /*
5d7e8359
PP
77 * This point is not expected to be reached as func() is
78 * expected to abort.
79 */
80}
81
486428e8 82static void run_in_comp_cls_init_defer(bt_self_component *self_comp, void *user_data)
5d7e8359 83{
486428e8
SM
84 cond_trigger_run_in_comp_cls_init_func user_func =
85 reinterpret_cast<cond_trigger_run_in_comp_cls_init_func>(user_data);
5d7e8359 86
486428e8 87 user_func(self_comp);
5d7e8359
PP
88}
89
486428e8 90static void run_trigger(const struct cond_trigger *trigger)
5d7e8359 91{
486428e8
SM
92 switch (trigger->func_type) {
93 case COND_TRIGGER_FUNC_TYPE_BASIC:
94 trigger->func.basic();
95 break;
96 case COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT:
97 run_in_comp_cls_init(run_in_comp_cls_init_defer,
98 reinterpret_cast<void *>(trigger->func.run_in_comp_cls_init));
99 break;
100 default:
101 abort();
102 }
5d7e8359
PP
103}
104
486428e8 105static void escape_json_string(const char *str, GString *escaped_str)
5d7e8359 106{
486428e8 107 g_string_assign(escaped_str, "");
5d7e8359 108
486428e8
SM
109 for (const char *ch = str; *ch; ch++) {
110 if (*ch == '\\' || *ch == '"') {
111 g_string_append_c(escaped_str, '\\');
112 }
5d7e8359 113
486428e8
SM
114 g_string_append_c(escaped_str, *ch);
115 }
5d7e8359
PP
116}
117
486428e8 118static void list_triggers(const struct cond_trigger triggers[], size_t trigger_count)
5d7e8359 119{
486428e8
SM
120 GString *escaped_str = g_string_new(NULL);
121 size_t i;
5d7e8359 122
486428e8
SM
123 BT_ASSERT(escaped_str);
124 printf("[");
5d7e8359 125
486428e8
SM
126 for (i = 0; i < trigger_count; i++) {
127 const struct cond_trigger *trigger = &triggers[i];
5d7e8359 128
486428e8
SM
129 /* Condition ID */
130 escape_json_string(trigger->cond_id, escaped_str);
131 printf("{\"cond-id\":\"%s\",", escaped_str->str);
5d7e8359 132
486428e8
SM
133 /* Name starts with condition ID */
134 printf("\"name\":\"%s", escaped_str->str);
5d7e8359 135
486428e8
SM
136 if (trigger->suffix) {
137 escape_json_string(trigger->suffix, escaped_str);
138 printf("-%s", escaped_str->str);
139 }
5d7e8359 140
486428e8 141 printf("\"}");
5d7e8359 142
486428e8
SM
143 if (i < trigger_count - 1) {
144 /* Comma between objects */
145 printf(",");
146 }
147 }
5d7e8359 148
486428e8
SM
149 printf("]");
150 g_string_free(escaped_str, TRUE);
151 fflush(stdout);
5d7e8359
PP
152}
153
486428e8
SM
154void cond_main(int argc, const char *argv[], const struct cond_trigger triggers[],
155 size_t trigger_count)
5d7e8359 156{
486428e8
SM
157 BT_ASSERT(argc >= 2);
158
159 if (strcmp(argv[1], "list") == 0) {
160 list_triggers(triggers, trigger_count);
161 } else if (strcmp(argv[1], "run") == 0) {
162 int index;
163
164 BT_ASSERT(argc >= 3);
165 index = atoi(argv[2]);
166 BT_ASSERT(index >= 0 && index < trigger_count);
167 run_trigger(&triggers[index]);
168 }
5d7e8359 169}
This page took 0.039818 seconds and 4 git commands to generate.