Loading...
Searching...
No Matches
event.h
Go to the documentation of this file.
/*******************************************************************************
* Copyright (c) 2024 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Frank Pagliughi - initial implementation and documentation
*******************************************************************************/
#ifndef __mqtt_event_h
#define __mqtt_event_h
#include <variant>
#include "mqtt/message.h"
#include "mqtt/properties.h"
#include "mqtt/reason_code.h"
#include "mqtt/types.h"
namespace mqtt {
struct connected_event
{
string cause;
};
struct connection_lost_event
{
string cause;
};
struct disconnected_event
{
properties props;
ReasonCode reasonCode;
};
struct shutdown_event
{
};
/* Event for when a message arrives is just a message pointer */
class event
{
public:
using event_type = std::variant<
const_message_ptr, connected_event, connection_lost_event, disconnected_event,
shutdown_event>;
private:
event_type evt_{};
public:
event() {}
event(event_type evt) : evt_{std::move(evt)} {}
event(message_ptr msg) : evt_{std::move(msg)} {}
event(const_message_ptr msg) : evt_{std::move(msg)} {}
event(connected_event evt) : evt_{std::move(evt)} {}
event(connection_lost_event evt) : evt_{std::move(evt)} {}
event(disconnected_event evt) : evt_{std::move(evt)} {}
event(shutdown_event evt) : evt_{std::move(evt)} {}
event(const event& evt) : evt_{evt.evt_} {}
event(event&& evt) : evt_{std::move(evt.evt_)} {}
event& operator=(event_type evt) {
evt_ = std::move(evt);
return *this;
}
event& operator=(const event& rhs) {
if (&rhs != this)
evt_ = rhs.evt_;
return *this;
}
event& operator=(event&& rhs) {
if (&rhs != this)
evt_ = std::move(rhs.evt_);
return *this;
}
bool is_message() const { return std::holds_alternative<const_message_ptr>(evt_); }
bool is_connected() const { return std::holds_alternative<connected_event>(evt_); }
bool is_connection_lost() const {
return std::holds_alternative<connection_lost_event>(evt_);
}
bool is_disconnected() const { return std::holds_alternative<disconnected_event>(evt_); }
bool is_shutdown() const { return std::holds_alternative<disconnected_event>(evt_); }
bool is_any_disconnect() const {
return std::holds_alternative<connection_lost_event>(evt_) ||
std::holds_alternative<disconnected_event>(evt_) ||
std::holds_alternative<shutdown_event>(evt_);
}
const_message_ptr get_message() { return std::get<const_message_ptr>(evt_); }
disconnected_event get_disconnected() { return std::get<disconnected_event>(evt_); }
constexpr std::add_pointer_t<const_message_ptr> get_message_if() noexcept {
return std::get_if<const_message_ptr>(&evt_);
}
constexpr std::add_pointer_t<disconnected_event> get_disconnected_if() noexcept {
return std::get_if<disconnected_event>(&evt_);
}
};
} // namespace mqtt
#endif // __mqtt_event_h
mqtt::event::is_message
bool is_message() const
Definition event.h:182
mqtt::event::event
event(event &&evt)
Definition event.h:147
mqtt::event::event
event(message_ptr msg)
Definition event.h:112
mqtt::event::get_message
const_message_ptr get_message()
Definition event.h:224
mqtt::event::event_type
std::variant< const_message_ptr, connected_event, connection_lost_event, disconnected_event, shutdown_event > event_type
Definition event.h:90
mqtt::event::operator=
event & operator=(event_type evt)
Definition event.h:153
mqtt::event::event
event(const event &evt)
Definition event.h:142
mqtt::event::event
event(shutdown_event evt)
Definition event.h:137
mqtt::event::is_shutdown
bool is_shutdown() const
Definition event.h:208
mqtt::event::get_message_if
constexpr std::add_pointer_t< const_message_ptr > get_message_if() noexcept
Definition event.h:241
mqtt::event::event
event(disconnected_event evt)
Definition event.h:132
mqtt::event::event
event(event_type evt)
Definition event.h:107
mqtt::event::event
event(const_message_ptr msg)
Definition event.h:117
mqtt::event::operator=
event & operator=(const event &rhs)
Definition event.h:162
mqtt::event::is_disconnected
bool is_disconnected() const
Definition event.h:202
mqtt::event::is_any_disconnect
bool is_any_disconnect() const
Definition event.h:214
mqtt::event::event
event(connection_lost_event evt)
Definition event.h:127
mqtt::event::operator=
event & operator=(event &&rhs)
Definition event.h:172
mqtt::event::get_disconnected_if
constexpr std::add_pointer_t< disconnected_event > get_disconnected_if() noexcept
Definition event.h:253
mqtt::event::is_connected
bool is_connected() const
Definition event.h:188
mqtt::event::is_connection_lost
bool is_connection_lost() const
Definition event.h:194
mqtt::event::event
event(connected_event evt)
Definition event.h:122
mqtt::event::event
event()
Definition event.h:102
mqtt::event::get_disconnected
disconnected_event get_disconnected()
Definition event.h:234
mqtt::properties
Definition properties.h:293
message.h
mqtt
Definition async_client.h:60
mqtt::ReasonCode
ReasonCode
Definition reason_code.h:39
mqtt::const_message_ptr
message::const_ptr_t const_message_ptr
Definition message.h:372
mqtt::message_ptr
message::ptr_t message_ptr
Definition message.h:369
properties.h
reason_code.h
mqtt::connected_event
Definition event.h:40
mqtt::connected_event::cause
string cause
Definition event.h:41
mqtt::connection_lost_event
Definition event.h:46
mqtt::connection_lost_event::cause
string cause
Definition event.h:47
mqtt::disconnected_event
Definition event.h:52
mqtt::disconnected_event::props
properties props
Definition event.h:53
mqtt::disconnected_event::reasonCode
ReasonCode reasonCode
Definition event.h:54
mqtt::shutdown_event
Definition event.h:59
types.h