async_mqtt 5.0.0
Loading...
Searching...
No Matches
packet_helper.hpp
1// Copyright Takatoshi Kondo 2023
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_HELPER_HPP)
8#define ASYNC_MQTT_PACKET_PACKET_HELPER_HPP
9
10#include <utility>
11#include <algorithm>
12#include <async_mqtt/packet/packet_iterator.hpp>
13#include <async_mqtt/packet/packet_traits.hpp>
14#include <async_mqtt/packet/packet_variant.hpp>
15
16namespace async_mqtt {
17
18template <typename Packet>
19constexpr bool is_packet() {
20 return
21 std::is_same_v<v3_1_1::connect_packet, Packet> ||
22 std::is_same_v<v3_1_1::connack_packet, Packet> ||
23 std::is_same_v<v3_1_1::pingreq_packet, Packet> ||
24 std::is_same_v<v3_1_1::pingresp_packet, Packet> ||
25 std::is_same_v<v3_1_1::disconnect_packet, Packet> ||
26 is_instance_of<v3_1_1::basic_publish_packet, Packet>::value ||
27 is_instance_of<v3_1_1::basic_puback_packet, Packet>::value ||
28 is_instance_of<v3_1_1::basic_pubrec_packet, Packet>::value ||
29 is_instance_of<v3_1_1::basic_pubrel_packet, Packet>::value ||
30 is_instance_of<v3_1_1::basic_pubcomp_packet, Packet>::value ||
31 is_instance_of<v3_1_1::basic_subscribe_packet, Packet>::value ||
32 is_instance_of<v3_1_1::basic_suback_packet, Packet>::value ||
33 is_instance_of<v3_1_1::basic_unsubscribe_packet, Packet>::value ||
34 is_instance_of<v3_1_1::basic_unsuback_packet, Packet>::value ||
35 std::is_same_v<v5::connect_packet, Packet> ||
36 std::is_same_v<v5::connack_packet, Packet> ||
37 std::is_same_v<v5::pingreq_packet, Packet> ||
38 std::is_same_v<v5::pingresp_packet, Packet> ||
39 std::is_same_v<v5::disconnect_packet, Packet> ||
40 is_instance_of<v5::basic_publish_packet, Packet>::value ||
41 is_instance_of<v5::basic_puback_packet, Packet>::value ||
42 is_instance_of<v5::basic_pubrec_packet, Packet>::value ||
43 is_instance_of<v5::basic_pubrel_packet, Packet>::value ||
44 is_instance_of<v5::basic_pubcomp_packet, Packet>::value ||
45 is_instance_of<v5::basic_subscribe_packet, Packet>::value ||
46 is_instance_of<v5::basic_suback_packet, Packet>::value ||
47 is_instance_of<v5::basic_unsubscribe_packet, Packet>::value ||
48 is_instance_of<v5::basic_unsuback_packet, Packet>::value ||
49 std::is_same_v<v5::auth_packet, Packet> ||
50 is_instance_of<basic_packet_variant, Packet>::value
51 ;
52}
53
54template<class InputIt1, class InputIt2>
55bool lexicographical_compare(
56 InputIt1 first1,
57 InputIt1 last1,
58 InputIt2 first2,
59 InputIt2 last2
60) {
61 for (; (first1 != last1) && (first2 != last2); ++first1, ++first2) {
62 if (*first1 < *first2) return true;
63 if (*first2 < *first1) return false;
64 }
65 return (first1 == last1) && (first2 != last2);
66}
67
68template <typename Lhs, typename Rhs>
69std::enable_if_t<is_packet<Lhs>() && is_packet<Rhs>(), bool>
70operator==(Lhs const& lhs, Rhs const& rhs) {
71 auto lcbs = lhs.const_buffer_sequence();
72 auto [lb, le] = make_packet_range(lcbs);
73 auto rcbs = rhs.const_buffer_sequence();
74 auto [rb, re] = make_packet_range(rcbs);
75 if (std::distance(lb, le) != std::distance(rb, re)) return false;
76 return std::equal(lb, le, rb);
77}
78
79template <typename Lhs, typename Rhs>
80std::enable_if_t<is_packet<Lhs>() && is_packet<Rhs>(), bool>
81operator<(Lhs const& lhs, Rhs const& rhs) {
82 auto lcbs = lhs.const_buffer_sequence();
83 auto [lb, le] = make_packet_range(lcbs);
84 auto rcbs = rhs.const_buffer_sequence();
85 auto [rb, re] = make_packet_range(rcbs);
86 return
87 lexicographical_compare(
88 lb, le, rb, re
89 );
90}
91
92} // namespace async_mqtt
93
94#endif // ASYNC_MQTT_PACKET_PACKET_HELPER_HPP
std::vector< as::const_buffer > const_buffer_sequence() const
Create const buffer sequence it is for boost asio APIs.
Definition packet_variant.hpp:166