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