From 172c6855479ac210e508880f4d32dc78c71a926e Mon Sep 17 00:00:00 2001 From: terik23 Date: Thu, 25 Jul 2019 00:37:59 +0500 Subject: [PATCH] improve name_impl, ostream_operators --- include/magic_enum.hpp | 16 ++++++++++------ test/test.cpp | 14 +++++++------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index cdf85c3..60e255b 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -147,11 +147,11 @@ template inline constexpr auto strings_v = strings_impl(range_v); template -[[nodiscard]] constexpr std::string_view name_impl(int value) noexcept { +[[nodiscard]] constexpr std::string_view name_impl(E value) noexcept { static_assert(std::is_enum_v, "magic_enum::detail::name_impl requires enum type."); constexpr auto strings = strings_v; - if (auto i = static_cast((value - min_v)); i < strings.size()) { + if (auto i = static_cast((static_cast(value) - min_v)); i < strings.size()) { return strings[i]; } @@ -291,7 +291,7 @@ template > [[nodiscard]] constexpr std::optional enum_cast(std::underlying_type_t value) noexcept { static_assert(detail::check_enum_v, "magic_enum::enum_cast requires enum type."); - if (detail::name_impl(static_cast(value)).empty()) { + if (detail::name_impl(static_cast(value)).empty()) { return std::nullopt; // Invalid value or out of range. } @@ -349,7 +349,7 @@ template > [[nodiscard]] constexpr std::string_view enum_name(E value) noexcept { static_assert(detail::check_enum_v, "magic_enum::enum_name requires enum type."); - return detail::name_impl(static_cast(value)); + return detail::name_impl(value); } // Obtains string enum name sequence. @@ -378,10 +378,12 @@ template & operator<<(std::basic_ostream& os, E value) { static_assert(detail::check_enum_v, "magic_enum::ostream_operators::operator<< requires enum type."); - if (auto name = detail::name_impl(static_cast(value)); !name.empty()) { + if (auto name = detail::name_impl(value); !name.empty()) { for (auto c : name) { os.put(c); } + } else { + os << static_cast>(value); } return os; @@ -392,10 +394,12 @@ std::basic_ostream& operator<<(std::basic_ostream& o static_assert(detail::check_enum_v, "magic_enum::ostream_operators::operator<< requires enum type."); if (value.has_value()) { - if (auto name = detail::name_impl(static_cast(value.value())); !name.empty()) { + if (auto name = detail::name_impl(value.value()); !name.empty()) { for (auto c : name) { os.put(c); } + } else { + os << static_cast>(value.value()); } } diff --git a/test/test.cpp b/test/test.cpp index 06c5b4a..8770816 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -33,7 +33,7 @@ enum class Color { RED = -12, GREEN = 7, BLUE = 15 }; -enum class Numbers : char { one = 10, two = 20, three = 30, many = 127 }; +enum class Numbers : int { one = 10, two = 20, three = 30, many = 127 }; enum Directions { Up = 85, Down = -42, Right = 120, Left = -120 }; @@ -302,25 +302,25 @@ TEST_CASE("ostream_operators") { test_ostream(Color::RED, "RED"); test_ostream(Color::GREEN, "GREEN"); test_ostream(Color::BLUE, "BLUE"); - test_ostream(static_cast(0), ""); + test_ostream(static_cast(0), "0"); test_ostream(Numbers::one, "one"); test_ostream(Numbers::two, "two"); test_ostream(Numbers::three, "three"); - test_ostream(Numbers::many, ""); - test_ostream(static_cast(0), ""); + test_ostream(Numbers::many, "127"); + test_ostream(static_cast(0), "0"); test_ostream(Directions::Up, "Up"); test_ostream(Directions::Down, "Down"); test_ostream(Directions::Right, "Right"); test_ostream(Directions::Left, "Left"); - test_ostream(static_cast(0), ""); + test_ostream(static_cast(0), "0"); test_ostream(number::one, "one"); test_ostream(number::two, "two"); test_ostream(number::three, "three"); - test_ostream(number::four, ""); - test_ostream(static_cast(0), ""); + test_ostream(number::four, "400"); + test_ostream(static_cast(0), "0"); } TEST_CASE("bitwise_operators") {