File: run-in.cpp

package info (click to toggle)
babeltrace2 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,660 kB
  • sloc: cpp: 106,162; ansic: 78,276; python: 27,115; sh: 9,053; makefile: 1,807; xml: 46
file content (126 lines) | stat: -rw-r--r-- 3,463 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * SPDX-License-Identifier: GPL-2.0-only
 *
 * Copyright (C) 2020-2023 EfficiOS, inc.
 */

#include "common/assert.h"
#include "cpp-common/bt2/component-class-dev.hpp"
#include "cpp-common/bt2/component-class.hpp"
#include "cpp-common/bt2/graph.hpp"
#include "cpp-common/bt2/plugin-load.hpp"
#include "cpp-common/bt2/plugin.hpp"
#include "cpp-common/bt2/query-executor.hpp"
#include "cpp-common/bt2c/call.hpp"

#include "run-in.hpp"

void RunIn::onQuery(bt2::SelfComponentClass)
{
}

void RunIn::onCompInit(bt2::SelfComponent)
{
}

void RunIn::onMsgIterInit(bt2::SelfMessageIterator)
{
}

void RunIn::onMsgIterNext(bt2::SelfMessageIterator, bt2::ConstMessageArray&)
{
}

namespace {

class RunInSource;

class RunInSourceMsgIter final : public bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource>
{
public:
    explicit RunInSourceMsgIter(const bt2::SelfMessageIterator self,
                                bt2::SelfMessageIteratorConfiguration,
                                const bt2::SelfComponentOutputPort port) :
        bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource> {self, "RUN-IN-SRC-MSG-ITER"},
        _mRunIn {&port.data<RunIn>()}, _mSelf {self}
    {
        _mRunIn->onMsgIterInit(self);
    }

    void _next(bt2::ConstMessageArray& msgs)
    {
        _mRunIn->onMsgIterNext(_mSelf, msgs);
    }

private:
    RunIn *_mRunIn;
    bt2::SelfMessageIterator _mSelf;
};

class RunInSource final :
    public bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, RunIn, RunIn>
{
public:
    static constexpr auto name = "run-in-src";

    explicit RunInSource(const bt2::SelfSourceComponent self, bt2::ConstMapValue,
                         RunIn * const runIn) :
        bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, RunIn, RunIn> {self,
                                                                                 "RUN-IN-SRC"},
        _mRunIn {runIn}
    {
        this->_addOutputPort("out", *runIn);
        _mRunIn->onCompInit(self);
    }

    static bt2::Value::Shared _query(const bt2::SelfComponentClass self, bt2::PrivateQueryExecutor,
                                     bt2c::CStringView, bt2::ConstValue, RunIn *data)
    {
        data->onQuery(self);
        return bt2::NullValue {}.shared();
    }

private:
    RunIn *_mRunIn;
};

} /* namespace */

void runIn(RunIn& runIn, const std::uint64_t graphMipVersion)
{
    const auto srcCompCls = bt2::SourceComponentClass::create<RunInSource>();

    /* Execute a query */
    bt2::QueryExecutor::create(*srcCompCls, "object-name", runIn)->query();

    /* Create graph */
    const auto graph = bt2::Graph::create(graphMipVersion);

    /* Add custom source component (executes `compCtxFunc`) */
    const auto srcComp = graph->addComponent(*srcCompCls, "the-source", runIn);

    /* Add dummy sink component */
    const auto sinkComp = bt2c::call([&] {
        const auto utilsPlugin = bt2::findPlugin("utils");

        BT_ASSERT(utilsPlugin);

        const auto dummySinkCompCls = utilsPlugin->sinkComponentClasses()["dummy"];

        BT_ASSERT(dummySinkCompCls);

        return graph->addComponent(*dummySinkCompCls, "the-sink");
    });

    /* Connect ports */
    const auto outPort = srcComp.outputPorts()["out"];
    BT_ASSERT(outPort);

    const auto inPort = sinkComp.inputPorts()["in"];
    BT_ASSERT(inPort);

    graph->connectPorts(*outPort, *inPort);

    /* Run graph (executes `msgIterCtxFunc`) */
    graph->run();
}