mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
v0.4.0
This commit is contained in:
parent
82a326876d
commit
11a0366421
5 changed files with 198 additions and 168 deletions
65
README.md
65
README.md
|
|
@ -16,17 +16,17 @@
|
|||
[](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/kXdow0AxI1Dss18p)
|
||||
[](https://wandbox.org/permlink/jPWeZxV1UcqvudZr)
|
||||
|
||||
## What is Magic Enum?
|
||||
|
||||
Header-only C++17 library provides Enum-to-String and String-to-Enum and other useful, functions work with any enum type without any macro or boilerplate code.
|
||||
* `yae::enum_cast` obtains enum value from string or integer.
|
||||
* `yae::enum_value` obtains indexed access to enum value.
|
||||
* `yae::enum_values` obtains enum value sequence.
|
||||
* `yae::enum_count` obtains number of enum values.
|
||||
* `yae::enum_name` obtains string name from enum value.
|
||||
* `yae::enum_names` obtains string enum name sequence.
|
||||
Header-only C++17 library provides static reflection on enums, work with any enum type without any macro or boilerplate code.
|
||||
* `enum_cast` obtains enum value from string or integer.
|
||||
* `enum_value` returns enum value at specified index.
|
||||
* `enum_values` obtains enum value sequence.
|
||||
* `enum_count` returns number of enum values.
|
||||
* `enum_name` obtains string name from enum value.
|
||||
* `enum_names` obtains string enum name sequence.
|
||||
|
||||
## Features
|
||||
|
||||
|
|
@ -34,6 +34,7 @@ Header-only C++17 library provides Enum-to-String and String-to-Enum and other u
|
|||
* Header-only
|
||||
* Dependency-free
|
||||
* Compile-time
|
||||
* Static reflection on enums
|
||||
* Enum to string
|
||||
* String to enum
|
||||
* Work with any enum type
|
||||
|
|
@ -49,7 +50,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* Enum value to string
|
||||
```cpp
|
||||
Color color = Color::RED;
|
||||
auto color_name = yae::enum_name(color);
|
||||
auto color_name = magic_enum::enum_name(color);
|
||||
if (color_name.has_value()) {
|
||||
// color_name.value() -> "RED"
|
||||
}
|
||||
|
|
@ -58,7 +59,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* Static storage enum variable to string
|
||||
```cpp
|
||||
constexpr Color color = Color::BLUE;
|
||||
constexpr auto color_name = yae::enum_name(color);
|
||||
constexpr auto color_name = magic_enum::enum_name(color);
|
||||
if (color_name.has_value()) {
|
||||
// color_name.value() -> "BLUE"
|
||||
}
|
||||
|
|
@ -67,7 +68,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* String to enum value
|
||||
```cpp
|
||||
std::string color_name{"GREEN"};
|
||||
auto color = yae::enum_cast<Color>(color_name);
|
||||
auto color = magic_enum::enum_cast<Color>(color_name);
|
||||
if (color.has_value()) {
|
||||
// color.value() -> Color::GREEN
|
||||
}
|
||||
|
|
@ -75,7 +76,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
|
||||
* Static storage string to enum value
|
||||
```cpp
|
||||
constexpr auto color = yae::enum_cast<Color>("BLUE");
|
||||
constexpr auto color = magic_enum::enum_cast<Color>("BLUE");
|
||||
if (color.has_value()) {
|
||||
// color.value() -> Color::BLUE
|
||||
}
|
||||
|
|
@ -84,7 +85,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* Integer to enum value
|
||||
```cpp
|
||||
int color_value = 2;
|
||||
auto color = yae::enum_cast<Color>(color_value);
|
||||
auto color = magic_enum::enum_cast<Color>(color_value);
|
||||
if (colo.has_value()) {
|
||||
// color.value() -> Color::RED
|
||||
}
|
||||
|
|
@ -92,7 +93,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
|
||||
* Static storage integer to enum value
|
||||
```cpp
|
||||
constexpr auto color = yae::enum_cast<Color>(4);
|
||||
constexpr auto color = magic_enum::enum_cast<Color>(4);
|
||||
if (color.has_value()) {
|
||||
// color.value() -> Color::BLUE
|
||||
}
|
||||
|
|
@ -101,55 +102,67 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* Indexed access to enum value
|
||||
```cpp
|
||||
int i = 1;
|
||||
Color colo = yae::enum_value<Color>(i);
|
||||
Color colo = magic_enum::enum_value<Color>(i);
|
||||
// color -> Color::BLUE
|
||||
```
|
||||
|
||||
* Compile-time indexed access.
|
||||
```cpp
|
||||
constexpr Color color = yae::enum_value<Color>(0);
|
||||
constexpr Color color = magic_enum::enum_value<Color>(0);
|
||||
// color -> Color::RED
|
||||
```
|
||||
|
||||
* Enum value sequence
|
||||
```cpp
|
||||
constexpr auto colors = yae::enum_values<Color>();
|
||||
constexpr auto colors = magic_enum::enum_values<Color>();
|
||||
// colors -> {Color::RED, Color::BLUE, Color::GREEN}
|
||||
```
|
||||
|
||||
* Number of enum elements
|
||||
```cpp
|
||||
constexpr std::size_t color_count = yae::enum_count<Color>();
|
||||
constexpr std::size_t color_count = magic_enum::enum_count<Color>();
|
||||
// color_count -> 3
|
||||
```
|
||||
|
||||
* Enum names sequence
|
||||
```cpp
|
||||
constexpr auto color_names = yae::enum_names<Color>();
|
||||
constexpr auto color_names = magic_enum::enum_names<Color>();
|
||||
// color_names -> {"RED", "BLUE", "GREEN"}
|
||||
```
|
||||
|
||||
* Stream operator for enum
|
||||
```cpp
|
||||
using namespace yae::ops; // out-of-the-box stream operator for enums.
|
||||
using namespace magic_enum::ops; // out-of-the-box stream operator for enums.
|
||||
Color color = Color::BLUE;
|
||||
std::cout << color << std::endl; // "BLUE"
|
||||
```
|
||||
|
||||
## Remarks
|
||||
|
||||
* `yae::enum_cast` returns `std::optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value.
|
||||
* `magic_enum::enum_cast` returns `std::optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value.
|
||||
|
||||
* `yae::enum_values` returns `std::array<E, N>` with all enum value where `N = number of enum values`, sorted by enum value.
|
||||
* `magic_enum::enum_value` no bounds checking is performed: the behavior is undefined if `index >= number of enum values`.
|
||||
|
||||
* `yae::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_values` returns `std::array<E, N>` with all enum value where `N = number of enum values`, sorted by enum value.
|
||||
|
||||
* `yae::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_name` returns `std::optional<std::string_view>`, using `has_value()` to check contains enum name and `value()` to get the enum name.
|
||||
|
||||
* Enum value must be in range `(-MAGIC_ENUM_RANGE, MAGIC_ENUM_RANGE)`. By default `MAGIC_ENUM_RANGE = 256`. If you need a larger range, redefine the macro `MAGIC_ENUM_RANGE`.
|
||||
* `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.
|
||||
|
||||
* Enum value must be in range `[-256, 256]`. If you need another range, add specialization enum_range for necessary enum type.
|
||||
```cpp
|
||||
#define MAGIC_ENUM_RANGE 1024 // Redefine MAGIC_ENUM_RANGE for larger range.
|
||||
#include <magic_enum.hpp>
|
||||
|
||||
enum number { one = 100, two = 200, three = 300 };
|
||||
|
||||
namespace magic_enum {
|
||||
template <>
|
||||
struct enum_range<number> {
|
||||
static constexpr int min = 100;
|
||||
static constexpr int max = 300;
|
||||
};
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Integration
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue