Remove using enum constructs

Summary: related to T13242

Reviewers: ivica

Reviewed By: ivica

Subscribers: miljen, iljazovic

Differential Revision: https://repo.mireo.local/D26841
This commit is contained in:
Korina Šimičević
2023-12-06 08:25:12 +01:00
parent 35b190ef67
commit 3640a4fb2a
6 changed files with 112 additions and 95 deletions

View File

@@ -18,17 +18,16 @@ inline qos_e extract_qos(uint8_t flags) {
}
inline control_code_e extract_code(uint8_t control_byte) {
using enum control_code_e;
constexpr uint8_t mask = 0b11110000;
constexpr uint8_t publish_bits = 0b0011;
constexpr uint8_t special_mask = 0b00000010;
constexpr control_code_e codes_with_non_zero_end[] = {
pubrel, subscribe, unsubscribe
control_code_e::pubrel, control_code_e::subscribe,
control_code_e::unsubscribe
};
if ((control_byte >> 4) == publish_bits)
return publish;
return control_code_e::publish;
if ((control_byte & mask) == control_byte)
return control_code_e(control_byte & mask);
@@ -36,29 +35,27 @@ inline control_code_e extract_code(uint8_t control_byte) {
if (control_byte == (uint8_t(special_code) | special_mask))
return special_code;
return no_packet;
return control_code_e::no_packet;
}
inline std::string_view code_to_str(control_code_e code) {
using enum control_code_e;
switch (code) {
case connect: return "CONNECT";
case connack: return "CONNACK";
case publish: return "PUBLISH";
case puback: return "PUBACK";
case pubrec: return "PUBREC";
case pubrel: return "PUBREL";
case pubcomp: return "PUBCOMP";
case subscribe: return "SUBSCRIBE";
case suback: return "SUBACK";
case unsubscribe: return "UNSUBSCRIBE";
case unsuback: return "UNSUBACK";
case auth: return "AUTH";
case disconnect: return "DISCONNECT";
case pingreq: return "PINGREQ";
case pingresp: return "PINGRESP";
case control_code_e::connect: return "CONNECT";
case control_code_e::connack: return "CONNACK";
case control_code_e::publish: return "PUBLISH";
case control_code_e::puback: return "PUBACK";
case control_code_e::pubrec: return "PUBREC";
case control_code_e::pubrel: return "PUBREL";
case control_code_e::pubcomp: return "PUBCOMP";
case control_code_e::subscribe: return "SUBSCRIBE";
case control_code_e::suback: return "SUBACK";
case control_code_e::unsubscribe: return "UNSUBSCRIBE";
case control_code_e::unsuback: return "UNSUBACK";
case control_code_e::auth: return "AUTH";
case control_code_e::disconnect: return "DISCONNECT";
case control_code_e::pingreq: return "PINGREQ";
case control_code_e::pingresp: return "PINGRESP";
}
return "UNKNOWN";
}
@@ -66,12 +63,10 @@ inline std::string_view code_to_str(control_code_e code) {
inline std::string to_readable_packet(
std::string packet, error_code ec = {}, bool incoming = false
) {
using enum control_code_e;
auto control_byte = uint8_t(*packet.data());
auto code = extract_code(control_byte);
if (code == no_packet)
if (code == control_code_e::no_packet)
return {};
std::ostringstream stream;
@@ -79,7 +74,10 @@ inline std::string to_readable_packet(
if (incoming)
stream << "-> ";
if (code == connect || code == connack || code == disconnect) {
if (
code == control_code_e::connect || code == control_code_e::connack ||
code == control_code_e::disconnect
) {
stream << code_to_str(code) << (ec ? " ec: " + ec.message() : "");
return stream.str();
}
@@ -89,7 +87,7 @@ inline std::string to_readable_packet(
begin, packet.cend(), decoders::basic::varint_
);
if (code == publish) {
if (code == control_code_e::publish) {
auto publish = decoders::decode_publish(
control_byte, *varlen, begin
);

View File

@@ -5,25 +5,24 @@
#include <boost/asio/detached.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/use_awaitable.hpp>
#include <async_mqtt5.hpp>
using namespace async_mqtt5;
constexpr auto use_nothrow_awaitable = asio::as_tuple(asio::use_awaitable);
namespace async_mqtt5::test {
enum cancellation_type {
enum cancel_type {
ioc_stop = 1,
client_cancel
};
} // end namespace async_mqtt5::test
template <test::cancellation_type type>
template <test::cancel_type type>
void cancel_async_receive() {
using enum test::cancellation_type;
using namespace test;
constexpr int num_handlers = 3;
constexpr int expected_handlers_called = type == ioc_stop ? 0 : num_handlers;
@@ -59,9 +58,9 @@ void cancel_async_receive() {
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
}
template <test::cancellation_type type>
template <test::cancel_type type>
void cancel_async_publish() {
using enum test::cancellation_type;
using namespace test;
constexpr int expected_handlers_called = type == ioc_stop ? 0 : 3;
int handlers_called = 0;
@@ -113,9 +112,9 @@ void cancel_async_publish() {
BOOST_CHECK_EQUAL(handlers_called, expected_handlers_called);
}
template <test::cancellation_type type>
template <test::cancel_type type>
void cancel_during_connecting() {
using enum test::cancellation_type;
using namespace test;
constexpr int expected_handlers_called = type == ioc_stop ? 0 : 1;
int handlers_called = 0;
@@ -155,32 +154,36 @@ BOOST_AUTO_TEST_SUITE(cancellation/*, *boost::unit_test::disabled()*/)
BOOST_AUTO_TEST_CASE(ioc_stop_async_receive) {
cancel_async_receive<test::ioc_stop>();
cancel_async_receive<test::cancel_type::ioc_stop>();
}
BOOST_AUTO_TEST_CASE(client_cancel_async_receive) {
cancel_async_receive<test::client_cancel>();
cancel_async_receive<test::cancel_type::client_cancel>();
}
// passes on debian, hangs on windows in io_context destructor
BOOST_AUTO_TEST_CASE(ioc_stop_async_publish, *boost::unit_test::disabled() ) {
cancel_async_publish<test::ioc_stop>();
cancel_async_publish<test::cancel_type::ioc_stop>();
}
BOOST_AUTO_TEST_CASE(client_cancel_async_publish) {
cancel_async_publish<test::client_cancel>();
cancel_async_publish<test::cancel_type::client_cancel>();
}
// passes on debian, hangs on windows
BOOST_AUTO_TEST_CASE(ioc_stop_cancel_during_connecting, *boost::unit_test::disabled() ) {
cancel_during_connecting<test::ioc_stop>();
cancel_during_connecting<test::cancel_type::ioc_stop>();
}
BOOST_AUTO_TEST_CASE(client_cancel_during_connecting) {
cancel_during_connecting<test::client_cancel>();
cancel_during_connecting<test::cancel_type::client_cancel>();
}
#ifdef BOOST_ASIO_HAS_CO_AWAIT
constexpr auto use_nothrow_awaitable = asio::as_tuple(asio::use_awaitable);
BOOST_AUTO_TEST_CASE(rerunning_the_client) {
asio::io_context ioc;
@@ -222,4 +225,6 @@ BOOST_AUTO_TEST_CASE(rerunning_the_client) {
ioc.run();
}
#endif
BOOST_AUTO_TEST_SUITE_END();

View File

@@ -1,11 +1,13 @@
#include <boost/test/unit_test.hpp>
#include <boost/asio/use_awaitable.hpp>
#ifdef BOOST_ASIO_HAS_CO_AWAIT
#include <boost/asio/as_tuple.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/use_awaitable.hpp>
#include <boost/beast/websocket.hpp>
@@ -60,7 +62,7 @@ asio::awaitable<void> sanity_check(mqtt_client<StreamType, TlsContext>& c) {
auto [rec, topic, payload, publish_props] = co_await c.async_receive(use_nothrow_awaitable);
auto [unsub_ec, unsub_codes, unsub_props] = co_await c.async_unsubscribe(
std::vector<std::string>{"test/mqtt-test"}, unsubscribe_props{},
std::vector<std::string>{"test/mqtt-test"}, unsubscribe_props {},
use_nothrow_awaitable
);
BOOST_CHECK(!unsub_ec);
@@ -145,3 +147,6 @@ BOOST_AUTO_TEST_CASE(websocket_tcp_client_check) {
}
BOOST_AUTO_TEST_SUITE_END()
#endif