tests/lib/conds: make `condMain()` take a span for args
[babeltrace.git] / tests / lib / conds / utils.hpp
CommitLineData
5d7e8359
PP
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
5 */
6
1a5aac02
SM
7#ifndef TESTS_LIB_CONDS_UTILS_HPP
8#define TESTS_LIB_CONDS_UTILS_HPP
5d7e8359 9
5d7e57e8 10#include <functional>
25fe6ea5 11#include <memory>
5d7e57e8
SM
12#include <string>
13#include <utility>
25fe6ea5 14#include <vector>
5d7e57e8 15
fb10c90e
SM
16#include <babeltrace2/babeltrace.h>
17
4ad43657 18#include "cpp-common/bt2c/c-string-view.hpp"
fb472529 19#include "cpp-common/bt2s/span.hpp"
5d15e4ca 20
5d7e57e8 21#include "../utils/run-in.hpp"
5d7e8359 22
5d7e57e8
SM
23/*
24 * Abstract condition trigger class.
25 *
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.
29 */
30class CondTrigger
486428e8 31{
5d7e57e8 32public:
25fe6ea5
SM
33 using UP = std::unique_ptr<CondTrigger>;
34
5d7e57e8
SM
35 /*
36 * Condition type.
37 */
38 enum class Type
39 {
40 PRE,
41 POST,
42 };
5d7e8359 43
5d7e57e8
SM
44protected:
45 /*
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`.
49 *
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().
53 */
54 explicit CondTrigger(Type type, const std::string& condId,
4ad43657 55 const bt2c::CStringView nameSuffix) noexcept;
5d7e8359 56
5d7e57e8
SM
57public:
58 virtual ~CondTrigger() = default;
59 virtual void operator()() noexcept = 0;
60
61 Type type() const noexcept
62 {
63 return _mType;
64 }
65
66 /*
67 * Condition ID, including any `pre:` or `post:` prefix.
68 */
69 const std::string& condId() const noexcept
486428e8 70 {
5d7e57e8
SM
71 return _mCondId;
72 }
73
74 const std::string& name() const noexcept
75 {
76 return _mName;
77 }
78
79private:
80 Type _mType;
81 std::string _mCondId;
82 std::string _mName;
5d7e8359
PP
83};
84
5d7e57e8
SM
85/*
86 * Simple condition trigger.
87 *
88 * Implements a condition trigger where a function provided at
89 * construction time triggers a condition.
90 */
91class SimpleCondTrigger : public CondTrigger
92{
93public:
94 explicit SimpleCondTrigger(std::function<void()> func, Type type, const std::string& condId,
4ad43657 95 const bt2c::CStringView nameSuffix = {});
5d7e57e8
SM
96
97 void operator()() noexcept override
98 {
99 _mFunc();
486428e8 100 }
5d7e8359 101
5d7e57e8
SM
102private:
103 std::function<void()> _mFunc;
104};
105
106/*
107 * Run-in condition trigger.
108 *
109 * Implements a condition trigger of which the triggering function
110 * happens in a graph or component class query context using the
111 * runIn() API.
112 */
113template <typename RunInT>
114class RunInCondTrigger : public CondTrigger
115{
116public:
117 explicit RunInCondTrigger(RunInT runIn, const Type type, const std::string& condId,
4ad43657 118 const bt2c::CStringView nameSuffix = {}) :
5d7e57e8
SM
119 CondTrigger {type, condId, nameSuffix},
120 _mRunIn {std::move(runIn)}
121 {
486428e8 122 }
5d7e8359 123
5d7e57e8 124 explicit RunInCondTrigger(const Type type, const std::string& condId,
4ad43657 125 const bt2c::CStringView nameSuffix = {}) :
5d7e57e8
SM
126 RunInCondTrigger {RunInT {}, type, condId, nameSuffix}
127 {
486428e8 128 }
5d7e8359 129
5d7e57e8
SM
130 void operator()() noexcept override
131 {
132 runIn(_mRunIn);
486428e8 133 }
5d7e8359 134
5d7e57e8
SM
135private:
136 RunInT _mRunIn;
137};
138
139/*
140 * List of condition triggers.
141 */
25fe6ea5 142using CondTriggers = std::vector<CondTrigger::UP>;
5d7e57e8
SM
143
144/*
145 * The entry point of a condition trigger program.
146 *
147 * Call this from your own main() with your list of condition triggers
148 * `triggers`.
149 *
150 * Each condition trigger of `triggers` must have a unique name, as
151 * returned by CondTrigger::name().
152 *
153 * This function uses `argc` and `argv` to respond to one of the
154 * following commands:
155 *
156 * `list`:
157 * Prints a list of condition triggers as a JSON array of objects.
158 *
159 * Each JSON object has:
160 *
161 * `cond-id`:
162 * The condition ID of the trigger, as returned by
163 * CondTrigger:condId().
164 *
165 * `name`:
166 * The condition ID name, as returned by CondTrigger::name().
167 *
168 * `run`:
169 * Runs the triggering function of the condition trigger at the
170 * index specified by the next command-line argument.
171 *
172 * For example,
173 *
174 * $ my-cond-trigger-program run 45
175 *
176 * would run the function of the condition trigger `triggers[45]`.
177 *
178 * The program is expected to abort through a libbabeltrace2
179 * condition failure.
180 */
fb472529 181void condMain(const bt2s::span<const char * const> argv, const CondTriggers& triggers) noexcept;
5d7e8359 182
1a5aac02 183#endif /* TESTS_LIB_CONDS_UTILS_HPP */
This page took 0.045845 seconds and 4 git commands to generate.