Skip to content

Commit

Permalink
Stat enums string conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 25, 2024
1 parent a24dd39 commit 641bfa5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 23 deletions.
30 changes: 15 additions & 15 deletions nano/core_test/enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@

TEST (enums, stat_type)
{
ASSERT_FALSE (nano::to_string (static_cast<nano::stat::type> (0)).empty ());
ASSERT_NO_THROW (std::string{ nano::to_string (static_cast<nano::stat::type> (0)) });
ASSERT_FALSE (to_string (static_cast<nano::stat::type> (0)).empty ());
ASSERT_NO_THROW (std::string{ to_string (static_cast<nano::stat::type> (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<nano::stat::detail> (0)).empty ());
ASSERT_NO_THROW (std::string{ nano::to_string (static_cast<nano::stat::detail> (0)) });
ASSERT_FALSE (to_string (static_cast<nano::stat::detail> (0)).empty ());
ASSERT_NO_THROW (std::string{ to_string (static_cast<nano::stat::detail> (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<nano::stat::dir> (0)).empty ());
ASSERT_NO_THROW (std::string{ nano::to_string (static_cast<nano::stat::dir> (0)) });
ASSERT_FALSE (to_string (static_cast<nano::stat::dir> (0)).empty ());
ASSERT_NO_THROW (std::string{ to_string (static_cast<nano::stat::dir> (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)
Expand Down
6 changes: 3 additions & 3 deletions nano/lib/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,19 +495,19 @@ void nano::stats::clear ()
std::string nano::stats::type_to_string (uint32_t key)
{
auto type = static_cast<stat::type> (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<stat::detail> (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<stat::dir> (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)
Expand Down
31 changes: 28 additions & 3 deletions nano/lib/stats_enums.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
#include <nano/lib/stats_enums.hpp>
#include <nano/lib/utility.hpp>

#include <magic_enum.hpp>

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<nano::stat::type> const & nano::stat::all_types ()
{
static std::vector<nano::stat::type> all = [] () {
return nano::util::enum_values<nano::stat::type> ();
}();
return all;
}

std::vector<nano::stat::detail> const & nano::stat::all_details ()
{
static std::vector<nano::stat::detail> all = [] () {
return nano::util::enum_values<nano::stat::detail> ();
}();
return all;
}

std::vector<nano::stat::dir> const & nano::stat::all_dirs ()
{
static std::vector<nano::stat::dir> all = [] () {
return nano::util::enum_values<nano::stat::dir> ();
}();
return all;
}
6 changes: 5 additions & 1 deletion nano/lib/stats_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<stat::type> const & all_types ();
std::vector<stat::detail> const & all_details ();
std::vector<stat::dir> const & all_dirs ();
}

// Ensure that the enum_range is large enough to hold all values (including future ones)
Expand Down
2 changes: 1 addition & 1 deletion nano/test_common/rate_observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void nano::test::rate_observer::observe (std::string name, std::function<int64_t

void nano::test::rate_observer::observe (nano::node & node, nano::stat::type type, nano::stat::detail detail, nano::stat::dir dir)
{
auto name = std::string{ nano::to_string (type) } + "::" + std::string{ nano::to_string (detail) } + "::" + std::string{ nano::to_string (dir) };
auto name = std::string{ to_string (type) } + "::" + std::string{ to_string (detail) } + "::" + std::string{ to_string (dir) };
observe (name, [&node, type, detail, dir] () {
return node.stats.count (type, detail, dir);
});
Expand Down

0 comments on commit 641bfa5

Please sign in to comment.