async_mqtt 4.1.0
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 <async_mqtt/util/variant.hpp>
11#include <async_mqtt/packet/v3_1_1_connect.hpp>
12#include <async_mqtt/packet/v3_1_1_connack.hpp>
13#include <async_mqtt/packet/v3_1_1_publish.hpp>
14#include <async_mqtt/packet/v3_1_1_puback.hpp>
15#include <async_mqtt/packet/v3_1_1_pubrec.hpp>
16#include <async_mqtt/packet/v3_1_1_pubrel.hpp>
17#include <async_mqtt/packet/v3_1_1_pubcomp.hpp>
18#include <async_mqtt/packet/v3_1_1_subscribe.hpp>
19#include <async_mqtt/packet/v3_1_1_suback.hpp>
20#include <async_mqtt/packet/v3_1_1_unsubscribe.hpp>
21#include <async_mqtt/packet/v3_1_1_unsuback.hpp>
22#include <async_mqtt/packet/v3_1_1_pingreq.hpp>
23#include <async_mqtt/packet/v3_1_1_pingresp.hpp>
24#include <async_mqtt/packet/v3_1_1_disconnect.hpp>
25#include <async_mqtt/packet/v5_connect.hpp>
26#include <async_mqtt/packet/v5_connack.hpp>
27#include <async_mqtt/packet/v5_publish.hpp>
28#include <async_mqtt/packet/v5_puback.hpp>
29#include <async_mqtt/packet/v5_pubrec.hpp>
30#include <async_mqtt/packet/v5_pubrel.hpp>
31#include <async_mqtt/packet/v5_pubcomp.hpp>
32#include <async_mqtt/packet/v5_subscribe.hpp>
33#include <async_mqtt/packet/v5_suback.hpp>
34#include <async_mqtt/packet/v5_unsubscribe.hpp>
35#include <async_mqtt/packet/v5_unsuback.hpp>
36#include <async_mqtt/packet/v5_pingreq.hpp>
37#include <async_mqtt/packet/v5_pingresp.hpp>
38#include <async_mqtt/packet/v5_disconnect.hpp>
39#include <async_mqtt/packet/v5_auth.hpp>
40#include <async_mqtt/exception.hpp>
41
42namespace async_mqtt {
43
48template <std::size_t PacketIdBytes>
50public:
51 basic_packet_variant() = default;
52
57 template <
58 typename Packet,
59 std::enable_if_t<
60 !std::is_same_v<
61 std::decay_t<Packet>,
63 >
64 >* = nullptr
65 >
66 basic_packet_variant(Packet&& packet):var_{std::forward<Packet>(packet)}
67 {}
68
73 template <typename Func>
74 auto visit(Func&& func) const& {
75 return
76 async_mqtt::visit(
77 std::forward<Func>(func),
78 var_
79 );
80 }
81
86 template <typename Func>
87 auto visit(Func&& func) & {
88 return
89 async_mqtt::visit(
90 std::forward<Func>(func),
91 var_
92 );
93 }
94
99 template <typename Func>
100 auto visit(Func&& func) && {
101 return
102 async_mqtt::visit(
103 std::forward<Func>(func),
104 force_move(var_)
105 );
106 }
107
112 template <typename T>
113 decltype(auto) get() {
114 return std::get<T>(var_);
115 }
116
121 template <typename T>
122 decltype(auto) get() const {
123 return std::get<T>(var_);
124 }
125
130 template <typename T>
131 decltype(auto) get_if() {
132 return std::get_if<T>(&var_);
133 }
134
139 template <typename T>
140 decltype(auto) get_if() const {
141 return std::get_if<T>(&var_);
142 }
143
149 return visit(
150 overload {
151 [] (auto const& p) -> optional<control_packet_type>{
152 return p.type();
153 },
155 return nullopt;
156 }
157 }
158 );
159 }
160
166 std::vector<as::const_buffer> const_buffer_sequence() const {
167 return visit(
168 overload {
169 [] (auto const& p) {
170 return p.const_buffer_sequence();
171 },
172 [] (system_error const&) {
173 return std::vector<as::const_buffer>{};
174 }
175 }
176 );
177 }
178
179 operator bool() const {
180 return var_.index() != 0;
181 }
182
183private:
184 using variant_t = variant<
185 system_error,
186 v3_1_1::connect_packet,
187 v3_1_1::connack_packet,
188 v3_1_1::basic_publish_packet<PacketIdBytes>,
189 v3_1_1::basic_puback_packet<PacketIdBytes>,
190 v3_1_1::basic_pubrec_packet<PacketIdBytes>,
191 v3_1_1::basic_pubrel_packet<PacketIdBytes>,
192 v3_1_1::basic_pubcomp_packet<PacketIdBytes>,
193 v3_1_1::basic_subscribe_packet<PacketIdBytes>,
194 v3_1_1::basic_suback_packet<PacketIdBytes>,
195 v3_1_1::basic_unsubscribe_packet<PacketIdBytes>,
196 v3_1_1::basic_unsuback_packet<PacketIdBytes>,
197 v3_1_1::pingreq_packet,
198 v3_1_1::pingresp_packet,
199 v3_1_1::disconnect_packet,
200 v5::connect_packet,
201 v5::connack_packet,
202 v5::basic_publish_packet<PacketIdBytes>,
203 v5::basic_puback_packet<PacketIdBytes>,
204 v5::basic_pubrec_packet<PacketIdBytes>,
205 v5::basic_pubrel_packet<PacketIdBytes>,
206 v5::basic_pubcomp_packet<PacketIdBytes>,
207 v5::basic_subscribe_packet<PacketIdBytes>,
208 v5::basic_suback_packet<PacketIdBytes>,
209 v5::basic_unsubscribe_packet<PacketIdBytes>,
210 v5::basic_unsuback_packet<PacketIdBytes>,
211 v5::pingreq_packet,
212 v5::pingresp_packet,
213 v5::disconnect_packet,
214 v5::auth_packet
215>;
216
217 variant_t var_;
218};
219
220template <std::size_t PacketIdBytes>
221inline std::ostream& operator<<(std::ostream& o, basic_packet_variant<PacketIdBytes> const& v) {
222 v.visit(
223 overload {
224 [&] (auto const& p) {
225 o << p;
226 },
227 [&] (system_error const& se) {
228 o << se.what();
229 }
230 }
231 );
232 return o;
233}
234
242
243} // namespace async_mqtt
244
245#endif // ASYNC_MQTT_PACKET_PACKET_VARIANT_HPP
Definition packet_variant.hpp:49
decltype(auto) get() const
Get by type. If not match, then throw std::bad_variant_access exception.
Definition packet_variant.hpp:122
auto visit(Func &&func) const &
visit to variant
Definition packet_variant.hpp:74
decltype(auto) get()
Get by type. If not match, then throw std::bad_variant_access exception.
Definition packet_variant.hpp:113
decltype(auto) get_if() const
Get by type pointer.
Definition packet_variant.hpp:140
decltype(auto) get_if()
Get by type pointer.
Definition packet_variant.hpp:131
basic_packet_variant(Packet &&packet)
constructor
Definition packet_variant.hpp:66
auto visit(Func &&func) &
visit to variant
Definition packet_variant.hpp:87
optional< control_packet_type > type() const
Get control_packet_type.
Definition packet_variant.hpp:148
auto visit(Func &&func) &&
visit to variant
Definition packet_variant.hpp:100
std::vector< as::const_buffer > const_buffer_sequence() const
Create const buffer sequence it is for boost asio APIs.
Definition packet_variant.hpp:166
async_mqtt error class. It is used as CompletionToken parameter and exception.
Definition exception.hpp:29