File: conds-triggers.cpp

package info (click to toggle)
babeltrace2 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,660 kB
  • sloc: cpp: 106,162; ansic: 78,276; python: 27,115; sh: 9,053; makefile: 1,807; xml: 46
file content (113 lines) | stat: -rw-r--r-- 3,336 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * SPDX-License-Identifier: GPL-2.0-only
 *
 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
 */

#include <utility>

#include <babeltrace2/babeltrace.h>

#include "cpp-common/bt2/graph.hpp"
#include "cpp-common/bt2c/c-string-view.hpp"
#include "cpp-common/bt2c/make-span.hpp"
#include "cpp-common/bt2s/make-unique.hpp"

#include "clk-cls-compat-postconds-triggers.hpp"
#include "utils.hpp"

namespace {

/*
 * Creates a simple condition trigger, calling `func`.
 */
template <typename FuncT>
CondTrigger::UP makeSimpleTrigger(FuncT&& func, const CondTrigger::Type type,
                                  const std::string& condId,
                                  const bt2c::CStringView nameSuffix = {})
{
    return bt2s::make_unique<SimpleCondTrigger>(std::forward<FuncT>(func), type, condId,
                                                nameSuffix);
}

using OnCompInitFunc = std::function<void(bt2::SelfComponent)>;

/*
 * A "run in" class that delegates the execution to stored callables.
 *
 * Use the makeRunIn*Trigger() helpers below.
 */
class RunInDelegator final : public RunIn
{
public:
    static RunInDelegator makeOnCompInit(OnCompInitFunc func)
    {
        return RunInDelegator {std::move(func)};
    }

    void onCompInit(const bt2::SelfComponent self) override
    {
        if (_mOnCompInitFunc) {
            _mOnCompInitFunc(self);
        }
    }

private:
    explicit RunInDelegator(OnCompInitFunc onCompInitFunc) :
        _mOnCompInitFunc {std::move(onCompInitFunc)}
    {
    }

    OnCompInitFunc _mOnCompInitFunc;
};

/*
 * Creates a condition trigger, calling `func` in a component
 * initialization context.
 */
CondTrigger::UP makeRunInCompInitTrigger(OnCompInitFunc func, const CondTrigger::Type type,
                                         const std::string& condId,
                                         const bt2c::CStringView nameSuffix = {})
{
    return bt2s::make_unique<RunInCondTrigger<RunInDelegator>>(
        RunInDelegator::makeOnCompInit(std::move(func)), type, condId, 0u, nameSuffix);
}

bt2::IntegerFieldClass::Shared createUIntFc(const bt2::SelfComponent self)
{
    return self.createTraceClass()->createUnsignedIntegerFieldClass();
}

} /* namespace */

int main(const int argc, const char ** const argv)
{
    CondTriggers triggers;

    triggers.emplace_back(makeSimpleTrigger(
        [] {
            bt2::Graph::create(292);
        },
        CondTrigger::Type::Pre, "graph-create:valid-mip-version"));

    triggers.emplace_back(makeRunInCompInitTrigger(
        [](const bt2::SelfComponent self) {
            createUIntFc(self)->fieldValueRange(0);
        },
        CondTrigger::Type::Pre, "field-class-integer-set-field-value-range:valid-n", "0"));

    triggers.emplace_back(makeRunInCompInitTrigger(
        [](const bt2::SelfComponent self) {
            createUIntFc(self)->fieldValueRange(65);
        },
        CondTrigger::Type::Pre, "field-class-integer-set-field-value-range:valid-n", "gt-64"));

    triggers.emplace_back(makeSimpleTrigger(
        [] {
            bt_field_class_integer_set_field_value_range(nullptr, 23);
        },
        CondTrigger::Type::Pre, "field-class-integer-set-field-value-range:not-null:field-class"));

    addClkClsCompatTriggers(triggers);
    condMain(bt2c::makeSpan(argv, argc), triggers);
}