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/bt2s/make-unique.hpp"
20 * Creates a simple condition trigger, calling `func`.
22 template <typename FuncT
>
23 CondTrigger::UP
makeSimpleTrigger(FuncT
&& func
, const CondTrigger::Type type
,
24 const std::string
& condId
,
25 const bt2c::CStringView nameSuffix
= {})
27 return bt2s::make_unique
<SimpleCondTrigger
>(std::forward
<FuncT
>(func
), type
, condId
,
31 using OnCompInitFunc
= std::function
<void(bt2::SelfComponent
)>;
34 * A "run in" class that delegates the execution to stored callables.
36 * Use the makeRunIn*Trigger() helpers below.
38 class RunInDelegator final
: public RunIn
41 static RunInDelegator
makeOnCompInit(OnCompInitFunc func
)
43 return RunInDelegator
{std::move(func
)};
46 void onCompInit(const bt2::SelfComponent self
) override
48 if (_mOnCompInitFunc
) {
49 _mOnCompInitFunc(self
);
54 explicit RunInDelegator(OnCompInitFunc onCompInitFunc
) :
55 _mOnCompInitFunc
{std::move(onCompInitFunc
)}
59 OnCompInitFunc _mOnCompInitFunc
;
63 * Creates a condition trigger, calling `func` in a component
64 * initialization context.
66 CondTrigger::UP
makeRunInCompInitTrigger(OnCompInitFunc func
, const CondTrigger::Type type
,
67 const std::string
& condId
,
68 const bt2c::CStringView nameSuffix
= {})
70 return bt2s::make_unique
<RunInCondTrigger
<RunInDelegator
>>(
71 RunInDelegator::makeOnCompInit(std::move(func
)), type
, condId
, nameSuffix
);
74 bt2::IntegerFieldClass::Shared
createUIntFc(const bt2::SelfComponent self
)
76 return self
.createTraceClass()->createUnsignedIntegerFieldClass();
81 int main(const int argc
, const char ** const argv
)
83 CondTriggers triggers
;
85 triggers
.emplace_back(makeSimpleTrigger(
87 bt2::Graph::create(292);
89 CondTrigger::Type::PRE
, "graph-create:valid-mip-version"));
91 triggers
.emplace_back(makeRunInCompInitTrigger(
92 [](const bt2::SelfComponent self
) {
93 createUIntFc(self
)->fieldValueRange(0);
95 CondTrigger::Type::PRE
, "field-class-integer-set-field-value-range:valid-n", "0"));
97 triggers
.emplace_back(makeRunInCompInitTrigger(
98 [](const bt2::SelfComponent self
) {
99 createUIntFc(self
)->fieldValueRange(65);
101 CondTrigger::Type::PRE
, "field-class-integer-set-field-value-range:valid-n", "gt-64"));
103 triggers
.emplace_back(makeSimpleTrigger(
105 bt_field_class_integer_set_field_value_range(nullptr, 23);
107 CondTrigger::Type::PRE
, "field-class-integer-set-field-value-range:not-null:field-class"));
109 condMain(argc
, argv
, triggers
);