From 506c5db2d47ff980e88121d3a3825206b7c80b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sat, 27 Apr 2024 13:53:16 +0200 Subject: [PATCH 01/13] Move to `enum_utils` header --- nano/lib/enum_utils.hpp | 47 +++++++++++++++++++++++++++++++ nano/lib/logging_enums.cpp | 20 ++++++------- nano/lib/utility.hpp | 43 ---------------------------- nano/node/active_transactions.hpp | 1 + 4 files changed, 56 insertions(+), 55 deletions(-) create mode 100644 nano/lib/enum_utils.hpp diff --git a/nano/lib/enum_utils.hpp b/nano/lib/enum_utils.hpp new file mode 100644 index 0000000000..61a42cae27 --- /dev/null +++ b/nano/lib/enum_utils.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include +#include + +namespace nano +{ +/** + * Array indexable by enum values + */ +template +using enum_array = magic_enum::containers::array; + +/** + * Same as `magic_enum::enum_values (...)` but ignores reserved values (starting with underscore) + */ +template +std::vector enum_values () +{ + std::vector result; + for (auto const & [val, name] : magic_enum::enum_entries ()) + { + if (!name.starts_with ('_')) + { + result.push_back (val); + } + } + return result; +} + +/** + * Same as `magic_enum::enum_cast (...)` but ignores reserved values (starting with underscore). + * Case insensitive. + */ +template +std::optional parse_enum (std::string_view name) +{ + if (name.starts_with ('_')) + { + return std::nullopt; + } + else + { + return magic_enum::enum_cast (name, magic_enum::case_insensitive); + } +} +} \ No newline at end of file diff --git a/nano/lib/logging_enums.cpp b/nano/lib/logging_enums.cpp index 7111ca6591..1a27605c62 100644 --- a/nano/lib/logging_enums.cpp +++ b/nano/lib/logging_enums.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -21,7 +22,7 @@ std::string_view nano::log::to_string (nano::log::level level) const std::vector & nano::log::all_levels () { static std::vector all = [] () { - return nano::util::enum_values (); + return nano::enum_values (); }(); return all; } @@ -29,14 +30,14 @@ const std::vector & nano::log::all_levels () const std::vector & nano::log::all_types () { static std::vector all = [] () { - return nano::util::enum_values (); + return nano::enum_values (); }(); return all; } nano::log::level nano::log::parse_level (std::string_view name) { - auto value = nano::util::parse_enum (name); + auto value = nano::parse_enum (name); if (value.has_value ()) { return value.value (); @@ -53,7 +54,7 @@ nano::log::level nano::log::parse_level (std::string_view name) nano::log::type nano::log::parse_type (std::string_view name) { - auto value = nano::util::parse_enum (name); + auto value = nano::parse_enum (name); if (value.has_value ()) { return value.value (); @@ -66,7 +67,7 @@ nano::log::type nano::log::parse_type (std::string_view name) nano::log::detail nano::log::parse_detail (std::string_view name) { - auto value = nano::util::parse_enum (name); + auto value = nano::parse_enum (name); if (value.has_value ()) { return value.value (); @@ -84,7 +85,7 @@ std::string_view nano::log::to_string (nano::log::tracing_format format) nano::log::tracing_format nano::log::parse_tracing_format (std::string_view name) { - auto value = magic_enum::enum_cast (name); + auto value = nano::parse_enum (name); if (value.has_value ()) { return value.value (); @@ -102,12 +103,7 @@ nano::log::tracing_format nano::log::parse_tracing_format (std::string_view name const std::vector & nano::log::all_tracing_formats () { static std::vector all = [] () { - std::vector result; - for (auto const & fmt : magic_enum::enum_values ()) - { - result.push_back (fmt); - } - return result; + return nano::enum_values (); }(); return all; } \ No newline at end of file diff --git a/nano/lib/utility.hpp b/nano/lib/utility.hpp index 321cfccdb5..35d19892dd 100644 --- a/nano/lib/utility.hpp +++ b/nano/lib/utility.hpp @@ -14,9 +14,6 @@ #include #include -#include -#include - namespace boost { namespace system @@ -58,12 +55,6 @@ namespace program_options namespace nano { -/** - * Array indexable by enum values - */ -template -using enum_array = magic_enum::containers::array; - /* These containers are used to collect information about sequence containers. * It makes use of the composite design pattern to collect information * from sequence containers and sequence containers inside member variables. @@ -294,38 +285,4 @@ std::string to_str (T const & val) { return boost::lexical_cast (val); } - -/** - * Same as `magic_enum::enum_values (...)` but ignores reserved values (starting with underscore) - */ -template -std::vector enum_values () -{ - std::vector result; - for (auto const & [val, name] : magic_enum::enum_entries ()) - { - if (!name.starts_with ('_')) - { - result.push_back (val); - } - } - return result; -} - -/** - * Same as `magic_enum::enum_cast (...)` but ignores reserved values (starting with underscore). - * Case insensitive. - */ -template -std::optional parse_enum (std::string_view name) -{ - if (name.starts_with ('_')) - { - return std::nullopt; - } - else - { - return magic_enum::enum_cast (name, magic_enum::case_insensitive); - } -} } \ No newline at end of file diff --git a/nano/node/active_transactions.hpp b/nano/node/active_transactions.hpp index 1fd12ae26f..b91d8515ae 100644 --- a/nano/node/active_transactions.hpp +++ b/nano/node/active_transactions.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include From 678d0f678986e68419928ffd2de42da114e0e107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sat, 27 Apr 2024 14:03:48 +0200 Subject: [PATCH 02/13] Add `enum_cast` helper --- nano/lib/enum_utils.hpp | 10 ++++++++++ nano/node/blockprocessor.cpp | 7 ++----- nano/node/election.cpp | 7 ++----- nano/node/messages.cpp | 9 +++------ nano/node/rep_tiers.cpp | 7 ++----- nano/node/transport/message_deserializer.cpp | 11 ++++++----- nano/secure/common.cpp | 18 ++++++++---------- 7 files changed, 33 insertions(+), 36 deletions(-) diff --git a/nano/lib/enum_utils.hpp b/nano/lib/enum_utils.hpp index 61a42cae27..cec67ecd07 100644 --- a/nano/lib/enum_utils.hpp +++ b/nano/lib/enum_utils.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -44,4 +46,12 @@ std::optional parse_enum (std::string_view name) return magic_enum::enum_cast (name, magic_enum::case_insensitive); } } + +template +T enum_cast (S value) +{ + auto conv = magic_enum::enum_cast (magic_enum::enum_name (value)); + debug_assert (conv); + return conv.value_or (T{}); +} } \ No newline at end of file diff --git a/nano/node/blockprocessor.cpp b/nano/node/blockprocessor.cpp index 1ee92ae2f4..31dd17207b 100644 --- a/nano/node/blockprocessor.cpp +++ b/nano/node/blockprocessor.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -11,8 +12,6 @@ #include -#include - /* * block_processor::context */ @@ -472,9 +471,7 @@ std::string_view nano::to_string (nano::block_source source) nano::stat::detail nano::to_stat_detail (nano::block_source type) { - auto value = magic_enum::enum_cast (magic_enum::enum_name (type)); - debug_assert (value); - return value.value_or (nano::stat::detail{}); + return nano::enum_cast (type); } /* diff --git a/nano/node/election.cpp b/nano/node/election.cpp index acc9736550..c9375e2fc3 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -8,8 +9,6 @@ #include #include -#include - using namespace std::chrono; std::chrono::milliseconds nano::election::base_latency () const @@ -804,9 +803,7 @@ std::string_view nano::to_string (nano::election_behavior behavior) nano::stat::detail nano::to_stat_detail (nano::election_behavior behavior) { - auto value = magic_enum::enum_cast (magic_enum::enum_name (behavior)); - debug_assert (value); - return value.value_or (nano::stat::detail{}); + return nano::enum_cast (behavior); } std::string_view nano::to_string (nano::election_state state) diff --git a/nano/node/messages.cpp b/nano/node/messages.cpp index 39665a4d37..54d2de02b1 100644 --- a/nano/node/messages.cpp +++ b/nano/node/messages.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -1970,14 +1971,10 @@ std::string_view nano::to_string (nano::message_type type) nano::stat::detail nano::to_stat_detail (nano::message_type type) { - auto value = magic_enum::enum_cast (magic_enum::enum_name (type)); - debug_assert (value); - return value.value_or (nano::stat::detail{}); + return nano::enum_cast (type); } nano::log::detail nano::to_log_detail (nano::message_type type) { - auto value = magic_enum::enum_cast (magic_enum::enum_name (type)); - debug_assert (value); - return value.value_or (nano::log::detail{}); + return nano::enum_cast (type); } diff --git a/nano/node/rep_tiers.cpp b/nano/node/rep_tiers.cpp index df65aea884..c889cc100c 100644 --- a/nano/node/rep_tiers.cpp +++ b/nano/node/rep_tiers.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,8 +6,6 @@ #include #include -#include - using namespace std::chrono_literals; nano::rep_tiers::rep_tiers (nano::ledger & ledger_a, nano::network_params & network_params_a, nano::online_reps & online_reps_a, nano::stats & stats_a, nano::logger & logger_a) : @@ -147,7 +146,5 @@ std::unique_ptr nano::rep_tiers::collect_contain nano::stat::detail nano::to_stat_detail (nano::rep_tier tier) { - auto value = magic_enum::enum_cast (magic_enum::enum_name (tier)); - debug_assert (value); - return value.value_or (nano::stat::detail{}); + return nano::enum_cast (tier); } \ No newline at end of file diff --git a/nano/node/transport/message_deserializer.cpp b/nano/node/transport/message_deserializer.cpp index 174a875eb7..0831e22cbb 100644 --- a/nano/node/transport/message_deserializer.cpp +++ b/nano/node/transport/message_deserializer.cpp @@ -1,8 +1,7 @@ +#include #include #include -#include - nano::transport::message_deserializer::message_deserializer (nano::network_constants const & network_constants_a, nano::network_filter & publish_filter_a, nano::block_uniquer & block_uniquer_a, nano::vote_uniquer & vote_uniquer_a, read_query read_op) : read_buffer{ std::make_shared> () }, @@ -382,11 +381,13 @@ std::unique_ptr nano::transport::message_deserializer::deser return {}; } +/* + * + */ + nano::stat::detail nano::transport::to_stat_detail (nano::transport::parse_status status) { - auto value = magic_enum::enum_cast (magic_enum::enum_name (status)); - debug_assert (value); - return value.value_or (nano::stat::detail{}); + return nano::enum_cast (status); } std::string_view nano::transport::to_string (nano::transport::parse_status status) diff --git a/nano/secure/common.cpp b/nano/secure/common.cpp index 5ce28c2088..966ffa1602 100644 --- a/nano/secure/common.cpp +++ b/nano/secure/common.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -16,7 +17,6 @@ #include #include -#include nano::networks nano::network_constants::active_network = nano::networks::ACTIVE_NETWORK; @@ -360,18 +360,18 @@ nano::block_hash const & nano::unchecked_key::key () const return previous; } +/* + * + */ + nano::stat::detail nano::to_stat_detail (nano::vote_code code) { - auto value = magic_enum::enum_cast (magic_enum::enum_name (code)); - debug_assert (value); - return value.value_or (nano::stat::detail{}); + return nano::enum_cast (code); } nano::stat::detail nano::to_stat_detail (nano::vote_source source) { - auto value = magic_enum::enum_cast (magic_enum::enum_name (source)); - debug_assert (value); - return value.value_or (nano::stat::detail{}); + return nano::enum_cast (source); } std::string_view nano::to_string (nano::block_status code) @@ -381,7 +381,5 @@ std::string_view nano::to_string (nano::block_status code) nano::stat::detail nano::to_stat_detail (nano::block_status code) { - auto value = magic_enum::enum_cast (magic_enum::enum_name (code)); - debug_assert (value); - return value.value_or (nano::stat::detail{}); + return nano::enum_cast (code); } From 117d1ace83826a38d7d80f88c84da48773b7fc39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sat, 27 Apr 2024 14:06:08 +0200 Subject: [PATCH 03/13] Rename to `enum_parse` --- nano/lib/enum_utils.hpp | 2 +- nano/lib/logging_enums.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/nano/lib/enum_utils.hpp b/nano/lib/enum_utils.hpp index cec67ecd07..6f68641dc5 100644 --- a/nano/lib/enum_utils.hpp +++ b/nano/lib/enum_utils.hpp @@ -35,7 +35,7 @@ std::vector enum_values () * Case insensitive. */ template -std::optional parse_enum (std::string_view name) +std::optional enum_parse (std::string_view name) { if (name.starts_with ('_')) { diff --git a/nano/lib/logging_enums.cpp b/nano/lib/logging_enums.cpp index 1a27605c62..1b1f7371cb 100644 --- a/nano/lib/logging_enums.cpp +++ b/nano/lib/logging_enums.cpp @@ -37,7 +37,7 @@ const std::vector & nano::log::all_types () nano::log::level nano::log::parse_level (std::string_view name) { - auto value = nano::parse_enum (name); + auto value = nano::enum_parse (name); if (value.has_value ()) { return value.value (); @@ -54,7 +54,7 @@ nano::log::level nano::log::parse_level (std::string_view name) nano::log::type nano::log::parse_type (std::string_view name) { - auto value = nano::parse_enum (name); + auto value = nano::enum_parse (name); if (value.has_value ()) { return value.value (); @@ -67,7 +67,7 @@ nano::log::type nano::log::parse_type (std::string_view name) nano::log::detail nano::log::parse_detail (std::string_view name) { - auto value = nano::parse_enum (name); + auto value = nano::enum_parse (name); if (value.has_value ()) { return value.value (); @@ -85,7 +85,7 @@ std::string_view nano::log::to_string (nano::log::tracing_format format) nano::log::tracing_format nano::log::parse_tracing_format (std::string_view name) { - auto value = nano::parse_enum (name); + auto value = nano::enum_parse (name); if (value.has_value ()) { return value.value (); From 8fc59508536efb2c97a0c480f74a6f5f96455b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sat, 27 Apr 2024 14:08:46 +0200 Subject: [PATCH 04/13] Add `ignore_reserved` flag --- nano/lib/enum_utils.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nano/lib/enum_utils.hpp b/nano/lib/enum_utils.hpp index 6f68641dc5..5998efbf36 100644 --- a/nano/lib/enum_utils.hpp +++ b/nano/lib/enum_utils.hpp @@ -17,12 +17,12 @@ using enum_array = magic_enum::containers::array; * Same as `magic_enum::enum_values (...)` but ignores reserved values (starting with underscore) */ template -std::vector enum_values () +std::vector enum_values (bool ignore_reserved = true) { std::vector result; for (auto const & [val, name] : magic_enum::enum_entries ()) { - if (!name.starts_with ('_')) + if (!ignore_reserved || !name.starts_with ('_')) { result.push_back (val); } @@ -35,9 +35,9 @@ std::vector enum_values () * Case insensitive. */ template -std::optional enum_parse (std::string_view name) +std::optional enum_parse (std::string_view name, bool ignore_reserved = true) { - if (name.starts_with ('_')) + if (ignore_reserved && name.starts_with ('_')) { return std::nullopt; } From 96094a0d87528d45fa164098ab9cde37f2693aba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sat, 27 Apr 2024 23:58:10 +0200 Subject: [PATCH 05/13] Enum name helper --- nano/lib/block_type.cpp | 5 ++--- nano/lib/blocks.cpp | 2 +- nano/lib/enum_utils.hpp | 10 +++++++++- nano/lib/logging.cpp | 2 +- nano/lib/logging_enums.cpp | 10 ++++------ nano/lib/stats_enums.cpp | 11 +++++------ nano/lib/thread_roles.cpp | 5 ++--- nano/node/blockprocessor.cpp | 2 +- nano/node/election.cpp | 4 ++-- nano/node/messages.cpp | 2 +- nano/node/transport/message_deserializer.cpp | 2 +- nano/node/transport/socket.cpp | 7 +++---- nano/secure/common.cpp | 2 +- 13 files changed, 33 insertions(+), 31 deletions(-) diff --git a/nano/lib/block_type.cpp b/nano/lib/block_type.cpp index 4afb9b0fdb..f2967ea594 100644 --- a/nano/lib/block_type.cpp +++ b/nano/lib/block_type.cpp @@ -1,10 +1,9 @@ #include - -#include +#include std::string_view nano::to_string (nano::block_type type) { - return magic_enum::enum_name (type); + return nano::enum_name (type); } void nano::serialize_block_type (nano::stream & stream, const nano::block_type & type) diff --git a/nano/lib/blocks.cpp b/nano/lib/blocks.cpp index 653ecd2920..b45c5613f0 100644 --- a/nano/lib/blocks.cpp +++ b/nano/lib/blocks.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -11,7 +12,6 @@ #include #include -#include size_t constexpr nano::send_block::size; size_t constexpr nano::receive_block::size; diff --git a/nano/lib/enum_utils.hpp b/nano/lib/enum_utils.hpp index 5998efbf36..8cf2ef348e 100644 --- a/nano/lib/enum_utils.hpp +++ b/nano/lib/enum_utils.hpp @@ -13,6 +13,14 @@ namespace nano template using enum_array = magic_enum::containers::array; +std::string_view enum_name (auto value) +{ + auto name = magic_enum::enum_name (value); + debug_assert (!name.empty ()); + release_assert (name.size () < 64); // Safety check + return name; +} + /** * Same as `magic_enum::enum_values (...)` but ignores reserved values (starting with underscore) */ @@ -50,7 +58,7 @@ std::optional enum_parse (std::string_view name, bool ignore_reserved = true) template T enum_cast (S value) { - auto conv = magic_enum::enum_cast (magic_enum::enum_name (value)); + auto conv = magic_enum::enum_cast (nano::enum_name (value)); debug_assert (conv); return conv.value_or (T{}); } diff --git a/nano/lib/logging.cpp b/nano/lib/logging.cpp index be90deb36a..b86e980d61 100644 --- a/nano/lib/logging.cpp +++ b/nano/lib/logging.cpp @@ -1,11 +1,11 @@ #include +#include #include #include #include #include #include -#include #include #include #include diff --git a/nano/lib/logging_enums.cpp b/nano/lib/logging_enums.cpp index 1b1f7371cb..9fab4e26fb 100644 --- a/nano/lib/logging_enums.cpp +++ b/nano/lib/logging_enums.cpp @@ -2,21 +2,19 @@ #include #include -#include - std::string_view nano::log::to_string (nano::log::type tag) { - return magic_enum::enum_name (tag); + return nano::enum_name (tag); } std::string_view nano::log::to_string (nano::log::detail detail) { - return magic_enum::enum_name (detail); + return nano::enum_name (detail); } std::string_view nano::log::to_string (nano::log::level level) { - return magic_enum::enum_name (level); + return nano::enum_name (level); } const std::vector & nano::log::all_levels () @@ -80,7 +78,7 @@ nano::log::detail nano::log::parse_detail (std::string_view name) std::string_view nano::log::to_string (nano::log::tracing_format format) { - return magic_enum::enum_name (format); + return nano::enum_name (format); } nano::log::tracing_format nano::log::parse_tracing_format (std::string_view name) diff --git a/nano/lib/stats_enums.cpp b/nano/lib/stats_enums.cpp index d58cf8fe21..a5f1b0e8b0 100644 --- a/nano/lib/stats_enums.cpp +++ b/nano/lib/stats_enums.cpp @@ -1,23 +1,22 @@ +#include #include -#include - std::string_view nano::to_string (nano::stat::type type) { - return magic_enum::enum_name (type); + return nano::enum_name (type); } std::string_view nano::to_string (nano::stat::detail detail) { - return magic_enum::enum_name (detail); + return nano::enum_name (detail); } std::string_view nano::to_string (nano::stat::dir dir) { - return magic_enum::enum_name (dir); + return nano::enum_name (dir); } std::string_view nano::to_string (nano::stat::sample sample) { - return magic_enum::enum_name (sample); + return nano::enum_name (sample); } \ No newline at end of file diff --git a/nano/lib/thread_roles.cpp b/nano/lib/thread_roles.cpp index ebd26a73f4..9825e6f738 100644 --- a/nano/lib/thread_roles.cpp +++ b/nano/lib/thread_roles.cpp @@ -1,11 +1,10 @@ +#include #include #include -#include - std::string_view nano::thread_role::to_string (nano::thread_role::name name) { - return magic_enum::enum_name (name); + return nano::enum_name (name); } std::string nano::thread_role::get_string (nano::thread_role::name role) diff --git a/nano/node/blockprocessor.cpp b/nano/node/blockprocessor.cpp index 31dd17207b..f530d7c110 100644 --- a/nano/node/blockprocessor.cpp +++ b/nano/node/blockprocessor.cpp @@ -466,7 +466,7 @@ std::unique_ptr nano::block_processor::collect_c std::string_view nano::to_string (nano::block_source source) { - return magic_enum::enum_name (source); + return nano::enum_name (source); } nano::stat::detail nano::to_stat_detail (nano::block_source type) diff --git a/nano/node/election.cpp b/nano/node/election.cpp index c9375e2fc3..00a5f7239a 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -798,7 +798,7 @@ void nano::election_extended_status::operator() (nano::object_stream & obs) cons std::string_view nano::to_string (nano::election_behavior behavior) { - return magic_enum::enum_name (behavior); + return nano::enum_name (behavior); } nano::stat::detail nano::to_stat_detail (nano::election_behavior behavior) @@ -808,5 +808,5 @@ nano::stat::detail nano::to_stat_detail (nano::election_behavior behavior) std::string_view nano::to_string (nano::election_state state) { - return magic_enum::enum_name (state); + return nano::enum_name (state); } \ No newline at end of file diff --git a/nano/node/messages.cpp b/nano/node/messages.cpp index 54d2de02b1..bce8bcb546 100644 --- a/nano/node/messages.cpp +++ b/nano/node/messages.cpp @@ -1966,7 +1966,7 @@ void nano::asc_pull_ack::frontiers_payload::operator() (nano::object_stream & ob std::string_view nano::to_string (nano::message_type type) { - return magic_enum::enum_name (type); + return nano::enum_name (type); } nano::stat::detail nano::to_stat_detail (nano::message_type type) diff --git a/nano/node/transport/message_deserializer.cpp b/nano/node/transport/message_deserializer.cpp index 0831e22cbb..bc6fb71899 100644 --- a/nano/node/transport/message_deserializer.cpp +++ b/nano/node/transport/message_deserializer.cpp @@ -392,5 +392,5 @@ nano::stat::detail nano::transport::to_stat_detail (nano::transport::parse_statu std::string_view nano::transport::to_string (nano::transport::parse_status status) { - return magic_enum::enum_name (status); + return nano::enum_name (status); } diff --git a/nano/node/transport/socket.cpp b/nano/node/transport/socket.cpp index 5747b03d17..d645a5301c 100644 --- a/nano/node/transport/socket.cpp +++ b/nano/node/transport/socket.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -13,8 +14,6 @@ #include #include -#include - /* * socket */ @@ -530,10 +529,10 @@ std::size_t network_prefix) std::string_view nano::transport::to_string (socket_type type) { - return magic_enum::enum_name (type); + return nano::enum_name (type); } std::string_view nano::transport::to_string (socket_endpoint type) { - return magic_enum::enum_name (type); + return nano::enum_name (type); } diff --git a/nano/secure/common.cpp b/nano/secure/common.cpp index 966ffa1602..ec99774277 100644 --- a/nano/secure/common.cpp +++ b/nano/secure/common.cpp @@ -376,7 +376,7 @@ nano::stat::detail nano::to_stat_detail (nano::vote_source source) std::string_view nano::to_string (nano::block_status code) { - return magic_enum::enum_name (code); + return nano::enum_name (code); } nano::stat::detail nano::to_stat_detail (nano::block_status code) From 726099d635cda6b10e37fa3608d0e891c836a736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sun, 28 Apr 2024 00:18:25 +0200 Subject: [PATCH 06/13] Namespace --- nano/lib/block_type.cpp | 2 +- nano/lib/enum_utils.hpp | 14 ++++++++----- nano/lib/logging_enums.cpp | 22 ++++++++++---------- nano/lib/stats_enums.cpp | 8 +++---- nano/lib/thread_roles.cpp | 2 +- nano/node/blockprocessor.cpp | 4 ++-- nano/node/election.cpp | 6 +++--- nano/node/messages.cpp | 6 +++--- nano/node/rep_tiers.cpp | 2 +- nano/node/transport/message_deserializer.cpp | 4 ++-- nano/node/transport/socket.cpp | 4 ++-- nano/secure/common.cpp | 8 +++---- 12 files changed, 43 insertions(+), 39 deletions(-) diff --git a/nano/lib/block_type.cpp b/nano/lib/block_type.cpp index f2967ea594..de1ff9147a 100644 --- a/nano/lib/block_type.cpp +++ b/nano/lib/block_type.cpp @@ -3,7 +3,7 @@ std::string_view nano::to_string (nano::block_type type) { - return nano::enum_name (type); + return nano::enum_util::name (type); } void nano::serialize_block_type (nano::stream & stream, const nano::block_type & type) diff --git a/nano/lib/enum_utils.hpp b/nano/lib/enum_utils.hpp index 8cf2ef348e..5c00801b7c 100644 --- a/nano/lib/enum_utils.hpp +++ b/nano/lib/enum_utils.hpp @@ -12,8 +12,12 @@ namespace nano */ template using enum_array = magic_enum::containers::array; +} -std::string_view enum_name (auto value) +// Needs nested namespace to avoid ADL collisions with magic_enum +namespace nano::enum_util +{ +std::string_view name (auto value) { auto name = magic_enum::enum_name (value); debug_assert (!name.empty ()); @@ -25,7 +29,7 @@ std::string_view enum_name (auto value) * Same as `magic_enum::enum_values (...)` but ignores reserved values (starting with underscore) */ template -std::vector enum_values (bool ignore_reserved = true) +std::vector values (bool ignore_reserved = true) { std::vector result; for (auto const & [val, name] : magic_enum::enum_entries ()) @@ -43,7 +47,7 @@ std::vector enum_values (bool ignore_reserved = true) * Case insensitive. */ template -std::optional enum_parse (std::string_view name, bool ignore_reserved = true) +std::optional parse (std::string_view name, bool ignore_reserved = true) { if (ignore_reserved && name.starts_with ('_')) { @@ -56,9 +60,9 @@ std::optional enum_parse (std::string_view name, bool ignore_reserved = true) } template -T enum_cast (S value) +T cast (S value) { - auto conv = magic_enum::enum_cast (nano::enum_name (value)); + auto conv = magic_enum::enum_cast (nano::enum_util::name (value)); debug_assert (conv); return conv.value_or (T{}); } diff --git a/nano/lib/logging_enums.cpp b/nano/lib/logging_enums.cpp index 9fab4e26fb..5bfa27fcd2 100644 --- a/nano/lib/logging_enums.cpp +++ b/nano/lib/logging_enums.cpp @@ -4,23 +4,23 @@ std::string_view nano::log::to_string (nano::log::type tag) { - return nano::enum_name (tag); + return nano::enum_util::name (tag); } std::string_view nano::log::to_string (nano::log::detail detail) { - return nano::enum_name (detail); + return nano::enum_util::name (detail); } std::string_view nano::log::to_string (nano::log::level level) { - return nano::enum_name (level); + return nano::enum_util::name (level); } const std::vector & nano::log::all_levels () { static std::vector all = [] () { - return nano::enum_values (); + return nano::enum_util::values (); }(); return all; } @@ -28,14 +28,14 @@ const std::vector & nano::log::all_levels () const std::vector & nano::log::all_types () { static std::vector all = [] () { - return nano::enum_values (); + return nano::enum_util::values (); }(); return all; } nano::log::level nano::log::parse_level (std::string_view name) { - auto value = nano::enum_parse (name); + auto value = nano::enum_util::parse (name); if (value.has_value ()) { return value.value (); @@ -52,7 +52,7 @@ nano::log::level nano::log::parse_level (std::string_view name) nano::log::type nano::log::parse_type (std::string_view name) { - auto value = nano::enum_parse (name); + auto value = nano::enum_util::parse (name); if (value.has_value ()) { return value.value (); @@ -65,7 +65,7 @@ nano::log::type nano::log::parse_type (std::string_view name) nano::log::detail nano::log::parse_detail (std::string_view name) { - auto value = nano::enum_parse (name); + auto value = nano::enum_util::parse (name); if (value.has_value ()) { return value.value (); @@ -78,12 +78,12 @@ nano::log::detail nano::log::parse_detail (std::string_view name) std::string_view nano::log::to_string (nano::log::tracing_format format) { - return nano::enum_name (format); + return nano::enum_util::name (format); } nano::log::tracing_format nano::log::parse_tracing_format (std::string_view name) { - auto value = nano::enum_parse (name); + auto value = nano::enum_util::parse (name); if (value.has_value ()) { return value.value (); @@ -101,7 +101,7 @@ nano::log::tracing_format nano::log::parse_tracing_format (std::string_view name const std::vector & nano::log::all_tracing_formats () { static std::vector all = [] () { - return nano::enum_values (); + return nano::enum_util::values (); }(); return all; } \ No newline at end of file diff --git a/nano/lib/stats_enums.cpp b/nano/lib/stats_enums.cpp index a5f1b0e8b0..e297a6d6fe 100644 --- a/nano/lib/stats_enums.cpp +++ b/nano/lib/stats_enums.cpp @@ -3,20 +3,20 @@ std::string_view nano::to_string (nano::stat::type type) { - return nano::enum_name (type); + return nano::enum_util::name (type); } std::string_view nano::to_string (nano::stat::detail detail) { - return nano::enum_name (detail); + return nano::enum_util::name (detail); } std::string_view nano::to_string (nano::stat::dir dir) { - return nano::enum_name (dir); + return nano::enum_util::name (dir); } std::string_view nano::to_string (nano::stat::sample sample) { - return nano::enum_name (sample); + return nano::enum_util::name (sample); } \ No newline at end of file diff --git a/nano/lib/thread_roles.cpp b/nano/lib/thread_roles.cpp index 9825e6f738..9124d94a20 100644 --- a/nano/lib/thread_roles.cpp +++ b/nano/lib/thread_roles.cpp @@ -4,7 +4,7 @@ std::string_view nano::thread_role::to_string (nano::thread_role::name name) { - return nano::enum_name (name); + return nano::enum_util::name (name); } std::string nano::thread_role::get_string (nano::thread_role::name role) diff --git a/nano/node/blockprocessor.cpp b/nano/node/blockprocessor.cpp index f530d7c110..304c728501 100644 --- a/nano/node/blockprocessor.cpp +++ b/nano/node/blockprocessor.cpp @@ -466,12 +466,12 @@ std::unique_ptr nano::block_processor::collect_c std::string_view nano::to_string (nano::block_source source) { - return nano::enum_name (source); + return nano::enum_util::name (source); } nano::stat::detail nano::to_stat_detail (nano::block_source type) { - return nano::enum_cast (type); + return nano::enum_util::cast (type); } /* diff --git a/nano/node/election.cpp b/nano/node/election.cpp index 00a5f7239a..5a6874e35a 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -798,15 +798,15 @@ void nano::election_extended_status::operator() (nano::object_stream & obs) cons std::string_view nano::to_string (nano::election_behavior behavior) { - return nano::enum_name (behavior); + return nano::enum_util::name (behavior); } nano::stat::detail nano::to_stat_detail (nano::election_behavior behavior) { - return nano::enum_cast (behavior); + return nano::enum_util::cast (behavior); } std::string_view nano::to_string (nano::election_state state) { - return nano::enum_name (state); + return nano::enum_util::name (state); } \ No newline at end of file diff --git a/nano/node/messages.cpp b/nano/node/messages.cpp index bce8bcb546..25170c583d 100644 --- a/nano/node/messages.cpp +++ b/nano/node/messages.cpp @@ -1966,15 +1966,15 @@ void nano::asc_pull_ack::frontiers_payload::operator() (nano::object_stream & ob std::string_view nano::to_string (nano::message_type type) { - return nano::enum_name (type); + return nano::enum_util::name (type); } nano::stat::detail nano::to_stat_detail (nano::message_type type) { - return nano::enum_cast (type); + return nano::enum_util::cast (type); } nano::log::detail nano::to_log_detail (nano::message_type type) { - return nano::enum_cast (type); + return nano::enum_util::cast (type); } diff --git a/nano/node/rep_tiers.cpp b/nano/node/rep_tiers.cpp index c889cc100c..ae10536bbe 100644 --- a/nano/node/rep_tiers.cpp +++ b/nano/node/rep_tiers.cpp @@ -146,5 +146,5 @@ std::unique_ptr nano::rep_tiers::collect_contain nano::stat::detail nano::to_stat_detail (nano::rep_tier tier) { - return nano::enum_cast (tier); + return nano::enum_util::cast (tier); } \ No newline at end of file diff --git a/nano/node/transport/message_deserializer.cpp b/nano/node/transport/message_deserializer.cpp index bc6fb71899..2013552c8d 100644 --- a/nano/node/transport/message_deserializer.cpp +++ b/nano/node/transport/message_deserializer.cpp @@ -387,10 +387,10 @@ std::unique_ptr nano::transport::message_deserializer::deser nano::stat::detail nano::transport::to_stat_detail (nano::transport::parse_status status) { - return nano::enum_cast (status); + return nano::enum_util::cast (status); } std::string_view nano::transport::to_string (nano::transport::parse_status status) { - return nano::enum_name (status); + return nano::enum_util::name (status); } diff --git a/nano/node/transport/socket.cpp b/nano/node/transport/socket.cpp index d645a5301c..7fd2cbe75f 100644 --- a/nano/node/transport/socket.cpp +++ b/nano/node/transport/socket.cpp @@ -529,10 +529,10 @@ std::size_t network_prefix) std::string_view nano::transport::to_string (socket_type type) { - return nano::enum_name (type); + return nano::enum_util::name (type); } std::string_view nano::transport::to_string (socket_endpoint type) { - return nano::enum_name (type); + return nano::enum_util::name (type); } diff --git a/nano/secure/common.cpp b/nano/secure/common.cpp index ec99774277..059ea8ef31 100644 --- a/nano/secure/common.cpp +++ b/nano/secure/common.cpp @@ -366,20 +366,20 @@ nano::block_hash const & nano::unchecked_key::key () const nano::stat::detail nano::to_stat_detail (nano::vote_code code) { - return nano::enum_cast (code); + return nano::enum_util::cast (code); } nano::stat::detail nano::to_stat_detail (nano::vote_source source) { - return nano::enum_cast (source); + return nano::enum_util::cast (source); } std::string_view nano::to_string (nano::block_status code) { - return nano::enum_name (code); + return nano::enum_util::name (code); } nano::stat::detail nano::to_stat_detail (nano::block_status code) { - return nano::enum_cast (code); + return nano::enum_util::cast (code); } From b0ff0e4f69bdb50097aa116f1dc32d88b66149cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sun, 5 May 2024 11:51:16 +0200 Subject: [PATCH 07/13] Convert usages --- nano/node/transport/tcp_listener.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/nano/node/transport/tcp_listener.cpp b/nano/node/transport/tcp_listener.cpp index 7cf6a0b6ee..162729b2cc 100644 --- a/nano/node/transport/tcp_listener.cpp +++ b/nano/node/transport/tcp_listener.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -10,8 +11,6 @@ #include #include -#include - using namespace std::chrono_literals; /* @@ -595,6 +594,10 @@ std::unique_ptr nano::transport::tcp_listener::c return composite; } +/* + * + */ + nano::stat::dir nano::transport::tcp_listener::to_stat_dir (connection_type type) { switch (type) @@ -610,7 +613,7 @@ nano::stat::dir nano::transport::tcp_listener::to_stat_dir (connection_type type std::string_view nano::transport::tcp_listener::to_string (connection_type type) { - return magic_enum::enum_name (type); + return nano::enum_util::name (type); } nano::transport::socket_endpoint nano::transport::tcp_listener::to_socket_endpoint (connection_type type) From 1433f46f5551ce2e7895c740d2335416eab9b043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sun, 5 May 2024 11:55:18 +0200 Subject: [PATCH 08/13] Simplify `enum_util::values ()` --- nano/lib/enum_utils.hpp | 19 +++++++++++-------- nano/lib/logging_enums.cpp | 15 +++------------ 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/nano/lib/enum_utils.hpp b/nano/lib/enum_utils.hpp index 5c00801b7c..cbc0453a48 100644 --- a/nano/lib/enum_utils.hpp +++ b/nano/lib/enum_utils.hpp @@ -29,17 +29,20 @@ std::string_view name (auto value) * Same as `magic_enum::enum_values (...)` but ignores reserved values (starting with underscore) */ template -std::vector values (bool ignore_reserved = true) +std::vector const & values (bool ignore_reserved = true) { - std::vector result; - for (auto const & [val, name] : magic_enum::enum_entries ()) - { - if (!ignore_reserved || !name.starts_with ('_')) + static std::vector all = [ignore_reserved] () { + std::vector result; + for (auto const & [val, name] : magic_enum::enum_entries ()) { - result.push_back (val); + if (!ignore_reserved || !name.starts_with ('_')) + { + result.push_back (val); + } } - } - return result; + return result; + }(); + return all; } /** diff --git a/nano/lib/logging_enums.cpp b/nano/lib/logging_enums.cpp index 5bfa27fcd2..c6b59b7fce 100644 --- a/nano/lib/logging_enums.cpp +++ b/nano/lib/logging_enums.cpp @@ -19,18 +19,12 @@ std::string_view nano::log::to_string (nano::log::level level) const std::vector & nano::log::all_levels () { - static std::vector all = [] () { - return nano::enum_util::values (); - }(); - return all; + return nano::enum_util::values (); } const std::vector & nano::log::all_types () { - static std::vector all = [] () { - return nano::enum_util::values (); - }(); - return all; + return nano::enum_util::values (); } nano::log::level nano::log::parse_level (std::string_view name) @@ -100,8 +94,5 @@ nano::log::tracing_format nano::log::parse_tracing_format (std::string_view name const std::vector & nano::log::all_tracing_formats () { - static std::vector all = [] () { - return nano::enum_util::values (); - }(); - return all; + return nano::enum_util::values (); } \ No newline at end of file From 29e892cdd6da6a8edef13dfefe486d3f98979ac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sun, 5 May 2024 12:06:12 +0200 Subject: [PATCH 09/13] Parsing --- nano/lib/enum_utils.hpp | 22 +++++++++++++++++--- nano/lib/logging_enums.cpp | 42 +++++++++++++------------------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/nano/lib/enum_utils.hpp b/nano/lib/enum_utils.hpp index cbc0453a48..524281d7d5 100644 --- a/nano/lib/enum_utils.hpp +++ b/nano/lib/enum_utils.hpp @@ -26,7 +26,7 @@ std::string_view name (auto value) } /** - * Same as `magic_enum::enum_values (...)` but ignores reserved values (starting with underscore) + * Same as `magic_enum::enum_values (...)` but ignores reserved values (starting with underscore) by default. */ template std::vector const & values (bool ignore_reserved = true) @@ -46,11 +46,11 @@ std::vector const & values (bool ignore_reserved = true) } /** - * Same as `magic_enum::enum_cast (...)` but ignores reserved values (starting with underscore). + * Same as `magic_enum::enum_cast (...)` but ignores reserved values (starting with underscore) by default. * Case insensitive. */ template -std::optional parse (std::string_view name, bool ignore_reserved = true) +std::optional try_parse (std::string_view name, bool ignore_reserved = true) { if (ignore_reserved && name.starts_with ('_')) { @@ -62,6 +62,22 @@ std::optional parse (std::string_view name, bool ignore_reserved = true) } } +/** + * Same as `magic_enum::enum_cast (...)` but ignores reserved values (starting with underscore) by default. + * Case insensitive. + * @throws std::invalid_argument if the name is not found + */ +template +E parse (std::string_view name, bool ignore_reserved = true) +{ + auto value = try_parse (name, ignore_reserved); + if (value) + { + return *value; + } + throw std::invalid_argument ("Invalid value of " + magic_enum::enum_type_name () + ": \"" + std::string (name) + "\""); +} + template T cast (S value) { diff --git a/nano/lib/logging_enums.cpp b/nano/lib/logging_enums.cpp index c6b59b7fce..64f5b3d55d 100644 --- a/nano/lib/logging_enums.cpp +++ b/nano/lib/logging_enums.cpp @@ -29,45 +29,35 @@ const std::vector & nano::log::all_types () nano::log::level nano::log::parse_level (std::string_view name) { - auto value = nano::enum_util::parse (name); + auto value = nano::enum_util::try_parse (name); if (value.has_value ()) { return value.value (); } - else - { - auto all_levels_str = nano::util::join (nano::log::all_levels (), ", ", [] (auto const & lvl) { - return to_string (lvl); - }); - - throw std::invalid_argument ("Invalid log level: " + std::string (name) + ". Must be one of: " + all_levels_str); - } + auto all_levels_str = nano::util::join (nano::log::all_levels (), ", ", [] (auto const & lvl) { + return to_string (lvl); + }); + throw std::invalid_argument ("Invalid log level: " + std::string (name) + ". Must be one of: " + all_levels_str); } nano::log::type nano::log::parse_type (std::string_view name) { - auto value = nano::enum_util::parse (name); + auto value = nano::enum_util::try_parse (name); if (value.has_value ()) { return value.value (); } - else - { - throw std::invalid_argument ("Invalid log type: " + std::string (name)); - } + throw std::invalid_argument ("Invalid log type: " + std::string (name)); } nano::log::detail nano::log::parse_detail (std::string_view name) { - auto value = nano::enum_util::parse (name); + auto value = nano::enum_util::try_parse (name); if (value.has_value ()) { return value.value (); } - else - { - throw std::invalid_argument ("Invalid log detail: " + std::string (name)); - } + throw std::invalid_argument ("Invalid log detail: " + std::string (name)); } std::string_view nano::log::to_string (nano::log::tracing_format format) @@ -77,19 +67,15 @@ std::string_view nano::log::to_string (nano::log::tracing_format format) nano::log::tracing_format nano::log::parse_tracing_format (std::string_view name) { - auto value = nano::enum_util::parse (name); + auto value = nano::enum_util::try_parse (name); if (value.has_value ()) { return value.value (); } - else - { - auto all_formats_str = nano::util::join (nano::log::all_tracing_formats (), ", ", [] (auto const & fmt) { - return to_string (fmt); - }); - - throw std::invalid_argument ("Invalid tracing format: " + std::string (name) + ". Must be one of: " + all_formats_str); - } + auto all_formats_str = nano::util::join (nano::log::all_tracing_formats (), ", ", [] (auto const & fmt) { + return to_string (fmt); + }); + throw std::invalid_argument ("Invalid tracing format: " + std::string (name) + ". Must be one of: " + all_formats_str); } const std::vector & nano::log::all_tracing_formats () From b07bb857f98a2eca438bfb3bf15494f6d26cc41d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sun, 5 May 2024 12:19:08 +0200 Subject: [PATCH 10/13] Enum casting --- nano/lib/enum_utils.hpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/nano/lib/enum_utils.hpp b/nano/lib/enum_utils.hpp index 524281d7d5..b9ee7c3bb2 100644 --- a/nano/lib/enum_utils.hpp +++ b/nano/lib/enum_utils.hpp @@ -78,9 +78,24 @@ E parse (std::string_view name, bool ignore_reserved = true) throw std::invalid_argument ("Invalid value of " + magic_enum::enum_type_name () + ": \"" + std::string (name) + "\""); } +template +consteval void ensure_all_castable () +{ + for (auto value : magic_enum::enum_values ()) + { + if (!magic_enum::enum_cast (magic_enum::enum_name (value))) + { + // If this fails, it means that the target enum is missing a value present in the source enum + throw std::logic_error ("Value of " + std::string{ magic_enum::enum_type_name () } + " (" + std::string{ magic_enum::enum_name (value) } + ") cannot be cast to " + std::string{ magic_enum::enum_type_name () }); + } + } +} + template T cast (S value) { + ensure_all_castable (); + auto conv = magic_enum::enum_cast (nano::enum_util::name (value)); debug_assert (conv); return conv.value_or (T{}); From ba3016e8add289010b009ab3c2202382e15f6f02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sun, 5 May 2024 13:52:28 +0200 Subject: [PATCH 11/13] Tests --- nano/core_test/enums.cpp | 63 ++++++++++++++++++++++++++++++++++++++++ nano/lib/enum_utils.hpp | 2 +- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/nano/core_test/enums.cpp b/nano/core_test/enums.cpp index 53b25a6d33..41c8acaa98 100644 --- a/nano/core_test/enums.cpp +++ b/nano/core_test/enums.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -62,4 +63,66 @@ TEST (enums, log_category) ASSERT_FALSE (to_string (nano::log::type::_last).empty ()); ASSERT_NO_THROW (std::string{ to_string (nano::log::type::_last) }); ASSERT_EQ (to_string (nano::log::type::_last), "_last"); +} + +namespace +{ +enum class test_enum +{ + _invalid, + one, + two, + three, + _last +}; + +enum class test_enum2 +{ + one, +}; +} + +TEST (enum_util, name) +{ + ASSERT_EQ (nano::enum_util::name (test_enum::_invalid), "_invalid"); + ASSERT_EQ (nano::enum_util::name (test_enum::one), "one"); + ASSERT_EQ (nano::enum_util::name (test_enum::two), "two"); + ASSERT_EQ (nano::enum_util::name (test_enum::three), "three"); + ASSERT_EQ (nano::enum_util::name (test_enum::_last), "_last"); +} + +TEST (enum_util, values) +{ + auto values = nano::enum_util::values (); + ASSERT_EQ (values.size (), 3); + ASSERT_EQ (values[0], test_enum::one); + ASSERT_EQ (values[1], test_enum::two); + ASSERT_EQ (values[2], test_enum::three); + + auto all_values = nano::enum_util::values (/* don't ignore reserved */ false); + ASSERT_EQ (all_values.size (), 5); + ASSERT_EQ (all_values[0], test_enum::_invalid); + ASSERT_EQ (all_values[1], test_enum::one); + ASSERT_EQ (all_values[2], test_enum::two); + ASSERT_EQ (all_values[3], test_enum::three); + ASSERT_EQ (all_values[4], test_enum::_last); +} + +TEST (enum_util, parse) +{ + ASSERT_EQ (nano::enum_util::try_parse ("one"), test_enum::one); + ASSERT_EQ (nano::enum_util::try_parse ("two"), test_enum::two); + ASSERT_EQ (nano::enum_util::try_parse ("three"), test_enum::three); + ASSERT_FALSE (nano::enum_util::try_parse ("four").has_value ()); + ASSERT_FALSE (nano::enum_util::try_parse ("_invalid").has_value ()); + ASSERT_FALSE (nano::enum_util::try_parse ("_last").has_value ()); + + ASSERT_NO_THROW (nano::enum_util::parse ("one")); + ASSERT_THROW (nano::enum_util::parse ("four"), std::invalid_argument); + ASSERT_THROW (nano::enum_util::parse ("_invalid"), std::invalid_argument); +} + +TEST (enum_util, cast) +{ + ASSERT_EQ (nano::enum_util::cast (test_enum2::one), test_enum::one); } \ No newline at end of file diff --git a/nano/lib/enum_utils.hpp b/nano/lib/enum_utils.hpp index b9ee7c3bb2..8e7eddc769 100644 --- a/nano/lib/enum_utils.hpp +++ b/nano/lib/enum_utils.hpp @@ -75,7 +75,7 @@ E parse (std::string_view name, bool ignore_reserved = true) { return *value; } - throw std::invalid_argument ("Invalid value of " + magic_enum::enum_type_name () + ": \"" + std::string (name) + "\""); + throw std::invalid_argument ("Invalid value of " + std::string{ magic_enum::enum_type_name () } + ": \"" + std::string{ name } + "\""); } template From 9844e79f2b0055aac66bf953ec16d64cc0d6e3e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sun, 5 May 2024 13:58:13 +0200 Subject: [PATCH 12/13] Naming consistency --- nano/core_test/enums.cpp | 2 +- nano/lib/block_type.cpp | 2 +- nano/lib/blocks.cpp | 2 +- nano/lib/{enum_utils.hpp => enum_util.hpp} | 0 nano/lib/logging.cpp | 2 +- nano/lib/logging_enums.cpp | 2 +- nano/lib/stats_enums.cpp | 2 +- nano/lib/thread_roles.cpp | 2 +- nano/node/active_transactions.hpp | 2 +- nano/node/blockprocessor.cpp | 2 +- nano/node/election.cpp | 2 +- nano/node/messages.cpp | 2 +- nano/node/rep_tiers.cpp | 2 +- nano/node/transport/message_deserializer.cpp | 2 +- nano/node/transport/socket.cpp | 2 +- nano/node/transport/tcp_listener.cpp | 2 +- nano/secure/common.cpp | 2 +- 17 files changed, 16 insertions(+), 16 deletions(-) rename nano/lib/{enum_utils.hpp => enum_util.hpp} (100%) diff --git a/nano/core_test/enums.cpp b/nano/core_test/enums.cpp index 41c8acaa98..a9527fc6de 100644 --- a/nano/core_test/enums.cpp +++ b/nano/core_test/enums.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/nano/lib/block_type.cpp b/nano/lib/block_type.cpp index de1ff9147a..70ec28b40c 100644 --- a/nano/lib/block_type.cpp +++ b/nano/lib/block_type.cpp @@ -1,5 +1,5 @@ #include -#include +#include std::string_view nano::to_string (nano::block_type type) { diff --git a/nano/lib/blocks.cpp b/nano/lib/blocks.cpp index b45c5613f0..a4c21b8678 100644 --- a/nano/lib/blocks.cpp +++ b/nano/lib/blocks.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include diff --git a/nano/lib/enum_utils.hpp b/nano/lib/enum_util.hpp similarity index 100% rename from nano/lib/enum_utils.hpp rename to nano/lib/enum_util.hpp diff --git a/nano/lib/logging.cpp b/nano/lib/logging.cpp index b86e980d61..da7f6e8dc3 100644 --- a/nano/lib/logging.cpp +++ b/nano/lib/logging.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/nano/lib/logging_enums.cpp b/nano/lib/logging_enums.cpp index 64f5b3d55d..8ef883e21a 100644 --- a/nano/lib/logging_enums.cpp +++ b/nano/lib/logging_enums.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/nano/lib/stats_enums.cpp b/nano/lib/stats_enums.cpp index e297a6d6fe..d9f74084e5 100644 --- a/nano/lib/stats_enums.cpp +++ b/nano/lib/stats_enums.cpp @@ -1,4 +1,4 @@ -#include +#include #include std::string_view nano::to_string (nano::stat::type type) diff --git a/nano/lib/thread_roles.cpp b/nano/lib/thread_roles.cpp index 9124d94a20..32dc0e2ba1 100644 --- a/nano/lib/thread_roles.cpp +++ b/nano/lib/thread_roles.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/nano/node/active_transactions.hpp b/nano/node/active_transactions.hpp index b91d8515ae..dcff01a678 100644 --- a/nano/node/active_transactions.hpp +++ b/nano/node/active_transactions.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include #include diff --git a/nano/node/blockprocessor.cpp b/nano/node/blockprocessor.cpp index 304c728501..c5ec31dca7 100644 --- a/nano/node/blockprocessor.cpp +++ b/nano/node/blockprocessor.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/nano/node/election.cpp b/nano/node/election.cpp index 5a6874e35a..e588352a2f 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/nano/node/messages.cpp b/nano/node/messages.cpp index 25170c583d..5a7bb54996 100644 --- a/nano/node/messages.cpp +++ b/nano/node/messages.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include diff --git a/nano/node/rep_tiers.cpp b/nano/node/rep_tiers.cpp index ae10536bbe..f8593ba48f 100644 --- a/nano/node/rep_tiers.cpp +++ b/nano/node/rep_tiers.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/nano/node/transport/message_deserializer.cpp b/nano/node/transport/message_deserializer.cpp index 2013552c8d..52d725b54f 100644 --- a/nano/node/transport/message_deserializer.cpp +++ b/nano/node/transport/message_deserializer.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/nano/node/transport/socket.cpp b/nano/node/transport/socket.cpp index 7fd2cbe75f..4176bf2ca9 100644 --- a/nano/node/transport/socket.cpp +++ b/nano/node/transport/socket.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include diff --git a/nano/node/transport/tcp_listener.cpp b/nano/node/transport/tcp_listener.cpp index 162729b2cc..044f3b5c4d 100644 --- a/nano/node/transport/tcp_listener.cpp +++ b/nano/node/transport/tcp_listener.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/nano/secure/common.cpp b/nano/secure/common.cpp index 059ea8ef31..28952a4c8c 100644 --- a/nano/secure/common.cpp +++ b/nano/secure/common.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include From 264ce72637f508a42997bbfaa306fff9afe8ec6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sun, 5 May 2024 15:48:58 +0200 Subject: [PATCH 13/13] Fix --- nano/core_test/enums.cpp | 2 +- nano/lib/enum_util.hpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/nano/core_test/enums.cpp b/nano/core_test/enums.cpp index a9527fc6de..0ea1c71443 100644 --- a/nano/core_test/enums.cpp +++ b/nano/core_test/enums.cpp @@ -99,7 +99,7 @@ TEST (enum_util, values) ASSERT_EQ (values[1], test_enum::two); ASSERT_EQ (values[2], test_enum::three); - auto all_values = nano::enum_util::values (/* don't ignore reserved */ false); + auto all_values = nano::enum_util::values (); ASSERT_EQ (all_values.size (), 5); ASSERT_EQ (all_values[0], test_enum::_invalid); ASSERT_EQ (all_values[1], test_enum::one); diff --git a/nano/lib/enum_util.hpp b/nano/lib/enum_util.hpp index 8e7eddc769..2190a3c165 100644 --- a/nano/lib/enum_util.hpp +++ b/nano/lib/enum_util.hpp @@ -26,12 +26,12 @@ std::string_view name (auto value) } /** - * Same as `magic_enum::enum_values (...)` but ignores reserved values (starting with underscore) by default. + * Same as `magic_enum::enum_values (...)` but ignores reserved values (starting with underscore). */ -template -std::vector const & values (bool ignore_reserved = true) +template +std::vector const & values () { - static std::vector all = [ignore_reserved] () { + static std::vector all = [] () { std::vector result; for (auto const & [val, name] : magic_enum::enum_entries ()) {