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