diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index dc85ee5..c900aca 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -47,9 +47,9 @@ function (lib_tools) target_link_libraries(${name} PUBLIC cc::comms Qt::Core ${CC_PLATFORM_SPECIFIC}) target_include_directories(${name} BEFORE PRIVATE - $ + $/src PUBLIC - $ + $ INTERFACE $ ) diff --git a/lib/include/cc_tools_qt/ToolsSocket.h b/lib/include/cc_tools_qt/ToolsSocket.h index 7d7a9e4..9d533d2 100644 --- a/lib/include/cc_tools_qt/ToolsSocket.h +++ b/lib/include/cc_tools_qt/ToolsSocket.h @@ -112,55 +112,14 @@ class CC_TOOLS_API ToolsSocket : public QObject /// @param[in] props Properties map. void applyInterPluginConfig(const QVariantMap& props); - /// @brief Callback to report incoming data. - using DataReceivedCallback = std::function; - - /// @brief Set callback to report incoming data - /// @details The callback must have the same signature as @ref DataReceivedCallback. - template - void setDataReceivedCallback(TFunc&& func) - { - m_dataReceivedCallback = std::forward(func); - } - - /// @brief Callback to report errors - using ErrorReportCallback = std::function; - - /// @brief Set callback to report errors - /// @details The callback must have the same signature as @ref ErrorReportCallback - template - void setErrorReportCallback(TFunc&& func) - { - m_errorReportCallback = std::forward(func); - } - - /// @brief Callback to report socket connection status - using ConnectionStatusReportCallback = std::function ; - - /// @brief Set callback to report socket connection - /// @details The callback must have the same signature as @ref ConnectionStatusReportCallback. - template - void setConnectionStatusReportCallback(TFunc&& func) - { - m_connectionStatusReportCallback = std::forward(func); - } - - /// @brief Callback to report inter-plugin configuration updates - using InterPluginConfigReportCallback = std::function ; - - /// @brief Set callback to report inter-plugin configuration. - /// @details The callback must have the same signature as @ref InterPluginConfigReportCallback. - template - void setInterPluginConfigReportCallback(TFunc&& func) - { - m_interPluginConfigReportCallback = std::forward(func); - } - /// @brief Set debug output level /// @param[in] level Debug level. If @b 0, debug output is disabled void setDebugOutputLevel(unsigned level = 0U); signals: + void sigDataReceivedReport(ToolsDataInfoPtr data); + void sigErrorReport(const QString& msg); + void sigConnectionStatusReport(bool connected); void sigInterPluginConfigReport(const QVariantMap& props) ; protected: @@ -240,11 +199,6 @@ class CC_TOOLS_API ToolsSocket : public QObject private: struct InnerState; std::unique_ptr m_state; - - DataReceivedCallback m_dataReceivedCallback; - ErrorReportCallback m_errorReportCallback; - ConnectionStatusReportCallback m_connectionStatusReportCallback; - InterPluginConfigReportCallback m_interPluginConfigReportCallback; }; /// @brief Pointer to @ref ToolsSocket object. diff --git a/lib/src/ToolsMsgMgrImpl.cpp b/lib/src/ToolsMsgMgrImpl.cpp index 4b25cb2..99c988e 100644 --- a/lib/src/ToolsMsgMgrImpl.cpp +++ b/lib/src/ToolsMsgMgrImpl.cpp @@ -266,42 +266,25 @@ void ToolsMsgMgrImpl::addMsgs(const MessagesList& msgs, bool reportAdded) void ToolsMsgMgrImpl::setSocket(ToolsSocketPtr socket) { - if (m_socket) { - m_socket->setDataReceivedCallback(nullptr); - m_socket->setErrorReportCallback(nullptr); - m_socket->setConnectionStatusReportCallback(nullptr); - } - if (!socket) { m_socket.reset(); return; } - socket->setDataReceivedCallback( - [this](ToolsDataInfoPtr dataPtr) - { - socketDataReceived(std::move(dataPtr)); - }); + connect( + socket.get(), &ToolsSocket::sigDataReceivedReport, + this, &ToolsMsgMgrImpl::socketDataReceived + ); - socket->setErrorReportCallback( - [this](const QString& msg) - { - reportError(msg); - }); + connect( + socket.get(), &ToolsSocket::sigErrorReport, + this, &ToolsMsgMgrImpl::socketErrorReport + ); - socket->setConnectionStatusReportCallback( - [this](bool connected) - { - for (auto& filter : m_filters) { - filter->socketConnectionReport(connected); - } - - if (m_protocol) { - m_protocol->socketConnectionReport(connected); - } - - reportSocketConnectionStatus(connected); - }); + connect( + socket.get(), &ToolsSocket::sigConnectionStatusReport, + this, &ToolsMsgMgrImpl::socketConnectionReport + ); m_socket = std::move(socket); } @@ -384,8 +367,38 @@ void ToolsMsgMgrImpl::addFilter(ToolsFilterPtr filter) m_filters.push_back(std::move(filter)); } +void ToolsMsgMgrImpl::socketErrorReport(const QString& msg) +{ + if (m_socket.get() != sender()) { + return; + } + + reportError(msg); +} + +void ToolsMsgMgrImpl::socketConnectionReport(bool connected) +{ + if (m_socket.get() != sender()) { + return; + } + + for (auto& filter : m_filters) { + filter->socketConnectionReport(connected); + } + + if (m_protocol) { + m_protocol->socketConnectionReport(connected); + } + + reportSocketConnectionStatus(connected); +} + void ToolsMsgMgrImpl::socketDataReceived(ToolsDataInfoPtr dataInfoPtr) { + if (m_socket.get() != sender()) { + return; + } + if ((!m_recvEnabled) || !(m_protocol) || (!dataInfoPtr)) { return; } diff --git a/lib/src/ToolsMsgMgrImpl.h b/lib/src/ToolsMsgMgrImpl.h index 7e2ef56..aef2da0 100644 --- a/lib/src/ToolsMsgMgrImpl.h +++ b/lib/src/ToolsMsgMgrImpl.h @@ -18,15 +18,18 @@ #pragma once -#include - #include "cc_tools_qt/ToolsMsgMgr.h" +#include + +#include + namespace cc_tools_qt { -class ToolsMsgMgrImpl +class ToolsMsgMgrImpl : public QObject { + Q_OBJECT public: using MessagesList = ToolsMsgMgr::MessagesList; using MsgType = ToolsMsgMgr::MsgType; @@ -84,11 +87,15 @@ class ToolsMsgMgrImpl m_socketConnectionStatusReportCallback = std::forward(func); } +private slots: + void socketErrorReport(const QString& msg); + void socketConnectionReport(bool connected); + void socketDataReceived(ToolsDataInfoPtr dataInfoPtr); + private: using MsgNumberType = unsigned long long; using FiltersList = std::vector; - void socketDataReceived(ToolsDataInfoPtr dataInfoPtr); void updateInternalId(ToolsMessage& msg); void reportMsgAdded(ToolsMessagePtr msg); void reportError(const QString& error); diff --git a/lib/src/ToolsSocket.cpp b/lib/src/ToolsSocket.cpp index 735e06e..36d7835 100644 --- a/lib/src/ToolsSocket.cpp +++ b/lib/src/ToolsSocket.cpp @@ -87,9 +87,7 @@ bool ToolsSocket::isRunning() const bool ToolsSocket::socketConnect() { m_state->m_connected = socketConnectImpl(); - if (m_connectionStatusReportCallback) { - m_connectionStatusReportCallback(m_state->m_connected); - } + emit sigConnectionStatusReport(m_state->m_connected); return m_state->m_connected; } @@ -97,9 +95,7 @@ void ToolsSocket::socketDisconnect() { socketDisconnectImpl(); m_state->m_connected = false; - if (m_connectionStatusReportCallback) { - m_connectionStatusReportCallback(false); - } + emit sigConnectionStatusReport(m_state->m_connected); } bool ToolsSocket::isSocketConnected() const @@ -174,7 +170,7 @@ void ToolsSocket::applyInterPluginConfigImpl([[maybe_unused]] const QVariantMap& void ToolsSocket::reportDataReceived(ToolsDataInfoPtr dataPtr) { - if ((!m_state->m_running) || (!m_dataReceivedCallback)) { + if (!m_state->m_running) { return; } @@ -192,31 +188,25 @@ void ToolsSocket::reportDataReceived(ToolsDataInfoPtr dataPtr) std::cout << std::endl; } - m_dataReceivedCallback(std::move(dataPtr)); + emit sigDataReceivedReport(std::move(dataPtr)); } void ToolsSocket::reportError(const QString& msg) { - if (m_state->m_running && m_errorReportCallback) { - m_errorReportCallback(msg); + if (m_state->m_running) { + emit sigErrorReport(msg); } } void ToolsSocket::reportDisconnected() { m_state->m_connected = false; - if (m_state->m_running && m_connectionStatusReportCallback) { - m_connectionStatusReportCallback(false); - } + emit sigConnectionStatusReport(false); } void ToolsSocket::reportInterPluginConfig(const QVariantMap& props) { emit sigInterPluginConfigReport(props); - - if (m_interPluginConfigReportCallback) { - m_interPluginConfigReportCallback(props); - } } unsigned long long ToolsSocket::currTimestamp()