From 641bfa509bd24a4b78f0819c262355687e82844c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sat, 23 Mar 2024 12:20:55 +0100 Subject: [PATCH] Stat enums string conversions --- nano/core_test/enums.cpp | 30 ++++++++++++++--------------- nano/lib/stats.cpp | 6 +++--- nano/lib/stats_enums.cpp | 31 +++++++++++++++++++++++++++--- nano/lib/stats_enums.hpp | 6 +++++- nano/test_common/rate_observer.cpp | 2 +- 5 files changed, 52 insertions(+), 23 deletions(-) diff --git a/nano/core_test/enums.cpp b/nano/core_test/enums.cpp index 53b25a6d33..427d791dc3 100644 --- a/nano/core_test/enums.cpp +++ b/nano/core_test/enums.cpp @@ -6,32 +6,32 @@ TEST (enums, stat_type) { - ASSERT_FALSE (nano::to_string (static_cast (0)).empty ()); - ASSERT_NO_THROW (std::string{ nano::to_string (static_cast (0)) }); + ASSERT_FALSE (to_string (static_cast (0)).empty ()); + ASSERT_NO_THROW (std::string{ to_string (static_cast (0)) }); - ASSERT_FALSE (nano::to_string (nano::stat::type::_last).empty ()); - ASSERT_NO_THROW (std::string{ nano::to_string (nano::stat::type::_last) }); - ASSERT_EQ (nano::to_string (nano::stat::type::_last), "_last"); + ASSERT_FALSE (to_string (nano::stat::type::_last).empty ()); + ASSERT_NO_THROW (std::string{ to_string (nano::stat::type::_last) }); + ASSERT_EQ (to_string (nano::stat::type::_last), "_last"); } TEST (enums, stat_detail) { - ASSERT_FALSE (nano::to_string (static_cast (0)).empty ()); - ASSERT_NO_THROW (std::string{ nano::to_string (static_cast (0)) }); + ASSERT_FALSE (to_string (static_cast (0)).empty ()); + ASSERT_NO_THROW (std::string{ to_string (static_cast (0)) }); - ASSERT_FALSE (nano::to_string (nano::stat::detail::_last).empty ()); - ASSERT_NO_THROW (std::string{ nano::to_string (nano::stat::detail::_last) }); - ASSERT_EQ (nano::to_string (nano::stat::detail::_last), "_last"); + ASSERT_FALSE (to_string (nano::stat::detail::_last).empty ()); + ASSERT_NO_THROW (std::string{ to_string (nano::stat::detail::_last) }); + ASSERT_EQ (to_string (nano::stat::detail::_last), "_last"); } TEST (enums, stat_dir) { - ASSERT_FALSE (nano::to_string (static_cast (0)).empty ()); - ASSERT_NO_THROW (std::string{ nano::to_string (static_cast (0)) }); + ASSERT_FALSE (to_string (static_cast (0)).empty ()); + ASSERT_NO_THROW (std::string{ to_string (static_cast (0)) }); - ASSERT_FALSE (nano::to_string (nano::stat::dir::_last).empty ()); - ASSERT_NO_THROW (std::string{ nano::to_string (nano::stat::dir::_last) }); - ASSERT_EQ (nano::to_string (nano::stat::dir::_last), "_last"); + ASSERT_FALSE (to_string (nano::stat::dir::_last).empty ()); + ASSERT_NO_THROW (std::string{ to_string (nano::stat::dir::_last) }); + ASSERT_EQ (to_string (nano::stat::dir::_last), "_last"); } TEST (enums, log_type) diff --git a/nano/lib/stats.cpp b/nano/lib/stats.cpp index 6085581b43..df7dd3d50f 100644 --- a/nano/lib/stats.cpp +++ b/nano/lib/stats.cpp @@ -495,19 +495,19 @@ void nano::stats::clear () std::string nano::stats::type_to_string (uint32_t key) { auto type = static_cast (key >> 16 & 0x000000ff); - return std::string{ nano::to_string (type) }; + return std::string{ nano::stat::to_string (type) }; } std::string nano::stats::detail_to_string (uint32_t key) { auto detail = static_cast (key >> 8 & 0x000000ff); - return std::string{ nano::to_string (detail) }; + return std::string{ nano::stat::to_string (detail) }; } std::string nano::stats::dir_to_string (uint32_t key) { auto dir = static_cast (key & 0x000000ff); - return std::string{ nano::to_string (dir) }; + return std::string{ nano::stat::to_string (dir) }; } nano::stat_datapoint::stat_datapoint (stat_datapoint const & other_a) diff --git a/nano/lib/stats_enums.cpp b/nano/lib/stats_enums.cpp index 8da30854e9..c4a4317c91 100644 --- a/nano/lib/stats_enums.cpp +++ b/nano/lib/stats_enums.cpp @@ -1,18 +1,43 @@ #include +#include #include -std::string_view nano::to_string (nano::stat::type type) +std::string_view nano::stat::to_string (nano::stat::type type) { return magic_enum::enum_name (type); } -std::string_view nano::to_string (nano::stat::detail detail) +std::string_view nano::stat::to_string (nano::stat::detail detail) { return magic_enum::enum_name (detail); } -std::string_view nano::to_string (nano::stat::dir dir) +std::string_view nano::stat::to_string (nano::stat::dir dir) { return magic_enum::enum_name (dir); +} + +std::vector const & nano::stat::all_types () +{ + static std::vector all = [] () { + return nano::util::enum_values (); + }(); + return all; +} + +std::vector const & nano::stat::all_details () +{ + static std::vector all = [] () { + return nano::util::enum_values (); + }(); + return all; +} + +std::vector const & nano::stat::all_dirs () +{ + static std::vector all = [] () { + return nano::util::enum_values (); + }(); + return all; } \ No newline at end of file diff --git a/nano/lib/stats_enums.hpp b/nano/lib/stats_enums.hpp index 01bf4b62d1..772eb53c3b 100644 --- a/nano/lib/stats_enums.hpp +++ b/nano/lib/stats_enums.hpp @@ -388,11 +388,15 @@ enum class dir : uint8_t }; } -namespace nano +namespace nano::stat { std::string_view to_string (stat::type); std::string_view to_string (stat::detail); std::string_view to_string (stat::dir); + +std::vector const & all_types (); +std::vector const & all_details (); +std::vector const & all_dirs (); } // Ensure that the enum_range is large enough to hold all values (including future ones) diff --git a/nano/test_common/rate_observer.cpp b/nano/test_common/rate_observer.cpp index 933b5a2e24..4e6407a346 100644 --- a/nano/test_common/rate_observer.cpp +++ b/nano/test_common/rate_observer.cpp @@ -98,7 +98,7 @@ void nano::test::rate_observer::observe (std::string name, std::function