Sort includes in C++ files
[babeltrace.git] / tests / lib / utils / run-in.cpp
CommitLineData
484a3024
SM
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2020-2023 EfficiOS, inc.
5 */
6
7#include <utility>
8
484a3024
SM
9#include "common/assert.h"
10
c802cacb
SM
11#include "run-in.hpp"
12
cc610c52
PP
13namespace {
14
484a3024
SM
15struct RunInData final
16{
17 RunInCompClsQueryFunc compClsCtxFunc;
18 RunInCompClsInitFunc compCtxFunc;
19 RunInMsgIterClsInitFunc msgIterCtxFunc;
20};
21
cc610c52 22const RunInData& runInDataFromMethodData(void * const methodData)
484a3024
SM
23{
24 return *static_cast<const RunInData *>(methodData);
25}
26
cc610c52
PP
27bt_component_class_initialize_method_status compClsInit(bt_self_component_source * const selfComp,
28 bt_self_component_source_configuration *,
29 const bt_value *,
30 void * const initMethodData)
484a3024
SM
31{
32 const auto status =
33 bt_self_component_source_add_output_port(selfComp, "out", initMethodData, nullptr);
34
35 BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK);
36
37 auto& data = runInDataFromMethodData(initMethodData);
38
39 if (data.compCtxFunc) {
40 data.compCtxFunc(bt_self_component_source_as_self_component(selfComp));
41 }
42
43 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
44}
45
cc610c52 46bt_component_class_query_method_status
484a3024
SM
47compClsQuery(bt_self_component_class_source * const selfCompCls, bt_private_query_executor *,
48 const char *, const bt_value *, void * const methodData,
49 const bt_value ** const result)
50{
51 auto& data = runInDataFromMethodData(methodData);
52
53 if (data.compClsCtxFunc) {
54 data.compClsCtxFunc(bt_self_component_class_source_as_self_component_class(selfCompCls));
55 }
56
57 *result = bt_value_null;
58 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
59}
60
cc610c52 61bt_message_iterator_class_initialize_method_status
484a3024
SM
62msgIterClsInit(bt_self_message_iterator * const selfMsgIter,
63 bt_self_message_iterator_configuration *, bt_self_component_port_output * const port)
64{
65 auto& data = runInDataFromMethodData(bt_self_component_port_get_data(
66 bt_self_component_port_output_as_self_component_port(port)));
67
68 if (data.msgIterCtxFunc) {
69 data.msgIterCtxFunc(selfMsgIter);
70 }
71
72 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
73}
74
cc610c52 75bt_message_iterator_class_next_method_status
484a3024
SM
76msgIterClsNext(bt_self_message_iterator *, bt_message_array_const, uint64_t, uint64_t *)
77{
78 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
79}
80
08b4db41
SM
81struct DummySinkData
82{
83 bt_message_iterator *msgIter;
84};
85
cc610c52
PP
86bt_component_class_initialize_method_status dummySinkInit(bt_self_component_sink * const self,
87 bt_self_component_sink_configuration *,
88 const bt_value *,
89 void * const initMethodData)
08b4db41
SM
90{
91 const auto status = bt_self_component_sink_add_input_port(self, "in", NULL, nullptr);
cc610c52 92
08b4db41
SM
93 BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK);
94 bt_self_component_set_data(bt_self_component_sink_as_self_component(self), initMethodData);
95 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
96}
97
cc610c52 98DummySinkData& dummySinkDataFromSelfCompSink(bt_self_component_sink * const self)
08b4db41
SM
99{
100 return *static_cast<DummySinkData *>(
101 bt_self_component_get_data(bt_self_component_sink_as_self_component(self)));
102}
103
cc610c52 104bt_component_class_sink_graph_is_configured_method_status
0ab97251 105dummySinkGraphIsConfigured(bt_self_component_sink * const self)
08b4db41 106{
08b4db41 107 const auto port = bt_self_component_sink_borrow_input_port_by_name(self, "in");
cc610c52 108
08b4db41 109 BT_ASSERT(port);
cc610c52
PP
110
111 const auto status = bt_message_iterator_create_from_sink_component(
112 self, port, &dummySinkDataFromSelfCompSink(self).msgIter);
113
08b4db41
SM
114 BT_ASSERT(status == BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK);
115 return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
116}
117
cc610c52 118bt_component_class_sink_consume_method_status dummySinkConsume(bt_self_component_sink * const self)
08b4db41 119{
08b4db41
SM
120 bt_message_array_const msgs;
121 uint64_t msgCount;
cc610c52
PP
122 const auto status =
123 bt_message_iterator_next(dummySinkDataFromSelfCompSink(self).msgIter, &msgs, &msgCount);
124
08b4db41
SM
125 BT_ASSERT(status == BT_MESSAGE_ITERATOR_NEXT_STATUS_END);
126 return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
127}
128
cc610c52
PP
129} /* namespace */
130
484a3024
SM
131void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFunc,
132 RunInMsgIterClsInitFunc msgIterCtxFunc)
133{
134 RunInData data {std::move(compClsCtxFunc), std::move(compCtxFunc), std::move(msgIterCtxFunc)};
135
136 /* Create and configure custom source component class */
137 const auto msgIterCls = bt_message_iterator_class_create(msgIterClsNext);
138
139 BT_ASSERT(msgIterCls);
140
141 {
142 const auto status =
143 bt_message_iterator_class_set_initialize_method(msgIterCls, msgIterClsInit);
144
145 BT_ASSERT(status == BT_MESSAGE_ITERATOR_CLASS_SET_METHOD_STATUS_OK);
146 }
147
148 const auto srcCompCls = bt_component_class_source_create("yo", msgIterCls);
149
150 BT_ASSERT(srcCompCls);
151
152 {
153 const auto status =
154 bt_component_class_source_set_initialize_method(srcCompCls, compClsInit);
155
156 BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
157 }
158
159 {
160 const auto status = bt_component_class_source_set_query_method(srcCompCls, compClsQuery);
161
162 BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
163 }
164
165 /* Execute a query (executes `compClsCtxFunc`) */
166 {
167 const auto queryExec = bt_query_executor_create_with_method_data(
168 bt_component_class_source_as_component_class(srcCompCls), "", nullptr, &data);
169
170 BT_ASSERT(queryExec);
171
172 const bt_value *queryRes;
173 const auto status = bt_query_executor_query(queryExec, &queryRes);
174
175 BT_ASSERT(status == BT_QUERY_EXECUTOR_QUERY_STATUS_OK);
484a3024
SM
176 bt_value_put_ref(queryRes);
177 bt_query_executor_put_ref(queryExec);
178 }
179
08b4db41
SM
180 /* Create a dummy sink component */
181 const auto sinkCompCls = bt_component_class_sink_create("dummy", dummySinkConsume);
cc610c52 182
08b4db41
SM
183 BT_ASSERT(sinkCompCls);
184
185 {
186 const auto status =
187 bt_component_class_sink_set_initialize_method(sinkCompCls, dummySinkInit);
cc610c52 188
08b4db41
SM
189 BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
190 }
191
192 {
193 const auto status = bt_component_class_sink_set_graph_is_configured_method(
194 sinkCompCls, dummySinkGraphIsConfigured);
cc610c52 195
08b4db41
SM
196 BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
197 }
198
484a3024
SM
199 /* Create graph */
200 const auto graph = bt_graph_create(0);
201
202 BT_ASSERT(graph);
203
204 /* Add custom source component (executes `compCtxFunc`) */
205 const bt_component_source *srcComp;
206
207 {
208 const auto status = bt_graph_add_source_component_with_initialize_method_data(
08b4db41 209 graph, srcCompCls, "the-source", NULL, &data, BT_LOGGING_LEVEL_NONE, &srcComp);
484a3024
SM
210
211 BT_ASSERT(status == BT_GRAPH_ADD_COMPONENT_STATUS_OK);
212 }
213
08b4db41 214 /* Add dummy sink component */
484a3024 215 const bt_component_sink *sinkComp;
08b4db41 216 DummySinkData dummySinkData;
484a3024
SM
217
218 {
08b4db41
SM
219 const auto status = bt_graph_add_sink_component_with_initialize_method_data(
220 graph, sinkCompCls, "the-sink", NULL, &dummySinkData, BT_LOGGING_LEVEL_NONE, &sinkComp);
cc610c52 221
484a3024
SM
222 BT_ASSERT(status == BT_GRAPH_ADD_COMPONENT_STATUS_OK);
223 }
224
225 /* Connect ports */
226 {
227 const auto outPort = bt_component_source_borrow_output_port_by_name_const(srcComp, "out");
228
229 BT_ASSERT(outPort);
230
231 const auto inPort = bt_component_sink_borrow_input_port_by_name_const(sinkComp, "in");
232
233 BT_ASSERT(inPort);
234
235 const auto status = bt_graph_connect_ports(graph, outPort, inPort, nullptr);
236
237 BT_ASSERT(status == BT_GRAPH_CONNECT_PORTS_STATUS_OK);
238 }
239
240 /* Run graph (executes `msgIterCtxFunc`) */
cc610c52
PP
241 {
242 const auto status = bt_graph_run(graph);
484a3024 243
cc610c52
PP
244 BT_ASSERT(status == BT_GRAPH_RUN_STATUS_OK);
245 }
484a3024 246
cc610c52 247 /* Discard owned objects */
484a3024
SM
248 bt_graph_put_ref(graph);
249 bt_component_class_source_put_ref(srcCompCls);
08b4db41 250 bt_component_class_sink_put_ref(sinkCompCls);
484a3024
SM
251 bt_message_iterator_class_put_ref(msgIterCls);
252}
253
254void runInCompClsQuery(RunInCompClsQueryFunc func)
255{
256 runIn(std::move(func), nullptr, nullptr);
257}
258
259void runInCompClsInit(RunInCompClsInitFunc func)
260{
261 runIn(nullptr, std::move(func), nullptr);
262}
263
264void runInMsgIterClsInit(RunInMsgIterClsInitFunc func)
265{
266 runIn(nullptr, nullptr, std::move(func));
267}
This page took 0.038353 seconds and 4 git commands to generate.