mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-10 23:44:29 +00:00
enum_name returns string_view
This commit is contained in:
parent
8ddfae18d1
commit
9dc22008a7
4 changed files with 25 additions and 36 deletions
10
README.md
10
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<E, N>` with all enum value where `N = number of enum values`, sorted by enum value.
|
||||
|
||||
* `magic_enum::enum_name` returns `std::optional<std::string_view>`, 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<std::string_view, N>` with all string enum name where `N = number of enum values`, sorted by enum value.
|
||||
|
||||
|
|
|
|||
|
|
@ -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<Color>();
|
||||
|
|
|
|||
|
|
@ -295,15 +295,10 @@ template <typename E, typename = detail::enable_if_enum_t<E>>
|
|||
|
||||
// Obtains string enum name from enum value.
|
||||
template <typename E, typename D = std::decay_t<E>, typename = detail::enable_if_enum_t<D>>
|
||||
[[nodiscard]] constexpr std::optional<std::string_view> enum_name(E value) noexcept {
|
||||
[[nodiscard]] constexpr std::string_view enum_name(E value) noexcept {
|
||||
static_assert(std::is_enum_v<D>, "magic_enum::enum_name requires enum type.");
|
||||
const auto name = detail::name_impl<D>(static_cast<int>(value));
|
||||
|
||||
if (name.empty()) {
|
||||
return std::nullopt; // Invalid value or out of range.
|
||||
} else {
|
||||
return name;
|
||||
}
|
||||
return detail::name_impl<D>(static_cast<int>(value));
|
||||
}
|
||||
|
||||
// Obtains string enum name sequence.
|
||||
|
|
|
|||
|
|
@ -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<Color>(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<Color>(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<Numbers>(0)).has_value());
|
||||
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Numbers>(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<Numbers>(0)).empty());
|
||||
REQUIRE(magic_enum::enum_name(static_cast<Numbers>(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<Directions>(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<Directions>(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<number>(0)).has_value());
|
||||
REQUIRE_FALSE(magic_enum::enum_name(static_cast<number>(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<number>(0)).empty());
|
||||
REQUIRE(magic_enum::enum_name(static_cast<number>(400)).empty());
|
||||
}
|
||||
|
||||
TEST_CASE("enum_names") {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue