| 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 | |
| 14 | #include "utils.hpp" |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | /* |
| 19 | * Creates a simple condition trigger, calling `func`. |
| 20 | */ |
| 21 | template <typename FuncT> |
| 22 | CondTrigger *makeSimpleTrigger(FuncT&& func, const CondTrigger::Type type, |
| 23 | const std::string& condId, const bt2c::CStringView nameSuffix = {}) |
| 24 | { |
| 25 | return new SimpleCondTrigger {std::forward<FuncT>(func), type, condId, nameSuffix}; |
| 26 | } |
| 27 | |
| 28 | using OnCompInitFunc = std::function<void(bt2::SelfComponent)>; |
| 29 | |
| 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 |
| 36 | { |
| 37 | public: |
| 38 | static RunInDelegator makeOnCompInit(OnCompInitFunc func) |
| 39 | { |
| 40 | return RunInDelegator {std::move(func)}; |
| 41 | } |
| 42 | |
| 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 bt2c::CStringView nameSuffix = {}) |
| 66 | { |
| 67 | return new RunInCondTrigger<RunInDelegator> {RunInDelegator::makeOnCompInit(std::move(func)), |
| 68 | type, condId, nameSuffix}; |
| 69 | } |
| 70 | |
| 71 | bt2::IntegerFieldClass::Shared createUIntFc(const bt2::SelfComponent self) |
| 72 | { |
| 73 | return self.createTraceClass()->createUnsignedIntegerFieldClass(); |
| 74 | } |
| 75 | |
| 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"), |
| 101 | }; |
| 102 | |
| 103 | } /* namespace */ |
| 104 | |
| 105 | int main(const int argc, const char ** const argv) |
| 106 | { |
| 107 | condMain(argc, argv, triggers); |
| 108 | } |