X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=tests%2Flib%2Fconds%2Futils.hpp;h=2ea53bc4e8d7739f488df13decac8f8d8f514986;hb=HEAD;hp=1a77bde5a92cc963787bbc40f01a3e9f5dff6744;hpb=053db9608beb2dbdd428ca6ce7677ea661f244f1;p=babeltrace.git diff --git a/tests/lib/conds/utils.hpp b/tests/lib/conds/utils.hpp index 1a77bde5..7833c072 100644 --- a/tests/lib/conds/utils.hpp +++ b/tests/lib/conds/utils.hpp @@ -4,79 +4,180 @@ * Copyright (C) 2020 Philippe Proulx */ -#ifndef TESTS_LIB_CONDS_UTILS_H -#define TESTS_LIB_CONDS_UTILS_H +#ifndef BABELTRACE_TESTS_LIB_CONDS_UTILS_HPP +#define BABELTRACE_TESTS_LIB_CONDS_UTILS_HPP -enum cond_trigger_func_type { - COND_TRIGGER_FUNC_TYPE_BASIC, - COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT, +#include +#include +#include +#include +#include + +#include + +#include "cpp-common/bt2c/c-string-view.hpp" +#include "cpp-common/bt2s/span.hpp" + +#include "../utils/run-in.hpp" + +/* + * Abstract condition trigger class. + * + * A derived class must provide operator()() which triggers a condition + * of which the specific type (precondition or postcondition) and ID are + * provided at construction time. + */ +class CondTrigger +{ +public: + using UP = std::unique_ptr; + + /* + * Condition type. + */ + enum class Type + { + Pre, + Post, + }; + +protected: + /* + * Builds a condition trigger having the type `type`, the condition + * ID `condId` (_without_ any `pre:` or `post:` prefix), and the + * optional name suffix `nameSuffix`. + * + * The concatenation of `condId` and, if it's set, `-` and + * `*nameSuffix`, forms the name of the condition trigger. Get the + * name of the created condition trigger with name(). + */ + explicit CondTrigger(Type type, const std::string& condId, + const bt2c::CStringView nameSuffix) noexcept; + +public: + virtual ~CondTrigger() = default; + virtual void operator()() noexcept = 0; + + Type type() const noexcept + { + return _mType; + } + + /* + * Condition ID, including any `pre:` or `post:` prefix. + */ + const std::string& condId() const noexcept + { + return _mCondId; + } + + const std::string& name() const noexcept + { + return _mName; + } + +private: + Type _mType; + std::string _mCondId; + std::string _mName; }; -enum cond_trigger_type { - COND_TRIGGER_TYPE_PRE, - COND_TRIGGER_TYPE_POST, +/* + * Simple condition trigger. + * + * Implements a condition trigger where a function provided at + * construction time triggers a condition. + */ +class SimpleCondTrigger : public CondTrigger +{ +public: + explicit SimpleCondTrigger(std::function func, Type type, const std::string& condId, + const bt2c::CStringView nameSuffix = {}); + + void operator()() noexcept override + { + _mFunc(); + } + +private: + std::function _mFunc; }; -typedef void (* cond_trigger_basic_func)(void); -typedef void (* cond_trigger_run_in_comp_cls_init_func)(bt_self_component *); - -struct cond_trigger { - enum cond_trigger_type type; - enum cond_trigger_func_type func_type; - const char *cond_id; - const char *suffix; - union { - cond_trigger_basic_func basic; - cond_trigger_run_in_comp_cls_init_func run_in_comp_cls_init; - } func; +/* + * Run-in condition trigger. + * + * Implements a condition trigger of which the triggering function + * happens in a graph or component class query context using the + * runIn() API. + */ +template +class RunInCondTrigger : public CondTrigger +{ +public: + explicit RunInCondTrigger(RunInT runIn, const Type type, const std::string& condId, + const bt2c::CStringView nameSuffix = {}) : + CondTrigger {type, condId, nameSuffix}, + _mRunIn {std::move(runIn)} + { + } + + explicit RunInCondTrigger(const Type type, const std::string& condId, + const bt2c::CStringView nameSuffix = {}) : + RunInCondTrigger {RunInT {}, type, condId, nameSuffix} + { + } + + void operator()() noexcept override + { + runIn(_mRunIn); + } + +private: + RunInT _mRunIn; }; -#define COND_TRIGGER_PRE_BASIC(_cond_id, _suffix, _func) \ - { \ - .type = COND_TRIGGER_TYPE_PRE, \ - .func_type = COND_TRIGGER_FUNC_TYPE_BASIC, \ - .cond_id = _cond_id, \ - .suffix = _suffix, \ - .func = { \ - .basic = _func, \ - } \ - } - -#define COND_TRIGGER_POST_BASIC(_cond_id, _suffix, _func) \ - { \ - .type = COND_TRIGGER_TYPE_POST, \ - .func_type = COND_TRIGGER_FUNC_TYPE_BASIC, \ - .cond_id = _cond_id, \ - .suffix = _suffix, \ - .func = { \ - .basic = _func, \ - } \ - } - -#define COND_TRIGGER_PRE_RUN_IN_COMP_CLS_INIT(_cond_id, _suffix, _func) \ - { \ - .type = COND_TRIGGER_TYPE_PRE, \ - .func_type = COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT, \ - .cond_id = _cond_id, \ - .suffix = _suffix, \ - .func = { \ - .run_in_comp_cls_init = _func, \ - } \ - } - -#define COND_TRIGGER_POST_RUN_IN_COMP_CLS_INIT(_cond_id, _suffix, _func) \ - { \ - .type = COND_TRIGGER_TYPE_POST, \ - .func_type = COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT, \ - .cond_id = _cond_id, \ - .suffix = _suffix, \ - .func = { \ - .run_in_comp_cls_init = _func, \ - } \ - } - -void cond_main(int argc, const char *argv[], - const struct cond_trigger triggers[], - size_t trigger_count); - -#endif /* TESTS_LIB_CONDS_UTILS_H */ +/* + * List of condition triggers. + */ +using CondTriggers = std::vector; + +/* + * The entry point of a condition trigger program. + * + * Call this from your own main() with your list of condition triggers + * `triggers`. + * + * Each condition trigger of `triggers` must have a unique name, as + * returned by CondTrigger::name(). + * + * This function uses `argc` and `argv` to respond to one of the + * following commands: + * + * `list`: + * Prints a list of condition triggers as a JSON array of objects. + * + * Each JSON object has: + * + * `cond-id`: + * The condition ID of the trigger, as returned by + * CondTrigger:condId(). + * + * `name`: + * The condition ID name, as returned by CondTrigger::name(). + * + * `run`: + * Runs the triggering function of the condition trigger at the + * index specified by the next command-line argument. + * + * For example, + * + * $ my-cond-trigger-program run 45 + * + * would run the function of the condition trigger `triggers[45]`. + * + * The program is expected to abort through a libbabeltrace2 + * condition failure. + */ +void condMain(const bt2s::span argv, const CondTriggers& triggers) noexcept; + +#endif /* BABELTRACE_TESTS_LIB_CONDS_UTILS_HPP */