2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
7 #ifndef TESTS_LIB_CONDS_UTILS_HPP
8 #define TESTS_LIB_CONDS_UTILS_HPP
16 #include <babeltrace2/babeltrace.h>
18 #include "cpp-common/bt2c/c-string-view.hpp"
20 #include "../utils/run-in.hpp"
23 * Abstract condition trigger class.
25 * A derived class must provide operator()() which triggers a condition
26 * of which the specific type (precondition or postcondition) and ID are
27 * provided at construction time.
32 using UP = std::unique_ptr<CondTrigger>;
45 * Builds a condition trigger having the type `type`, the condition
46 * ID `condId` (_without_ any `pre:` or `post:` prefix), and the
47 * optional name suffix `nameSuffix`.
49 * The concatenation of `condId` and, if it's set, `-` and
50 * `*nameSuffix`, forms the name of the condition trigger. Get the
51 * name of the created condition trigger with name().
53 explicit CondTrigger(Type type, const std::string& condId,
54 const bt2c::CStringView nameSuffix) noexcept;
57 virtual ~CondTrigger() = default;
58 virtual void operator()() noexcept = 0;
60 Type type() const noexcept
66 * Condition ID, including any `pre:` or `post:` prefix.
68 const std::string& condId() const noexcept
73 const std::string& name() const noexcept
85 * Simple condition trigger.
87 * Implements a condition trigger where a function provided at
88 * construction time triggers a condition.
90 class SimpleCondTrigger : public CondTrigger
93 explicit SimpleCondTrigger(std::function<void()> func, Type type, const std::string& condId,
94 const bt2c::CStringView nameSuffix = {});
96 void operator()() noexcept override
102 std::function<void()> _mFunc;
106 * Run-in condition trigger.
108 * Implements a condition trigger of which the triggering function
109 * happens in a graph or component class query context using the
112 template <typename RunInT>
113 class RunInCondTrigger : public CondTrigger
116 explicit RunInCondTrigger(RunInT runIn, const Type type, const std::string& condId,
117 const bt2c::CStringView nameSuffix = {}) :
118 CondTrigger {type, condId, nameSuffix},
119 _mRunIn {std::move(runIn)}
123 explicit RunInCondTrigger(const Type type, const std::string& condId,
124 const bt2c::CStringView nameSuffix = {}) :
125 RunInCondTrigger {RunInT {}, type, condId, nameSuffix}
129 void operator()() noexcept override
139 * List of condition triggers.
141 using CondTriggers = std::vector<CondTrigger::UP>;
144 * The entry point of a condition trigger program.
146 * Call this from your own main() with your list of condition triggers
149 * Each condition trigger of `triggers` must have a unique name, as
150 * returned by CondTrigger::name().
152 * This function uses `argc` and `argv` to respond to one of the
153 * following commands:
156 * Prints a list of condition triggers as a JSON array of objects.
158 * Each JSON object has:
161 * The condition ID of the trigger, as returned by
162 * CondTrigger:condId().
165 * The condition ID name, as returned by CondTrigger::name().
168 * Runs the triggering function of the condition trigger at the
169 * index specified by the next command-line argument.
173 * $ my-cond-trigger-program run 45
175 * would run the function of the condition trigger `triggers[45]`.
177 * The program is expected to abort through a libbabeltrace2
180 void condMain(int argc, const char **argv, const CondTriggers& triggers) noexcept;
182 #endif /* TESTS_LIB_CONDS_UTILS_HPP */