cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / tests / lib / conds / conds-triggers.cpp
1 /*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #include <utility>
8
9 #include <babeltrace2/babeltrace.h>
10
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"
15
16 #include "clk-cls-compat-postconds-triggers.hpp"
17 #include "utils.hpp"
18
19 namespace {
20
21 /*
22 * Creates a simple condition trigger, calling `func`.
23 */
24 template <typename FuncT>
25 CondTrigger::UP makeSimpleTrigger(FuncT&& func, const CondTrigger::Type type,
26 const std::string& condId,
27 const bt2c::CStringView nameSuffix = {})
28 {
29 return bt2s::make_unique<SimpleCondTrigger>(std::forward<FuncT>(func), type, condId,
30 nameSuffix);
31 }
32
33 using OnCompInitFunc = std::function<void(bt2::SelfComponent)>;
34
35 /*
36 * A "run in" class that delegates the execution to stored callables.
37 *
38 * Use the makeRunIn*Trigger() helpers below.
39 */
40 class RunInDelegator final : public RunIn
41 {
42 public:
43 static RunInDelegator makeOnCompInit(OnCompInitFunc func)
44 {
45 return RunInDelegator {std::move(func)};
46 }
47
48 void onCompInit(const bt2::SelfComponent self) override
49 {
50 if (_mOnCompInitFunc) {
51 _mOnCompInitFunc(self);
52 }
53 }
54
55 private:
56 explicit RunInDelegator(OnCompInitFunc onCompInitFunc) :
57 _mOnCompInitFunc {std::move(onCompInitFunc)}
58 {
59 }
60
61 OnCompInitFunc _mOnCompInitFunc;
62 };
63
64 /*
65 * Creates a condition trigger, calling `func` in a component
66 * initialization context.
67 */
68 CondTrigger::UP makeRunInCompInitTrigger(OnCompInitFunc func, const CondTrigger::Type type,
69 const std::string& condId,
70 const bt2c::CStringView nameSuffix = {})
71 {
72 return bt2s::make_unique<RunInCondTrigger<RunInDelegator>>(
73 RunInDelegator::makeOnCompInit(std::move(func)), type, condId, nameSuffix);
74 }
75
76 bt2::IntegerFieldClass::Shared createUIntFc(const bt2::SelfComponent self)
77 {
78 return self.createTraceClass()->createUnsignedIntegerFieldClass();
79 }
80
81 } /* namespace */
82
83 int main(const int argc, const char ** const argv)
84 {
85 CondTriggers triggers;
86
87 triggers.emplace_back(makeSimpleTrigger(
88 [] {
89 bt2::Graph::create(292);
90 },
91 CondTrigger::Type::Pre, "graph-create:valid-mip-version"));
92
93 triggers.emplace_back(makeRunInCompInitTrigger(
94 [](const bt2::SelfComponent self) {
95 createUIntFc(self)->fieldValueRange(0);
96 },
97 CondTrigger::Type::Pre, "field-class-integer-set-field-value-range:valid-n", "0"));
98
99 triggers.emplace_back(makeRunInCompInitTrigger(
100 [](const bt2::SelfComponent self) {
101 createUIntFc(self)->fieldValueRange(65);
102 },
103 CondTrigger::Type::Pre, "field-class-integer-set-field-value-range:valid-n", "gt-64"));
104
105 triggers.emplace_back(makeSimpleTrigger(
106 [] {
107 bt_field_class_integer_set_field_value_range(nullptr, 23);
108 },
109 CondTrigger::Type::Pre, "field-class-integer-set-field-value-range:not-null:field-class"));
110
111 addClkClsCompatTriggers(triggers);
112 condMain(bt2c::makeSpan(argv, argc), triggers);
113 }
This page took 0.030878 seconds and 4 git commands to generate.