| 1 | /* |
| 2 | * Copyright (c) 2023 Simon Marchi <simon.marchi@efficios.com> |
| 3 | * Copyright (c) 2023 Philippe Proulx <pproulx@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: MIT |
| 6 | */ |
| 7 | |
| 8 | #ifndef BABELTRACE_CPP_COMMON_BT2_PLUGIN_DEV_HPP |
| 9 | #define BABELTRACE_CPP_COMMON_BT2_PLUGIN_DEV_HPP |
| 10 | |
| 11 | #include <cstdint> |
| 12 | |
| 13 | #include <glib.h> |
| 14 | |
| 15 | #include <babeltrace2/babeltrace.h> |
| 16 | |
| 17 | #include "cpp-common/bt2c/c-string-view.hpp" |
| 18 | #include "cpp-common/bt2c/logging.hpp" |
| 19 | #include "cpp-common/vendor/fmt/core.h" |
| 20 | |
| 21 | #include "exc.hpp" |
| 22 | #include "wrap.hpp" |
| 23 | |
| 24 | namespace bt2 { |
| 25 | namespace internal { |
| 26 | |
| 27 | constexpr bt2c::CStringView unhandledExcLogStr() noexcept |
| 28 | { |
| 29 | return "Unhandled exception."; |
| 30 | } |
| 31 | |
| 32 | constexpr bt2c::CStringView unhandledExcLogTag() noexcept |
| 33 | { |
| 34 | return "PLUGIN-DEV-HPP"; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Base class of any component class bridge. |
| 39 | * |
| 40 | * `UserCompClsT` is the actual C++ user component class and `LibTypesT` |
| 41 | * is a structure offering the following specific library types: |
| 42 | * |
| 43 | * `SelfCompCls`: |
| 44 | * Self component class. |
| 45 | * |
| 46 | * `SelfComp`: |
| 47 | * Self component. |
| 48 | * |
| 49 | * `SelfCompCfg`: |
| 50 | * Self component configuration. |
| 51 | */ |
| 52 | template <typename UserCompClsT, typename LibTypesT> |
| 53 | class CompClsBridge |
| 54 | { |
| 55 | private: |
| 56 | using _LibSelfCompPtr = typename LibTypesT::SelfComp *; |
| 57 | |
| 58 | public: |
| 59 | static UserCompClsT& userCompFromLibSelfCompPtr(const _LibSelfCompPtr libSelfCompPtr) noexcept |
| 60 | { |
| 61 | return wrap(libSelfCompPtr).template data<UserCompClsT>(); |
| 62 | } |
| 63 | |
| 64 | static bt_component_class_initialize_method_status init(const _LibSelfCompPtr libSelfCompPtr, |
| 65 | typename LibTypesT::SelfCompCfg *, |
| 66 | const bt_value * const libParamsPtr, |
| 67 | void * const initData) noexcept |
| 68 | { |
| 69 | const auto selfComp = wrap(libSelfCompPtr); |
| 70 | |
| 71 | try { |
| 72 | const auto comp = |
| 73 | new UserCompClsT {selfComp, wrap(libParamsPtr).asMap(), |
| 74 | static_cast<typename UserCompClsT::InitData *>(initData)}; |
| 75 | |
| 76 | selfComp.data(*comp); |
| 77 | } catch (const std::bad_alloc&) { |
| 78 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
| 79 | } catch (const Error&) { |
| 80 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
| 81 | } catch (...) { |
| 82 | BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, static_cast<int>(selfComp.loggingLevel()), |
| 83 | unhandledExcLogTag(), unhandledExcLogStr()); |
| 84 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
| 85 | } |
| 86 | |
| 87 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
| 88 | } |
| 89 | |
| 90 | static void finalize(const _LibSelfCompPtr libSelfCompPtr) noexcept |
| 91 | { |
| 92 | delete &userCompFromLibSelfCompPtr(libSelfCompPtr); |
| 93 | } |
| 94 | |
| 95 | static bt_component_class_get_supported_mip_versions_method_status |
| 96 | getSupportedMipVersions(typename LibTypesT::SelfCompCls * const libSelfCompClsPtr, |
| 97 | const bt_value * const libParamsPtr, void *, |
| 98 | const bt_logging_level logLevel, |
| 99 | bt_integer_range_set_unsigned * const libSupportedVersionsPtr) noexcept |
| 100 | { |
| 101 | try { |
| 102 | UserCompClsT::getSupportedMipVersions(wrap(libSelfCompClsPtr), wrap(libParamsPtr), |
| 103 | static_cast<LoggingLevel>(logLevel), |
| 104 | wrap(libSupportedVersionsPtr)); |
| 105 | return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_OK; |
| 106 | } catch (const std::bad_alloc&) { |
| 107 | return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_MEMORY_ERROR; |
| 108 | } catch (const Error&) { |
| 109 | return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR; |
| 110 | } catch (...) { |
| 111 | BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, static_cast<int>(logLevel), unhandledExcLogTag(), |
| 112 | unhandledExcLogStr()); |
| 113 | return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static bt_component_class_query_method_status |
| 118 | query(typename LibTypesT::SelfCompCls * const libSelfCompClsPtr, |
| 119 | bt_private_query_executor * const libPrivQueryExecPtr, const char * const object, |
| 120 | const bt_value * const libParamsPtr, void * const data, |
| 121 | const bt_value ** const libResultPtr) noexcept |
| 122 | { |
| 123 | const auto privQueryExec = wrap(libPrivQueryExecPtr); |
| 124 | |
| 125 | try { |
| 126 | auto result = UserCompClsT::query( |
| 127 | wrap(libSelfCompClsPtr), privQueryExec, object, wrap(libParamsPtr), |
| 128 | static_cast<typename UserCompClsT::QueryData *>(data)); |
| 129 | |
| 130 | *libResultPtr = result.release().libObjPtr(); |
| 131 | return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; |
| 132 | } catch (const TryAgain&) { |
| 133 | return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; |
| 134 | } catch (const UnknownObject&) { |
| 135 | return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT; |
| 136 | } catch (const std::bad_alloc&) { |
| 137 | return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
| 138 | } catch (const Error&) { |
| 139 | return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 140 | } catch (...) { |
| 141 | BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, static_cast<int>(privQueryExec.loggingLevel()), |
| 142 | unhandledExcLogTag(), unhandledExcLogStr()); |
| 143 | return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 144 | } |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | template <typename SpecCompClsBridgeT, typename LibTypesT> |
| 149 | struct CompClsBridgeWithInputPorts |
| 150 | { |
| 151 | static bt_component_class_port_connected_method_status |
| 152 | inputPortConnected(typename LibTypesT::SelfComp * const libSelfCompPtr, |
| 153 | bt_self_component_port_input * const libSelfCompPortPtr, |
| 154 | const bt_port_output * const libOtherPortPtr) noexcept |
| 155 | { |
| 156 | try { |
| 157 | SpecCompClsBridgeT::userCompFromLibSelfCompPtr(libSelfCompPtr) |
| 158 | .inputPortConnected(wrap(libSelfCompPortPtr), wrap(libOtherPortPtr)); |
| 159 | } catch (const std::bad_alloc&) { |
| 160 | return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR; |
| 161 | } catch (const Error&) { |
| 162 | return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; |
| 163 | } catch (...) { |
| 164 | BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, |
| 165 | static_cast<int>(wrap(libSelfCompPtr).loggingLevel()), |
| 166 | unhandledExcLogTag(), unhandledExcLogStr()); |
| 167 | return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; |
| 168 | } |
| 169 | |
| 170 | return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK; |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | template <typename SpecCompClsBridgeT, typename LibTypesT> |
| 175 | struct CompClsBridgeWithOutputPorts |
| 176 | { |
| 177 | static bt_component_class_port_connected_method_status |
| 178 | outputPortConnected(typename LibTypesT::SelfComp * const libSelfCompPtr, |
| 179 | bt_self_component_port_output * const libSelfCompPortPtr, |
| 180 | const bt_port_input * const libOtherPortPtr) noexcept |
| 181 | { |
| 182 | try { |
| 183 | SpecCompClsBridgeT::userCompFromLibSelfCompPtr(libSelfCompPtr) |
| 184 | .outputPortConnected(wrap(libSelfCompPortPtr), wrap(libOtherPortPtr)); |
| 185 | } catch (const std::bad_alloc&) { |
| 186 | return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR; |
| 187 | } catch (const Error&) { |
| 188 | return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; |
| 189 | } catch (...) { |
| 190 | BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, |
| 191 | static_cast<int>(wrap(libSelfCompPtr).loggingLevel()), |
| 192 | unhandledExcLogTag(), unhandledExcLogStr()); |
| 193 | return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; |
| 194 | } |
| 195 | |
| 196 | return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK; |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | struct SrcCompClsLibTypes final |
| 201 | { |
| 202 | using SelfCompCls = bt_self_component_class_source; |
| 203 | using SelfComp = bt_self_component_source; |
| 204 | using SelfCompCfg = bt_self_component_source_configuration; |
| 205 | }; |
| 206 | |
| 207 | template <typename UserCompClsT> |
| 208 | class SrcCompClsBridge final : |
| 209 | public CompClsBridge<UserCompClsT, SrcCompClsLibTypes>, |
| 210 | public CompClsBridgeWithOutputPorts<SrcCompClsBridge<UserCompClsT>, SrcCompClsLibTypes> |
| 211 | { |
| 212 | }; |
| 213 | |
| 214 | struct FltCompClsLibTypes final |
| 215 | { |
| 216 | using SelfCompCls = bt_self_component_class_filter; |
| 217 | using SelfComp = bt_self_component_filter; |
| 218 | using SelfCompCfg = bt_self_component_filter_configuration; |
| 219 | }; |
| 220 | |
| 221 | template <typename UserCompClsT> |
| 222 | class FltCompClsBridge final : |
| 223 | public CompClsBridge<UserCompClsT, FltCompClsLibTypes>, |
| 224 | public CompClsBridgeWithInputPorts<FltCompClsBridge<UserCompClsT>, FltCompClsLibTypes>, |
| 225 | public CompClsBridgeWithOutputPorts<FltCompClsBridge<UserCompClsT>, FltCompClsLibTypes> |
| 226 | { |
| 227 | }; |
| 228 | |
| 229 | struct SinkCompClsLibTypes final |
| 230 | { |
| 231 | using SelfCompCls = bt_self_component_class_sink; |
| 232 | using SelfComp = bt_self_component_sink; |
| 233 | using SelfCompCfg = bt_self_component_sink_configuration; |
| 234 | }; |
| 235 | |
| 236 | template <typename UserCompClsT> |
| 237 | class SinkCompClsBridge final : |
| 238 | CompClsBridge<UserCompClsT, SinkCompClsLibTypes>, |
| 239 | CompClsBridgeWithInputPorts<SinkCompClsBridge<UserCompClsT>, SinkCompClsLibTypes> |
| 240 | { |
| 241 | private: |
| 242 | using CompClsBridge<UserCompClsT, SinkCompClsLibTypes>::userCompFromLibSelfCompPtr; |
| 243 | |
| 244 | public: |
| 245 | static bt_component_class_sink_consume_method_status |
| 246 | consume(bt_self_component_sink * const libSelfCompPtr) noexcept |
| 247 | { |
| 248 | try { |
| 249 | if (userCompFromLibSelfCompPtr(libSelfCompPtr).consume()) { |
| 250 | return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; |
| 251 | } else { |
| 252 | return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; |
| 253 | } |
| 254 | } catch (const TryAgain&) { |
| 255 | return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN; |
| 256 | } catch (const std::bad_alloc&) { |
| 257 | return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR; |
| 258 | } catch (const Error&) { |
| 259 | return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR; |
| 260 | } catch (...) { |
| 261 | BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, |
| 262 | static_cast<int>(wrap(libSelfCompPtr).loggingLevel()), |
| 263 | unhandledExcLogTag(), unhandledExcLogStr()); |
| 264 | return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static bt_component_class_sink_graph_is_configured_method_status |
| 269 | graphIsConfigured(bt_self_component_sink * const libSelfCompPtr) noexcept |
| 270 | { |
| 271 | try { |
| 272 | userCompFromLibSelfCompPtr(libSelfCompPtr).graphIsConfigured(); |
| 273 | } catch (const std::bad_alloc&) { |
| 274 | return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_MEMORY_ERROR; |
| 275 | } catch (const Error&) { |
| 276 | return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR; |
| 277 | } catch (...) { |
| 278 | BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, |
| 279 | static_cast<int>(wrap(libSelfCompPtr).loggingLevel()), |
| 280 | unhandledExcLogTag(), unhandledExcLogStr()); |
| 281 | return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR; |
| 282 | } |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | template <typename UserMsgIterT> |
| 287 | class MsgIterClsBridge final |
| 288 | { |
| 289 | public: |
| 290 | static UserMsgIterT& |
| 291 | userMsgIterFromLibSelfMsgIterPtr(bt_self_message_iterator * const libSelfMsgIterPtr) noexcept |
| 292 | { |
| 293 | return bt2::wrap(libSelfMsgIterPtr).data<UserMsgIterT>(); |
| 294 | } |
| 295 | |
| 296 | static bt_message_iterator_class_initialize_method_status |
| 297 | init(bt_self_message_iterator * const libSelfMsgIterPtr, |
| 298 | bt_self_message_iterator_configuration * const libSelfMsgIterConfigPtr, |
| 299 | bt_self_component_port_output * const libSelfCompPortPtr) noexcept |
| 300 | { |
| 301 | const auto selfMsgIter = bt2::wrap(libSelfMsgIterPtr); |
| 302 | |
| 303 | try { |
| 304 | const auto msgIter = new UserMsgIterT {selfMsgIter, bt2::wrap(libSelfMsgIterConfigPtr), |
| 305 | bt2::wrap(libSelfCompPortPtr)}; |
| 306 | |
| 307 | selfMsgIter.data(*msgIter); |
| 308 | } catch (const std::bad_alloc&) { |
| 309 | return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
| 310 | } catch (const bt2::Error&) { |
| 311 | return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
| 312 | } catch (...) { |
| 313 | BT_LOG_WRITE_CUR_LVL( |
| 314 | BT_LOG_WARNING, |
| 315 | static_cast<int>(wrap(libSelfMsgIterPtr).component().loggingLevel()), |
| 316 | unhandledExcLogTag(), unhandledExcLogStr()); |
| 317 | return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
| 318 | } |
| 319 | |
| 320 | return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; |
| 321 | } |
| 322 | |
| 323 | static void finalize(bt_self_message_iterator * const libSelfMsgIterPtr) noexcept |
| 324 | { |
| 325 | delete &userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr); |
| 326 | } |
| 327 | |
| 328 | static bt_message_iterator_class_next_method_status |
| 329 | next(bt_self_message_iterator * const libSelfMsgIterPtr, bt_message_array_const libMsgsPtr, |
| 330 | const uint64_t capacity, uint64_t * const count) noexcept |
| 331 | { |
| 332 | try { |
| 333 | auto msgArray = bt2::ConstMessageArray::wrapEmpty(libMsgsPtr, capacity); |
| 334 | auto& msgIter = userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr); |
| 335 | |
| 336 | msgIter.next(msgArray); |
| 337 | *count = msgArray.release(); |
| 338 | |
| 339 | if (G_LIKELY(*count > 0)) { |
| 340 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; |
| 341 | } else { |
| 342 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; |
| 343 | } |
| 344 | } catch (const bt2::TryAgain&) { |
| 345 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; |
| 346 | } catch (const std::bad_alloc&) { |
| 347 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; |
| 348 | } catch (const bt2::Error&) { |
| 349 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
| 350 | } catch (...) { |
| 351 | BT_LOG_WRITE_CUR_LVL( |
| 352 | BT_LOG_WARNING, |
| 353 | static_cast<int>(wrap(libSelfMsgIterPtr).component().loggingLevel()), |
| 354 | unhandledExcLogTag(), unhandledExcLogStr()); |
| 355 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | static bt_message_iterator_class_can_seek_beginning_method_status |
| 360 | canSeekBeginning(bt_self_message_iterator * const libSelfMsgIterPtr, |
| 361 | bt_bool * const canSeek) noexcept |
| 362 | { |
| 363 | try { |
| 364 | *canSeek = static_cast<bt_bool>( |
| 365 | userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr).canSeekBeginning()); |
| 366 | } catch (const bt2::TryAgain&) { |
| 367 | return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_AGAIN; |
| 368 | } catch (const std::bad_alloc&) { |
| 369 | return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_MEMORY_ERROR; |
| 370 | } catch (const bt2::Error&) { |
| 371 | return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_ERROR; |
| 372 | } catch (...) { |
| 373 | BT_LOG_WRITE_CUR_LVL( |
| 374 | BT_LOG_WARNING, |
| 375 | static_cast<int>(wrap(libSelfMsgIterPtr).component().loggingLevel()), |
| 376 | unhandledExcLogTag(), unhandledExcLogStr()); |
| 377 | return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_ERROR; |
| 378 | } |
| 379 | |
| 380 | return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK; |
| 381 | } |
| 382 | |
| 383 | static bt_message_iterator_class_seek_beginning_method_status |
| 384 | seekBeginning(bt_self_message_iterator * const libSelfMsgIterPtr) noexcept |
| 385 | { |
| 386 | try { |
| 387 | userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr).seekBeginning(); |
| 388 | } catch (const bt2::TryAgain&) { |
| 389 | return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_AGAIN; |
| 390 | } catch (const std::bad_alloc&) { |
| 391 | return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_MEMORY_ERROR; |
| 392 | } catch (const bt2::Error&) { |
| 393 | return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_ERROR; |
| 394 | } catch (...) { |
| 395 | BT_LOG_WRITE_CUR_LVL( |
| 396 | BT_LOG_WARNING, |
| 397 | static_cast<int>(wrap(libSelfMsgIterPtr).component().loggingLevel()), |
| 398 | unhandledExcLogTag(), unhandledExcLogStr()); |
| 399 | return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_ERROR; |
| 400 | } |
| 401 | |
| 402 | return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK; |
| 403 | } |
| 404 | |
| 405 | static bt_message_iterator_class_can_seek_ns_from_origin_method_status |
| 406 | canSeekNsFromOrigin(bt_self_message_iterator * const libSelfMsgIterPtr, |
| 407 | const std::int64_t nsFromOrigin, bt_bool * const canSeek) noexcept |
| 408 | { |
| 409 | try { |
| 410 | *canSeek = static_cast<bt_bool>(userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr) |
| 411 | .canSeekNsFromOrigin(nsFromOrigin)); |
| 412 | } catch (const bt2::TryAgain&) { |
| 413 | return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_AGAIN; |
| 414 | } catch (const std::bad_alloc&) { |
| 415 | return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_MEMORY_ERROR; |
| 416 | } catch (const bt2::Error&) { |
| 417 | return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR; |
| 418 | } catch (...) { |
| 419 | BT_LOG_WRITE_CUR_LVL( |
| 420 | BT_LOG_WARNING, |
| 421 | static_cast<int>(wrap(libSelfMsgIterPtr).component().loggingLevel()), |
| 422 | unhandledExcLogTag(), unhandledExcLogStr()); |
| 423 | return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR; |
| 424 | } |
| 425 | |
| 426 | return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; |
| 427 | } |
| 428 | |
| 429 | static bt_message_iterator_class_seek_ns_from_origin_method_status |
| 430 | seekNsFromOrigin(bt_self_message_iterator * const libSelfMsgIterPtr, |
| 431 | const std::int64_t nsFromOrigin) noexcept |
| 432 | { |
| 433 | try { |
| 434 | userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr).seekNsFromOrigin(nsFromOrigin); |
| 435 | } catch (const bt2::TryAgain&) { |
| 436 | return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_AGAIN; |
| 437 | } catch (const std::bad_alloc&) { |
| 438 | return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_MEMORY_ERROR; |
| 439 | } catch (const bt2::Error&) { |
| 440 | return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR; |
| 441 | } catch (...) { |
| 442 | BT_LOG_WRITE_CUR_LVL( |
| 443 | BT_LOG_WARNING, |
| 444 | static_cast<int>(wrap(libSelfMsgIterPtr).component().loggingLevel()), |
| 445 | unhandledExcLogTag(), unhandledExcLogStr()); |
| 446 | return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR; |
| 447 | } |
| 448 | |
| 449 | return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; |
| 450 | } |
| 451 | }; |
| 452 | |
| 453 | } /* namespace internal */ |
| 454 | |
| 455 | template <typename UserMessageIteratorT, typename UserComponentT> |
| 456 | class UserMessageIterator; |
| 457 | |
| 458 | /* |
| 459 | * Base class of any user component. |
| 460 | * |
| 461 | * See the specific `bt2::UserSourceComponent`, |
| 462 | * `bt2::UserFilterComponent`, and `bt2::UserSinkComponent`. |
| 463 | */ |
| 464 | template <typename SelfCompT, typename InitDataT, typename QueryDataT> |
| 465 | class UserComponent |
| 466 | { |
| 467 | /* Give a related message iterator access to this logger */ |
| 468 | template <typename, typename> |
| 469 | friend class UserMessageIterator; |
| 470 | |
| 471 | public: |
| 472 | using InitData = InitDataT; |
| 473 | using QueryData = QueryDataT; |
| 474 | |
| 475 | protected: |
| 476 | explicit UserComponent(const SelfCompT selfComp, const std::string& logTag) : |
| 477 | _mLogger {selfComp, fmt::format("{}/[{}]", logTag, selfComp.name())}, _mSelfComp {selfComp} |
| 478 | { |
| 479 | } |
| 480 | |
| 481 | protected: |
| 482 | bt2c::CStringView _name() const noexcept |
| 483 | { |
| 484 | return _mSelfComp.name(); |
| 485 | } |
| 486 | |
| 487 | LoggingLevel _loggingLevel() const noexcept |
| 488 | { |
| 489 | return _mSelfComp.loggingLevel(); |
| 490 | } |
| 491 | |
| 492 | std::uint64_t _graphMipVersion() const noexcept |
| 493 | { |
| 494 | return _mSelfComp.graphMipVersion(); |
| 495 | } |
| 496 | |
| 497 | SelfCompT _selfComp() noexcept |
| 498 | { |
| 499 | return _mSelfComp; |
| 500 | } |
| 501 | |
| 502 | bt2c::Logger _mLogger; |
| 503 | |
| 504 | private: |
| 505 | SelfCompT _mSelfComp; |
| 506 | }; |
| 507 | |
| 508 | /* |
| 509 | * Base class of a user source component `UserComponentT` (CRTP). |
| 510 | * |
| 511 | * UserComponentT::UserComponentT() must accept, in this order: |
| 512 | * |
| 513 | * 1. A `bt2::SelfSourceComponent` parameter, which it needs to forward |
| 514 | * to bt2::UserSourceComponent::UserSourceComponent(). |
| 515 | * |
| 516 | * 2. A `bt2::ConstValue` parameter (the initialization parameters). |
| 517 | * |
| 518 | * 3. An `InitDataT *` parameter (the initialization method data). |
| 519 | * |
| 520 | * `UserMessageIteratorT`, the message iterator class to use, must inherit |
| 521 | * `UserMessageIterator`. |
| 522 | * |
| 523 | * UserComponentT::_query() receives a query method data pointer of type |
| 524 | * `QueryDataT *` as its last parameter. |
| 525 | */ |
| 526 | template <typename UserComponentT, typename UserMessageIteratorT, typename InitDataT = void, |
| 527 | typename QueryDataT = void> |
| 528 | class UserSourceComponent : public UserComponent<SelfSourceComponent, InitDataT, QueryDataT> |
| 529 | { |
| 530 | static_assert(std::is_base_of<UserMessageIterator<UserMessageIteratorT, UserComponentT>, |
| 531 | UserMessageIteratorT>::value, |
| 532 | "`UserMessageIteratorT` inherits `UserMessageIterator`"); |
| 533 | |
| 534 | public: |
| 535 | using MessageIterator = UserMessageIteratorT; |
| 536 | |
| 537 | protected: |
| 538 | using _OutputPorts = SelfSourceComponent::OutputPorts; |
| 539 | |
| 540 | explicit UserSourceComponent(const SelfSourceComponent selfComp, const std::string& logTag) : |
| 541 | UserComponent<SelfSourceComponent, InitDataT, QueryDataT> {selfComp, logTag} |
| 542 | { |
| 543 | } |
| 544 | |
| 545 | public: |
| 546 | static Value::Shared query(const SelfComponentClass selfCompCls, |
| 547 | const PrivateQueryExecutor privQueryExec, |
| 548 | const bt2c::CStringView obj, const ConstValue params, |
| 549 | QueryDataT * const data) |
| 550 | { |
| 551 | return UserComponentT::_query(selfCompCls, privQueryExec, obj, params, data); |
| 552 | } |
| 553 | |
| 554 | static void getSupportedMipVersions(const SelfComponentClass selfCompCls, |
| 555 | const ConstValue params, const LoggingLevel loggingLevel, |
| 556 | const UnsignedIntegerRangeSet ranges) |
| 557 | { |
| 558 | UserComponentT::_getSupportedMipVersions(selfCompCls, params, loggingLevel, ranges); |
| 559 | } |
| 560 | |
| 561 | void outputPortConnected(const SelfComponentOutputPort outputPort, |
| 562 | const ConstInputPort inputPort) |
| 563 | { |
| 564 | static_cast<UserComponentT&>(*this).outputPortConnected(outputPort, inputPort); |
| 565 | } |
| 566 | |
| 567 | protected: |
| 568 | /* Overloadable */ |
| 569 | static Value::Shared _query(SelfComponentClass, PrivateQueryExecutor, bt2c::CStringView, |
| 570 | ConstValue, QueryDataT *) |
| 571 | { |
| 572 | throw UnknownObject {}; |
| 573 | } |
| 574 | |
| 575 | /* Overloadable */ |
| 576 | static void _getSupportedMipVersions(SelfComponentClass, ConstValue, LoggingLevel, |
| 577 | const UnsignedIntegerRangeSet ranges) |
| 578 | { |
| 579 | ranges.addRange(0, 0); |
| 580 | } |
| 581 | |
| 582 | /* Overloadable */ |
| 583 | void _outputPortConnected(SelfComponentOutputPort, ConstInputPort) |
| 584 | { |
| 585 | } |
| 586 | |
| 587 | template <typename DataT> |
| 588 | _OutputPorts::Port _addOutputPort(const bt2c::CStringView name, DataT * const data) |
| 589 | { |
| 590 | return this->_selfComp().addOutputPort(name, data); |
| 591 | } |
| 592 | |
| 593 | _OutputPorts::Port _addOutputPort(const bt2c::CStringView name) |
| 594 | { |
| 595 | return this->_selfComp().addOutputPort(name); |
| 596 | } |
| 597 | |
| 598 | _OutputPorts _outputPorts() noexcept |
| 599 | { |
| 600 | return this->_selfComp().outputPorts(); |
| 601 | } |
| 602 | }; |
| 603 | |
| 604 | /* |
| 605 | * Base class of a user filter component `UserComponentT` (CRTP). |
| 606 | * |
| 607 | * UserComponentT::UserComponentT() must accept, in this order: |
| 608 | * |
| 609 | * 1. A `bt2::SelfFilterComponent` parameter, which it needs to forward |
| 610 | * to bt2::UserFilterComponent::UserFilterComponent(). |
| 611 | * |
| 612 | * 2. A `bt2::ConstValue` parameter (the initialization parameters). |
| 613 | * |
| 614 | * 3. An `InitDataT *` parameter (the initialization method data). |
| 615 | * |
| 616 | * `UserMessageIteratorT`, the message iterator class to use, must inherit |
| 617 | * `UserMessageIterator`. |
| 618 | * |
| 619 | * UserComponentT::_query() receives a query method data pointer of type |
| 620 | * `QueryDataT *` as its last parameter. |
| 621 | */ |
| 622 | template <typename UserComponentT, typename UserMessageIteratorT, typename InitDataT = void, |
| 623 | typename QueryDataT = void> |
| 624 | class UserFilterComponent : public UserComponent<SelfFilterComponent, InitDataT, QueryDataT> |
| 625 | { |
| 626 | static_assert(std::is_base_of<UserMessageIterator<UserMessageIteratorT, UserComponentT>, |
| 627 | UserMessageIteratorT>::value, |
| 628 | "`UserMessageIteratorT` inherits `UserMessageIterator`"); |
| 629 | |
| 630 | public: |
| 631 | using MessageIterator = UserMessageIteratorT; |
| 632 | |
| 633 | protected: |
| 634 | using _InputPorts = SelfFilterComponent::InputPorts; |
| 635 | using _OutputPorts = SelfFilterComponent::OutputPorts; |
| 636 | |
| 637 | explicit UserFilterComponent(const SelfFilterComponent selfComp, const std::string& logTag) : |
| 638 | UserComponent<SelfFilterComponent, InitDataT, QueryDataT> {selfComp, logTag} |
| 639 | { |
| 640 | } |
| 641 | |
| 642 | public: |
| 643 | static Value::Shared query(const SelfComponentClass selfCompCls, |
| 644 | const PrivateQueryExecutor privQueryExec, |
| 645 | const bt2c::CStringView obj, const ConstValue params, |
| 646 | QueryDataT * const data) |
| 647 | { |
| 648 | return UserComponentT::_query(selfCompCls, privQueryExec, obj, params, data); |
| 649 | } |
| 650 | |
| 651 | static void getSupportedMipVersions(const SelfComponentClass selfCompCls, |
| 652 | const ConstValue params, const LoggingLevel loggingLevel, |
| 653 | const UnsignedIntegerRangeSet ranges) |
| 654 | { |
| 655 | UserComponentT::_getSupportedMipVersions(selfCompCls, params, loggingLevel, ranges); |
| 656 | } |
| 657 | |
| 658 | void inputPortConnected(const SelfComponentInputPort inputPort, |
| 659 | const ConstOutputPort outputPort) |
| 660 | { |
| 661 | static_cast<UserComponentT&>(*this)._inputPortConnected(inputPort, outputPort); |
| 662 | } |
| 663 | |
| 664 | void outputPortConnected(const SelfComponentOutputPort outputPort, |
| 665 | const ConstInputPort inputPort) |
| 666 | { |
| 667 | static_cast<UserComponentT&>(*this)._outputPortConnected(outputPort, inputPort); |
| 668 | } |
| 669 | |
| 670 | protected: |
| 671 | /* Overloadable */ |
| 672 | static Value::Shared _query(SelfComponentClass, PrivateQueryExecutor, bt2c::CStringView, |
| 673 | ConstValue, QueryDataT *) |
| 674 | { |
| 675 | throw UnknownObject {}; |
| 676 | } |
| 677 | |
| 678 | /* Overloadable */ |
| 679 | static void _getSupportedMipVersions(SelfComponentClass, ConstValue, LoggingLevel, |
| 680 | const UnsignedIntegerRangeSet ranges) |
| 681 | { |
| 682 | ranges.addRange(0, 0); |
| 683 | } |
| 684 | |
| 685 | /* Overloadable */ |
| 686 | void _inputPortConnected(SelfComponentInputPort, ConstOutputPort) |
| 687 | { |
| 688 | } |
| 689 | |
| 690 | /* Overloadable */ |
| 691 | void _outputPortConnected(SelfComponentOutputPort, ConstInputPort) |
| 692 | { |
| 693 | } |
| 694 | |
| 695 | template <typename DataT> |
| 696 | _OutputPorts::Port _addInputPort(const bt2c::CStringView name, DataT * const data) |
| 697 | { |
| 698 | return this->_selfComp().addInputPort(name, data); |
| 699 | } |
| 700 | |
| 701 | _InputPorts::Port _addInputPort(const bt2c::CStringView name) |
| 702 | { |
| 703 | return this->_selfComp().addInputPort(name); |
| 704 | } |
| 705 | |
| 706 | _InputPorts _inputPorts() noexcept |
| 707 | { |
| 708 | return this->_selfComp().inputPorts(); |
| 709 | } |
| 710 | |
| 711 | template <typename DataT> |
| 712 | _OutputPorts::Port _addOutputPort(const bt2c::CStringView name, DataT * const data) |
| 713 | { |
| 714 | return this->_selfComp().addOutputPort(name, data); |
| 715 | } |
| 716 | |
| 717 | _OutputPorts::Port _addOutputPort(const bt2c::CStringView name) |
| 718 | { |
| 719 | return this->_selfComp().addOutputPort(name); |
| 720 | } |
| 721 | |
| 722 | _OutputPorts _outputPorts() noexcept |
| 723 | { |
| 724 | return this->_selfComp().outputPorts(); |
| 725 | } |
| 726 | }; |
| 727 | |
| 728 | /* |
| 729 | * Base class of a user sink component `UserComponentT` (CRTP). |
| 730 | * |
| 731 | * UserComponentT::UserComponentT() must accept, in this order: |
| 732 | * |
| 733 | * 1. A `bt2::SelfSinkComponent` parameter, which it needs to forward |
| 734 | * to bt2::UserSinkComponent::UserSinkComponent(). |
| 735 | * |
| 736 | * 2. A `bt2::ConstValue` parameter (the initialization parameters). |
| 737 | * |
| 738 | * 3. An `InitDataT *` parameter (the initialization method data). |
| 739 | * |
| 740 | * `UserComponentT` must implement: |
| 741 | * |
| 742 | * bool _consume(); |
| 743 | * |
| 744 | * This method returns `true` if the sink component still needs to |
| 745 | * consume, or `false` if it's finished. |
| 746 | * |
| 747 | * UserComponentT::_query() receives a query method data pointer of type |
| 748 | * `QueryDataT *` as its last parameter. |
| 749 | |
| 750 | */ |
| 751 | template <typename UserComponentT, typename InitDataT = void, typename QueryDataT = void> |
| 752 | class UserSinkComponent : public UserComponent<SelfSinkComponent, InitDataT, QueryDataT> |
| 753 | { |
| 754 | protected: |
| 755 | using _InputPorts = SelfSinkComponent::InputPorts; |
| 756 | |
| 757 | explicit UserSinkComponent(const SelfSinkComponent selfComp, const std::string& logTag) : |
| 758 | UserComponent<SelfSinkComponent, InitDataT, QueryDataT> {selfComp, logTag} |
| 759 | { |
| 760 | } |
| 761 | |
| 762 | public: |
| 763 | static Value::Shared query(const SelfComponentClass selfCompCls, |
| 764 | const PrivateQueryExecutor privQueryExec, |
| 765 | const bt2c::CStringView obj, const ConstValue params, |
| 766 | QueryDataT * const data) |
| 767 | { |
| 768 | return UserComponentT::_query(selfCompCls, privQueryExec, obj, params, data); |
| 769 | } |
| 770 | |
| 771 | static void getSupportedMipVersions(const SelfComponentClass selfCompCls, |
| 772 | const ConstValue params, const LoggingLevel loggingLevel, |
| 773 | const UnsignedIntegerRangeSet ranges) |
| 774 | { |
| 775 | UserComponentT::_getSupportedMipVersions(selfCompCls, params, loggingLevel, ranges); |
| 776 | } |
| 777 | |
| 778 | void graphIsConfigured() |
| 779 | { |
| 780 | static_cast<UserComponentT&>(*this)._graphIsConfigured(); |
| 781 | } |
| 782 | |
| 783 | void inputPortConnected(const SelfComponentInputPort inputPort, |
| 784 | const ConstOutputPort outputPort) |
| 785 | { |
| 786 | static_cast<UserComponentT&>(*this)._inputPortConnected(inputPort, outputPort); |
| 787 | } |
| 788 | |
| 789 | bool consume() |
| 790 | { |
| 791 | return static_cast<UserComponentT&>(*this)._consume(); |
| 792 | } |
| 793 | |
| 794 | protected: |
| 795 | /* Overloadable */ |
| 796 | static Value::Shared _query(SelfComponentClass, PrivateQueryExecutor, bt2c::CStringView, |
| 797 | ConstValue, QueryDataT *) |
| 798 | { |
| 799 | throw UnknownObject {}; |
| 800 | } |
| 801 | |
| 802 | /* Overloadable */ |
| 803 | static void _getSupportedMipVersions(SelfComponentClass, ConstValue, LoggingLevel, |
| 804 | const UnsignedIntegerRangeSet ranges) |
| 805 | { |
| 806 | ranges.addRange(0, 0); |
| 807 | } |
| 808 | |
| 809 | /* Overloadable */ |
| 810 | void _graphIsConfigured() |
| 811 | { |
| 812 | } |
| 813 | |
| 814 | /* Overloadable */ |
| 815 | void _inputPortConnected(SelfComponentInputPort, ConstOutputPort) |
| 816 | { |
| 817 | } |
| 818 | |
| 819 | MessageIterator::Shared _createMessageIterator(const _InputPorts::Port port) |
| 820 | { |
| 821 | return this->_selfComp().createMessageIterator(port); |
| 822 | } |
| 823 | |
| 824 | template <typename DataT> |
| 825 | _InputPorts::Port _addInputPort(const bt2c::CStringView name, DataT * const data) |
| 826 | { |
| 827 | return this->_selfComp().addInputPort(name, data); |
| 828 | } |
| 829 | |
| 830 | _InputPorts::Port _addInputPort(const bt2c::CStringView name) |
| 831 | { |
| 832 | return this->_selfComp().addInputPort(name); |
| 833 | } |
| 834 | |
| 835 | _InputPorts _inputPorts() noexcept |
| 836 | { |
| 837 | return this->_selfComp().inputPorts(); |
| 838 | } |
| 839 | }; |
| 840 | |
| 841 | /* |
| 842 | * Base class of a user message iterator `UserMessageIteratorT` (CRTP) |
| 843 | * of which the parent user component class is `UserComponentT`. |
| 844 | * |
| 845 | * `UserMessageIteratorT::UserMessageIteratorT()` must accept a |
| 846 | * `bt2::SelfMessageIterator` parameter, which it needs to forward to |
| 847 | * bt2::UserMessageIterator::UserMessageIterator(). |
| 848 | * |
| 849 | * The public next() method below (called by the bridge) implements the |
| 850 | * very common pattern of appending messages into the output array, and, |
| 851 | * meanwhile: |
| 852 | * |
| 853 | * If it catches a `bt2::TryAgain` exception: |
| 854 | * If the message array isn't empty, transform this into a success |
| 855 | * (don't throw). |
| 856 | * |
| 857 | * Otherwise rethrow. |
| 858 | * |
| 859 | * If it catches an error: |
| 860 | * If the message array isn't empty, transform this into a success |
| 861 | * (don't throw), but save the error of the current thread and the |
| 862 | * type of error to throw the next time the user calls next(). |
| 863 | * |
| 864 | * Otherwise rethrow. |
| 865 | * |
| 866 | * `UserMessageIteratorT` must implement: |
| 867 | * |
| 868 | * void _next(bt2::ConstMessageArray& messages); |
| 869 | * |
| 870 | * This method fills `messages` with at most `messages.capacity()` |
| 871 | * messages and may throw `bt2::TryAgain` or a valid error whenever. |
| 872 | * Leaving an empty `messages` means the end of iteration. |
| 873 | */ |
| 874 | template <typename UserMessageIteratorT, typename UserComponentT> |
| 875 | class UserMessageIterator |
| 876 | { |
| 877 | private: |
| 878 | /* Type of `_mExcToThrowType` */ |
| 879 | enum class _ExcToThrowType |
| 880 | { |
| 881 | NONE, |
| 882 | ERROR, |
| 883 | MEM_ERROR, |
| 884 | }; |
| 885 | |
| 886 | protected: |
| 887 | explicit UserMessageIterator(const SelfMessageIterator selfMsgIter, |
| 888 | const std::string& logTagSuffix) : |
| 889 | _mSelfMsgIter {selfMsgIter}, |
| 890 | _mLogger {selfMsgIter, |
| 891 | fmt::format("{}/{}", this->_component()._mLogger.tag(), logTagSuffix)} |
| 892 | { |
| 893 | } |
| 894 | |
| 895 | public: |
| 896 | ~UserMessageIterator() |
| 897 | { |
| 898 | this->_resetError(); |
| 899 | } |
| 900 | |
| 901 | void next(bt2::ConstMessageArray& messages) |
| 902 | { |
| 903 | /* Any saved error? Now is the time to throw */ |
| 904 | if (G_UNLIKELY(_mExcToThrowType != _ExcToThrowType::NONE)) { |
| 905 | /* Move `_mSavedLibError`, if any, as current thread error */ |
| 906 | if (_mSavedLibError) { |
| 907 | BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(_mSavedLibError); |
| 908 | } |
| 909 | |
| 910 | /* Throw the corresponding exception */ |
| 911 | if (_mExcToThrowType == _ExcToThrowType::ERROR) { |
| 912 | throw bt2::Error {}; |
| 913 | } else { |
| 914 | BT_ASSERT(_mExcToThrowType == _ExcToThrowType::MEM_ERROR); |
| 915 | throw bt2::MemoryError {}; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * When catching some exception below, if our message array |
| 921 | * isn't empty, then return immediately before throwing to |
| 922 | * provide those messages to downstream. |
| 923 | * |
| 924 | * When catching an error, also save the current thread error, |
| 925 | * if any, so that we can restore it later (see the beginning of |
| 926 | * this method). |
| 927 | */ |
| 928 | BT_ASSERT_DBG(_mExcToThrowType == _ExcToThrowType::NONE); |
| 929 | |
| 930 | try { |
| 931 | this->_userObj()._next(messages); |
| 932 | |
| 933 | /* We're done: everything below is exception handling */ |
| 934 | return; |
| 935 | } catch (const bt2::TryAgain&) { |
| 936 | if (messages.isEmpty()) { |
| 937 | throw; |
| 938 | } |
| 939 | } catch (const std::bad_alloc&) { |
| 940 | if (messages.isEmpty()) { |
| 941 | throw; |
| 942 | } |
| 943 | |
| 944 | _mExcToThrowType = _ExcToThrowType::MEM_ERROR; |
| 945 | } catch (const bt2::Error&) { |
| 946 | if (messages.isEmpty()) { |
| 947 | throw; |
| 948 | } |
| 949 | |
| 950 | _mExcToThrowType = _ExcToThrowType::ERROR; |
| 951 | } |
| 952 | |
| 953 | if (_mExcToThrowType != _ExcToThrowType::NONE) { |
| 954 | BT_CPPLOGE( |
| 955 | "An error occurred, but there are {} messages to return: delaying the error reporting.", |
| 956 | messages.length()); |
| 957 | BT_ASSERT(!_mSavedLibError); |
| 958 | _mSavedLibError = bt_current_thread_take_error(); |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | bool canSeekBeginning() |
| 963 | { |
| 964 | this->_resetError(); |
| 965 | return this->_userObj()._canSeekBeginning(); |
| 966 | } |
| 967 | |
| 968 | void seekBeginning() |
| 969 | { |
| 970 | this->_resetError(); |
| 971 | return this->_userObj()._seekBeginning(); |
| 972 | } |
| 973 | |
| 974 | bool canSeekNsFromOrigin(const std::int64_t nsFromOrigin) |
| 975 | { |
| 976 | this->_resetError(); |
| 977 | return this->_userObj()._canSeekNsFromOrigin(nsFromOrigin); |
| 978 | } |
| 979 | |
| 980 | void seekNsFromOrigin(const std::int64_t nsFromOrigin) |
| 981 | { |
| 982 | this->_resetError(); |
| 983 | this->_userObj()._seekNsFromOrigin(nsFromOrigin); |
| 984 | } |
| 985 | |
| 986 | protected: |
| 987 | /* Overloadable */ |
| 988 | bool _canSeekBeginning() noexcept |
| 989 | { |
| 990 | return false; |
| 991 | } |
| 992 | |
| 993 | /* Overloadable */ |
| 994 | void _seekBeginning() noexcept |
| 995 | { |
| 996 | } |
| 997 | |
| 998 | /* Overloadable */ |
| 999 | bool _canSeekNsFromOrigin(std::int64_t) noexcept |
| 1000 | { |
| 1001 | return false; |
| 1002 | } |
| 1003 | |
| 1004 | /* Overloadable */ |
| 1005 | void _seekNsFromOrigin(std::int64_t) noexcept |
| 1006 | { |
| 1007 | } |
| 1008 | |
| 1009 | MessageIterator::Shared _createMessageIterator(const SelfComponentInputPort port) |
| 1010 | { |
| 1011 | return _mSelfMsgIter.createMessageIterator(port); |
| 1012 | } |
| 1013 | |
| 1014 | UserComponentT& _component() noexcept |
| 1015 | { |
| 1016 | return _mSelfMsgIter.component().template data<UserComponentT>(); |
| 1017 | } |
| 1018 | |
| 1019 | SelfComponentOutputPort _port() noexcept |
| 1020 | { |
| 1021 | return _mSelfMsgIter.port(); |
| 1022 | } |
| 1023 | |
| 1024 | bool _isInterrupted() const noexcept |
| 1025 | { |
| 1026 | return _mSelfMsgIter.isInterrupted(); |
| 1027 | } |
| 1028 | |
| 1029 | private: |
| 1030 | UserMessageIteratorT& _userObj() noexcept |
| 1031 | { |
| 1032 | return static_cast<UserMessageIteratorT&>(*this); |
| 1033 | } |
| 1034 | |
| 1035 | void _resetError() noexcept |
| 1036 | { |
| 1037 | _mExcToThrowType = _ExcToThrowType::NONE; |
| 1038 | |
| 1039 | if (_mSavedLibError) { |
| 1040 | bt_error_release(_mSavedLibError); |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | SelfMessageIterator _mSelfMsgIter; |
| 1045 | |
| 1046 | /* |
| 1047 | * next() may accumulate messages, and then catch an error before |
| 1048 | * returning. In that case, it saves the error of the current thread |
| 1049 | * here so that it can return its accumulated messages and throw the |
| 1050 | * next time. |
| 1051 | * |
| 1052 | * It also saves the type of the exception to throw the next time. |
| 1053 | */ |
| 1054 | _ExcToThrowType _mExcToThrowType = _ExcToThrowType::NONE; |
| 1055 | const bt_error *_mSavedLibError = nullptr; |
| 1056 | |
| 1057 | protected: |
| 1058 | bt2c::Logger _mLogger; |
| 1059 | }; |
| 1060 | |
| 1061 | } /* namespace bt2 */ |
| 1062 | |
| 1063 | #define BT_CPP_PLUGIN_SOURCE_COMPONENT_CLASS_WITH_ID(_pluginId, _componentClassId, _name, \ |
| 1064 | _userComponentClass) \ |
| 1065 | BT_PLUGIN_SOURCE_COMPONENT_CLASS_WITH_ID( \ |
| 1066 | _pluginId, _componentClassId, _name, \ |
| 1067 | bt2::internal::MsgIterClsBridge<_userComponentClass::MessageIterator>::next); \ |
| 1068 | BT_PLUGIN_SOURCE_COMPONENT_CLASS_INITIALIZE_METHOD_WITH_ID( \ |
| 1069 | _pluginId, _componentClassId, bt2::internal::SrcCompClsBridge<_userComponentClass>::init); \ |
| 1070 | BT_PLUGIN_SOURCE_COMPONENT_CLASS_FINALIZE_METHOD_WITH_ID( \ |
| 1071 | _pluginId, _componentClassId, \ |
| 1072 | bt2::internal::SrcCompClsBridge<_userComponentClass>::finalize); \ |
| 1073 | BT_PLUGIN_SOURCE_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_WITH_ID( \ |
| 1074 | _pluginId, _componentClassId, \ |
| 1075 | bt2::internal::SrcCompClsBridge<_userComponentClass>::getSupportedMipVersions); \ |
| 1076 | BT_PLUGIN_SOURCE_COMPONENT_CLASS_OUTPUT_PORT_CONNECTED_METHOD_WITH_ID( \ |
| 1077 | _pluginId, _componentClassId, \ |
| 1078 | bt2::internal::SrcCompClsBridge<_userComponentClass>::outputPortConnected); \ |
| 1079 | BT_PLUGIN_SOURCE_COMPONENT_CLASS_QUERY_METHOD_WITH_ID( \ |
| 1080 | _pluginId, _componentClassId, \ |
| 1081 | bt2::internal::SrcCompClsBridge<_userComponentClass>::query); \ |
| 1082 | BT_PLUGIN_SOURCE_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_WITH_ID( \ |
| 1083 | _pluginId, _componentClassId, \ |
| 1084 | bt2::internal::MsgIterClsBridge<_userComponentClass::MessageIterator>::init); \ |
| 1085 | BT_PLUGIN_SOURCE_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_FINALIZE_METHOD_WITH_ID( \ |
| 1086 | _pluginId, _componentClassId, \ |
| 1087 | bt2::internal::MsgIterClsBridge<_userComponentClass::MessageIterator>::finalize); \ |
| 1088 | BT_PLUGIN_SOURCE_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHODS_WITH_ID( \ |
| 1089 | _pluginId, _componentClassId, \ |
| 1090 | bt2::internal::MsgIterClsBridge<_userComponentClass::MessageIterator>::seekBeginning, \ |
| 1091 | bt2::internal::MsgIterClsBridge<_userComponentClass::MessageIterator>::canSeekBeginning); \ |
| 1092 | BT_PLUGIN_SOURCE_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHODS_WITH_ID( \ |
| 1093 | _pluginId, _componentClassId, \ |
| 1094 | bt2::internal::MsgIterClsBridge<_userComponentClass::MessageIterator>::seekNsFromOrigin, \ |
| 1095 | bt2::internal::MsgIterClsBridge< \ |
| 1096 | _userComponentClass::MessageIterator>::canSeekNsFromOrigin); |
| 1097 | |
| 1098 | #define BT_CPP_PLUGIN_FILTER_COMPONENT_CLASS_WITH_ID(_pluginId, _componentClassId, _name, \ |
| 1099 | _userComponentClass) \ |
| 1100 | BT_PLUGIN_FILTER_COMPONENT_CLASS_WITH_ID( \ |
| 1101 | _pluginId, _componentClassId, _name, \ |
| 1102 | bt2::internal::MsgIterClsBridge<_userComponentClass::MessageIterator>::next); \ |
| 1103 | BT_PLUGIN_FILTER_COMPONENT_CLASS_INITIALIZE_METHOD_WITH_ID( \ |
| 1104 | _pluginId, _componentClassId, bt2::internal::FltCompClsBridge<_userComponentClass>::init); \ |
| 1105 | BT_PLUGIN_FILTER_COMPONENT_CLASS_FINALIZE_METHOD_WITH_ID( \ |
| 1106 | _pluginId, _componentClassId, \ |
| 1107 | bt2::internal::FltCompClsBridge<_userComponentClass>::finalize); \ |
| 1108 | BT_PLUGIN_FILTER_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_WITH_ID( \ |
| 1109 | _pluginId, _componentClassId, \ |
| 1110 | bt2::internal::FltCompClsBridge<_userComponentClass>::getSupportedMipVersions); \ |
| 1111 | BT_PLUGIN_FILTER_COMPONENT_CLASS_INPUT_PORT_CONNECTED_METHOD_WITH_ID( \ |
| 1112 | _pluginId, _componentClassId, \ |
| 1113 | bt2::internal::FltCompClsBridge<_userComponentClass>::inputPortConnected); \ |
| 1114 | BT_PLUGIN_FILTER_COMPONENT_CLASS_OUTPUT_PORT_CONNECTED_METHOD_WITH_ID( \ |
| 1115 | _pluginId, _componentClassId, \ |
| 1116 | bt2::internal::FltCompClsBridge<_userComponentClass>::outputPortConnected); \ |
| 1117 | BT_PLUGIN_FILTER_COMPONENT_CLASS_QUERY_METHOD_WITH_ID( \ |
| 1118 | _pluginId, _componentClassId, \ |
| 1119 | bt2::internal::FltCompClsBridge<_userComponentClass>::query); \ |
| 1120 | BT_PLUGIN_FILTER_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_WITH_ID( \ |
| 1121 | _pluginId, _componentClassId, \ |
| 1122 | bt2::internal::MsgIterClsBridge<_userComponentClass::MessageIterator>::init); \ |
| 1123 | BT_PLUGIN_FILTER_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_FINALIZE_METHOD_WITH_ID( \ |
| 1124 | _pluginId, _componentClassId, \ |
| 1125 | bt2::internal::MsgIterClsBridge<_userComponentClass::MessageIterator>::finalize); \ |
| 1126 | BT_PLUGIN_FILTER_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHODS_WITH_ID( \ |
| 1127 | _pluginId, _componentClassId, \ |
| 1128 | bt2::internal::MsgIterClsBridge<_userComponentClass::MessageIterator>::seekBeginning, \ |
| 1129 | bt2::internal::MsgIterClsBridge<_userComponentClass::MessageIterator>::canSeekBeginning); \ |
| 1130 | BT_PLUGIN_FILTER_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHODS_WITH_ID( \ |
| 1131 | _pluginId, _componentClassId, \ |
| 1132 | bt2::internal::MsgIterClsBridge<_userComponentClass::MessageIterator>::seekNsFromOrigin, \ |
| 1133 | bt2::internal::MsgIterClsBridge< \ |
| 1134 | _userComponentClass::MessageIterator>::canSeekNsFromOrigin); |
| 1135 | |
| 1136 | #define BT_CPP_PLUGIN_SINK_COMPONENT_CLASS_WITH_ID(_pluginId, _componentClassId, _name, \ |
| 1137 | _userComponentClass) \ |
| 1138 | BT_PLUGIN_SINK_COMPONENT_CLASS_WITH_ID( \ |
| 1139 | _pluginId, _componentClassId, _name, \ |
| 1140 | bt2::internal::SinkCompClsBridge<_userComponentClass>::consume); \ |
| 1141 | BT_PLUGIN_SINK_COMPONENT_CLASS_INITIALIZE_METHOD_WITH_ID( \ |
| 1142 | _pluginId, _componentClassId, \ |
| 1143 | bt2::internal::SinkCompClsBridge<_userComponentClass>::init); \ |
| 1144 | BT_PLUGIN_SINK_COMPONENT_CLASS_FINALIZE_METHOD_WITH_ID( \ |
| 1145 | _pluginId, _componentClassId, \ |
| 1146 | bt2::internal::SinkCompClsBridge<_userComponentClass>::finalize); \ |
| 1147 | BT_PLUGIN_SINK_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_WITH_ID( \ |
| 1148 | _pluginId, _componentClassId, \ |
| 1149 | bt2::internal::SinkCompClsBridge<_userComponentClass>::getSupportedMipVersions); \ |
| 1150 | BT_PLUGIN_SINK_COMPONENT_CLASS_INPUT_PORT_CONNECTED_METHOD_WITH_ID( \ |
| 1151 | _pluginId, _componentClassId, \ |
| 1152 | bt2::internal::SinkCompClsBridge<_userComponentClass>::inputPortConnected); \ |
| 1153 | BT_PLUGIN_SINK_COMPONENT_CLASS_GRAPH_IS_CONFIGURED_METHOD_WITH_ID( \ |
| 1154 | _pluginId, _componentClassId, \ |
| 1155 | bt2::internal::SinkCompClsBridge<_userComponentClass>::graphIsConfigured); \ |
| 1156 | BT_PLUGIN_SINK_COMPONENT_CLASS_QUERY_METHOD_WITH_ID( \ |
| 1157 | _pluginId, _componentClassId, \ |
| 1158 | bt2::internal::SinkCompClsBridge<_userComponentClass>::query); |
| 1159 | |
| 1160 | #define BT_CPP_PLUGIN_SOURCE_COMPONENT_CLASS(_name, _userComponentClass) \ |
| 1161 | BT_CPP_PLUGIN_SOURCE_COMPONENT_CLASS_WITH_ID(auto, _name, #_name, _userComponentClass) |
| 1162 | |
| 1163 | #define BT_CPP_PLUGIN_FILTER_COMPONENT_CLASS(_name, _userComponentClass) \ |
| 1164 | BT_CPP_PLUGIN_FILTER_COMPONENT_CLASS_WITH_ID(auto, _name, #_name, _userComponentClass) |
| 1165 | |
| 1166 | #define BT_CPP_PLUGIN_SINK_COMPONENT_CLASS(_name, _userComponentClass) \ |
| 1167 | BT_CPP_PLUGIN_SINK_COMPONENT_CLASS_WITH_ID(auto, _name, #_name, _userComponentClass) |
| 1168 | |
| 1169 | #endif /* BABELTRACE_CPP_COMMON_BT2_PLUGIN_DEV_HPP */ |