From ef28d9ce574b89813c96596a1ee5eec8a4707dfc Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Tue, 12 Jul 2022 11:44:55 +0200 Subject: [PATCH] Fixed pass by reference to pass by value --- include/mqttclient.h | 8 ++++---- src/mqttclient.cpp | 11 +++++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/include/mqttclient.h b/include/mqttclient.h index 7d9e666..7eaa795 100644 --- a/include/mqttclient.h +++ b/include/mqttclient.h @@ -46,20 +46,20 @@ namespace mqtt { class Subscription { public: - Subscription( const std::string &topic, int qos, const std::function& call_back ) + Subscription( const std::string &topic, int qos, const std::function call_back ) : m_topic( topic ) , m_qos( qos ) - , m_call_back(const_cast&>(call_back )) + , m_call_back(call_back ) {} std::string getTopic() const { return m_topic; } int getQoS() const { return m_qos; } - std::function& getCallBack() const { return m_call_back; } + std::function getCallBack() { return m_call_back; } private: std::string m_topic; int m_qos; - std::function& m_call_back; + std::function m_call_back; }; // Forward definition diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index d39e536..e1454f4 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -256,7 +256,7 @@ Token MqttClient::subscribe(const std::string& topic, int qos, const std::functi // Store the subscription in the buffer for later processing. { OSDEV_COMPONENTS_LOCKGUARD(m_subscriptionMutex); - m_deferredSubscriptions.push_back( Subscription( topic, qos, cb ) ); + m_deferredSubscriptions.emplace_back( topic, qos, cb ); } return Token(m_clientId, -1); @@ -271,7 +271,8 @@ Token MqttClient::subscribe(const std::string& topic, int qos, const std::functi { for (const auto& c : m_additionalClients) { - if (!c->isOverlapping(topic)) { + if (!c->isOverlapping(topic)) + { client = c.get(); clientFound = true; break; @@ -306,13 +307,15 @@ std::set MqttClient::unsubscribe(const std::string& topic, int qos) std::vector clients{}; { OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex); - if (!m_principalClient || m_principalClient->connectionStatus() == ConnectionStatus::Disconnected) { + if (!m_principalClient || m_principalClient->connectionStatus() == ConnectionStatus::Disconnected) + { LogError("[MqttClient::unsubscribe]", std::string( m_clientId + " - Unable to unsubscribe, not connected" ) ); // Throw (MqttException, "Not connected"); return std::set(); } clients.push_back(m_principalClient.get()); - for (const auto& c : m_additionalClients) { + for (const auto& c : m_additionalClients) + { clients.push_back(c.get()); } } -- libgit2 0.21.4