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