2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2020-2023 EfficiOS, inc.
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"
18 void RunIn::onQuery(bt2::SelfComponentClass
)
22 void RunIn::onCompInit(bt2::SelfComponent
)
26 void RunIn::onMsgIterInit(bt2::SelfMessageIterator
)
34 class RunInSourceMsgIter final
: public bt2::UserMessageIterator
<RunInSourceMsgIter
, RunInSource
>
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"}
42 port
.data
<RunIn
>().onMsgIterInit(self
);
45 void _next(bt2::ConstMessageArray
&)
50 class RunInSource final
:
51 public bt2::UserSourceComponent
<RunInSource
, RunInSourceMsgIter
, RunIn
, RunIn
>
54 static constexpr auto name
= "run-in-src";
56 explicit RunInSource(const bt2::SelfSourceComponent self
, bt2::ConstMapValue
,
57 RunIn
* const runIn
) :
58 bt2::UserSourceComponent
<RunInSource
, RunInSourceMsgIter
, RunIn
, RunIn
> {self
,
62 this->_addOutputPort("out", *runIn
);
63 _mRunIn
->onCompInit(self
);
66 static bt2::Value::Shared
_query(const bt2::SelfComponentClass self
, bt2::PrivateQueryExecutor
,
67 bt2c::CStringView
, bt2::ConstValue
, RunIn
*data
)
70 return bt2::NullValue
{}.shared();
79 void runIn(RunIn
& runIn
)
81 const auto srcCompCls
= bt2::SourceComponentClass::create
<RunInSource
>();
84 bt2::QueryExecutor::create(*srcCompCls
, "object-name", runIn
)->query();
87 const auto graph
= bt2::Graph::create(0);
89 /* Add custom source component (executes `compCtxFunc`) */
90 const auto srcComp
= graph
->addComponent(*srcCompCls
, "the-source", runIn
);
92 /* Add dummy sink component */
93 const auto sinkComp
= bt2c::call([&] {
94 const auto utilsPlugin
= bt2::findPlugin("utils");
96 BT_ASSERT(utilsPlugin
);
98 const auto dummySinkCompCls
= utilsPlugin
->sinkComponentClasses()["dummy"];
100 BT_ASSERT(dummySinkCompCls
);
102 return graph
->addComponent(*dummySinkCompCls
, "the-sink");
106 const auto outPort
= srcComp
.outputPorts()["out"];
109 const auto inPort
= sinkComp
.inputPorts()["in"];
112 graph
->connectPorts(*outPort
, *inPort
);
114 /* Run graph (executes `msgIterCtxFunc`) */