diff --git a/examples/async_consume.cpp b/examples/async_consume.cpp index 1b7d79b8..4a204c6d 100644 --- a/examples/async_consume.cpp +++ b/examples/async_consume.cpp @@ -95,8 +95,8 @@ int main(int argc, char* argv[]) while (true) { auto evt = cli.consume_event(); - if (const auto* p = std::get_if(&evt)) { - auto& msg = p->msg; + if (const auto* p = std::get_if(&evt)) { + auto& msg = *p; if (msg) cout << msg->get_topic() << ": " << msg->to_string() << endl; } diff --git a/examples/async_consume_v5.cpp b/examples/async_consume_v5.cpp index 68e2c7a5..32422554 100644 --- a/examples/async_consume_v5.cpp +++ b/examples/async_consume_v5.cpp @@ -99,8 +99,8 @@ int main(int argc, char* argv[]) auto evt = cli.consume_event(); - if (const auto* p = std::get_if(&evt)) { - auto& msg = p->msg; + if (const auto* p = std::get_if(&evt)) { + auto& msg = *p; if (msg) cout << msg->get_topic() << ": " << msg->to_string() << endl; } diff --git a/include/mqtt/async_client.h b/include/mqtt/async_client.h index c09f0910..d378c643 100644 --- a/include/mqtt/async_client.h +++ b/include/mqtt/async_client.h @@ -785,8 +785,8 @@ class async_client : public virtual iasync_client if (!que_->try_get_for(&evt, relTime)) return false; - if (const auto* pval = std::get_if(&evt)) - *msg = std::move(pval->msg); + if (const auto* pval = std::get_if(&evt)) + *msg = std::move(*pval); else *msg = const_message_ptr{}; return true; @@ -820,8 +820,8 @@ class async_client : public virtual iasync_client if (!que_->try_get_until(&evt, absTime)) return false; - if (const auto* pval = std::get_if(&evt)) - *msg = std::move(pval->msg); + if (const auto* pval = std::get_if(&evt)) + *msg = std::move(*pval); else *msg = const_message_ptr{}; return true; diff --git a/include/mqtt/event.h b/include/mqtt/event.h index e9d992d6..8e61527e 100644 --- a/include/mqtt/event.h +++ b/include/mqtt/event.h @@ -54,15 +54,11 @@ struct disconnected_event ReasonCode reasonCode; }; -/** Event for when a message arrives */ -struct message_arrived_event -{ - const_message_ptr msg; -}; +/* Event for when a message arrives is just a message pointer */ /** The variant type for any possible event. */ using event_type = std::variant< - message_arrived_event, connected_event, connection_lost_event, disconnected_event>; + const_message_ptr, connected_event, connection_lost_event, disconnected_event>; ///////////////////////////////////////////////////////////////////////////// } // namespace mqtt diff --git a/src/async_client.cpp b/src/async_client.cpp index bed084f4..fc4762ae 100644 --- a/src/async_client.cpp +++ b/src/async_client.cpp @@ -207,7 +207,7 @@ int async_client::on_message_arrived( cb->message_arrived(m); if (que) - que->put(message_arrived_event{m}); + que->put(m); } MQTTAsync_freeMessage(&msg); @@ -877,8 +877,8 @@ void async_client::stop_consuming() const_message_ptr async_client::consume_message() { auto evt = que_->get(); - if (const auto* pval = std::get_if(&evt)) - return pval->msg; + if (const auto* pval = std::get_if(&evt)) + return *pval; return const_message_ptr{}; } @@ -888,8 +888,8 @@ bool async_client::try_consume_message(const_message_ptr* msg) if (!que_->try_get(&evt)) return false; - if (const auto* pval = std::get_if(&evt)) - *msg = std::move(pval->msg); + if (const auto* pval = std::get_if(&evt)) + *msg = std::move(*pval); else *msg = const_message_ptr{}; return true; diff --git a/test/unit/mock_async_client.h b/test/unit/mock_async_client.h index 02dbbb9e..105d6016 100644 --- a/test/unit/mock_async_client.h +++ b/test/unit/mock_async_client.h @@ -240,7 +240,7 @@ class mock_async_client : public virtual mqtt::iasync_client bool try_consume_message(const_message_ptr*) override { return false; } - event_type consume_event() override { return message_arrived_event{const_message_ptr{}}; } + event_type consume_event() override { return const_message_ptr{}; } bool try_consume_event(event_type* evt) override { return false; } };