tests/lib: C++ify run-in and condition trigger code
[babeltrace.git] / tests / lib / utils / run-in.cpp
1 /*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2020-2023 EfficiOS, inc.
5 */
6
7 #include "common/assert.h"
8 #include "cpp-common/bt2/component-class-dev.hpp"
9 #include "cpp-common/bt2/component-class.hpp"
10 #include "cpp-common/bt2/graph.hpp"
11 #include "cpp-common/bt2/plugin-load.hpp"
12 #include "cpp-common/bt2/plugin.hpp"
13 #include "cpp-common/bt2/query-executor.hpp"
14 #include "cpp-common/bt2c/call.hpp"
15
16 #include "run-in.hpp"
17
18 void RunIn::onQuery(bt2::SelfComponentClass)
19 {
20 }
21
22 void RunIn::onCompInit(bt2::SelfComponent)
23 {
24 }
25
26 void RunIn::onMsgIterInit(bt2::SelfMessageIterator)
27 {
28 }
29
30 namespace {
31
32 class RunInSource;
33
34 class RunInSourceMsgIter final : public bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource>
35 {
36 public:
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 {
42 port.data<RunIn>().onMsgIterInit(self);
43 }
44
45 void _next(bt2::ConstMessageArray&)
46 {
47 }
48 };
49
50 class RunInSource final :
51 public bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, RunIn, RunIn>
52 {
53 public:
54 static constexpr auto name = "run-in-src";
55
56 explicit RunInSource(const bt2::SelfSourceComponent self, bt2::ConstMapValue,
57 RunIn * const runIn) :
58 bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, RunIn, RunIn> {self,
59 "RUN-IN-SRC"},
60 _mRunIn {runIn}
61 {
62 this->_addOutputPort("out", *runIn);
63 _mRunIn->onCompInit(self);
64 }
65
66 static bt2::Value::Shared _query(const bt2::SelfComponentClass self, bt2::PrivateQueryExecutor,
67 bt2c::CStringView, bt2::ConstValue, RunIn *data)
68 {
69 data->onQuery(self);
70 return bt2::NullValue {}.shared();
71 }
72
73 private:
74 RunIn *_mRunIn;
75 };
76
77 } /* namespace */
78
79 void runIn(RunIn& runIn)
80 {
81 const auto srcCompCls = bt2::SourceComponentClass::create<RunInSource>();
82
83 /* Execute a query */
84 bt2::QueryExecutor::create(*srcCompCls, "object-name", runIn)->query();
85
86 /* Create graph */
87 const auto graph = bt2::Graph::create(0);
88
89 /* Add custom source component (executes `compCtxFunc`) */
90 const auto srcComp = graph->addComponent(*srcCompCls, "the-source", runIn);
91
92 /* Add dummy sink component */
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 });
104
105 /* Connect ports */
106 const auto outPort = srcComp.outputPorts()["out"];
107 BT_ASSERT(outPort);
108
109 const auto inPort = sinkComp.inputPorts()["in"];
110 BT_ASSERT(inPort);
111
112 graph->connectPorts(*outPort, *inPort);
113
114 /* Run graph (executes `msgIterCtxFunc`) */
115 graph->run();
116 }
This page took 0.030663 seconds and 4 git commands to generate.