6b16e733a38aa3bce63b6c30ee0e8d7adbdd0805
[babeltrace.git] / src / cpp-common / bt2 / internal / comp-cls-bridge.hpp
1 /*
2 * Copyright (c) 2024 EfficiOS, Inc.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #ifndef BABELTRACE_CPP_COMMON_BT2_INTERNAL_COMP_CLS_BRIDGE_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_INTERNAL_COMP_CLS_BRIDGE_HPP
9
10 #include <babeltrace2/babeltrace.h>
11
12 #include "cpp-common/bt2c/c-string-view.hpp"
13 #include "logging/log-api.h"
14
15 #include "../wrap.hpp"
16
17 namespace bt2 {
18 namespace internal {
19
20 constexpr bt2c::CStringView unhandledExcLogStr() noexcept
21 {
22 return "Unhandled exception.";
23 }
24
25 constexpr bt2c::CStringView unhandledExcLogTag() noexcept
26 {
27 return "PLUGIN-DEV-HPP";
28 }
29
30 /*
31 * Base class of any component class bridge.
32 *
33 * `UserCompClsT` is the actual C++ user component class and `LibTypesT`
34 * is a structure offering the following specific library types:
35 *
36 * `SelfCompCls`:
37 * Self component class.
38 *
39 * `SelfComp`:
40 * Self component.
41 *
42 * `SelfCompCfg`:
43 * Self component configuration.
44 */
45 template <typename UserCompClsT, typename LibTypesT>
46 class CompClsBridge
47 {
48 private:
49 using _LibSelfCompPtr = typename LibTypesT::SelfComp *;
50
51 public:
52 static UserCompClsT& userCompFromLibSelfCompPtr(const _LibSelfCompPtr libSelfCompPtr) noexcept
53 {
54 return wrap(libSelfCompPtr).template data<UserCompClsT>();
55 }
56
57 static bt_component_class_initialize_method_status init(const _LibSelfCompPtr libSelfCompPtr,
58 typename LibTypesT::SelfCompCfg *,
59 const bt_value * const libParamsPtr,
60 void * const initData) noexcept
61 {
62 const auto selfComp = wrap(libSelfCompPtr);
63
64 try {
65 const auto comp =
66 new UserCompClsT {selfComp, wrap(libParamsPtr).asMap(),
67 static_cast<typename UserCompClsT::InitData *>(initData)};
68
69 selfComp.data(*comp);
70 } catch (const std::bad_alloc&) {
71 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
72 } catch (const Error&) {
73 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
74 } catch (...) {
75 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, static_cast<int>(selfComp.loggingLevel()),
76 unhandledExcLogTag(), unhandledExcLogStr());
77 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
78 }
79
80 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
81 }
82
83 static void finalize(const _LibSelfCompPtr libSelfCompPtr) noexcept
84 {
85 delete &userCompFromLibSelfCompPtr(libSelfCompPtr);
86 }
87
88 static bt_component_class_get_supported_mip_versions_method_status
89 getSupportedMipVersions(typename LibTypesT::SelfCompCls * const libSelfCompClsPtr,
90 const bt_value * const libParamsPtr, void *,
91 const bt_logging_level logLevel,
92 bt_integer_range_set_unsigned * const libSupportedVersionsPtr) noexcept
93 {
94 try {
95 UserCompClsT::getSupportedMipVersions(wrap(libSelfCompClsPtr), wrap(libParamsPtr),
96 static_cast<LoggingLevel>(logLevel),
97 wrap(libSupportedVersionsPtr));
98 return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_OK;
99 } catch (const std::bad_alloc&) {
100 return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_MEMORY_ERROR;
101 } catch (const Error&) {
102 return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR;
103 } catch (...) {
104 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, static_cast<int>(logLevel), unhandledExcLogTag(),
105 unhandledExcLogStr());
106 return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR;
107 }
108 }
109
110 static bt_component_class_query_method_status
111 query(typename LibTypesT::SelfCompCls * const libSelfCompClsPtr,
112 bt_private_query_executor * const libPrivQueryExecPtr, const char * const object,
113 const bt_value * const libParamsPtr, void * const data,
114 const bt_value ** const libResultPtr) noexcept
115 {
116 const auto privQueryExec = wrap(libPrivQueryExecPtr);
117
118 try {
119 auto result = UserCompClsT::query(
120 wrap(libSelfCompClsPtr), privQueryExec, object, wrap(libParamsPtr),
121 static_cast<typename UserCompClsT::QueryData *>(data));
122
123 *libResultPtr = result.release().libObjPtr();
124 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
125 } catch (const TryAgain&) {
126 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN;
127 } catch (const UnknownObject&) {
128 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT;
129 } catch (const std::bad_alloc&) {
130 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
131 } catch (const Error&) {
132 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
133 } catch (...) {
134 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, static_cast<int>(privQueryExec.loggingLevel()),
135 unhandledExcLogTag(), unhandledExcLogStr());
136 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
137 }
138 }
139 };
140
141 template <typename SpecCompClsBridgeT, typename LibTypesT>
142 struct CompClsBridgeWithInputPorts
143 {
144 static bt_component_class_port_connected_method_status
145 inputPortConnected(typename LibTypesT::SelfComp * const libSelfCompPtr,
146 bt_self_component_port_input * const libSelfCompPortPtr,
147 const bt_port_output * const libOtherPortPtr) noexcept
148 {
149 try {
150 SpecCompClsBridgeT::userCompFromLibSelfCompPtr(libSelfCompPtr)
151 .inputPortConnected(wrap(libSelfCompPortPtr), wrap(libOtherPortPtr));
152 } catch (const std::bad_alloc&) {
153 return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR;
154 } catch (const Error&) {
155 return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR;
156 } catch (...) {
157 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING,
158 static_cast<int>(wrap(libSelfCompPtr).loggingLevel()),
159 unhandledExcLogTag(), unhandledExcLogStr());
160 return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR;
161 }
162
163 return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK;
164 }
165 };
166
167 template <typename SpecCompClsBridgeT, typename LibTypesT>
168 struct CompClsBridgeWithOutputPorts
169 {
170 static bt_component_class_port_connected_method_status
171 outputPortConnected(typename LibTypesT::SelfComp * const libSelfCompPtr,
172 bt_self_component_port_output * const libSelfCompPortPtr,
173 const bt_port_input * const libOtherPortPtr) noexcept
174 {
175 try {
176 SpecCompClsBridgeT::userCompFromLibSelfCompPtr(libSelfCompPtr)
177 .outputPortConnected(wrap(libSelfCompPortPtr), wrap(libOtherPortPtr));
178 } catch (const std::bad_alloc&) {
179 return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR;
180 } catch (const Error&) {
181 return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR;
182 } catch (...) {
183 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING,
184 static_cast<int>(wrap(libSelfCompPtr).loggingLevel()),
185 unhandledExcLogTag(), unhandledExcLogStr());
186 return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR;
187 }
188
189 return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK;
190 }
191 };
192
193 struct SrcCompClsLibTypes final
194 {
195 using SelfCompCls = bt_self_component_class_source;
196 using SelfComp = bt_self_component_source;
197 using SelfCompCfg = bt_self_component_source_configuration;
198 };
199
200 template <typename UserCompClsT>
201 class SrcCompClsBridge final :
202 public CompClsBridge<UserCompClsT, SrcCompClsLibTypes>,
203 public CompClsBridgeWithOutputPorts<SrcCompClsBridge<UserCompClsT>, SrcCompClsLibTypes>
204 {
205 };
206
207 struct FltCompClsLibTypes final
208 {
209 using SelfCompCls = bt_self_component_class_filter;
210 using SelfComp = bt_self_component_filter;
211 using SelfCompCfg = bt_self_component_filter_configuration;
212 };
213
214 template <typename UserCompClsT>
215 class FltCompClsBridge final :
216 public CompClsBridge<UserCompClsT, FltCompClsLibTypes>,
217 public CompClsBridgeWithInputPorts<FltCompClsBridge<UserCompClsT>, FltCompClsLibTypes>,
218 public CompClsBridgeWithOutputPorts<FltCompClsBridge<UserCompClsT>, FltCompClsLibTypes>
219 {
220 };
221
222 struct SinkCompClsLibTypes final
223 {
224 using SelfCompCls = bt_self_component_class_sink;
225 using SelfComp = bt_self_component_sink;
226 using SelfCompCfg = bt_self_component_sink_configuration;
227 };
228
229 template <typename UserCompClsT>
230 class SinkCompClsBridge final :
231 public CompClsBridge<UserCompClsT, SinkCompClsLibTypes>,
232 public CompClsBridgeWithInputPorts<SinkCompClsBridge<UserCompClsT>, SinkCompClsLibTypes>
233 {
234 public:
235 using CompClsBridge<UserCompClsT, SinkCompClsLibTypes>::userCompFromLibSelfCompPtr;
236
237 static bt_component_class_sink_consume_method_status
238 consume(bt_self_component_sink * const libSelfCompPtr) noexcept
239 {
240 try {
241 if (userCompFromLibSelfCompPtr(libSelfCompPtr).consume()) {
242 return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
243 } else {
244 return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
245 }
246 } catch (const TryAgain&) {
247 return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
248 } catch (const std::bad_alloc&) {
249 return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
250 } catch (const Error&) {
251 return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
252 } catch (...) {
253 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING,
254 static_cast<int>(wrap(libSelfCompPtr).loggingLevel()),
255 unhandledExcLogTag(), unhandledExcLogStr());
256 return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
257 }
258 }
259
260 static bt_component_class_sink_graph_is_configured_method_status
261 graphIsConfigured(bt_self_component_sink * const libSelfCompPtr) noexcept
262 {
263 try {
264 userCompFromLibSelfCompPtr(libSelfCompPtr).graphIsConfigured();
265 return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
266 } catch (const std::bad_alloc&) {
267 return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_MEMORY_ERROR;
268 } catch (const Error&) {
269 return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
270 } catch (...) {
271 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING,
272 static_cast<int>(wrap(libSelfCompPtr).loggingLevel()),
273 unhandledExcLogTag(), unhandledExcLogStr());
274 return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
275 }
276 }
277 };
278
279 template <typename UserMsgIterT>
280 class MsgIterClsBridge final
281 {
282 public:
283 static UserMsgIterT&
284 userMsgIterFromLibSelfMsgIterPtr(bt_self_message_iterator * const libSelfMsgIterPtr) noexcept
285 {
286 return wrap(libSelfMsgIterPtr).data<UserMsgIterT>();
287 }
288
289 static bt_message_iterator_class_initialize_method_status
290 init(bt_self_message_iterator * const libSelfMsgIterPtr,
291 bt_self_message_iterator_configuration * const libSelfMsgIterConfigPtr,
292 bt_self_component_port_output * const libSelfCompPortPtr) noexcept
293 {
294 const auto selfMsgIter = wrap(libSelfMsgIterPtr);
295
296 try {
297 const auto msgIter = new UserMsgIterT {selfMsgIter, wrap(libSelfMsgIterConfigPtr),
298 wrap(libSelfCompPortPtr)};
299
300 selfMsgIter.data(*msgIter);
301 } catch (const std::bad_alloc&) {
302 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
303 } catch (const Error&) {
304 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
305 } catch (...) {
306 BT_LOG_WRITE_CUR_LVL(
307 BT_LOG_WARNING,
308 static_cast<int>(wrap(libSelfMsgIterPtr).component().loggingLevel()),
309 unhandledExcLogTag(), unhandledExcLogStr());
310 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
311 }
312
313 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
314 }
315
316 static void finalize(bt_self_message_iterator * const libSelfMsgIterPtr) noexcept
317 {
318 delete &userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr);
319 }
320
321 static bt_message_iterator_class_next_method_status
322 next(bt_self_message_iterator * const libSelfMsgIterPtr, bt_message_array_const libMsgsPtr,
323 const uint64_t capacity, uint64_t * const count) noexcept
324 {
325 try {
326 auto msgArray = ConstMessageArray::wrapEmpty(libMsgsPtr, capacity);
327 auto& msgIter = userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr);
328
329 msgIter.next(msgArray);
330 *count = msgArray.release();
331
332 if (G_LIKELY(*count > 0)) {
333 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
334 } else {
335 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
336 }
337 } catch (const TryAgain&) {
338 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN;
339 } catch (const std::bad_alloc&) {
340 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
341 } catch (const Error&) {
342 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
343 } catch (...) {
344 BT_LOG_WRITE_CUR_LVL(
345 BT_LOG_WARNING,
346 static_cast<int>(wrap(libSelfMsgIterPtr).component().loggingLevel()),
347 unhandledExcLogTag(), unhandledExcLogStr());
348 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
349 }
350 }
351
352 static bt_message_iterator_class_can_seek_beginning_method_status
353 canSeekBeginning(bt_self_message_iterator * const libSelfMsgIterPtr,
354 bt_bool * const canSeek) noexcept
355 {
356 try {
357 *canSeek = static_cast<bt_bool>(
358 userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr).canSeekBeginning());
359 } catch (const TryAgain&) {
360 return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_AGAIN;
361 } catch (const std::bad_alloc&) {
362 return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_MEMORY_ERROR;
363 } catch (const Error&) {
364 return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_ERROR;
365 } catch (...) {
366 BT_LOG_WRITE_CUR_LVL(
367 BT_LOG_WARNING,
368 static_cast<int>(wrap(libSelfMsgIterPtr).component().loggingLevel()),
369 unhandledExcLogTag(), unhandledExcLogStr());
370 return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_ERROR;
371 }
372
373 return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK;
374 }
375
376 static bt_message_iterator_class_seek_beginning_method_status
377 seekBeginning(bt_self_message_iterator * const libSelfMsgIterPtr) noexcept
378 {
379 try {
380 userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr).seekBeginning();
381 } catch (const TryAgain&) {
382 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_AGAIN;
383 } catch (const std::bad_alloc&) {
384 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_MEMORY_ERROR;
385 } catch (const Error&) {
386 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_ERROR;
387 } catch (...) {
388 BT_LOG_WRITE_CUR_LVL(
389 BT_LOG_WARNING,
390 static_cast<int>(wrap(libSelfMsgIterPtr).component().loggingLevel()),
391 unhandledExcLogTag(), unhandledExcLogStr());
392 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_ERROR;
393 }
394
395 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK;
396 }
397
398 static bt_message_iterator_class_can_seek_ns_from_origin_method_status
399 canSeekNsFromOrigin(bt_self_message_iterator * const libSelfMsgIterPtr,
400 const std::int64_t nsFromOrigin, bt_bool * const canSeek) noexcept
401 {
402 try {
403 *canSeek = static_cast<bt_bool>(userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr)
404 .canSeekNsFromOrigin(nsFromOrigin));
405 } catch (const TryAgain&) {
406 return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_AGAIN;
407 } catch (const std::bad_alloc&) {
408 return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_MEMORY_ERROR;
409 } catch (const Error&) {
410 return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR;
411 } catch (...) {
412 BT_LOG_WRITE_CUR_LVL(
413 BT_LOG_WARNING,
414 static_cast<int>(wrap(libSelfMsgIterPtr).component().loggingLevel()),
415 unhandledExcLogTag(), unhandledExcLogStr());
416 return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR;
417 }
418
419 return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK;
420 }
421
422 static bt_message_iterator_class_seek_ns_from_origin_method_status
423 seekNsFromOrigin(bt_self_message_iterator * const libSelfMsgIterPtr,
424 const std::int64_t nsFromOrigin) noexcept
425 {
426 try {
427 userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr).seekNsFromOrigin(nsFromOrigin);
428 } catch (const TryAgain&) {
429 return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_AGAIN;
430 } catch (const std::bad_alloc&) {
431 return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_MEMORY_ERROR;
432 } catch (const Error&) {
433 return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR;
434 } catch (...) {
435 BT_LOG_WRITE_CUR_LVL(
436 BT_LOG_WARNING,
437 static_cast<int>(wrap(libSelfMsgIterPtr).component().loggingLevel()),
438 unhandledExcLogTag(), unhandledExcLogStr());
439 return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR;
440 }
441
442 return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK;
443 }
444 };
445
446 } /* namespace internal */
447 } /* namespace bt2 */
448
449 #endif /* BABELTRACE_CPP_COMMON_BT2_INTERNAL_COMP_CLS_BRIDGE_HPP */
This page took 0.03848 seconds and 3 git commands to generate.