mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
v0.2.0
This commit is contained in:
parent
122aa8028d
commit
5a3d4b53e6
5 changed files with 105 additions and 70 deletions
30
README.md
30
README.md
|
|
@ -16,13 +16,15 @@
|
|||
[](https://travis-ci.org/Neargye/magic_enum)
|
||||
[](https://ci.appveyor.com/project/Neargye/magic-enum-hf8vk/branch/master)
|
||||
[](https://www.codacy.com/app/Neargye/magic_enum?utm_source=github.com&utm_medium=referral&utm_content=Neargye/magic_enum&utm_campaign=Badge_Grade)
|
||||
[](https://wandbox.org/permlink/gkjJ86ur57I3KOO6)
|
||||
[](https://wandbox.org/permlink/uCF1Op6ZgSI6cDJS)
|
||||
|
||||
## What is Magic Enum?
|
||||
|
||||
Header-only C++17 library provides Enum-to-String and String-to-Enum functions.
|
||||
* `magic_enum::enum_to_string` obtains string enum name from enum variable.
|
||||
* `magic_enum::enum_from_string` obtains enum variable from enum string name.
|
||||
* `magic_enum::enum_from_string` obtains enum value from enum string name.
|
||||
* `magic_enum::enum_to_sequence` obtains value enum sequence.
|
||||
* `magic_enum::enum_to_string_sequence` obtains string enum name sequence.
|
||||
|
||||
## Features
|
||||
|
||||
|
|
@ -53,7 +55,7 @@ Header-only C++17 library provides Enum-to-String and String-to-Enum functions.
|
|||
}
|
||||
```
|
||||
|
||||
* String enum name to enum variable
|
||||
* String enum name to enum value
|
||||
```cpp
|
||||
constexpr auto color = magic_enum::enum_from_string<Color>("GREEN");
|
||||
if (color.has_value()) {
|
||||
|
|
@ -61,13 +63,29 @@ Header-only C++17 library provides Enum-to-String and String-to-Enum functions.
|
|||
}
|
||||
```
|
||||
|
||||
* Enum to value sequence
|
||||
```cpp
|
||||
constexpr auto colors = magic_enum::enum_to_sequence<Color>();
|
||||
// colors -> {Color::RED, Color::BLUE, Color::GREEN}
|
||||
```
|
||||
|
||||
* Enum to string enum name sequence
|
||||
```cpp
|
||||
constexpr auto color_names = magic_enum::enum_to_string_sequence<Color>();
|
||||
// color_names -> {"RED", "BLUE", "GREEN"}
|
||||
```
|
||||
|
||||
## Remarks
|
||||
|
||||
* `magic_enum::enum_to_string` returns `std::optional<std::string_view>`, using `has_value()` to check contains enum name and `value()` to get the enum name. If enum variable does not have name or out of range `MAGIC_ENUM_RANGE`, returns `std::nullopt`.
|
||||
* `magic_enum::enum_to_string` returns `std::optional<std::string_view>`, using `has_value()` to check contains enum name and `value()` to get the enum name. If enum value does not have name or out of range `MAGIC_ENUM_RANGE`, returns `std::nullopt`.
|
||||
|
||||
* `magic_enum::enum_from_string` returns `std::optional<E>`, using `has_value()` to check contains enum variable and `value()` to get the enum variable. If enum variable does not have name or out of range `MAGIC_ENUM_RANGE`, returns `std::nullopt`.
|
||||
* `magic_enum::enum_from_string` returns `std::optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value. If enum value does not have name or out of range `MAGIC_ENUM_RANGE`, returns `std::nullopt`.
|
||||
|
||||
* Enum variable must be in range `(-MAGIC_ENUM_RANGE, MAGIC_ENUM_RANGE)`. By default `MAGIC_ENUM_RANGE = 128`. If you need larger range, redefine the macro `MAGIC_ENUM_RANGE`.
|
||||
* `magic_enum::enum_to_sequence` returns `std::array<E, N>` with all value enum, sorted by enum value.
|
||||
|
||||
* `magic_enum::enum_to_string_sequence` returns `std::array<std::string_view, N>` with all string enum name, sorted by enum value.
|
||||
|
||||
* Enum value must be in range `(-MAGIC_ENUM_RANGE, MAGIC_ENUM_RANGE)`. By default `MAGIC_ENUM_RANGE = 128`. If you need larger range, redefine the macro `MAGIC_ENUM_RANGE`.
|
||||
```cpp
|
||||
#define MAGIC_ENUM_RANGE 1028 // Redefine MAGIC_ENUM_RANGE for larger range.
|
||||
#include <magic_enum.hpp>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue