diff --git a/doc/reference.md b/doc/reference.md index 04f7f4f..949a3f7 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -422,6 +422,8 @@ template constexpr bool enum_flags_contains(string_view value, BinaryPredicate p) noexcept(is_nothrow_invocable_v); ``` +* You should add the required file ``. + * Examples ```cpp @@ -437,8 +439,15 @@ constexpr bool enum_flags_contains(string_view value, BinaryPredicate p) noexcep }; magic_enum::enum_flags_name(Directions::Up | Directions::Right); // directions_name -> "Directions::Up|Directions::Right" + magic_enum::enum_flags_contains(Directions::Up | Directions::Right); // -> true + magic_enum::enum_flags_cast(3); // -> "Directions::Left|Directions::Down" + + magic_enum::enum_flags_test(Left|Down, Down); // -> "true" + magic_enum::enum_flags_test(Left|Down, Right); // -> "false" + + magic_enum::enum_flags_test_any(Left|Down|Right, Down|Right); // -> "true" ``` ## `is_unscoped_enum` diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index 5d289c7..2306856 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -1272,33 +1272,6 @@ template return enum_name(value); } -// Returns name from enum-flags value. -// If enum-flags value does not have name or value out of range, returns empty string. -template -[[nodiscard]] auto enum_flags_name(E value, char_type sep = static_cast('|')) -> detail::enable_if_t { - using D = std::decay_t; - using U = underlying_type_t; - constexpr auto S = detail::enum_subtype::flags; - - string name; - auto check_value = U{0}; - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (const auto v = static_cast(enum_value(i)); (static_cast(value) & v) != 0) { - check_value |= v; - const auto n = detail::names_v[i]; - if (!name.empty()) { - name.append(1, sep); - } - name.append(n.data(), n.size()); - } - } - - if (check_value != 0 && check_value == static_cast(value)) { - return name; - } - return {}; // Invalid value or out of range. -} - // Returns std::array with names, sorted by enum value. template > [[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_t> { @@ -1347,41 +1320,6 @@ template > } } -// Obtains enum-flags value from integer value. -// Returns optional with enum-flags value. -template -[[nodiscard]] constexpr auto enum_flags_cast(underlying_type_t value) noexcept -> detail::enable_if_t>> { - using D = std::decay_t; - using U = underlying_type_t; - constexpr auto S = detail::enum_subtype::flags; - - if constexpr (detail::count_v == 0) { - static_cast(value); - return {}; // Empty enum. - } else { - if constexpr (detail::is_sparse_v) { - auto check_value = U{0}; - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (const auto v = static_cast(enum_value(i)); (value & v) != 0) { - check_value |= v; - } - } - - if (check_value != 0 && check_value == value) { - return static_cast(value); - } - } else { - constexpr auto min = detail::min_v; - constexpr auto max = detail::values_ors(); - - if (value >= min && value <= max) { - return static_cast(value); - } - } - return {}; // Invalid value or out of range. - } -} - // Obtains enum value from name. // Returns optional with enum value. template , typename BinaryPredicate = std::equal_to<>> @@ -1413,43 +1351,6 @@ template , typename Bi } } -// Obtains enum-flags value from name. -// Returns optional with enum-flags value. -template > -[[nodiscard]] constexpr auto enum_flags_cast(string_view value, [[maybe_unused]] BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable()) -> detail::enable_if_t>, BinaryPredicate> { - using D = std::decay_t; - using U = underlying_type_t; - constexpr auto S = detail::enum_subtype::flags; - - if constexpr (detail::count_v == 0) { - static_cast(value); - return {}; // Empty enum. - } else { - auto result = U{0}; - while (!value.empty()) { - const auto d = detail::find(value, '|'); - const auto s = (d == string_view::npos) ? value : value.substr(0, d); - auto f = U{0}; - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (detail::cmp_equal(s, detail::names_v[i], p)) { - f = static_cast(enum_value(i)); - result |= f; - break; - } - } - if (f == U{0}) { - return {}; // Invalid value or out of range. - } - value.remove_prefix((d == string_view::npos) ? value.size() : d + 1); - } - - if (result != U{0}) { - return static_cast(result); - } - return {}; // Invalid value or out of range. - } -} - // Checks whether enum contains value with such value. template > [[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_t { @@ -1468,15 +1369,6 @@ template return static_cast(enum_cast(static_cast(value))); } -// Checks whether enum-flags contains value with such value. -template -[[nodiscard]] constexpr auto enum_flags_contains(E value) noexcept -> detail::enable_if_t { - using D = std::decay_t; - using U = underlying_type_t; - - return static_cast(enum_flags_cast(static_cast(value))); -} - // Checks whether enum contains value with such integer value. template > [[nodiscard]] constexpr auto enum_contains(underlying_type_t value) noexcept -> detail::enable_if_t { @@ -1485,14 +1377,6 @@ template > return static_cast(enum_cast(value)); } -// Checks whether enum-flags contains value with such integer value. -template -[[nodiscard]] constexpr auto enum_flags_contains(underlying_type_t value) noexcept -> detail::enable_if_t { - using D = std::decay_t; - - return static_cast(enum_flags_cast(value)); -} - // Checks whether enum contains enumerator with such name. template , typename BinaryPredicate = std::equal_to<>> [[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable()) -> detail::enable_if_t { @@ -1501,14 +1385,6 @@ template , typename Bi return static_cast(enum_cast(value, std::move(p))); } -// Checks whether enum-flags contains enumerator with such name. -template > -[[nodiscard]] constexpr auto enum_flags_contains(string_view value, BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable()) -> detail::enable_if_t { - using D = std::decay_t; - - return static_cast(enum_flags_cast(value, std::move(p))); -} - template , typename F, detail::enable_if_t = 0> constexpr auto enum_for_each(F&& f) { using D = std::decay_t; diff --git a/include/magic_enum_flags.hpp b/include/magic_enum_flags.hpp index b10593f..ce9d34c 100644 --- a/include/magic_enum_flags.hpp +++ b/include/magic_enum_flags.hpp @@ -36,13 +36,137 @@ namespace magic_enum { +// Returns name from enum-flags value. +// If enum-flags value does not have name or value out of range, returns empty string. +template +[[nodiscard]] auto enum_flags_name(E value, char_type sep = static_cast('|')) -> detail::enable_if_t { + using D = std::decay_t; + using U = underlying_type_t; + constexpr auto S = detail::enum_subtype::flags; + + string name; + auto check_value = U{0}; + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (const auto v = static_cast(enum_value(i)); (static_cast(value) & v) != 0) { + check_value |= v; + const auto n = detail::names_v[i]; + if (!name.empty()) { + name.append(1, sep); + } + name.append(n.data(), n.size()); + } + } + + if (check_value != 0 && check_value == static_cast(value)) { + return name; + } + return {}; // Invalid value or out of range. +} + +// Obtains enum-flags value from integer value. +// Returns optional with enum-flags value. +template +[[nodiscard]] constexpr auto enum_flags_cast(underlying_type_t value) noexcept -> detail::enable_if_t>> { + using D = std::decay_t; + using U = underlying_type_t; + constexpr auto S = detail::enum_subtype::flags; + + if constexpr (detail::count_v == 0) { + static_cast(value); + return {}; // Empty enum. + } else { + if constexpr (detail::is_sparse_v) { + auto check_value = U{0}; + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (const auto v = static_cast(enum_value(i)); (value & v) != 0) { + check_value |= v; + } + } + + if (check_value != 0 && check_value == value) { + return static_cast(value); + } + } else { + constexpr auto min = detail::min_v; + constexpr auto max = detail::values_ors(); + + if (value >= min && value <= max) { + return static_cast(value); + } + } + return {}; // Invalid value or out of range. + } +} + +// Obtains enum-flags value from name. +// Returns optional with enum-flags value. +template > +[[nodiscard]] constexpr auto enum_flags_cast(string_view value, [[maybe_unused]] BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable()) -> detail::enable_if_t>, BinaryPredicate> { + using D = std::decay_t; + using U = underlying_type_t; + constexpr auto S = detail::enum_subtype::flags; + + if constexpr (detail::count_v == 0) { + static_cast(value); + return {}; // Empty enum. + } else { + auto result = U{0}; + while (!value.empty()) { + const auto d = detail::find(value, '|'); + const auto s = (d == string_view::npos) ? value : value.substr(0, d); + auto f = U{0}; + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (detail::cmp_equal(s, detail::names_v[i], p)) { + f = static_cast(enum_value(i)); + result |= f; + break; + } + } + if (f == U{0}) { + return {}; // Invalid value or out of range. + } + value.remove_prefix((d == string_view::npos) ? value.size() : d + 1); + } + + if (result != U{0}) { + return static_cast(result); + } + return {}; // Invalid value or out of range. + } +} + +// Checks whether enum-flags contains value with such value. +template +[[nodiscard]] constexpr auto enum_flags_contains(E value) noexcept -> detail::enable_if_t { + using D = std::decay_t; + using U = underlying_type_t; + + return static_cast(enum_flags_cast(static_cast(value))); +} + +// Checks whether enum-flags contains value with such integer value. +template +[[nodiscard]] constexpr auto enum_flags_contains(underlying_type_t value) noexcept -> detail::enable_if_t { + using D = std::decay_t; + + return static_cast(enum_flags_cast(value)); +} + +// Checks whether enum-flags contains enumerator with such name. +template > +[[nodiscard]] constexpr auto enum_flags_contains(string_view value, BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable()) -> detail::enable_if_t { + using D = std::decay_t; + + return static_cast(enum_flags_cast(value, std::move(p))); +} + // Checks whether `flags set` contains `flag`. // Note: If `flag` equals 0, it returns false, as 0 is not a flag. template constexpr auto enum_flags_test(E flags, E flag) noexcept -> detail::enable_if_t { using U = underlying_type_t; - return static_cast(static_cast(flags) & static_cast(flag)); + return static_cast(flag) && ((static_cast(flags) & static_cast(flag)) == static_cast(flag)); } // Checks whether `lhs flags set` and `rhs flags set` have common flags. diff --git a/include/magic_enum_format.hpp b/include/magic_enum_format.hpp index 65d6326..8b3fb71 100644 --- a/include/magic_enum_format.hpp +++ b/include/magic_enum_format.hpp @@ -33,6 +33,7 @@ #define NEARGYE_MAGIC_ENUM_FORMAT_HPP #include "magic_enum.hpp" +#include "magic_enum_flags.hpp" #if !defined(MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT) # define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT 1 diff --git a/include/magic_enum_iostream.hpp b/include/magic_enum_iostream.hpp index 4d7110f..556b385 100644 --- a/include/magic_enum_iostream.hpp +++ b/include/magic_enum_iostream.hpp @@ -33,6 +33,8 @@ #define NEARGYE_MAGIC_ENUM_IOSTREAM_HPP #include "magic_enum.hpp" +#include "magic_enum_flags.hpp" + #include namespace magic_enum { diff --git a/test/test_aliases.cpp b/test/test_aliases.cpp index ffb63a3..92f11df 100644 --- a/test/test_aliases.cpp +++ b/test/test_aliases.cpp @@ -97,6 +97,7 @@ constexpr bool operator==(MyStringView lhs, MyStringView rhs) { #define MAGIC_ENUM_USING_ALIAS_STRING_VIEW using string_view = MyStringView; #include +#include using namespace magic_enum; using namespace magic_enum::bitwise_operators; diff --git a/test/test_flags.cpp b/test/test_flags.cpp index 1f13435..37e9796 100644 --- a/test/test_flags.cpp +++ b/test/test_flags.cpp @@ -761,6 +761,7 @@ TEST_CASE("enum_flags_test_any") { REQUIRE_FALSE(enum_flags_test_any(Left|Up|Down, Right)); REQUIRE(enum_flags_test_any(number::one|number::two|number::four, number::one)); + REQUIRE(enum_flags_test_any(number::one|number::two|number::four, number::one|number::two)); REQUIRE_FALSE(enum_flags_test_any(number::one|number::two|number::four, number::three)); REQUIRE_FALSE(enum_flags_test_any(number::one|number::two|number::four, number::no_number));