mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
update doc and example
This commit is contained in:
parent
6992f41db7
commit
aa465f7f37
5 changed files with 27 additions and 7 deletions
|
|
@ -431,6 +431,19 @@ constexpr bool enum_flags_contains(string_view value, BinaryPredicate p) noexcep
|
|||
|
||||
* You should add the required file `<magic_enum_flags.hpp>`.
|
||||
|
||||
* For enum-flags add `is_flags` to specialization `enum_range` for necessary enum type. Specialization of `enum_range` must be injected in `namespace magic_enum::customize`.
|
||||
```cpp
|
||||
enum class Directions { Up = 1 << 1, Down = 1 << 2, Right = 1 << 3, Left = 1 << 4 };
|
||||
template <>
|
||||
struct magic_enum::customize::enum_range<Directions> {
|
||||
static constexpr bool is_flags = true;
|
||||
};
|
||||
```
|
||||
|
||||
* `MAGIC_ENUM_RANGE_MAX/MAGIC_ENUM_RANGE_MIN` does not affect the maximum amount of flags.
|
||||
|
||||
* If enum is declared as flags, then it will not reflect the value of zero and is logically AND.
|
||||
|
||||
* Examples
|
||||
|
||||
```cpp
|
||||
|
|
@ -439,15 +452,18 @@ constexpr bool enum_flags_contains(string_view value, BinaryPredicate p) noexcep
|
|||
Down = 2,
|
||||
Up = 4,
|
||||
Right = 8,
|
||||
LeftAndDown = 3
|
||||
};
|
||||
template <>
|
||||
struct magic_enum::customize::enum_range<Directions> {
|
||||
static constexpr bool is_flags = true;
|
||||
};
|
||||
|
||||
magic_enum::enum_flags_name(Directions::Up | Directions::Right); // directions_name -> "Directions::Up|Directions::Right"
|
||||
magic_enum::enum_flags_name(Directions::Up | Directions::Right); // -> "Directions::Up|Directions::Right"
|
||||
magic_enum::enum_flags_name(Directions::LeftAndDown); // -> "Directions::Left|Directions::Down"
|
||||
|
||||
magic_enum::enum_flags_contains(Directions::Up | Directions::Right); // -> true
|
||||
magic_enum::enum_flags_contains(Directions::LeftAndDown); // -> false
|
||||
|
||||
magic_enum::enum_flags_cast(3); // -> "Directions::Left|Directions::Down"
|
||||
|
||||
|
|
@ -549,7 +565,7 @@ basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os, optiona
|
|||
* Examples
|
||||
|
||||
```cpp
|
||||
using namespace magic_enum::ostream_operators; // out-of-the-box ostream operators for enums.
|
||||
using magic_enum::iostream_operators::operator<<; // out-of-the-box ostream operators for enums.
|
||||
Color color = Color::BLUE;
|
||||
std::cout << color << std::endl; // "BLUE"
|
||||
```
|
||||
|
|
@ -568,7 +584,7 @@ basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is, E& valu
|
|||
* Examples
|
||||
|
||||
```cpp
|
||||
using namespace magic_enum::istream_operators; // out-of-the-box istream operators for enums.
|
||||
using magic_enum::iostream_operators::operator>>; // out-of-the-box istream operators for enums.
|
||||
Color color;
|
||||
std::cin >> color;
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue