From dd78fa6b6a901a63442fafd874d4eee60868e5cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Thu, 25 Jan 2024 18:23:06 +0100 Subject: [PATCH] Simplify enum conversions --- nano/node/election.cpp | 21 ++---------- nano/node/messages.cpp | 76 +++--------------------------------------- 2 files changed, 7 insertions(+), 90 deletions(-) diff --git a/nano/node/election.cpp b/nano/node/election.cpp index ab4aab345c..36aefeb1d1 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -693,24 +693,9 @@ std::vector nano::election::votes_with_weight () co nano::stat::detail nano::to_stat_detail (nano::election_behavior behavior) { - switch (behavior) - { - case nano::election_behavior::normal: - { - return nano::stat::detail::normal; - } - case nano::election_behavior::hinted: - { - return nano::stat::detail::hinted; - } - case nano::election_behavior::optimistic: - { - return nano::stat::detail::optimistic; - } - } - - debug_assert (false, "unknown election behavior"); - return {}; + auto value = magic_enum::enum_cast (magic_enum::enum_integer (behavior)); + debug_assert (value); + return value.value_or (nano::stat::detail{}); } nano::election_behavior nano::election::behavior () const diff --git a/nano/node/messages.cpp b/nano/node/messages.cpp index 0631dcddfa..b27e26c151 100644 --- a/nano/node/messages.cpp +++ b/nano/node/messages.cpp @@ -1995,80 +1995,12 @@ void nano::asc_pull_ack::frontiers_payload::deserialize (nano::stream & stream) std::string_view nano::to_string (nano::message_type type) { - switch (type) - { - case nano::message_type::invalid: - return "invalid"; - case nano::message_type::not_a_type: - return "not_a_type"; - case nano::message_type::keepalive: - return "keepalive"; - case nano::message_type::publish: - return "publish"; - case nano::message_type::confirm_req: - return "confirm_req"; - case nano::message_type::confirm_ack: - return "confirm_ack"; - case nano::message_type::bulk_pull: - return "bulk_pull"; - case nano::message_type::bulk_push: - return "bulk_push"; - case nano::message_type::frontier_req: - return "frontier_req"; - case nano::message_type::node_id_handshake: - return "node_id_handshake"; - case nano::message_type::bulk_pull_account: - return "bulk_pull_account"; - case nano::message_type::telemetry_req: - return "telemetry_req"; - case nano::message_type::telemetry_ack: - return "telemetry_ack"; - case nano::message_type::asc_pull_req: - return "asc_pull_req"; - case nano::message_type::asc_pull_ack: - return "asc_pull_ack"; - // default case intentionally omitted to cause warnings for unhandled enums - } - - return "n/a"; + return magic_enum::enum_name (type); } nano::stat::detail nano::to_stat_detail (nano::message_type type) { - switch (type) - { - case nano::message_type::invalid: - return nano::stat::detail::invalid; - case nano::message_type::not_a_type: - return nano::stat::detail::not_a_type; - case nano::message_type::keepalive: - return nano::stat::detail::keepalive; - case nano::message_type::publish: - return nano::stat::detail::publish; - case nano::message_type::confirm_req: - return nano::stat::detail::confirm_req; - case nano::message_type::confirm_ack: - return nano::stat::detail::confirm_ack; - case nano::message_type::bulk_pull: - return nano::stat::detail::bulk_pull; - case nano::message_type::bulk_push: - return nano::stat::detail::bulk_push; - case nano::message_type::frontier_req: - return nano::stat::detail::frontier_req; - case nano::message_type::node_id_handshake: - return nano::stat::detail::node_id_handshake; - case nano::message_type::bulk_pull_account: - return nano::stat::detail::bulk_pull_account; - case nano::message_type::telemetry_req: - return nano::stat::detail::telemetry_req; - case nano::message_type::telemetry_ack: - return nano::stat::detail::telemetry_ack; - case nano::message_type::asc_pull_req: - return nano::stat::detail::asc_pull_req; - case nano::message_type::asc_pull_ack: - return nano::stat::detail::asc_pull_ack; - // default case intentionally omitted to cause warnings for unhandled enums - } - debug_assert (false); - return {}; + auto value = magic_enum::enum_cast (magic_enum::enum_name (type)); + debug_assert (value); + return value.value_or (nano::stat::detail{}); }