tests/lib: C++ify run-in and condition trigger code
[babeltrace.git] / tests / lib / conds / utils.cpp
CommitLineData
5d7e8359
PP
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
5 */
6
c802cacb 7#include <stdlib.h>
5d7e8359 8#include <string.h>
c802cacb 9
5d7e8359 10#include <babeltrace2/babeltrace.h>
5d7e8359
PP
11
12#include "common/assert.h"
6e3150f4 13#include "cpp-common/vendor/fmt/core.h"
78c888d1 14#include "cpp-common/vendor/nlohmann/json.hpp"
c802cacb 15
c802cacb 16#include "utils.hpp"
5d7e8359 17
5d7e57e8
SM
18CondTrigger::CondTrigger(const Type type, const std::string& condId,
19 const bt2s::optional<std::string>& nameSuffix) noexcept :
20 _mType {type},
21 _mCondId {fmt::format("{}:{}", type == Type::PRE ? "pre" : "post", condId)},
22 _mName {
23 fmt::format("{}{}{}", condId, nameSuffix ? "-" : "", nameSuffix ? nameSuffix->data() : "")}
5d7e8359 24{
5d7e8359
PP
25}
26
5d7e57e8
SM
27SimpleCondTrigger::SimpleCondTrigger(std::function<void()> func, const Type type,
28 const std::string& condId,
29 const bt2s::optional<std::string>& nameSuffix) :
30 CondTrigger {type, condId, nameSuffix},
31 _mFunc {std::move(func)}
5d7e8359 32{
5d7e57e8 33}
5d7e8359 34
5d7e57e8 35namespace {
5d7e8359 36
5d7e57e8
SM
37void listCondTriggers(const CondTriggers condTriggers) noexcept
38{
39 auto condTriggerArray = nlohmann::json::array();
5d7e8359 40
5d7e57e8
SM
41 for (const auto condTrigger : condTriggers) {
42 condTriggerArray.push_back(nlohmann::json {
43 {"cond-id", condTrigger->condId()},
44 {"name", condTrigger->name()},
45 });
486428e8 46 }
5d7e8359 47
5d7e57e8 48 fmt::println("{}", condTriggerArray.dump());
5d7e8359
PP
49}
50
6e3150f4
SM
51} /* namespace */
52
5d7e57e8 53void condMain(const int argc, const char ** const argv, const CondTriggers condTriggers) noexcept
5d7e8359 54{
486428e8
SM
55 BT_ASSERT(argc >= 2);
56
57 if (strcmp(argv[1], "list") == 0) {
5d7e57e8 58 listCondTriggers(condTriggers);
486428e8 59 } else if (strcmp(argv[1], "run") == 0) {
486428e8 60 BT_ASSERT(argc >= 3);
5d7e57e8
SM
61
62 const auto index = atoi(argv[2]);
63
64 BT_ASSERT(index >= 0 && index < condTriggers.size());
65 (*condTriggers[index])();
486428e8 66 }
5d7e8359 67}
This page took 0.041196 seconds and 4 git commands to generate.