Commit ef28d9ce574b89813c96596a1ee5eec8a4707dfc

Authored by Peter M. Groen
1 parent 8ae7f1fe

Fixed pass by reference to pass by value

include/mqttclient.h
... ... @@ -46,20 +46,20 @@ namespace mqtt {
46 46 class Subscription
47 47 {
48 48 public:
49   - Subscription( const std::string &topic, int qos, const std::function<void(MqttMessage)>& call_back )
  49 + Subscription( const std::string &topic, int qos, const std::function<void(MqttMessage)> call_back )
50 50 : m_topic( topic )
51 51 , m_qos( qos )
52   - , m_call_back(const_cast<std::function<void(MqttMessage)>&>(call_back ))
  52 + , m_call_back(call_back )
53 53 {}
54 54  
55 55 std::string getTopic() const { return m_topic; }
56 56 int getQoS() const { return m_qos; }
57   - std::function<void(MqttMessage)>& getCallBack() const { return m_call_back; }
  57 + std::function<void(MqttMessage)> getCallBack() { return m_call_back; }
58 58  
59 59 private:
60 60 std::string m_topic;
61 61 int m_qos;
62   - std::function<void(MqttMessage)>& m_call_back;
  62 + std::function<void(MqttMessage)> m_call_back;
63 63 };
64 64  
65 65 // Forward definition
... ...
src/mqttclient.cpp
... ... @@ -256,7 +256,7 @@ Token MqttClient::subscribe(const std::string&amp; topic, int qos, const std::functi
256 256 // Store the subscription in the buffer for later processing.
257 257 {
258 258 OSDEV_COMPONENTS_LOCKGUARD(m_subscriptionMutex);
259   - m_deferredSubscriptions.push_back( Subscription( topic, qos, cb ) );
  259 + m_deferredSubscriptions.emplace_back( topic, qos, cb );
260 260 }
261 261  
262 262 return Token(m_clientId, -1);
... ... @@ -271,7 +271,8 @@ Token MqttClient::subscribe(const std::string&amp; topic, int qos, const std::functi
271 271 {
272 272 for (const auto& c : m_additionalClients)
273 273 {
274   - if (!c->isOverlapping(topic)) {
  274 + if (!c->isOverlapping(topic))
  275 + {
275 276 client = c.get();
276 277 clientFound = true;
277 278 break;
... ... @@ -306,13 +307,15 @@ std::set&lt;Token&gt; MqttClient::unsubscribe(const std::string&amp; topic, int qos)
306 307 std::vector<IMqttClientImpl*> clients{};
307 308 {
308 309 OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex);
309   - if (!m_principalClient || m_principalClient->connectionStatus() == ConnectionStatus::Disconnected) {
  310 + if (!m_principalClient || m_principalClient->connectionStatus() == ConnectionStatus::Disconnected)
  311 + {
310 312 LogError("[MqttClient::unsubscribe]", std::string( m_clientId + " - Unable to unsubscribe, not connected" ) );
311 313 // Throw (MqttException, "Not connected");
312 314 return std::set<Token>();
313 315 }
314 316 clients.push_back(m_principalClient.get());
315   - for (const auto& c : m_additionalClients) {
  317 + for (const auto& c : m_additionalClients)
  318 + {
316 319 clients.push_back(c.get());
317 320 }
318 321 }
... ...