1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-09 23:34:23 +00:00

update readme

This commit is contained in:
neargye 2019-05-02 19:51:02 +05:00
parent 34fb3ba22a
commit 18bb7ae1bd

View file

@ -56,13 +56,6 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
// color_name -> "RED"
```
* Static storage enum variable to string
```cpp
constexpr Color color = Color::BLUE;
constexpr auto color_name = magic_enum::enum_name(color);
// color_name -> "BLUE"
```
* String to enum value
```cpp
std::string color_name{"GREEN"};
@ -72,14 +65,6 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
}
```
* Static storage string to enum value
```cpp
constexpr auto color = magic_enum::enum_cast<Color>("BLUE");
if (color.has_value()) {
// color.value() -> Color::BLUE
}
```
* Integer to enum value
```cpp
int color_integer = 2;
@ -89,14 +74,6 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
}
```
* Static storage integer to enum value
```cpp
constexpr auto color = magic_enum::enum_cast<Color>(4);
if (color.has_value()) {
// color.value() -> Color::BLUE
}
```
* Indexed access to enum value
```cpp
int i = 1;
@ -178,6 +155,13 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
magic_enum::is_scoped_enum_v<direction> -> true
```
* Static storage enum variable to string
```cpp
constexpr Color color = Color::BLUE;
constexpr auto color_name = magic_enum::enum_name<color>();
// color_name -> "BLUE"
```
## Remarks
* `magic_enum::enum_cast` returns `std::optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value.
@ -188,6 +172,8 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
* `magic_enum::enum_name` returns `std::string_view`. If enum value does not have name, returns empty string.
* `magic_enum::enum_name<value>()` is much lighter on the compile times and is not restricted to the enum_range limitation.
* `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.
* `magic_enum::enum_entries` returns `std::array<std::pair<E, std::string_view>, N>` with all std::pair (value enum, string enum name) where `N = number of enum values`, sorted by enum value.