tests/lib/utils: add RunIn::onMsgIterNext()
[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
484a3024 7#include "common/assert.h"
402b595c
SM
8#include "cpp-common/bt2/component-class-dev.hpp"
9#include "cpp-common/bt2/component-class.hpp"
50761a49 10#include "cpp-common/bt2/graph.hpp"
0133a2ba
SM
11#include "cpp-common/bt2/plugin-load.hpp"
12#include "cpp-common/bt2/plugin.hpp"
5ecc1dc9 13#include "cpp-common/bt2/query-executor.hpp"
0133a2ba 14#include "cpp-common/bt2c/call.hpp"
484a3024 15
c802cacb
SM
16#include "run-in.hpp"
17
5d7e57e8
SM
18void RunIn::onQuery(bt2::SelfComponentClass)
19{
20}
cc610c52 21
5d7e57e8 22void RunIn::onCompInit(bt2::SelfComponent)
484a3024 23{
5d7e57e8
SM
24}
25
26void RunIn::onMsgIterInit(bt2::SelfMessageIterator)
27{
28}
29
a72c7ff6
SM
30void RunIn::onMsgIterNext(bt2::SelfMessageIterator, bt2::ConstMessageArray&)
31{
32}
33
5d7e57e8 34namespace {
484a3024 35
402b595c 36class RunInSource;
484a3024 37
402b595c 38class RunInSourceMsgIter final : public bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource>
484a3024 39{
402b595c
SM
40public:
41 explicit RunInSourceMsgIter(const bt2::SelfMessageIterator self,
42 bt2::SelfMessageIteratorConfiguration,
43 const bt2::SelfComponentOutputPort port) :
a72c7ff6
SM
44 bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource> {self, "RUN-IN-SRC-MSG-ITER"},
45 _mRunIn {&port.data<RunIn>()}, _mSelf {self}
402b595c 46 {
a72c7ff6 47 _mRunIn->onMsgIterInit(self);
484a3024
SM
48 }
49
a72c7ff6 50 void _next(bt2::ConstMessageArray& msgs)
402b595c 51 {
a72c7ff6 52 _mRunIn->onMsgIterNext(_mSelf, msgs);
484a3024 53 }
a72c7ff6
SM
54
55private:
56 RunIn *_mRunIn;
57 bt2::SelfMessageIterator _mSelf;
402b595c 58};
484a3024 59
402b595c 60class RunInSource final :
5d7e57e8 61 public bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, RunIn, RunIn>
484a3024 62{
402b595c
SM
63public:
64 static constexpr auto name = "run-in-src";
65
66 explicit RunInSource(const bt2::SelfSourceComponent self, bt2::ConstMapValue,
5d7e57e8
SM
67 RunIn * const runIn) :
68 bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, RunIn, RunIn> {self,
69 "RUN-IN-SRC"},
70 _mRunIn {runIn}
402b595c 71 {
5d7e57e8
SM
72 this->_addOutputPort("out", *runIn);
73 _mRunIn->onCompInit(self);
484a3024
SM
74 }
75
402b595c 76 static bt2::Value::Shared _query(const bt2::SelfComponentClass self, bt2::PrivateQueryExecutor,
5d7e57e8 77 bt2c::CStringView, bt2::ConstValue, RunIn *data)
402b595c 78 {
5d7e57e8 79 data->onQuery(self);
402b595c
SM
80 return bt2::NullValue {}.shared();
81 }
484a3024 82
402b595c 83private:
5d7e57e8 84 RunIn *_mRunIn;
08b4db41
SM
85};
86
cc610c52
PP
87} /* namespace */
88
5d7e57e8 89void runIn(RunIn& runIn)
484a3024 90{
402b595c 91 const auto srcCompCls = bt2::SourceComponentClass::create<RunInSource>();
484a3024 92
5d7e57e8
SM
93 /* Execute a query */
94 bt2::QueryExecutor::create(*srcCompCls, "object-name", runIn)->query();
484a3024
SM
95
96 /* Create graph */
50761a49 97 const auto graph = bt2::Graph::create(0);
484a3024
SM
98
99 /* Add custom source component (executes `compCtxFunc`) */
5d7e57e8 100 const auto srcComp = graph->addComponent(*srcCompCls, "the-source", runIn);
484a3024 101
08b4db41 102 /* Add dummy sink component */
0133a2ba
SM
103 const auto sinkComp = bt2c::call([&] {
104 const auto utilsPlugin = bt2::findPlugin("utils");
105
106 BT_ASSERT(utilsPlugin);
107
108 const auto dummySinkCompCls = utilsPlugin->sinkComponentClasses()["dummy"];
109
110 BT_ASSERT(dummySinkCompCls);
111
112 return graph->addComponent(*dummySinkCompCls, "the-sink");
113 });
484a3024
SM
114
115 /* Connect ports */
50761a49
SM
116 const auto outPort = srcComp.outputPorts()["out"];
117 BT_ASSERT(outPort);
484a3024 118
50761a49
SM
119 const auto inPort = sinkComp.inputPorts()["in"];
120 BT_ASSERT(inPort);
484a3024 121
50761a49 122 graph->connectPorts(*outPort, *inPort);
484a3024
SM
123
124 /* Run graph (executes `msgIterCtxFunc`) */
50761a49 125 graph->run();
484a3024 126}
This page took 0.03729 seconds and 4 git commands to generate.