async_mqtt 9.0.1
Loading...
Searching...
No Matches
packet_variant.hpp
1// Copyright Takatoshi Kondo 2022
2//
3// Distributed under the Boost Software License, Version 1.0.
4// (See accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#if !defined(ASYNC_MQTT_PACKET_PACKET_VARIANT_HPP)
8#define ASYNC_MQTT_PACKET_PACKET_VARIANT_HPP
9
10#include <variant>
11
12#include <boost/asio/buffer.hpp>
13
14#include <async_mqtt/packet/control_packet_type.hpp>
15#include <async_mqtt/packet/packet_variant_fwd.hpp>
16#include <async_mqtt/packet/packet_fwd.hpp>
17#include <async_mqtt/packet/v3_1_1_connect.hpp>
18#include <async_mqtt/packet/v3_1_1_connack.hpp>
19#include <async_mqtt/packet/v3_1_1_publish.hpp>
20#include <async_mqtt/packet/v3_1_1_puback.hpp>
21#include <async_mqtt/packet/v3_1_1_pubrec.hpp>
22#include <async_mqtt/packet/v3_1_1_pubrel.hpp>
23#include <async_mqtt/packet/v3_1_1_pubcomp.hpp>
24#include <async_mqtt/packet/v3_1_1_subscribe.hpp>
25#include <async_mqtt/packet/v3_1_1_suback.hpp>
26#include <async_mqtt/packet/v3_1_1_unsubscribe.hpp>
27#include <async_mqtt/packet/v3_1_1_unsuback.hpp>
28#include <async_mqtt/packet/v3_1_1_pingreq.hpp>
29#include <async_mqtt/packet/v3_1_1_pingresp.hpp>
30#include <async_mqtt/packet/v3_1_1_disconnect.hpp>
31#include <async_mqtt/packet/v5_connect.hpp>
32#include <async_mqtt/packet/v5_connack.hpp>
33#include <async_mqtt/packet/v5_publish.hpp>
34#include <async_mqtt/packet/v5_puback.hpp>
35#include <async_mqtt/packet/v5_pubrec.hpp>
36#include <async_mqtt/packet/v5_pubrel.hpp>
37#include <async_mqtt/packet/v5_pubcomp.hpp>
38#include <async_mqtt/packet/v5_subscribe.hpp>
39#include <async_mqtt/packet/v5_suback.hpp>
40#include <async_mqtt/packet/v5_unsubscribe.hpp>
41#include <async_mqtt/packet/v5_unsuback.hpp>
42#include <async_mqtt/packet/v5_pingreq.hpp>
43#include <async_mqtt/packet/v5_pingresp.hpp>
44#include <async_mqtt/packet/v5_disconnect.hpp>
45#include <async_mqtt/packet/v5_auth.hpp>
46
47#include <async_mqtt/util/overload.hpp>
48
49namespace async_mqtt {
50namespace as = boost::asio;
51
52template <std::size_t PacketIdBytes>
53class basic_packet_variant {
54public:
55
60 explicit basic_packet_variant() = default;
61
66 template <
67 typename Packet,
68 std::enable_if_t<
69 !std::is_same_v<
70 std::decay_t<Packet>,
72 >,
73 std::nullptr_t
74 > = nullptr
75 >
76 basic_packet_variant(Packet&& packet);
77
82 template <typename Func>
83 auto visit(Func&& func) const&;
84
89 template <typename Func>
90 auto visit(Func&& func) &;
91
96 template <typename Func>
97 auto visit(Func&& func) &&;
98
103 template <typename T>
104 decltype(auto) get();
105
110 template <typename T>
111 decltype(auto) get() const;
112
117 template <typename T>
118 decltype(auto) get_if();
119
124 template <typename T>
125 decltype(auto) get_if() const;
126
131 std::optional<control_packet_type> type() const;
132
138 std::vector<as::const_buffer> const_buffer_sequence() const;
139
140 operator bool() const;
141
142private:
143
151 return lhs.var_ < rhs.var_;
152 }
153
161 return lhs.var_ == rhs.var_;
162 }
163
164 using variant_t = std::variant<
165 std::monostate,
195 >;
196
197 variant_t var_;
198};
199
207template <std::size_t PacketIdBytes>
208std::ostream& operator<<(std::ostream& o, basic_packet_variant<PacketIdBytes> const& v);
209
210} // namespace async_mqtt
211
212#include <async_mqtt/packet/impl/packet_variant.hpp>
213
214#if !defined(ASYNC_MQTT_SEPARATE_COMPILATION)
215#include <async_mqtt/packet/impl/packet_variant.ipp>
216#endif // !defined(ASYNC_MQTT_SEPARATE_COMPILATION)
217
218#endif // ASYNC_MQTT_PACKET_PACKET_VARIANT_HPP
The varaint type of all packets and system_error.
Definition packet_variant_fwd.hpp:37
basic_packet_variant()=default
constructor the variant value is monostate
decltype(auto) get() const
Get by type. If not match, then throw std::bad_variant_access exception.
auto visit(Func &&func) const &
visit to variant
friend bool operator<(basic_packet_variant< PacketIdBytes > const &lhs, basic_packet_variant< PacketIdBytes > const &rhs)
less than operator
Definition packet_variant.hpp:150
std::optional< control_packet_type > type() const
Get control_packet_type.
decltype(auto) get()
Get by type. If not match, then throw std::bad_variant_access exception.
decltype(auto) get_if() const
Get by type pointer.
decltype(auto) get_if()
Get by type pointer.
std::ostream & operator<<(std::ostream &o, basic_packet_variant< PacketIdBytes > const &v)
stream output operator
friend bool operator==(basic_packet_variant< PacketIdBytes > const &lhs, basic_packet_variant< PacketIdBytes > const &rhs)
equal operator
Definition packet_variant.hpp:160
basic_packet_variant(Packet &&packet)
constructor
auto visit(Func &&func) &
visit to variant
auto visit(Func &&func) &&
visit to variant
std::vector< as::const_buffer > const_buffer_sequence() const
Create const buffer sequence it is for boost asio APIs.
MQTT PUBACK packet (v3.1.1)
Definition v3_1_1_puback.hpp:54
MQTT PUBCOMP packet (v3.1.1)
Definition v3_1_1_pubcomp.hpp:54
MQTT PUBLISH packet (v3.1.1)
Definition v3_1_1_publish.hpp:61
MQTT PUBREC packet (v3.1.1)
Definition v3_1_1_pubrec.hpp:53
MQTT PUBREL packet (v3.1.1)
Definition v3_1_1_pubrel.hpp:55
MQTT SUBACK packet (v3.1.1)
Definition v3_1_1_suback.hpp:53
MQTT SUBSCRIBE packet (v3.1.1)
Definition v3_1_1_subscribe.hpp:52
MQTT UNSUBACK packet (v3.1.1)
Definition v3_1_1_unsuback.hpp:50
MQTT UNSUBSCRIBE packet (v3.1.1)
Definition v3_1_1_unsubscribe.hpp:53
MQTT CONNACK packet (v3.1.1)
Definition v3_1_1_connack.hpp:44
MQTT CONNECT packet (v3.1.1)
Definition v3_1_1_connect.hpp:42
MQTT DISCONNECT packet (v3.1.1)
Definition v3_1_1_disconnect.hpp:57
MQTT PINGREQ packet (v3.1.1)
Definition v3_1_1_pingreq.hpp:48
MQTT PINGRESP packet (v3.1.1)
Definition v3_1_1_pingresp.hpp:48
MQTT AUTH packet (v5)
Definition v5_auth.hpp:47
MQTT PUBACK packet (v5)
Definition v5_puback.hpp:55
MQTT PUBCOMP packet (v5)
Definition v5_pubcomp.hpp:56
MQTT PUBLISH packet (v5)
Definition v5_publish.hpp:63
MQTT PUBREC packet (v5)
Definition v5_pubrec.hpp:55
MQTT PUBREL packet (v5)
Definition v5_pubrel.hpp:56
MQTT SUBACK packet (v5)
Definition v5_suback.hpp:53
MQTT SUBSCRIBE packet (v5)
Definition v5_subscribe.hpp:53
MQTT UNSUBACK packet (v5)
Definition v5_unsuback.hpp:52
MQTT UNSUBSCRIBE packet (v5)
Definition v5_unsubscribe.hpp:54
MQTT CONNACK packet (v5)
Definition v5_connack.hpp:44
MQTT CONNECT packet (v5)
Definition v5_connect.hpp:49
MQTT DISCONNECT packet (v5)
Definition v5_disconnect.hpp:50
MQTT PINGREQ packet (v5)
Definition v5_pingreq.hpp:50
MQTT PINGRESP packet (v5)
Definition v5_pingresp.hpp:57