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