tests/lib: create and manipulate graph using C++ bindings in `utils/run-in.cpp`
[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"
402b595c
SM
10#include "cpp-common/bt2/component-class-dev.hpp"
11#include "cpp-common/bt2/component-class.hpp"
50761a49 12#include "cpp-common/bt2/graph.hpp"
484a3024 13
c802cacb
SM
14#include "run-in.hpp"
15
cc610c52
PP
16namespace {
17
484a3024
SM
18struct RunInData final
19{
20 RunInCompClsQueryFunc compClsCtxFunc;
21 RunInCompClsInitFunc compCtxFunc;
22 RunInMsgIterClsInitFunc msgIterCtxFunc;
23};
24
402b595c 25class RunInSource;
484a3024 26
402b595c 27class RunInSourceMsgIter final : public bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource>
484a3024 28{
402b595c
SM
29public:
30 explicit RunInSourceMsgIter(const bt2::SelfMessageIterator self,
31 bt2::SelfMessageIteratorConfiguration,
32 const bt2::SelfComponentOutputPort port) :
33 bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource> {self, "RUN-IN-SRC-MSG-ITER"}
34 {
35 const auto& data = port.data<const RunInData>();
484a3024 36
402b595c
SM
37 if (data.msgIterCtxFunc) {
38 data.msgIterCtxFunc(self);
39 }
484a3024
SM
40 }
41
402b595c
SM
42 void _next(bt2::ConstMessageArray&)
43 {
484a3024 44 }
402b595c 45};
484a3024 46
402b595c
SM
47class RunInSource final :
48 public bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, const RunInData,
49 const RunInData>
484a3024 50{
402b595c
SM
51public:
52 static constexpr auto name = "run-in-src";
53
54 explicit RunInSource(const bt2::SelfSourceComponent self, bt2::ConstMapValue,
55 const RunInData * const runInData) :
56 bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, const RunInData,
57 const RunInData> {self, "RUN-IN-SRC"},
58 _mRunInData {runInData}
59 {
60 this->_addOutputPort("out", *runInData);
484a3024 61
402b595c
SM
62 if (_mRunInData->compCtxFunc) {
63 _mRunInData->compCtxFunc(self);
64 }
484a3024
SM
65 }
66
402b595c
SM
67 static bt2::Value::Shared _query(const bt2::SelfComponentClass self, bt2::PrivateQueryExecutor,
68 bt2c::CStringView, bt2::ConstValue,
69 const RunInData * const data)
70 {
71 if (data->compClsCtxFunc) {
72 data->compClsCtxFunc(self);
73 }
484a3024 74
402b595c
SM
75 return bt2::NullValue {}.shared();
76 }
484a3024 77
402b595c
SM
78private:
79 const RunInData *_mRunInData;
08b4db41
SM
80};
81
402b595c 82class DummySink : public bt2::UserSinkComponent<DummySink>
08b4db41 83{
402b595c
SM
84public:
85 static constexpr auto name = "dummy";
cc610c52 86
402b595c
SM
87 explicit DummySink(const bt2::SelfSinkComponent self, bt2::ConstMapValue, void *) :
88 bt2::UserSinkComponent<DummySink>(self, "DUMMY-SINK")
89 {
90 this->_addInputPort("in");
91 }
cc610c52 92
402b595c
SM
93 void _graphIsConfigured()
94 {
95 _mMsgIter = this->_createMessageIterator(this->_inputPorts()["in"]);
96 }
08b4db41 97
402b595c
SM
98 bool _consume()
99 {
100 return _mMsgIter->next().has_value();
101 }
cc610c52 102
402b595c
SM
103private:
104 bt2::MessageIterator::Shared _mMsgIter;
105};
08b4db41 106
cc610c52
PP
107} /* namespace */
108
484a3024
SM
109void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFunc,
110 RunInMsgIterClsInitFunc msgIterCtxFunc)
111{
112 RunInData data {std::move(compClsCtxFunc), std::move(compCtxFunc), std::move(msgIterCtxFunc)};
402b595c 113 const auto srcCompCls = bt2::SourceComponentClass::create<RunInSource>();
484a3024
SM
114
115 /* Execute a query (executes `compClsCtxFunc`) */
116 {
117 const auto queryExec = bt_query_executor_create_with_method_data(
402b595c
SM
118 bt_component_class_source_as_component_class(srcCompCls->libObjPtr()), "", nullptr,
119 &data);
484a3024
SM
120
121 BT_ASSERT(queryExec);
122
123 const bt_value *queryRes;
124 const auto status = bt_query_executor_query(queryExec, &queryRes);
125
126 BT_ASSERT(status == BT_QUERY_EXECUTOR_QUERY_STATUS_OK);
484a3024
SM
127 bt_value_put_ref(queryRes);
128 bt_query_executor_put_ref(queryExec);
129 }
130
131 /* Create graph */
50761a49 132 const auto graph = bt2::Graph::create(0);
484a3024
SM
133
134 /* Add custom source component (executes `compCtxFunc`) */
50761a49 135 const auto srcComp = graph->addComponent(*srcCompCls, "the-source", data);
484a3024 136
08b4db41 137 /* Add dummy sink component */
50761a49
SM
138 const auto sinkComp =
139 graph->addComponent(*bt2::SinkComponentClass::create<DummySink>(), "the-sink");
484a3024
SM
140
141 /* Connect ports */
50761a49
SM
142 const auto outPort = srcComp.outputPorts()["out"];
143 BT_ASSERT(outPort);
484a3024 144
50761a49
SM
145 const auto inPort = sinkComp.inputPorts()["in"];
146 BT_ASSERT(inPort);
484a3024 147
50761a49 148 graph->connectPorts(*outPort, *inPort);
484a3024
SM
149
150 /* Run graph (executes `msgIterCtxFunc`) */
50761a49 151 graph->run();
484a3024
SM
152}
153
154void runInCompClsQuery(RunInCompClsQueryFunc func)
155{
156 runIn(std::move(func), nullptr, nullptr);
157}
158
159void runInCompClsInit(RunInCompClsInitFunc func)
160{
161 runIn(nullptr, std::move(func), nullptr);
162}
163
164void runInMsgIterClsInit(RunInMsgIterClsInitFunc func)
165{
166 runIn(nullptr, nullptr, std::move(func));
167}
This page took 0.038118 seconds and 4 git commands to generate.