From aa465f7f376b68d16f98a1c3a86a3efccdcb4bf8 Mon Sep 17 00:00:00 2001 From: neargye Date: Tue, 2 Jan 2024 02:39:10 +0400 Subject: [PATCH] update doc and example --- doc/limitations.md | 4 ++++ doc/reference.md | 22 +++++++++++++++++++--- example/enum_flag_example.cpp | 2 +- example/example.cpp | 2 +- include/magic_enum/magic_enum_iostream.hpp | 4 ++-- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/doc/limitations.md b/doc/limitations.md index b5d1827..965d77a 100644 --- a/doc/limitations.md +++ b/doc/limitations.md @@ -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`. diff --git a/doc/reference.md b/doc/reference.md index ff00731..b6772fa 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -431,6 +431,19 @@ constexpr bool enum_flags_contains(string_view value, BinaryPredicate p) noexcep * You should add the required file ``. +* 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 { + 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 { 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& operator<<(basic_ostream& 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& operator>>(basic_istream& 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; ``` diff --git a/example/enum_flag_example.cpp b/example/enum_flag_example.cpp index f6e5520..26e1e4b 100644 --- a/example/enum_flag_example.cpp +++ b/example/enum_flag_example.cpp @@ -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 diff --git a/example/example.cpp b/example/example.cpp index 4408dbe..6c7996a 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -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 diff --git a/include/magic_enum/magic_enum_iostream.hpp b/include/magic_enum/magic_enum_iostream.hpp index 72093ee..950ffda 100644 --- a/include/magic_enum/magic_enum_iostream.hpp +++ b/include/magic_enum/magic_enum_iostream.hpp @@ -105,8 +105,8 @@ std::basic_istream& operator>>(std::basic_istream& 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