From 9dc22008a7c8a207e4131c8415073b3b6017a474 Mon Sep 17 00:00:00 2001 From: terik23 Date: Sun, 21 Apr 2019 20:52:14 +0500 Subject: [PATCH] enum_name returns string_view --- README.md | 10 +++------- example/example.cpp | 4 +--- include/magic_enum.hpp | 9 ++------- test/test.cpp | 38 +++++++++++++++++++------------------- 4 files changed, 25 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 9ecf8c3..e8698d3 100644 --- a/README.md +++ b/README.md @@ -53,18 +53,14 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 }; ```cpp Color color = Color::RED; auto color_name = magic_enum::enum_name(color); - if (color_name.has_value()) { - // color_name.value() -> "RED" - } + // color_name -> "RED" ``` * Static storage enum variable to string ```cpp constexpr Color color = Color::BLUE; constexpr auto color_name = magic_enum::enum_name(color); - if (color_name.has_value()) { - // color_name.value() -> "BLUE" - } + // color_name -> "BLUE" ``` * String to enum value @@ -173,7 +169,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 }; * `magic_enum::enum_values` returns `std::array` with all enum value where `N = number of enum values`, sorted by enum value. -* `magic_enum::enum_name` returns `std::optional`, using `has_value()` to check contains enum name and `value()` to get the enum name. +* `magic_enum::enum_name` returns `std::string_view`. If enum value does not have name, returns empty string. * `magic_enum::enum_names` returns `std::array` with all string enum name where `N = number of enum values`, sorted by enum value. diff --git a/example/example.cpp b/example/example.cpp index 5a84899..93ec1af 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -30,9 +30,7 @@ int main() { // Enum variable to string name. Color c1 = Color::RED; auto c1_name = magic_enum::enum_name(c1); - if (c1_name.has_value()) { - std::cout << c1_name.value() << std::endl; // RED - } + std::cout << c1_name << std::endl; // RED // String enum name sequence. constexpr auto color_names = magic_enum::enum_names(); diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index a3cf83e..005f81d 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -295,15 +295,10 @@ template > // Obtains string enum name from enum value. template , typename = detail::enable_if_enum_t> -[[nodiscard]] constexpr std::optional enum_name(E value) noexcept { +[[nodiscard]] constexpr std::string_view enum_name(E value) noexcept { static_assert(std::is_enum_v, "magic_enum::enum_name requires enum type."); - const auto name = detail::name_impl(static_cast(value)); - if (name.empty()) { - return std::nullopt; // Invalid value or out of range. - } else { - return name; - } + return detail::name_impl(static_cast(value)); } // Obtains string enum name sequence. diff --git a/test/test.cpp b/test/test.cpp index 0b29562..fe33d5e 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -170,34 +170,34 @@ TEST_CASE("enum_name") { constexpr Color cr = Color::RED; constexpr auto cr_name = magic_enum::enum_name(cr); Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE}; - REQUIRE(cr_name.value() == "RED"); - REQUIRE(magic_enum::enum_name(Color::BLUE).value() == "BLUE"); - REQUIRE(magic_enum::enum_name(cm[1]).value() == "GREEN"); - REQUIRE_FALSE(magic_enum::enum_name(static_cast(0)).has_value()); + REQUIRE(cr_name == "RED"); + REQUIRE(magic_enum::enum_name(Color::BLUE) == "BLUE"); + REQUIRE(magic_enum::enum_name(cm[1]) == "GREEN"); + REQUIRE(magic_enum::enum_name(static_cast(0)).empty()); constexpr Numbers no = Numbers::one; constexpr auto no_name = magic_enum::enum_name(no); - REQUIRE(no_name.value() == "one"); - REQUIRE(magic_enum::enum_name(Numbers::two).value() == "two"); - REQUIRE(magic_enum::enum_name(Numbers::three).value() == "three"); - REQUIRE_FALSE(magic_enum::enum_name(static_cast(0)).has_value()); - REQUIRE_FALSE(magic_enum::enum_name(static_cast(127)).has_value()); + REQUIRE(no_name == "one"); + REQUIRE(magic_enum::enum_name(Numbers::two) == "two"); + REQUIRE(magic_enum::enum_name(Numbers::three) == "three"); + REQUIRE(magic_enum::enum_name(static_cast(0)).empty()); + REQUIRE(magic_enum::enum_name(static_cast(127)).empty()); constexpr Directions dr = Directions::Right; constexpr auto dr_name = magic_enum::enum_name(dr); - REQUIRE(magic_enum::enum_name(Directions::Up).value() == "Up"); - REQUIRE(magic_enum::enum_name(Directions::Down).value() == "Down"); - REQUIRE(dr_name.value() == "Right"); - REQUIRE(magic_enum::enum_name(Directions::Left).value() == "Left"); - REQUIRE_FALSE(magic_enum::enum_name(static_cast(0)).has_value()); + REQUIRE(magic_enum::enum_name(Directions::Up) == "Up"); + REQUIRE(magic_enum::enum_name(Directions::Down) == "Down"); + REQUIRE(dr_name == "Right"); + REQUIRE(magic_enum::enum_name(Directions::Left) == "Left"); + REQUIRE(magic_enum::enum_name(static_cast(0)).empty()); constexpr number nt = number::three; constexpr auto nt_name = magic_enum::enum_name(nt); - REQUIRE(magic_enum::enum_name(number::one).value() == "one"); - REQUIRE(magic_enum::enum_name(number::two).value() == "two"); - REQUIRE(nt_name.value() == "three"); - REQUIRE_FALSE(magic_enum::enum_name(static_cast(0)).has_value()); - REQUIRE_FALSE(magic_enum::enum_name(static_cast(400)).has_value()); + REQUIRE(magic_enum::enum_name(number::one) == "one"); + REQUIRE(magic_enum::enum_name(number::two) == "two"); + REQUIRE(nt_name == "three"); + REQUIRE(magic_enum::enum_name(static_cast(0)).empty()); + REQUIRE(magic_enum::enum_name(static_cast(400)).empty()); } TEST_CASE("enum_names") {