2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
9 #include <babeltrace2/babeltrace.h>
11 #include "cpp-common/bt2/graph.hpp"
12 #include "cpp-common/bt2c/c-string-view.hpp"
13 #include "cpp-common/bt2c/span.hpp"
14 #include "cpp-common/bt2s/make-unique.hpp"
21 * Creates a simple condition trigger, calling `func`.
23 template <typename FuncT
>
24 CondTrigger::UP
makeSimpleTrigger(FuncT
&& func
, const CondTrigger::Type type
,
25 const std::string
& condId
,
26 const bt2c::CStringView nameSuffix
= {})
28 return bt2s::make_unique
<SimpleCondTrigger
>(std::forward
<FuncT
>(func
), type
, condId
,
32 using OnCompInitFunc
= std::function
<void(bt2::SelfComponent
)>;
35 * A "run in" class that delegates the execution to stored callables.
37 * Use the makeRunIn*Trigger() helpers below.
39 class RunInDelegator final
: public RunIn
42 static RunInDelegator
makeOnCompInit(OnCompInitFunc func
)
44 return RunInDelegator
{std::move(func
)};
47 void onCompInit(const bt2::SelfComponent self
) override
49 if (_mOnCompInitFunc
) {
50 _mOnCompInitFunc(self
);
55 explicit RunInDelegator(OnCompInitFunc onCompInitFunc
) :
56 _mOnCompInitFunc
{std::move(onCompInitFunc
)}
60 OnCompInitFunc _mOnCompInitFunc
;
64 * Creates a condition trigger, calling `func` in a component
65 * initialization context.
67 CondTrigger::UP
makeRunInCompInitTrigger(OnCompInitFunc func
, const CondTrigger::Type type
,
68 const std::string
& condId
,
69 const bt2c::CStringView nameSuffix
= {})
71 return bt2s::make_unique
<RunInCondTrigger
<RunInDelegator
>>(
72 RunInDelegator::makeOnCompInit(std::move(func
)), type
, condId
, nameSuffix
);
75 bt2::IntegerFieldClass::Shared
createUIntFc(const bt2::SelfComponent self
)
77 return self
.createTraceClass()->createUnsignedIntegerFieldClass();
82 int main(const int argc
, const char ** const argv
)
84 CondTriggers triggers
;
86 triggers
.emplace_back(makeSimpleTrigger(
88 bt2::Graph::create(292);
90 CondTrigger::Type::PRE
, "graph-create:valid-mip-version"));
92 triggers
.emplace_back(makeRunInCompInitTrigger(
93 [](const bt2::SelfComponent self
) {
94 createUIntFc(self
)->fieldValueRange(0);
96 CondTrigger::Type::PRE
, "field-class-integer-set-field-value-range:valid-n", "0"));
98 triggers
.emplace_back(makeRunInCompInitTrigger(
99 [](const bt2::SelfComponent self
) {
100 createUIntFc(self
)->fieldValueRange(65);
102 CondTrigger::Type::PRE
, "field-class-integer-set-field-value-range:valid-n", "gt-64"));
104 triggers
.emplace_back(makeSimpleTrigger(
106 bt_field_class_integer_set_field_value_range(nullptr, 23);
108 CondTrigger::Type::PRE
, "field-class-integer-set-field-value-range:not-null:field-class"));
110 condMain(bt2c::makeSpan(argv
, argc
), triggers
);