Skip to content

Commit

Permalink
update doc and example
Browse files Browse the repository at this point in the history
  • Loading branch information
Neargye committed Jan 1, 2024
1 parent 6992f41 commit aa465f7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
4 changes: 4 additions & 0 deletions doc/limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
22 changes: 19 additions & 3 deletions doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

Expand Down Expand Up @@ -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"
```
Expand All @@ -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;
```
Expand Down
2 changes: 1 addition & 1 deletion example/enum_flag_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions include/magic_enum/magic_enum_iostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit aa465f7

Please sign in to comment.