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