From: Simon Marchi Date: Thu, 15 Feb 2024 21:17:55 +0000 (-0500) Subject: cpp-common/bt2: move component class bridges to `internal/comp-cls-bridge.hpp` X-Git-Url: https://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=223096991003ecb21c555060226717932f6e2180 cpp-common/bt2: move component class bridges to `internal/comp-cls-bridge.hpp` The bridges are going to be used when creating component classes at runtime, not only in the context of plugins. Change-Id: If6d04531ee660692d1a6eb84950a584530a23566 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/11813 Tested-by: jenkins Reviewed-by: Philippe Proulx --- diff --git a/src/Makefile.am b/src/Makefile.am index 4cdbdd1b..0d990faa 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -124,6 +124,7 @@ cpp_common_libcpp_common_la_SOURCES = \ cpp-common/bt2/field.hpp \ cpp-common/bt2/integer-range-set.hpp \ cpp-common/bt2/integer-range.hpp \ + cpp-common/bt2/internal/comp-cls-bridge.hpp \ cpp-common/bt2/internal/utils.hpp \ cpp-common/bt2/logging.hpp \ cpp-common/bt2/message-array.hpp \ diff --git a/src/cpp-common/bt2/internal/comp-cls-bridge.hpp b/src/cpp-common/bt2/internal/comp-cls-bridge.hpp new file mode 100644 index 00000000..5d64c4b4 --- /dev/null +++ b/src/cpp-common/bt2/internal/comp-cls-bridge.hpp @@ -0,0 +1,450 @@ +/* + * Copyright (c) 2024 EfficiOS, Inc. + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2_INTERNAL_COMP_CLS_BRIDGE_HPP +#define BABELTRACE_CPP_COMMON_BT2_INTERNAL_COMP_CLS_BRIDGE_HPP + +#include + +#include "cpp-common/bt2c/c-string-view.hpp" +#include "logging/log-api.h" + +#include "../wrap.hpp" + +namespace bt2 { +namespace internal { + +constexpr bt2c::CStringView unhandledExcLogStr() noexcept +{ + return "Unhandled exception."; +} + +constexpr bt2c::CStringView unhandledExcLogTag() noexcept +{ + return "PLUGIN-DEV-HPP"; +} + +/* + * Base class of any component class bridge. + * + * `UserCompClsT` is the actual C++ user component class and `LibTypesT` + * is a structure offering the following specific library types: + * + * `SelfCompCls`: + * Self component class. + * + * `SelfComp`: + * Self component. + * + * `SelfCompCfg`: + * Self component configuration. + */ +template +class CompClsBridge +{ +private: + using _LibSelfCompPtr = typename LibTypesT::SelfComp *; + +public: + static UserCompClsT& userCompFromLibSelfCompPtr(const _LibSelfCompPtr libSelfCompPtr) noexcept + { + return wrap(libSelfCompPtr).template data(); + } + + static bt_component_class_initialize_method_status init(const _LibSelfCompPtr libSelfCompPtr, + typename LibTypesT::SelfCompCfg *, + const bt_value * const libParamsPtr, + void * const initData) noexcept + { + const auto selfComp = wrap(libSelfCompPtr); + + try { + const auto comp = + new UserCompClsT {selfComp, wrap(libParamsPtr).asMap(), + static_cast(initData)}; + + selfComp.data(*comp); + } catch (const std::bad_alloc&) { + return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; + } catch (const Error&) { + return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, static_cast(selfComp.loggingLevel()), + unhandledExcLogTag(), unhandledExcLogStr()); + return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; + } + + return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; + } + + static void finalize(const _LibSelfCompPtr libSelfCompPtr) noexcept + { + delete &userCompFromLibSelfCompPtr(libSelfCompPtr); + } + + static bt_component_class_get_supported_mip_versions_method_status + getSupportedMipVersions(typename LibTypesT::SelfCompCls * const libSelfCompClsPtr, + const bt_value * const libParamsPtr, void *, + const bt_logging_level logLevel, + bt_integer_range_set_unsigned * const libSupportedVersionsPtr) noexcept + { + try { + UserCompClsT::getSupportedMipVersions(wrap(libSelfCompClsPtr), wrap(libParamsPtr), + static_cast(logLevel), + wrap(libSupportedVersionsPtr)); + return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_OK; + } catch (const std::bad_alloc&) { + return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_MEMORY_ERROR; + } catch (const Error&) { + return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, static_cast(logLevel), unhandledExcLogTag(), + unhandledExcLogStr()); + return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR; + } + } + + static bt_component_class_query_method_status + query(typename LibTypesT::SelfCompCls * const libSelfCompClsPtr, + bt_private_query_executor * const libPrivQueryExecPtr, const char * const object, + const bt_value * const libParamsPtr, void * const data, + const bt_value ** const libResultPtr) noexcept + { + const auto privQueryExec = wrap(libPrivQueryExecPtr); + + try { + auto result = UserCompClsT::query( + wrap(libSelfCompClsPtr), privQueryExec, object, wrap(libParamsPtr), + static_cast(data)); + + *libResultPtr = result.release().libObjPtr(); + return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; + } catch (const TryAgain&) { + return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; + } catch (const UnknownObject&) { + return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT; + } catch (const std::bad_alloc&) { + return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; + } catch (const Error&) { + return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, static_cast(privQueryExec.loggingLevel()), + unhandledExcLogTag(), unhandledExcLogStr()); + return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; + } + } +}; + +template +struct CompClsBridgeWithInputPorts +{ + static bt_component_class_port_connected_method_status + inputPortConnected(typename LibTypesT::SelfComp * const libSelfCompPtr, + bt_self_component_port_input * const libSelfCompPortPtr, + const bt_port_output * const libOtherPortPtr) noexcept + { + try { + SpecCompClsBridgeT::userCompFromLibSelfCompPtr(libSelfCompPtr) + .inputPortConnected(wrap(libSelfCompPortPtr), wrap(libOtherPortPtr)); + } catch (const std::bad_alloc&) { + return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR; + } catch (const Error&) { + return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, + static_cast(wrap(libSelfCompPtr).loggingLevel()), + unhandledExcLogTag(), unhandledExcLogStr()); + return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; + } + + return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK; + } +}; + +template +struct CompClsBridgeWithOutputPorts +{ + static bt_component_class_port_connected_method_status + outputPortConnected(typename LibTypesT::SelfComp * const libSelfCompPtr, + bt_self_component_port_output * const libSelfCompPortPtr, + const bt_port_input * const libOtherPortPtr) noexcept + { + try { + SpecCompClsBridgeT::userCompFromLibSelfCompPtr(libSelfCompPtr) + .outputPortConnected(wrap(libSelfCompPortPtr), wrap(libOtherPortPtr)); + } catch (const std::bad_alloc&) { + return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR; + } catch (const Error&) { + return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, + static_cast(wrap(libSelfCompPtr).loggingLevel()), + unhandledExcLogTag(), unhandledExcLogStr()); + return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; + } + + return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK; + } +}; + +struct SrcCompClsLibTypes final +{ + using SelfCompCls = bt_self_component_class_source; + using SelfComp = bt_self_component_source; + using SelfCompCfg = bt_self_component_source_configuration; +}; + +template +class SrcCompClsBridge final : + public CompClsBridge, + public CompClsBridgeWithOutputPorts, SrcCompClsLibTypes> +{ +}; + +struct FltCompClsLibTypes final +{ + using SelfCompCls = bt_self_component_class_filter; + using SelfComp = bt_self_component_filter; + using SelfCompCfg = bt_self_component_filter_configuration; +}; + +template +class FltCompClsBridge final : + public CompClsBridge, + public CompClsBridgeWithInputPorts, FltCompClsLibTypes>, + public CompClsBridgeWithOutputPorts, FltCompClsLibTypes> +{ +}; + +struct SinkCompClsLibTypes final +{ + using SelfCompCls = bt_self_component_class_sink; + using SelfComp = bt_self_component_sink; + using SelfCompCfg = bt_self_component_sink_configuration; +}; + +template +class SinkCompClsBridge final : + public CompClsBridge, + public CompClsBridgeWithInputPorts, SinkCompClsLibTypes> +{ +private: + using CompClsBridge::userCompFromLibSelfCompPtr; + +public: + static bt_component_class_sink_consume_method_status + consume(bt_self_component_sink * const libSelfCompPtr) noexcept + { + try { + if (userCompFromLibSelfCompPtr(libSelfCompPtr).consume()) { + return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; + } else { + return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; + } + } catch (const TryAgain&) { + return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN; + } catch (const std::bad_alloc&) { + return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR; + } catch (const Error&) { + return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, + static_cast(wrap(libSelfCompPtr).loggingLevel()), + unhandledExcLogTag(), unhandledExcLogStr()); + return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR; + } + } + + static bt_component_class_sink_graph_is_configured_method_status + graphIsConfigured(bt_self_component_sink * const libSelfCompPtr) noexcept + { + try { + userCompFromLibSelfCompPtr(libSelfCompPtr).graphIsConfigured(); + return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; + } catch (const std::bad_alloc&) { + return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_MEMORY_ERROR; + } catch (const Error&) { + return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, + static_cast(wrap(libSelfCompPtr).loggingLevel()), + unhandledExcLogTag(), unhandledExcLogStr()); + return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR; + } + } +}; + +template +class MsgIterClsBridge final +{ +public: + static UserMsgIterT& + userMsgIterFromLibSelfMsgIterPtr(bt_self_message_iterator * const libSelfMsgIterPtr) noexcept + { + return bt2::wrap(libSelfMsgIterPtr).data(); + } + + static bt_message_iterator_class_initialize_method_status + init(bt_self_message_iterator * const libSelfMsgIterPtr, + bt_self_message_iterator_configuration * const libSelfMsgIterConfigPtr, + bt_self_component_port_output * const libSelfCompPortPtr) noexcept + { + const auto selfMsgIter = bt2::wrap(libSelfMsgIterPtr); + + try { + const auto msgIter = new UserMsgIterT {selfMsgIter, bt2::wrap(libSelfMsgIterConfigPtr), + bt2::wrap(libSelfCompPortPtr)}; + + selfMsgIter.data(*msgIter); + } catch (const std::bad_alloc&) { + return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; + } catch (const bt2::Error&) { + return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL( + BT_LOG_WARNING, + static_cast(wrap(libSelfMsgIterPtr).component().loggingLevel()), + unhandledExcLogTag(), unhandledExcLogStr()); + return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; + } + + return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; + } + + static void finalize(bt_self_message_iterator * const libSelfMsgIterPtr) noexcept + { + delete &userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr); + } + + static bt_message_iterator_class_next_method_status + next(bt_self_message_iterator * const libSelfMsgIterPtr, bt_message_array_const libMsgsPtr, + const uint64_t capacity, uint64_t * const count) noexcept + { + try { + auto msgArray = bt2::ConstMessageArray::wrapEmpty(libMsgsPtr, capacity); + auto& msgIter = userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr); + + msgIter.next(msgArray); + *count = msgArray.release(); + + if (G_LIKELY(*count > 0)) { + return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; + } else { + return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; + } + } catch (const bt2::TryAgain&) { + return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; + } catch (const std::bad_alloc&) { + return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; + } catch (const bt2::Error&) { + return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL( + BT_LOG_WARNING, + static_cast(wrap(libSelfMsgIterPtr).component().loggingLevel()), + unhandledExcLogTag(), unhandledExcLogStr()); + return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; + } + } + + static bt_message_iterator_class_can_seek_beginning_method_status + canSeekBeginning(bt_self_message_iterator * const libSelfMsgIterPtr, + bt_bool * const canSeek) noexcept + { + try { + *canSeek = static_cast( + userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr).canSeekBeginning()); + } catch (const bt2::TryAgain&) { + return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_AGAIN; + } catch (const std::bad_alloc&) { + return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_MEMORY_ERROR; + } catch (const bt2::Error&) { + return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL( + BT_LOG_WARNING, + static_cast(wrap(libSelfMsgIterPtr).component().loggingLevel()), + unhandledExcLogTag(), unhandledExcLogStr()); + return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_ERROR; + } + + return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK; + } + + static bt_message_iterator_class_seek_beginning_method_status + seekBeginning(bt_self_message_iterator * const libSelfMsgIterPtr) noexcept + { + try { + userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr).seekBeginning(); + } catch (const bt2::TryAgain&) { + return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_AGAIN; + } catch (const std::bad_alloc&) { + return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_MEMORY_ERROR; + } catch (const bt2::Error&) { + return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL( + BT_LOG_WARNING, + static_cast(wrap(libSelfMsgIterPtr).component().loggingLevel()), + unhandledExcLogTag(), unhandledExcLogStr()); + return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_ERROR; + } + + return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK; + } + + static bt_message_iterator_class_can_seek_ns_from_origin_method_status + canSeekNsFromOrigin(bt_self_message_iterator * const libSelfMsgIterPtr, + const std::int64_t nsFromOrigin, bt_bool * const canSeek) noexcept + { + try { + *canSeek = static_cast(userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr) + .canSeekNsFromOrigin(nsFromOrigin)); + } catch (const bt2::TryAgain&) { + return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_AGAIN; + } catch (const std::bad_alloc&) { + return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_MEMORY_ERROR; + } catch (const bt2::Error&) { + return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL( + BT_LOG_WARNING, + static_cast(wrap(libSelfMsgIterPtr).component().loggingLevel()), + unhandledExcLogTag(), unhandledExcLogStr()); + return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR; + } + + return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; + } + + static bt_message_iterator_class_seek_ns_from_origin_method_status + seekNsFromOrigin(bt_self_message_iterator * const libSelfMsgIterPtr, + const std::int64_t nsFromOrigin) noexcept + { + try { + userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr).seekNsFromOrigin(nsFromOrigin); + } catch (const bt2::TryAgain&) { + return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_AGAIN; + } catch (const std::bad_alloc&) { + return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_MEMORY_ERROR; + } catch (const bt2::Error&) { + return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR; + } catch (...) { + BT_LOG_WRITE_CUR_LVL( + BT_LOG_WARNING, + static_cast(wrap(libSelfMsgIterPtr).component().loggingLevel()), + unhandledExcLogTag(), unhandledExcLogStr()); + return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR; + } + + return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; + } +}; + +} /* namespace internal */ +} /* namespace bt2 */ + +#endif /* BABELTRACE_CPP_COMMON_BT2_INTERNAL_COMP_CLS_BRIDGE_HPP */ diff --git a/src/cpp-common/bt2/plugin-dev.hpp b/src/cpp-common/bt2/plugin-dev.hpp index 916ff1ae..526266d1 100644 --- a/src/cpp-common/bt2/plugin-dev.hpp +++ b/src/cpp-common/bt2/plugin-dev.hpp @@ -19,439 +19,9 @@ #include "cpp-common/vendor/fmt/core.h" #include "exc.hpp" -#include "wrap.hpp" +#include "internal/comp-cls-bridge.hpp" /* IWYU pragma: keep */ namespace bt2 { -namespace internal { - -constexpr bt2c::CStringView unhandledExcLogStr() noexcept -{ - return "Unhandled exception."; -} - -constexpr bt2c::CStringView unhandledExcLogTag() noexcept -{ - return "PLUGIN-DEV-HPP"; -} - -/* - * Base class of any component class bridge. - * - * `UserCompClsT` is the actual C++ user component class and `LibTypesT` - * is a structure offering the following specific library types: - * - * `SelfCompCls`: - * Self component class. - * - * `SelfComp`: - * Self component. - * - * `SelfCompCfg`: - * Self component configuration. - */ -template -class CompClsBridge -{ -private: - using _LibSelfCompPtr = typename LibTypesT::SelfComp *; - -public: - static UserCompClsT& userCompFromLibSelfCompPtr(const _LibSelfCompPtr libSelfCompPtr) noexcept - { - return wrap(libSelfCompPtr).template data(); - } - - static bt_component_class_initialize_method_status init(const _LibSelfCompPtr libSelfCompPtr, - typename LibTypesT::SelfCompCfg *, - const bt_value * const libParamsPtr, - void * const initData) noexcept - { - const auto selfComp = wrap(libSelfCompPtr); - - try { - const auto comp = - new UserCompClsT {selfComp, wrap(libParamsPtr).asMap(), - static_cast(initData)}; - - selfComp.data(*comp); - } catch (const std::bad_alloc&) { - return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; - } catch (const Error&) { - return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, static_cast(selfComp.loggingLevel()), - unhandledExcLogTag(), unhandledExcLogStr()); - return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; - } - - return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; - } - - static void finalize(const _LibSelfCompPtr libSelfCompPtr) noexcept - { - delete &userCompFromLibSelfCompPtr(libSelfCompPtr); - } - - static bt_component_class_get_supported_mip_versions_method_status - getSupportedMipVersions(typename LibTypesT::SelfCompCls * const libSelfCompClsPtr, - const bt_value * const libParamsPtr, void *, - const bt_logging_level logLevel, - bt_integer_range_set_unsigned * const libSupportedVersionsPtr) noexcept - { - try { - UserCompClsT::getSupportedMipVersions(wrap(libSelfCompClsPtr), wrap(libParamsPtr), - static_cast(logLevel), - wrap(libSupportedVersionsPtr)); - return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_OK; - } catch (const std::bad_alloc&) { - return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_MEMORY_ERROR; - } catch (const Error&) { - return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, static_cast(logLevel), unhandledExcLogTag(), - unhandledExcLogStr()); - return BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR; - } - } - - static bt_component_class_query_method_status - query(typename LibTypesT::SelfCompCls * const libSelfCompClsPtr, - bt_private_query_executor * const libPrivQueryExecPtr, const char * const object, - const bt_value * const libParamsPtr, void * const data, - const bt_value ** const libResultPtr) noexcept - { - const auto privQueryExec = wrap(libPrivQueryExecPtr); - - try { - auto result = UserCompClsT::query( - wrap(libSelfCompClsPtr), privQueryExec, object, wrap(libParamsPtr), - static_cast(data)); - - *libResultPtr = result.release().libObjPtr(); - return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; - } catch (const TryAgain&) { - return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; - } catch (const UnknownObject&) { - return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT; - } catch (const std::bad_alloc&) { - return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; - } catch (const Error&) { - return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, static_cast(privQueryExec.loggingLevel()), - unhandledExcLogTag(), unhandledExcLogStr()); - return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; - } - } -}; - -template -struct CompClsBridgeWithInputPorts -{ - static bt_component_class_port_connected_method_status - inputPortConnected(typename LibTypesT::SelfComp * const libSelfCompPtr, - bt_self_component_port_input * const libSelfCompPortPtr, - const bt_port_output * const libOtherPortPtr) noexcept - { - try { - SpecCompClsBridgeT::userCompFromLibSelfCompPtr(libSelfCompPtr) - .inputPortConnected(wrap(libSelfCompPortPtr), wrap(libOtherPortPtr)); - } catch (const std::bad_alloc&) { - return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR; - } catch (const Error&) { - return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, - static_cast(wrap(libSelfCompPtr).loggingLevel()), - unhandledExcLogTag(), unhandledExcLogStr()); - return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; - } - - return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK; - } -}; - -template -struct CompClsBridgeWithOutputPorts -{ - static bt_component_class_port_connected_method_status - outputPortConnected(typename LibTypesT::SelfComp * const libSelfCompPtr, - bt_self_component_port_output * const libSelfCompPortPtr, - const bt_port_input * const libOtherPortPtr) noexcept - { - try { - SpecCompClsBridgeT::userCompFromLibSelfCompPtr(libSelfCompPtr) - .outputPortConnected(wrap(libSelfCompPortPtr), wrap(libOtherPortPtr)); - } catch (const std::bad_alloc&) { - return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR; - } catch (const Error&) { - return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, - static_cast(wrap(libSelfCompPtr).loggingLevel()), - unhandledExcLogTag(), unhandledExcLogStr()); - return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; - } - - return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK; - } -}; - -struct SrcCompClsLibTypes final -{ - using SelfCompCls = bt_self_component_class_source; - using SelfComp = bt_self_component_source; - using SelfCompCfg = bt_self_component_source_configuration; -}; - -template -class SrcCompClsBridge final : - public CompClsBridge, - public CompClsBridgeWithOutputPorts, SrcCompClsLibTypes> -{ -}; - -struct FltCompClsLibTypes final -{ - using SelfCompCls = bt_self_component_class_filter; - using SelfComp = bt_self_component_filter; - using SelfCompCfg = bt_self_component_filter_configuration; -}; - -template -class FltCompClsBridge final : - public CompClsBridge, - public CompClsBridgeWithInputPorts, FltCompClsLibTypes>, - public CompClsBridgeWithOutputPorts, FltCompClsLibTypes> -{ -}; - -struct SinkCompClsLibTypes final -{ - using SelfCompCls = bt_self_component_class_sink; - using SelfComp = bt_self_component_sink; - using SelfCompCfg = bt_self_component_sink_configuration; -}; - -template -class SinkCompClsBridge final : - public CompClsBridge, - public CompClsBridgeWithInputPorts, SinkCompClsLibTypes> -{ -private: - using CompClsBridge::userCompFromLibSelfCompPtr; - -public: - static bt_component_class_sink_consume_method_status - consume(bt_self_component_sink * const libSelfCompPtr) noexcept - { - try { - if (userCompFromLibSelfCompPtr(libSelfCompPtr).consume()) { - return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; - } else { - return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; - } - } catch (const TryAgain&) { - return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN; - } catch (const std::bad_alloc&) { - return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR; - } catch (const Error&) { - return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, - static_cast(wrap(libSelfCompPtr).loggingLevel()), - unhandledExcLogTag(), unhandledExcLogStr()); - return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR; - } - } - - static bt_component_class_sink_graph_is_configured_method_status - graphIsConfigured(bt_self_component_sink * const libSelfCompPtr) noexcept - { - try { - userCompFromLibSelfCompPtr(libSelfCompPtr).graphIsConfigured(); - return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; - } catch (const std::bad_alloc&) { - return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_MEMORY_ERROR; - } catch (const Error&) { - return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, - static_cast(wrap(libSelfCompPtr).loggingLevel()), - unhandledExcLogTag(), unhandledExcLogStr()); - return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR; - } - } -}; - -template -class MsgIterClsBridge final -{ -public: - static UserMsgIterT& - userMsgIterFromLibSelfMsgIterPtr(bt_self_message_iterator * const libSelfMsgIterPtr) noexcept - { - return bt2::wrap(libSelfMsgIterPtr).data(); - } - - static bt_message_iterator_class_initialize_method_status - init(bt_self_message_iterator * const libSelfMsgIterPtr, - bt_self_message_iterator_configuration * const libSelfMsgIterConfigPtr, - bt_self_component_port_output * const libSelfCompPortPtr) noexcept - { - const auto selfMsgIter = bt2::wrap(libSelfMsgIterPtr); - - try { - const auto msgIter = new UserMsgIterT {selfMsgIter, bt2::wrap(libSelfMsgIterConfigPtr), - bt2::wrap(libSelfCompPortPtr)}; - - selfMsgIter.data(*msgIter); - } catch (const std::bad_alloc&) { - return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; - } catch (const bt2::Error&) { - return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL( - BT_LOG_WARNING, - static_cast(wrap(libSelfMsgIterPtr).component().loggingLevel()), - unhandledExcLogTag(), unhandledExcLogStr()); - return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; - } - - return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; - } - - static void finalize(bt_self_message_iterator * const libSelfMsgIterPtr) noexcept - { - delete &userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr); - } - - static bt_message_iterator_class_next_method_status - next(bt_self_message_iterator * const libSelfMsgIterPtr, bt_message_array_const libMsgsPtr, - const uint64_t capacity, uint64_t * const count) noexcept - { - try { - auto msgArray = bt2::ConstMessageArray::wrapEmpty(libMsgsPtr, capacity); - auto& msgIter = userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr); - - msgIter.next(msgArray); - *count = msgArray.release(); - - if (G_LIKELY(*count > 0)) { - return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; - } else { - return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; - } - } catch (const bt2::TryAgain&) { - return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; - } catch (const std::bad_alloc&) { - return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; - } catch (const bt2::Error&) { - return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL( - BT_LOG_WARNING, - static_cast(wrap(libSelfMsgIterPtr).component().loggingLevel()), - unhandledExcLogTag(), unhandledExcLogStr()); - return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; - } - } - - static bt_message_iterator_class_can_seek_beginning_method_status - canSeekBeginning(bt_self_message_iterator * const libSelfMsgIterPtr, - bt_bool * const canSeek) noexcept - { - try { - *canSeek = static_cast( - userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr).canSeekBeginning()); - } catch (const bt2::TryAgain&) { - return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_AGAIN; - } catch (const std::bad_alloc&) { - return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_MEMORY_ERROR; - } catch (const bt2::Error&) { - return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL( - BT_LOG_WARNING, - static_cast(wrap(libSelfMsgIterPtr).component().loggingLevel()), - unhandledExcLogTag(), unhandledExcLogStr()); - return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_ERROR; - } - - return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK; - } - - static bt_message_iterator_class_seek_beginning_method_status - seekBeginning(bt_self_message_iterator * const libSelfMsgIterPtr) noexcept - { - try { - userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr).seekBeginning(); - } catch (const bt2::TryAgain&) { - return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_AGAIN; - } catch (const std::bad_alloc&) { - return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_MEMORY_ERROR; - } catch (const bt2::Error&) { - return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL( - BT_LOG_WARNING, - static_cast(wrap(libSelfMsgIterPtr).component().loggingLevel()), - unhandledExcLogTag(), unhandledExcLogStr()); - return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_ERROR; - } - - return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK; - } - - static bt_message_iterator_class_can_seek_ns_from_origin_method_status - canSeekNsFromOrigin(bt_self_message_iterator * const libSelfMsgIterPtr, - const std::int64_t nsFromOrigin, bt_bool * const canSeek) noexcept - { - try { - *canSeek = static_cast(userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr) - .canSeekNsFromOrigin(nsFromOrigin)); - } catch (const bt2::TryAgain&) { - return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_AGAIN; - } catch (const std::bad_alloc&) { - return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_MEMORY_ERROR; - } catch (const bt2::Error&) { - return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL( - BT_LOG_WARNING, - static_cast(wrap(libSelfMsgIterPtr).component().loggingLevel()), - unhandledExcLogTag(), unhandledExcLogStr()); - return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR; - } - - return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; - } - - static bt_message_iterator_class_seek_ns_from_origin_method_status - seekNsFromOrigin(bt_self_message_iterator * const libSelfMsgIterPtr, - const std::int64_t nsFromOrigin) noexcept - { - try { - userMsgIterFromLibSelfMsgIterPtr(libSelfMsgIterPtr).seekNsFromOrigin(nsFromOrigin); - } catch (const bt2::TryAgain&) { - return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_AGAIN; - } catch (const std::bad_alloc&) { - return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_MEMORY_ERROR; - } catch (const bt2::Error&) { - return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR; - } catch (...) { - BT_LOG_WRITE_CUR_LVL( - BT_LOG_WARNING, - static_cast(wrap(libSelfMsgIterPtr).component().loggingLevel()), - unhandledExcLogTag(), unhandledExcLogStr()); - return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_ERROR; - } - - return BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; - } -}; - -} /* namespace internal */ template class UserMessageIterator; diff --git a/src/plugins/utils/muxer/msg-iter.hpp b/src/plugins/utils/muxer/msg-iter.hpp index 993ed4a4..ff1e8240 100644 --- a/src/plugins/utils/muxer/msg-iter.hpp +++ b/src/plugins/utils/muxer/msg-iter.hpp @@ -12,6 +12,7 @@ #include "cpp-common/bt2/optional-borrowed-object.hpp" #include "cpp-common/bt2/plugin-dev.hpp" +#include "cpp-common/bt2/self-message-iterator-configuration.hpp" #include "cpp-common/bt2c/prio-heap.hpp" #include "cpp-common/bt2c/uuid.hpp" #include "cpp-common/bt2s/optional.hpp"