1
0
Fork 0
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:
neargye 2024-01-02 02:39:10 +04:00
parent 6992f41db7
commit aa465f7f37
5 changed files with 27 additions and 7 deletions

View file

@ -16,6 +16,10 @@
};
```
* `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.
* 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`.

View file

@ -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;
```

View file

@ -64,7 +64,7 @@ int main() {
auto f4_integer = magic_enum::enum_integer(AnimalFlags::HasClaws);
std::cout << "HasClaws = " << f4_integer << std::endl; // HasClaws = 1024
using namespace magic_enum::ostream_operators; // out-of-the-box ostream operator for enum-flags.
using magic_enum::iostream_operators::operator<<; // out-of-the-box ostream operator for enum-flags.
// Ostream operator for enum-flags.
std::cout << f1 << " " << f2 << " " << f3 << std::endl; // Endangered CanFly|EatsFish HasClaws|EatsFish

View file

@ -70,7 +70,7 @@ int main() {
auto c4_integer = magic_enum::enum_integer(Color::RED);
std::cout << "RED = " << c4_integer << std::endl; // RED = -10
using namespace magic_enum::ostream_operators; // out-of-the-box ostream operator for all enums.
using magic_enum::iostream_operators::operator<<; // out-of-the-box ostream operator for all enums.
// Ostream operator for enum.
std::cout << "Color: " << c1 << " " << c2 << " " << c3 << std::endl; // Color: RED BLUE GREEN

View file

@ -105,8 +105,8 @@ std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& i
namespace iostream_operators {
using namespace ostream_operators;
using namespace istream_operators;
using magic_enum::ostream_operators::operator<<;
using magic_enum::istream_operators::operator>>;
} // namespace magic_enum::iostream_operators