async_mqtt 5.0.0
Loading...
Searching...
No Matches
pubopts.hpp
Go to the documentation of this file.
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_PUBOPTS_HPP)
8#define ASYNC_MQTT_PACKET_PUBOPTS_HPP
9
10#include <boost/assert.hpp>
11
13
15
16namespace async_mqtt {
17
18namespace pub {
19
26constexpr bool is_dup(std::uint8_t v) {
27 return (v & 0b00001000) != 0;
28}
29
36constexpr qos get_qos(std::uint8_t v) {
37 return static_cast<qos>((v & 0b00000110) >> 1);
38}
39
46constexpr bool is_retain(std::uint8_t v) {
47 return (v & 0b00000001) != 0;
48}
49
56constexpr void set_dup(std::uint8_t& fixed_header, bool dup) {
57 if (dup) fixed_header |= 0b00001000;
58 else fixed_header &= static_cast<std::uint8_t>(~0b00001000);
59}
60
66enum class retain : std::uint8_t {
67 yes = 0b00000001,
68 no = 0b00000000,
69};
70
71
77enum class dup : std::uint8_t {
78 yes = 0b00001000,
79 no = 0b00000000,
80};
81
87struct opts final {
88 constexpr opts() = default;
89 ~opts() = default;
90 constexpr opts(opts &&) = default;
91 constexpr opts(opts const&) = default;
92 constexpr opts& operator=(opts &&) = default;
93 constexpr opts& operator=(opts const&) = default;
94
99 explicit constexpr opts(std::uint8_t value) : data_(value) { }
100
105 constexpr opts(retain value) : data_(static_cast<std::uint8_t>(value)) { }
106
111 constexpr opts(dup value) : data_(static_cast<std::uint8_t>(value)) { }
112
117 constexpr opts(qos value) : data_(static_cast<std::uint8_t>(static_cast<std::uint8_t>(value) << 1))
118 {
119 BOOST_ASSERT(value == qos::at_most_once || value == qos::at_least_once || value == qos::exactly_once);
120 }
121
125 constexpr opts operator|(opts rhs) const { return opts(data_ | rhs.data_); }
129 constexpr opts operator|(retain rhs) const { return *this | opts(rhs); }
133 constexpr opts operator|(dup rhs) const { return *this | opts(rhs); }
137 constexpr opts operator|(qos rhs) const { return *this | opts(rhs); }
138
142 constexpr opts& operator|=(opts rhs) { return (*this = (*this | rhs)); }
146 constexpr opts& operator|=(retain rhs) { return (*this = (*this | rhs)); }
150 constexpr opts& operator|=(dup rhs) { return (*this = (*this | rhs)); }
154 constexpr opts& operator|=(qos rhs) { return (*this = (*this | rhs)); }
155
161 constexpr retain get_retain() const
162 { return static_cast<enum retain>(data_ & 0b00000001); }
163
169 constexpr dup get_dup() const
170 { return static_cast<enum dup>(data_ & 0b00001000); }
171
177 constexpr qos get_qos() const
178 { return static_cast<enum qos>((data_ & 0b00000110) >> 1); }
179
185 explicit constexpr operator std::uint8_t() const { return data_; }
186
190 constexpr bool operator==(opts rhs) const { return data_ == rhs.data_; }
191
195 constexpr bool operator<(opts rhs) const { return data_ < rhs.data_; }
196
197private:
198 std::uint8_t data_ = 0; // defaults to retain::no, dup::no, qos::at_most_once
199};
200
205constexpr opts operator|(retain lhs, dup rhs) { return opts(lhs) | rhs; }
210constexpr opts operator|(retain lhs, qos rhs) { return opts(lhs) | rhs; }
211
216constexpr opts operator|(dup lhs, retain rhs) { return opts(lhs) | rhs; }
221constexpr opts operator|(dup lhs, qos rhs) { return opts(lhs) | rhs; }
222
227constexpr opts operator|(qos lhs, retain rhs) { return opts(lhs) | rhs; }
232constexpr opts operator|(qos lhs, dup rhs) { return opts(lhs) | rhs; }
233
234
239constexpr char const* retain_to_str(retain v) {
240 switch(v) {
241 case retain::yes: return "yes";
242 case retain::no: return "no";
243 default: return "invalid_retain";
244 }
245}
246
251inline
252std::ostream& operator<<(std::ostream& os, retain val)
253{
254 os << retain_to_str(val);
255 return os;
256}
257
262constexpr char const* dup_to_str(dup v) {
263 switch(v) {
264 case dup::yes: return "yes";
265 case dup::no: return "no";
266 default: return "invalid_dup";
267 }
268}
269
270
275inline
276std::ostream& operator<<(std::ostream& os, dup val)
277{
278 os << dup_to_str(val);
279 return os;
280}
281
282} // namespace pub
283
284} // namespace async_mqtt
285
286#endif // ASYNC_MQTT_PACKET_PUBOPTS_HPP
Definition packet_variant.hpp:49
retain
MQTT RETAIN.
Definition pubopts.hpp:66
dup
MQTT DUP.
Definition pubopts.hpp:77
qos
MQTT QoS.
Definition qos.hpp:23
MQTT PublishOptions.
Definition pubopts.hpp:87
constexpr operator std::uint8_t() const
Get byte image.
Definition pubopts.hpp:185
constexpr opts operator|(dup lhs, retain rhs)
Combine opts operator.
Definition pubopts.hpp:216
constexpr char const * dup_to_str(dup v)
stringize dup
Definition pubopts.hpp:262
constexpr opts(std::uint8_t value)
constructor
Definition pubopts.hpp:99
constexpr opts(dup value)
constructor
Definition pubopts.hpp:111
constexpr opts & operator|=(retain rhs)
Combine opts operator.
Definition pubopts.hpp:146
constexpr qos get_qos(std::uint8_t v)
Get qos from the fixed header.
Definition pubopts.hpp:36
constexpr char const * retain_to_str(retain v)
stringize retain
Definition pubopts.hpp:239
constexpr void set_dup(std::uint8_t &fixed_header, bool dup)
Set DUP to the fixed header.
Definition pubopts.hpp:56
constexpr bool operator==(opts rhs) const
equal operator
Definition pubopts.hpp:190
constexpr opts operator|(qos lhs, retain rhs)
Combine opts operator.
Definition pubopts.hpp:227
constexpr dup get_dup() const
Get dup.
Definition pubopts.hpp:169
constexpr bool operator<(opts rhs) const
less than operator
Definition pubopts.hpp:195
constexpr opts operator|(dup lhs, qos rhs)
Combine opts operator.
Definition pubopts.hpp:221
constexpr retain get_retain() const
Get retain.
Definition pubopts.hpp:161
constexpr bool is_retain(std::uint8_t v)
Check fixed header is RETAIN.
Definition pubopts.hpp:46
constexpr qos get_qos() const
Get qos.
Definition pubopts.hpp:177
constexpr opts & operator|=(qos rhs)
Combine opts operator.
Definition pubopts.hpp:154
constexpr opts operator|(opts rhs) const
Combine opts operator.
Definition pubopts.hpp:125
std::ostream & operator<<(std::ostream &os, retain val)
output to the stream retain
Definition pubopts.hpp:252
constexpr opts & operator|=(dup rhs)
Combine opts operator.
Definition pubopts.hpp:150
constexpr opts(retain value)
constructor
Definition pubopts.hpp:105
constexpr opts operator|(qos rhs) const
Combine opts operator.
Definition pubopts.hpp:137
std::ostream & operator<<(std::ostream &os, dup val)
output to the stream dup
Definition pubopts.hpp:276
constexpr opts operator|(retain lhs, dup rhs)
Combine opts operator.
Definition pubopts.hpp:205
constexpr opts operator|(qos lhs, dup rhs)
Combine opts operator.
Definition pubopts.hpp:232
constexpr opts operator|(retain lhs, qos rhs)
Combine opts operator.
Definition pubopts.hpp:210
constexpr opts(qos value)
constructor
Definition pubopts.hpp:117
constexpr opts operator|(dup rhs) const
Combine opts operator.
Definition pubopts.hpp:133
constexpr opts operator|(retain rhs) const
Combine opts operator.
Definition pubopts.hpp:129
constexpr bool is_dup(std::uint8_t v)
Check fixed header is DUP.
Definition pubopts.hpp:26
constexpr opts & operator|=(opts rhs)
Combine opts operator.
Definition pubopts.hpp:142