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"
19 * Creates a simple condition trigger, calling `func`.
21 template <typename FuncT
>
22 CondTrigger
*makeSimpleTrigger(FuncT
&& func
, const CondTrigger::Type type
,
23 const std::string
& condId
, const bt2c::CStringView nameSuffix
= {})
25 return new SimpleCondTrigger
{std::forward
<FuncT
>(func
), type
, condId
, nameSuffix
};
28 using OnCompInitFunc
= std::function
<void(bt2::SelfComponent
)>;
31 * A "run in" class that delegates the execution to stored callables.
33 * Use the makeRunIn*Trigger() helpers below.
35 class RunInDelegator final
: public RunIn
38 static RunInDelegator
makeOnCompInit(OnCompInitFunc func
)
40 return RunInDelegator
{std::move(func
)};
43 void onCompInit(const bt2::SelfComponent self
) override
45 if (_mOnCompInitFunc
) {
46 _mOnCompInitFunc(self
);
51 explicit RunInDelegator(OnCompInitFunc onCompInitFunc
) :
52 _mOnCompInitFunc
{std::move(onCompInitFunc
)}
56 OnCompInitFunc _mOnCompInitFunc
;
60 * Creates a condition trigger, calling `func` in a component
61 * initialization context.
63 CondTrigger
*makeRunInCompInitTrigger(OnCompInitFunc func
, const CondTrigger::Type type
,
64 const std::string
& condId
,
65 const bt2c::CStringView nameSuffix
= {})
67 return new RunInCondTrigger
<RunInDelegator
> {RunInDelegator::makeOnCompInit(std::move(func
)),
68 type
, condId
, nameSuffix
};
71 bt2::IntegerFieldClass::Shared
createUIntFc(const bt2::SelfComponent self
)
73 return self
.createTraceClass()->createUnsignedIntegerFieldClass();
76 /* Our condition triggers */
77 CondTrigger
* const triggers
[] = {
80 bt2::Graph::create(292);
82 CondTrigger::Type::PRE
, "graph-create:valid-mip-version"),
84 makeRunInCompInitTrigger(
85 [](const bt2::SelfComponent self
) {
86 createUIntFc(self
)->fieldValueRange(0);
88 CondTrigger::Type::PRE
, "field-class-integer-set-field-value-range:valid-n", "0"),
90 makeRunInCompInitTrigger(
91 [](const bt2::SelfComponent self
) {
92 createUIntFc(self
)->fieldValueRange(65);
94 CondTrigger::Type::PRE
, "field-class-integer-set-field-value-range:valid-n", "gt-64"),
98 bt_field_class_integer_set_field_value_range(nullptr, 23);
100 CondTrigger::Type::PRE
, "field-class-integer-set-field-value-range:not-null:field-class"),
105 int main(const int argc
, const char ** const argv
)
107 condMain(argc
, argv
, triggers
);