Skip to content

Commit

Permalink
Made 'message_arrived_event' just a message pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
fpagliughi committed Jul 7, 2024
1 parent 604716c commit 0b5c312
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 20 deletions.
4 changes: 2 additions & 2 deletions examples/async_consume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ int main(int argc, char* argv[])
while (true) {
auto evt = cli.consume_event();

if (const auto* p = std::get_if<mqtt::message_arrived_event>(&evt)) {
auto& msg = p->msg;
if (const auto* p = std::get_if<mqtt::const_message_ptr>(&evt)) {
auto& msg = *p;
if (msg)
cout << msg->get_topic() << ": " << msg->to_string() << endl;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/async_consume_v5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ int main(int argc, char* argv[])

auto evt = cli.consume_event();

if (const auto* p = std::get_if<mqtt::message_arrived_event>(&evt)) {
auto& msg = p->msg;
if (const auto* p = std::get_if<mqtt::const_message_ptr>(&evt)) {
auto& msg = *p;
if (msg)
cout << msg->get_topic() << ": " << msg->to_string() << endl;
}
Expand Down
8 changes: 4 additions & 4 deletions include/mqtt/async_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<message_arrived_event>(&evt))
*msg = std::move(pval->msg);
if (const auto* pval = std::get_if<const_message_ptr>(&evt))
*msg = std::move(*pval);
else
*msg = const_message_ptr{};
return true;
Expand Down Expand Up @@ -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<message_arrived_event>(&evt))
*msg = std::move(pval->msg);
if (const auto* pval = std::get_if<const_message_ptr>(&evt))
*msg = std::move(*pval);
else
*msg = const_message_ptr{};
return true;
Expand Down
8 changes: 2 additions & 6 deletions include/mqtt/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/async_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<message_arrived_event>(&evt))
return pval->msg;
if (const auto* pval = std::get_if<const_message_ptr>(&evt))
return *pval;
return const_message_ptr{};
}

Expand All @@ -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<message_arrived_event>(&evt))
*msg = std::move(pval->msg);
if (const auto* pval = std::get_if<const_message_ptr>(&evt))
*msg = std::move(*pval);
else
*msg = const_message_ptr{};
return true;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/mock_async_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
};

Expand Down

0 comments on commit 0b5c312

Please sign in to comment.