cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[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 void RunIn::onMsgIterNext(bt2::SelfMessageIterator, bt2::ConstMessageArray&)
31 {
32 }
33
34 namespace {
35
36 class RunInSource;
37
38 class RunInSourceMsgIter final : public bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource>
39 {
40 public:
41 explicit RunInSourceMsgIter(const bt2::SelfMessageIterator self,
42 bt2::SelfMessageIteratorConfiguration,
43 const bt2::SelfComponentOutputPort port) :
44 bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource> {self, "RUN-IN-SRC-MSG-ITER"},
45 _mRunIn {&port.data<RunIn>()}, _mSelf {self}
46 {
47 _mRunIn->onMsgIterInit(self);
48 }
49
50 void _next(bt2::ConstMessageArray& msgs)
51 {
52 _mRunIn->onMsgIterNext(_mSelf, msgs);
53 }
54
55 private:
56 RunIn *_mRunIn;
57 bt2::SelfMessageIterator _mSelf;
58 };
59
60 class RunInSource final :
61 public bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, RunIn, RunIn>
62 {
63 public:
64 static constexpr auto name = "run-in-src";
65
66 explicit RunInSource(const bt2::SelfSourceComponent self, bt2::ConstMapValue,
67 RunIn * const runIn) :
68 bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, RunIn, RunIn> {self,
69 "RUN-IN-SRC"},
70 _mRunIn {runIn}
71 {
72 this->_addOutputPort("out", *runIn);
73 _mRunIn->onCompInit(self);
74 }
75
76 static bt2::Value::Shared _query(const bt2::SelfComponentClass self, bt2::PrivateQueryExecutor,
77 bt2c::CStringView, bt2::ConstValue, RunIn *data)
78 {
79 data->onQuery(self);
80 return bt2::NullValue {}.shared();
81 }
82
83 private:
84 RunIn *_mRunIn;
85 };
86
87 } /* namespace */
88
89 void runIn(RunIn& runIn)
90 {
91 const auto srcCompCls = bt2::SourceComponentClass::create<RunInSource>();
92
93 /* Execute a query */
94 bt2::QueryExecutor::create(*srcCompCls, "object-name", runIn)->query();
95
96 /* Create graph */
97 const auto graph = bt2::Graph::create(0);
98
99 /* Add custom source component (executes `compCtxFunc`) */
100 const auto srcComp = graph->addComponent(*srcCompCls, "the-source", runIn);
101
102 /* Add dummy sink component */
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 });
114
115 /* Connect ports */
116 const auto outPort = srcComp.outputPorts()["out"];
117 BT_ASSERT(outPort);
118
119 const auto inPort = sinkComp.inputPorts()["in"];
120 BT_ASSERT(inPort);
121
122 graph->connectPorts(*outPort, *inPort);
123
124 /* Run graph (executes `msgIterCtxFunc`) */
125 graph->run();
126 }
This page took 0.030836 seconds and 4 git commands to generate.