Skip to content

Commit

Permalink
Refined packet comparison.
Browse files Browse the repository at this point in the history
  • Loading branch information
redboltz committed Feb 6, 2024
1 parent 97a235a commit be27626
Show file tree
Hide file tree
Showing 11 changed files with 381 additions and 320 deletions.
93 changes: 93 additions & 0 deletions include/async_mqtt/util/packet_variant_operator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright Takatoshi Kondo 2022
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#if !defined(ASYNC_MQTT_UTIL_PACKET_VARIANT_OPERATOR_HPP)
#define ASYNC_MQTT_UTIL_PACKET_VARIANT_OPERATOR_HPP

#include <algorithm>

#include <async_mqtt/packet/packet_variant.hpp>
#include <async_mqtt/packet/packet_traits.hpp>

namespace async_mqtt {

namespace as = boost::asio;

template <std::size_t PacketIdBytes>
inline bool basic_packet_variant_equal(
basic_packet_variant<PacketIdBytes> const& lhs,
basic_packet_variant<PacketIdBytes> const& rhs
) {
auto lhs_cbs = lhs.const_buffer_sequence();
auto [lhs_b, lhs_e] = make_packet_range(lhs_cbs);

auto rhs_cbs = rhs.const_buffer_sequence();
auto [rhs_b, rhs_e] = make_packet_range(rhs_cbs);

return std::equal(lhs_b, lhs_e, rhs_b, rhs_e);
}

inline bool packet_variant_equal(
packet_variant const& lhs,
packet_variant const& rhs
) {
return basic_packet_variant_equal<2>(lhs, rhs);
}

template <std::size_t PacketIdBytes>
inline bool operator==(
basic_packet_variant<PacketIdBytes> const& lhs,
basic_packet_variant<PacketIdBytes> const& rhs
) {
return basic_packet_variant_equal<PacketIdBytes>(lhs, rhs);
}

inline bool operator==(
packet_variant const& lhs,
packet_variant const& rhs
) {
return packet_variant_equal(lhs, rhs);
}

template <std::size_t PacketIdBytes>
inline bool basic_packet_variant_less_than(
basic_packet_variant<PacketIdBytes> const& lhs,
basic_packet_variant<PacketIdBytes> const& rhs
) {
auto lhs_cbs = lhs.const_buffer_sequence();
auto [lhs_b, lhs_e] = make_packet_range(lhs_cbs);

auto rhs_cbs = rhs.const_buffer_sequence();
auto [rhs_b, rhs_e] = make_packet_range(rhs_cbs);

return std::lexicographical_compare(lhs_b, lhs_e, rhs_b, rhs_e);
}

inline bool packet_variant_less_than(
packet_variant const& lhs,
packet_variant const& rhs
) {
return basic_packet_variant_less_than<2>(lhs, rhs);
}

template <std::size_t PacketIdBytes>
inline bool operator<(
basic_packet_variant<PacketIdBytes> const& lhs,
basic_packet_variant<PacketIdBytes> const& rhs
) {
return basic_packet_variant_less_than<PacketIdBytes>(lhs, rhs);
}

inline bool operator<(
packet_variant const& lhs,
packet_variant const& rhs
) {
return packet_variant_less_than(lhs, rhs);
}

} // namespace async_mqtt

#endif // ASYNC_MQTT_UTIL_PACKET_VARIANT_OPERATOR_HPP
32 changes: 0 additions & 32 deletions test/unit/packet_compare.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion test/unit/ut_cpp20coro_exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <async_mqtt/endpoint.hpp>

#include "stub_socket.hpp"
#include "packet_compare.hpp"
#include <async_mqtt/util/packet_variant_operator.hpp>

BOOST_AUTO_TEST_SUITE(ut_cpp20coro_exec)

Expand Down
Loading

0 comments on commit be27626

Please sign in to comment.