mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
add doc
This commit is contained in:
parent
1f61e72c26
commit
cd73e2b974
3 changed files with 373 additions and 82 deletions
91
README.md
91
README.md
|
|
@ -35,8 +35,14 @@ Header-only C++17 library provides static reflection for enums, work with any en
|
|||
* `is_unscoped_enum` checks whether type is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration).
|
||||
* `is_scoped_enum` checks whether type is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations).
|
||||
* `underlying_type` improved UB-free "SFINAE-friendly" [std::underlying_type](https://en.cppreference.com/w/cpp/types/underlying_type).
|
||||
* `using namespace magic_enum::ostream_operators;` ostream operators for enums.
|
||||
* `using namespace magic_enum::bitwise_operators;` bitwise operators for enums.
|
||||
* `ostream_operators` ostream operators for enums.
|
||||
* `bitwise_operators` bitwise operators for enums.
|
||||
|
||||
## Documentation
|
||||
|
||||
* [Reference](doc/reference.md)
|
||||
* [Limitations](doc/limitations.md)
|
||||
* [Integration](#Integration)
|
||||
|
||||
## Features
|
||||
|
||||
|
|
@ -122,7 +128,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
// color_entries[0].second -> "RED"
|
||||
```
|
||||
|
||||
* Stream operator for enum
|
||||
* Ostream operator for enum
|
||||
```cpp
|
||||
using namespace magic_enum::ostream_operators; // out-of-the-box ostream operators for enums.
|
||||
Color color = Color::BLUE;
|
||||
|
|
@ -171,85 +177,6 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
// color_name -> "BLUE"
|
||||
```
|
||||
|
||||
## Remarks
|
||||
|
||||
* `magic_enum::enum_cast(value)` returns `std::optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value.
|
||||
|
||||
* `magic_enum::enum_value(index)` no bounds checking is performed: the behavior is undefined if `index >= number of enum values`.
|
||||
|
||||
* `magic_enum::enum_values<E>()` returns `std::array<E, N>` with all enum value where `N = number of enum values`, sorted by enum value.
|
||||
|
||||
* `magic_enum::enum_name(value)` 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<E>()` 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<E>()` 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.
|
||||
|
||||
* Enum value must be in range `[MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]`. By default `MAGIC_ENUM_RANGE_MIN = -128`, `MAGIC_ENUM_RANGE_MAX = 128`.
|
||||
|
||||
If need another range for all enum types by default, redefine the macro `MAGIC_ENUM_RANGE_MIN` and `MAGIC_ENUM_RANGE_MAX`.
|
||||
```cpp
|
||||
#define MAGIC_ENUM_RANGE_MIN 0
|
||||
#define MAGIC_ENUM_RANGE_MAX 256
|
||||
#include <magic_enum.hpp>
|
||||
```
|
||||
|
||||
If need another range for specific enum type, add specialization `enum_range` for necessary enum type.
|
||||
```cpp
|
||||
#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;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
* `magic_enum` obtains the first defined value enums, and won't work if value are aliased.
|
||||
```cpp
|
||||
enum ShapeKind {
|
||||
ConvexBegin = 0,
|
||||
Box = 0, // Won't work.
|
||||
Sphere = 1,
|
||||
ConvexEnd = 2,
|
||||
Donut = 2, // Won't work too.
|
||||
Banana = 3,
|
||||
COUNT = 4,
|
||||
};
|
||||
// magic_enum::enum_cast<ShapeKind>("Box") -> std::nullopt
|
||||
// magic_enum::enum_name(ShapeKind::Box) -> "ConvexBegin"
|
||||
```
|
||||
Work around the issue:
|
||||
```cpp
|
||||
enum ShapeKind {
|
||||
// Convex shapes, see ConvexBegin and ConvexEnd below.
|
||||
Box = 0,
|
||||
Sphere = 1,
|
||||
|
||||
// Non-convex shapes.
|
||||
Donut = 2,
|
||||
Banana = 3,
|
||||
|
||||
COUNT = Banana + 1,
|
||||
|
||||
// Non-reflected aliases.
|
||||
ConvexBegin = Box,
|
||||
ConvexEnd = Sphere + 1,
|
||||
};
|
||||
// magic_enum::enum_cast<ShapeKind>("Box") -> ShapeKind::Box
|
||||
// magic_enum::enum_name(ShapeKind::Box) -> "Box"
|
||||
|
||||
// Non-reflected aliases.
|
||||
// magic_enum::enum_cast<ShapeKind>("ConvexBegin") -> std::nullopt
|
||||
// magic_enum::enum_name(ShapeKind::ConvexBegin) -> "Box"
|
||||
```
|
||||
|
||||
## Integration
|
||||
|
||||
You should add the required file [magic_enum.hpp](include/magic_enum.hpp).
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue