tests/lib: C++ify run-in and condition trigger code
[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
30namespace {
484a3024 31
402b595c 32class RunInSource;
484a3024 33
402b595c 34class RunInSourceMsgIter final : public bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource>
484a3024 35{
402b595c
SM
36public:
37 explicit RunInSourceMsgIter(const bt2::SelfMessageIterator self,
38 bt2::SelfMessageIteratorConfiguration,
39 const bt2::SelfComponentOutputPort port) :
40 bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource> {self, "RUN-IN-SRC-MSG-ITER"}
41 {
5d7e57e8 42 port.data<RunIn>().onMsgIterInit(self);
484a3024
SM
43 }
44
402b595c
SM
45 void _next(bt2::ConstMessageArray&)
46 {
484a3024 47 }
402b595c 48};
484a3024 49
402b595c 50class RunInSource final :
5d7e57e8 51 public bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, RunIn, RunIn>
484a3024 52{
402b595c
SM
53public:
54 static constexpr auto name = "run-in-src";
55
56 explicit RunInSource(const bt2::SelfSourceComponent self, bt2::ConstMapValue,
5d7e57e8
SM
57 RunIn * const runIn) :
58 bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, RunIn, RunIn> {self,
59 "RUN-IN-SRC"},
60 _mRunIn {runIn}
402b595c 61 {
5d7e57e8
SM
62 this->_addOutputPort("out", *runIn);
63 _mRunIn->onCompInit(self);
484a3024
SM
64 }
65
402b595c 66 static bt2::Value::Shared _query(const bt2::SelfComponentClass self, bt2::PrivateQueryExecutor,
5d7e57e8 67 bt2c::CStringView, bt2::ConstValue, RunIn *data)
402b595c 68 {
5d7e57e8 69 data->onQuery(self);
402b595c
SM
70 return bt2::NullValue {}.shared();
71 }
484a3024 72
402b595c 73private:
5d7e57e8 74 RunIn *_mRunIn;
08b4db41
SM
75};
76
cc610c52
PP
77} /* namespace */
78
5d7e57e8 79void runIn(RunIn& runIn)
484a3024 80{
402b595c 81 const auto srcCompCls = bt2::SourceComponentClass::create<RunInSource>();
484a3024 82
5d7e57e8
SM
83 /* Execute a query */
84 bt2::QueryExecutor::create(*srcCompCls, "object-name", runIn)->query();
484a3024
SM
85
86 /* Create graph */
50761a49 87 const auto graph = bt2::Graph::create(0);
484a3024
SM
88
89 /* Add custom source component (executes `compCtxFunc`) */
5d7e57e8 90 const auto srcComp = graph->addComponent(*srcCompCls, "the-source", runIn);
484a3024 91
08b4db41 92 /* Add dummy sink component */
0133a2ba
SM
93 const auto sinkComp = bt2c::call([&] {
94 const auto utilsPlugin = bt2::findPlugin("utils");
95
96 BT_ASSERT(utilsPlugin);
97
98 const auto dummySinkCompCls = utilsPlugin->sinkComponentClasses()["dummy"];
99
100 BT_ASSERT(dummySinkCompCls);
101
102 return graph->addComponent(*dummySinkCompCls, "the-sink");
103 });
484a3024
SM
104
105 /* Connect ports */
50761a49
SM
106 const auto outPort = srcComp.outputPorts()["out"];
107 BT_ASSERT(outPort);
484a3024 108
50761a49
SM
109 const auto inPort = sinkComp.inputPorts()["in"];
110 BT_ASSERT(inPort);
484a3024 111
50761a49 112 graph->connectPorts(*outPort, *inPort);
484a3024
SM
113
114 /* Run graph (executes `msgIterCtxFunc`) */
50761a49 115 graph->run();
484a3024 116}
This page took 0.036251 seconds and 4 git commands to generate.