Commit | Line | Data |
---|---|---|
14d33b5a SM |
1 | /* |
2 | * Copyright (c) 2024 EfficiOS, Inc. | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_BT2_GRAPH_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_GRAPH_HPP | |
9 | ||
10 | #include <cstdint> | |
11 | ||
12 | #include <babeltrace2/babeltrace.h> | |
13 | ||
14 | #include "borrowed-object.hpp" | |
15 | #include "component-class.hpp" | |
16 | #include "exc.hpp" | |
17 | #include "shared-object.hpp" | |
18 | ||
19 | namespace bt2 { | |
20 | namespace internal { | |
21 | ||
22 | struct GraphRefFuncs final | |
23 | { | |
24 | static void get(const bt_graph * const libObjPtr) noexcept | |
25 | { | |
26 | bt_graph_get_ref(libObjPtr); | |
27 | } | |
28 | ||
29 | static void put(const bt_graph * const libObjPtr) noexcept | |
30 | { | |
31 | bt_graph_put_ref(libObjPtr); | |
32 | } | |
33 | }; | |
34 | ||
35 | } /* namespace internal */ | |
36 | ||
37 | class Graph final : public BorrowedObject<bt_graph> | |
38 | { | |
39 | public: | |
40 | using Shared = SharedObject<Graph, bt_graph, internal::GraphRefFuncs>; | |
41 | ||
42 | explicit Graph(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} | |
43 | { | |
44 | } | |
45 | ||
46 | static Shared create(const std::uint64_t mipVersion) | |
47 | { | |
48 | const auto libObjPtr = bt_graph_create(mipVersion); | |
49 | ||
50 | if (!libObjPtr) { | |
51 | throw MemoryError {}; | |
52 | } | |
53 | ||
54 | return Shared::createWithoutRef(libObjPtr); | |
55 | } | |
56 | ||
57 | ConstSourceComponent addComponent(const ConstSourceComponentClass componentClass, | |
58 | const bt2c::CStringView name, | |
59 | const OptionalBorrowedObject<ConstMapValue> params = {}, | |
1c5ea5eb | 60 | const LoggingLevel loggingLevel = LoggingLevel::None) const |
14d33b5a SM |
61 | { |
62 | return this->_addComponent<ConstSourceComponent>( | |
63 | componentClass, name, params, static_cast<void *>(nullptr), loggingLevel, | |
64 | bt_graph_add_source_component_with_initialize_method_data); | |
65 | } | |
66 | ||
67 | template <typename InitDataT> | |
68 | ConstSourceComponent addComponent(const ConstSourceComponentClass componentClass, | |
1c11db0b | 69 | const bt2c::CStringView name, InitDataT&& initData, |
14d33b5a | 70 | const OptionalBorrowedObject<ConstMapValue> params = {}, |
1c5ea5eb | 71 | const LoggingLevel loggingLevel = LoggingLevel::None) const |
14d33b5a SM |
72 | { |
73 | return this->_addComponent<ConstSourceComponent>( | |
74 | componentClass, name, params, &initData, loggingLevel, | |
75 | bt_graph_add_source_component_with_initialize_method_data); | |
76 | } | |
77 | ||
78 | ConstFilterComponent addComponent(const ConstFilterComponentClass componentClass, | |
79 | const bt2c::CStringView name, | |
80 | const OptionalBorrowedObject<ConstMapValue> params = {}, | |
1c5ea5eb | 81 | const LoggingLevel loggingLevel = LoggingLevel::None) const |
14d33b5a SM |
82 | { |
83 | return this->_addComponent<ConstFilterComponent>( | |
84 | componentClass, name, params, static_cast<void *>(nullptr), loggingLevel, | |
85 | bt_graph_add_filter_component_with_initialize_method_data); | |
86 | } | |
87 | ||
88 | template <typename InitDataT> | |
89 | ConstFilterComponent addComponent(const ConstFilterComponentClass componentClass, | |
1c11db0b | 90 | const bt2c::CStringView name, InitDataT&& initData, |
14d33b5a | 91 | const OptionalBorrowedObject<ConstMapValue> params = {}, |
1c5ea5eb | 92 | const LoggingLevel loggingLevel = LoggingLevel::None) const |
14d33b5a SM |
93 | { |
94 | return this->_addComponent<ConstFilterComponent>( | |
95 | componentClass, name, params, &initData, loggingLevel, | |
96 | bt_graph_add_filter_component_with_initialize_method_data); | |
97 | } | |
98 | ||
99 | ConstSinkComponent addComponent(const ConstSinkComponentClass componentClass, | |
100 | const bt2c::CStringView name, | |
101 | const OptionalBorrowedObject<ConstMapValue> params = {}, | |
1c5ea5eb | 102 | const LoggingLevel loggingLevel = LoggingLevel::None) const |
14d33b5a SM |
103 | { |
104 | return this->_addComponent<ConstSinkComponent>( | |
105 | componentClass, name, params, static_cast<void *>(nullptr), loggingLevel, | |
106 | bt_graph_add_sink_component_with_initialize_method_data); | |
107 | } | |
108 | ||
109 | template <typename InitDataT> | |
110 | ConstSinkComponent addComponent(const ConstSinkComponentClass componentClass, | |
1c11db0b | 111 | const bt2c::CStringView name, InitDataT&& initData, |
14d33b5a | 112 | const OptionalBorrowedObject<ConstMapValue> params = {}, |
1c5ea5eb | 113 | const LoggingLevel loggingLevel = LoggingLevel::None) const |
14d33b5a SM |
114 | { |
115 | return this->_addComponent<ConstSinkComponent>( | |
116 | componentClass, name, params, &initData, loggingLevel, | |
117 | bt_graph_add_sink_component_with_initialize_method_data); | |
118 | } | |
119 | ||
2a24eba8 | 120 | Graph connectPorts(const ConstOutputPort outputPort, const ConstInputPort inputPort) const |
14d33b5a SM |
121 | { |
122 | const auto status = bt_graph_connect_ports(this->libObjPtr(), outputPort.libObjPtr(), | |
123 | inputPort.libObjPtr(), nullptr); | |
124 | ||
125 | if (status == BT_GRAPH_CONNECT_PORTS_STATUS_ERROR) { | |
126 | throw Error {}; | |
127 | } else if (status == BT_GRAPH_CONNECT_PORTS_STATUS_MEMORY_ERROR) { | |
128 | throw MemoryError {}; | |
129 | } | |
2a24eba8 SM |
130 | |
131 | return *this; | |
14d33b5a SM |
132 | } |
133 | ||
2a24eba8 | 134 | Graph runOnce() const |
14d33b5a SM |
135 | { |
136 | const auto status = bt_graph_run_once(this->libObjPtr()); | |
137 | ||
138 | if (status == BT_GRAPH_RUN_ONCE_STATUS_ERROR) { | |
139 | throw Error {}; | |
140 | } else if (status == BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR) { | |
141 | throw MemoryError {}; | |
142 | } else if (status == BT_GRAPH_RUN_ONCE_STATUS_AGAIN) { | |
143 | throw TryAgain {}; | |
144 | } | |
2a24eba8 SM |
145 | |
146 | return *this; | |
14d33b5a SM |
147 | } |
148 | ||
2a24eba8 | 149 | Graph run() const |
14d33b5a SM |
150 | { |
151 | const auto status = bt_graph_run(this->libObjPtr()); | |
152 | ||
153 | if (status == BT_GRAPH_RUN_STATUS_ERROR) { | |
154 | throw Error {}; | |
155 | } else if (status == BT_GRAPH_RUN_STATUS_MEMORY_ERROR) { | |
156 | throw MemoryError {}; | |
157 | } else if (status == BT_GRAPH_RUN_STATUS_AGAIN) { | |
158 | throw TryAgain {}; | |
159 | } | |
2a24eba8 SM |
160 | |
161 | return *this; | |
14d33b5a SM |
162 | } |
163 | ||
164 | private: | |
165 | template <typename ConstComponentT, typename ConstComponentClassT, typename InitDataT, | |
166 | typename AddFuncT> | |
167 | ConstComponentT | |
168 | _addComponent(const ConstComponentClassT componentClass, const bt2c::CStringView name, | |
169 | const OptionalBorrowedObject<ConstMapValue> params, InitDataT * const initData, | |
170 | const LoggingLevel loggingLevel, AddFuncT&& addFunc) const | |
171 | { | |
172 | typename ConstComponentT::LibObjPtr libObjPtr = nullptr; | |
173 | ||
174 | const auto status = addFunc(this->libObjPtr(), componentClass.libObjPtr(), name, | |
175 | params ? params->libObjPtr() : nullptr, | |
176 | const_cast<void *>(static_cast<const void *>(initData)), | |
177 | static_cast<bt_logging_level>(loggingLevel), &libObjPtr); | |
178 | ||
179 | if (status == BT_GRAPH_ADD_COMPONENT_STATUS_ERROR) { | |
180 | throw Error {}; | |
181 | } else if (status == BT_GRAPH_ADD_COMPONENT_STATUS_MEMORY_ERROR) { | |
182 | throw MemoryError {}; | |
183 | } | |
184 | ||
0b383721 | 185 | return ConstComponentT {libObjPtr}; |
14d33b5a SM |
186 | } |
187 | }; | |
188 | ||
189 | } /* namespace bt2 */ | |
190 | ||
191 | #endif /* BABELTRACE_CPP_COMMON_BT2_GRAPH_HPP */ |