2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
7 #ifndef BABELTRACE_TESTS_LIB_CONDS_UTILS_HPP
8 #define BABELTRACE_TESTS_LIB_CONDS_UTILS_HPP
16 #include <babeltrace2/babeltrace.h>
18 #include "cpp-common/bt2c/c-string-view.hpp"
19 #include "cpp-common/bt2s/span.hpp"
21 #include "../utils/run-in.hpp"
24 * Abstract condition trigger class.
26 * A derived class must provide operator()() which triggers a condition
27 * of which the specific type (precondition or postcondition) and ID are
28 * provided at construction time.
33 using UP = std::unique_ptr<CondTrigger>;
46 * Builds a condition trigger having the type `type`, the condition
47 * ID `condId` (_without_ any `pre:` or `post:` prefix), and the
48 * optional name suffix `nameSuffix`.
50 * The concatenation of `condId` and, if it's set, `-` and
51 * `*nameSuffix`, forms the name of the condition trigger. Get the
52 * name of the created condition trigger with name().
54 explicit CondTrigger(Type type, const std::string& condId,
55 const bt2c::CStringView nameSuffix) noexcept;
58 virtual ~CondTrigger() = default;
59 virtual void operator()() noexcept = 0;
61 Type type() const noexcept
67 * Condition ID, including any `pre:` or `post:` prefix.
69 const std::string& condId() const noexcept
74 const std::string& name() const noexcept
86 * Simple condition trigger.
88 * Implements a condition trigger where a function provided at
89 * construction time triggers a condition.
91 class SimpleCondTrigger : public CondTrigger
94 explicit SimpleCondTrigger(std::function<void()> func, Type type, const std::string& condId,
95 const bt2c::CStringView nameSuffix = {});
97 void operator()() noexcept override
103 std::function<void()> _mFunc;
107 * Run-in condition trigger.
109 * Implements a condition trigger of which the triggering function
110 * happens in a graph or component class query context using the
113 template <typename RunInT>
114 class RunInCondTrigger : public CondTrigger
117 explicit RunInCondTrigger(RunInT runIn, const Type type, const std::string& condId,
118 const bt2c::CStringView nameSuffix = {}) :
119 CondTrigger {type, condId, nameSuffix},
120 _mRunIn {std::move(runIn)}
124 explicit RunInCondTrigger(const Type type, const std::string& condId,
125 const bt2c::CStringView nameSuffix = {}) :
126 RunInCondTrigger {RunInT {}, type, condId, nameSuffix}
130 void operator()() noexcept override
140 * List of condition triggers.
142 using CondTriggers = std::vector<CondTrigger::UP>;
145 * The entry point of a condition trigger program.
147 * Call this from your own main() with your list of condition triggers
150 * Each condition trigger of `triggers` must have a unique name, as
151 * returned by CondTrigger::name().
153 * This function uses `argc` and `argv` to respond to one of the
154 * following commands:
157 * Prints a list of condition triggers as a JSON array of objects.
159 * Each JSON object has:
162 * The condition ID of the trigger, as returned by
163 * CondTrigger:condId().
166 * The condition ID name, as returned by CondTrigger::name().
169 * Runs the triggering function of the condition trigger at the
170 * index specified by the next command-line argument.
174 * $ my-cond-trigger-program run 45
176 * would run the function of the condition trigger `triggers[45]`.
178 * The program is expected to abort through a libbabeltrace2
181 void condMain(const bt2s::span<const char * const> argv, const CondTriggers& triggers) noexcept;
183 #endif /* BABELTRACE_TESTS_LIB_CONDS_UTILS_HPP */