// __ __ _ ______ _____ // | \/ | (_) | ____| / ____|_ _ // | \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_ // | |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _| // | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_| // |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____| // __/ | https://github.com/Neargye/magic_enum // |___/ version 0.6.6 // // Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2019 - 2020 Daniil Goncharov . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #ifndef NEARGYE_MAGIC_ENUM_HPP #define NEARGYE_MAGIC_ENUM_HPP #define MAGIC_ENUM_VERSION_MAJOR 0 #define MAGIC_ENUM_VERSION_MINOR 6 #define MAGIC_ENUM_VERSION_PATCH 6 #include #include #include #include #include #include #include #include #include #include #if defined(__clang__) # pragma clang diagnostic push #elif defined(__GNUC__) # pragma GCC diagnostic push #elif defined(_MSC_VER) # pragma warning(push) # pragma warning(disable : 26495) // Variable 'static_string::chars' is uninitialized. #endif // Checks magic_enum compiler compatibility. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910 # undef MAGIC_ENUM_SUPPORTED # define MAGIC_ENUM_SUPPORTED 1 #endif // Checks magic_enum compiler aliases compatibility. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1920 # undef MAGIC_ENUM_SUPPORTED_ALIASES # define MAGIC_ENUM_SUPPORTED_ALIASES 1 #endif // Enum value must be greater or equals than MAGIC_ENUM_RANGE_MIN. By default MAGIC_ENUM_RANGE_MIN = -128. // If need another min range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN. #if !defined(MAGIC_ENUM_RANGE_MIN) # define MAGIC_ENUM_RANGE_MIN -128 #endif // Enum value must be less or equals than MAGIC_ENUM_RANGE_MAX. By default MAGIC_ENUM_RANGE_MAX = 128. // If need another max range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MAX. #if !defined(MAGIC_ENUM_RANGE_MAX) # define MAGIC_ENUM_RANGE_MAX 128 #endif #if !defined(NEARGYE_DETAIL_N) # define NEARGYE_DETAIL_N 1 namespace neargye::detail { template struct identity { using type = T; }; template struct static_string { constexpr explicit static_string(std::string_view str) noexcept : static_string{str, std::make_index_sequence{}} { assert(str.size() == N); } constexpr const char* data() const noexcept { return chars.data(); } constexpr std::size_t size() const noexcept { return N; } constexpr operator std::string_view() const noexcept { return {data(), size()}; } private: template constexpr static_string(std::string_view str, std::index_sequence) noexcept : chars{{str[I]..., '\0'}} {} const std::array chars; }; template <> struct static_string<0> { constexpr explicit static_string(std::string_view) noexcept {} constexpr const char* data() const noexcept { return nullptr; } constexpr std::size_t size() const noexcept { return 0; } constexpr operator std::string_view() const noexcept { return {}; } }; constexpr std::string_view enum_name(std::string_view name) noexcept { for (std::size_t i = name.size(); i > 0; --i) { if (!((name[i - 1] >= '0' && name[i - 1] <= '9') || (name[i - 1] >= 'a' && name[i - 1] <= 'z') || (name[i - 1] >= 'A' && name[i - 1] <= 'Z') || (name[i - 1] == '_'))) { name.remove_prefix(i); break; } } if (name.size() > 0 && ((name.front() >= 'a' && name.front() <= 'z') || (name.front() >= 'A' && name.front() <= 'Z') || (name.front() == '_'))) { return name; } return {}; // Invalid name. } template constexpr auto n() noexcept { #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 || defined(_MSC_VER) && _MSC_VER >= 1910 # if defined(__clang__) constexpr std::string_view name{__PRETTY_FUNCTION__ + 32, sizeof(__PRETTY_FUNCTION__) - 35}; # elif defined(__GNUC__) constexpr std::string_view name{__PRETTY_FUNCTION__ + 47, sizeof(__PRETTY_FUNCTION__) - 50}; # elif defined(_MSC_VER) constexpr std::string_view name{__FUNCSIG__ + 65, sizeof(__FUNCSIG__) - 83 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)}; # endif return static_string{name}; #else return std::string_view{}; // Unsupported compiler. #endif } #if defined(_MSC_VER) template > #else template #endif inline constexpr auto type_name_v = n(); template constexpr auto n() noexcept { static_assert(std::is_enum_v && std::is_same_v>); #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910 # if defined(__clang__) || defined(__GNUC__) constexpr auto name = enum_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); # elif defined(_MSC_VER) constexpr auto name = enum_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); # endif return static_string{name}; #else return std::string_view{}; // Unsupported compiler. #endif } template inline constexpr auto enum_name_v = n(); } // namespace neargye::detail #endif namespace magic_enum { // 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. // If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX. // If need another range for specific enum type, add specialization enum_range for necessary enum type. template struct enum_range { static_assert(std::is_enum_v, "magic_enum::enum_range requires enum type."); inline static constexpr int min = MAGIC_ENUM_RANGE_MIN; inline static constexpr int max = MAGIC_ENUM_RANGE_MAX; static_assert(max > min, "magic_enum::enum_range requires max > min."); }; static_assert(MAGIC_ENUM_RANGE_MIN <= 0, "MAGIC_ENUM_RANGE_MIN must be less or equals than 0."); static_assert(MAGIC_ENUM_RANGE_MIN > (std::numeric_limits::min)(), "MAGIC_ENUM_RANGE_MIN must be greater than INT16_MIN."); static_assert(MAGIC_ENUM_RANGE_MAX > 0, "MAGIC_ENUM_RANGE_MAX must be greater than 0."); static_assert(MAGIC_ENUM_RANGE_MAX < (std::numeric_limits::max)(), "MAGIC_ENUM_RANGE_MAX must be less than INT16_MAX."); static_assert(MAGIC_ENUM_RANGE_MAX > MAGIC_ENUM_RANGE_MIN, "MAGIC_ENUM_RANGE_MAX must be greater than MAGIC_ENUM_RANGE_MIN."); namespace detail { using ::neargye::detail::identity; using ::neargye::detail::static_string; using ::neargye::detail::n; using ::neargye::detail::type_name_v; using ::neargye::detail::enum_name_v; template struct supported #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED || defined(MAGIC_ENUM_NO_CHECK_SUPPORT) : std::true_type {}; #else : std::false_type {}; #endif template inline constexpr bool is_enum_v = std::is_enum_v && std::is_same_v>; struct char_equal_to { constexpr bool operator()(char lhs, char rhs) const noexcept { return lhs == rhs; } }; template constexpr bool cmp_equal(std::string_view lhs, std::string_view rhs, BinaryPredicate&& p) noexcept(std::is_nothrow_invocable_r_v) { if constexpr (std::is_same_v) { static_cast(p); return lhs == rhs; } else { if (lhs.size() != rhs.size()) { return false; } const auto size = lhs.size(); for (std::size_t i = 0; i < size; ++i) { if (!p(lhs[i], rhs[i])) { return false; } } return true; } } template constexpr bool cmp_less(L lhs, R rhs) noexcept { static_assert(std::is_integral_v && std::is_integral_v, "magic_enum::detail::cmp_less requires integral type."); if constexpr (std::is_signed_v == std::is_signed_v) { // If same signedness (both signed or both unsigned). return lhs < rhs; } else if constexpr (std::is_signed_v) { // If 'right' is negative, then result is 'false', otherwise cast & compare. return rhs > 0 && lhs < static_cast>(rhs); } else { // If 'left' is negative, then result is 'true', otherwise cast & compare. return lhs < 0 || static_cast>(lhs) < rhs; } } template constexpr std::uint8_t log2(T value) noexcept { if constexpr (std::is_enum_v) { using U = std::underlying_type_t; return log2(static_cast(value)); } else { static_assert(std::is_integral_v, "magic_enum::detail::log2 requires integral type."); auto ret = std::uint8_t{0}; for (; value > static_cast(1); value >>= static_cast(1), ++ret) {}; return ret; } } template constexpr bool is_pow2(T x) noexcept { if constexpr (std::is_enum_v) { using U = std::underlying_type_t; return is_pow2(static_cast(x)); } else { static_assert(std::is_integral_v, "magic_enum::detail::is_pow2 requires integral type."); return x != 0 && (x & (x - 1)) == 0; } } template constexpr bool is_valid() noexcept { static_assert(is_enum_v, "magic_enum::detail::is_valid requires enum type."); return n(V)>().size() != 0; } template > constexpr int reflected_min() noexcept { static_assert(is_enum_v, "magic_enum::detail::reflected_min requires enum type."); if constexpr (IsFlags) { return 0; } else { constexpr auto lhs = enum_range::min; static_assert(lhs > (std::numeric_limits::min)(), "magic_enum::enum_range requires min must be greater than INT16_MIN."); constexpr auto rhs = (std::numeric_limits::min)(); if constexpr (cmp_less(lhs, rhs)) { return rhs; } else { return lhs; } } } template > constexpr int reflected_max() noexcept { static_assert(is_enum_v, "magic_enum::detail::reflected_max requires enum type."); if constexpr (IsFlags) { return (std::numeric_limits::max)(); } else { constexpr auto lhs = enum_range::max; static_assert(lhs < (std::numeric_limits::max)(), "magic_enum::enum_range requires max must be less than INT16_MAX."); constexpr auto rhs = (std::numeric_limits::max)(); if constexpr (cmp_less(lhs, rhs)) { return lhs; } else { return rhs; } } } template inline constexpr auto reflected_min_v = reflected_min(); template inline constexpr auto reflected_max_v = reflected_max(); template > constexpr E value(std::size_t i) noexcept { static_assert(is_enum_v, "magic_enum::detail::value requires enum type."); if constexpr (IsFlags) { return static_cast(static_cast(1) << static_cast(static_cast(i) + O)); } else { return static_cast(static_cast(i) + O); } } template constexpr auto values(std::integer_sequence) noexcept { static_assert(is_enum_v, "magic_enum::detail::values requires enum type."); constexpr std::array valid{{is_valid(I)>()...}}; constexpr std::size_t count = ((valid[I] ? 1 : 0) + ...); std::array values{}; for (std::size_t i = 0, v = 0; v < count; ++i) { if (valid[i]) { values[v++] = value(i); } } return values; } template > constexpr auto values() noexcept { static_assert(is_enum_v, "magic_enum::detail::values requires enum type."); if constexpr (IsFlags) { return values(std::make_integer_sequence::digits>{}); } else { constexpr auto range_size = reflected_max_v - reflected_min_v + 1; static_assert(range_size > 0, "magic_enum::enum_range requires valid size."); static_assert(range_size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); return values>(std::make_integer_sequence{}); } } template inline constexpr auto values_v = values(); template > using values_t = decltype((values_v)); template inline constexpr auto count_v = values_v.size(); template > inline constexpr auto min_v = static_cast(values_v.front()); template > inline constexpr auto max_v = static_cast(values_v.back()); template > constexpr std::size_t range_size() noexcept { static_assert(is_enum_v, "magic_enum::detail::range_size requires enum type."); if constexpr (IsFlags) { return std::numeric_limits::digits; } else { constexpr auto range_size = max_v - min_v + 1; static_assert(range_size > 0, "magic_enum::enum_range requires valid size."); static_assert(range_size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); return static_cast(range_size); } } template inline constexpr auto range_size_v = range_size(); template using index_t = std::conditional_t < (std::numeric_limits::max)(), std::uint8_t, std::uint16_t>; template inline constexpr auto invalid_index_v = (std::numeric_limits>::max)(); template constexpr auto indexes(std::integer_sequence) noexcept { static_assert(is_enum_v, "magic_enum::detail::indexes requires enum type."); [[maybe_unused]] auto i = index_t{0}; return std::array, sizeof...(I)>{{(is_valid>() ? i++ : invalid_index_v)...}}; } template inline constexpr auto indexes_v = indexes(std::make_integer_sequence>{}); template constexpr auto names(std::index_sequence) noexcept { static_assert(is_enum_v, "magic_enum::detail::names requires enum type."); return std::array{{enum_name_v[I]>...}}; } template inline constexpr auto names_v = names(std::make_index_sequence>{}); template > using names_t = decltype((names_v)); template constexpr auto entries(std::index_sequence) noexcept { static_assert(is_enum_v, "magic_enum::detail::entries requires enum type."); return std::array, sizeof...(I)>{{{values_v[I], enum_name_v[I]>}...}}; } template inline constexpr auto entries_v = entries(std::make_index_sequence>{}); template > using entries_t = decltype((entries_v)); template > constexpr bool is_sparse() noexcept { static_assert(is_enum_v, "magic_enum::detail::is_sparse requires enum type."); if constexpr (IsFlags) { auto range_count = std::size_t{1}; for (auto i = min_v; i != max_v; i <<= static_cast(1), ++range_count) {}; return range_count != count_v; } else { return range_size_v != count_v; } } template inline constexpr bool is_sparse_v = is_sparse(); template > constexpr std::size_t undex(U value) noexcept { static_assert(is_enum_v, "magic_enum::detail::undex requires enum type."); if (const auto i = static_cast(value - min_v); value >= min_v && value <= max_v) { if constexpr (is_sparse_v) { if (const auto idx = indexes_v[i]; idx != invalid_index_v) { return idx; } } else { return i; } } return invalid_index_v; // Value out of range. } template > constexpr std::size_t endex(E value) noexcept { static_assert(is_enum_v, "magic_enum::detail::endex requires enum type."); return undex(static_cast(value)); } template > constexpr U value_ors() noexcept { static_assert(is_enum_v, "magic_enum::detail::endex requires enum type."); auto value = U{0}; for (std::size_t i = 0; i < count_v; ++i) { value |= static_cast(values_v[i]); } return value; } template struct enable_if_enum {}; template struct enable_if_enum { using type = R; using D = std::decay_t; static_assert(supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); static_assert(count_v > 0, "magic_enum requires enum implementation and valid max and min."); }; template struct enable_if_enum { using type = R; using D = std::decay_t; static_assert(supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); static_assert(count_v > 0, "magic_enum::flags requires enum-flags implementation."); }; template using enable_if_enum_t = typename enable_if_enum>, false, T, R>::type; template using enable_if_enum_flags_t = typename enable_if_enum>, true, T, R>::type; template >>> using enum_concept = T; template > struct is_scoped_enum : std::false_type {}; template struct is_scoped_enum : std::bool_constant>> {}; template > struct is_unscoped_enum : std::false_type {}; template struct is_unscoped_enum : std::bool_constant>> {}; template >> struct underlying_type {}; template struct underlying_type : std::underlying_type> {}; } // namespace magic_enum::detail // Checks is magic_enum supported compiler. inline constexpr bool is_magic_enum_supported = detail::supported::value; template using Enum = detail::enum_concept; // Checks whether T is an Unscoped enumeration type. // Provides the member constant value which is equal to true, if T is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration) type. Otherwise, value is equal to false. template struct is_unscoped_enum : detail::is_unscoped_enum {}; template inline constexpr bool is_unscoped_enum_v = is_unscoped_enum::value; // Checks whether T is an Scoped enumeration type. // Provides the member constant value which is equal to true, if T is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations) type. Otherwise, value is equal to false. template struct is_scoped_enum : detail::is_scoped_enum {}; template inline constexpr bool is_scoped_enum_v = is_scoped_enum::value; // If T is a complete enumeration type, provides a member typedef type that names the underlying type of T. // Otherwise, if T is not an enumeration type, there is no member type. Otherwise (T is an incomplete enumeration type), the program is ill-formed. template struct underlying_type : detail::underlying_type {}; template using underlying_type_t = typename underlying_type::type; // Returns string name of enum type. template [[nodiscard]] constexpr std::string_view enum_type_name() noexcept { using D = std::decay_t; static_assert(std::is_enum_v, "Requires enum type."); #if defined(_MSC_VER) // TODO: https://github.com/Neargye/nameof/issues/22 constexpr std::string_view name = std::string_view{detail::type_name_v}.substr(5, detail::type_name_v.size() - 5); #else constexpr std::string_view name = detail::type_name_v; #endif static_assert(name.size() > 0, "Enum type does not have a name."); return name; } // Returns number of enum values. template [[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t { using D = std::decay_t; return detail::count_v; } // Returns enum value at specified index. // No bounds checking is performed: the behavior is undefined if index >= number of enum values. template [[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_t> { using D = std::decay_t; if constexpr (detail::is_sparse_v) { return assert((index < detail::count_v)), detail::values_v[index]; } else { return assert((index < detail::count_v)), detail::value>(index); } } // Returns std::array with enum values, sorted by enum value. template [[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t> { using D = std::decay_t; return detail::values_v; } // Returns string name from static storage enum variable. // This version is much lighter on the compile times and is not restricted to the enum_range limitation. template [[nodiscard]] constexpr std::string_view enum_name() noexcept { using D = std::decay_t; static_assert(std::is_enum_v, "Requires enum type."); constexpr std::string_view name = detail::enum_name_v; static_assert(name.size() > 0, "Enum value does not have a name."); return name; } // Returns string name from enum value. // If enum value does not have name or value out of range, returns empty string. template [[nodiscard]] constexpr auto enum_name(E value) noexcept -> detail::enable_if_enum_t { using D = std::decay_t; if (const auto i = detail::endex(value); i != detail::invalid_index_v) { return detail::names_v[i]; } return {}; // Invalid value or out of range. } // Returns std::array with string names, sorted by enum value. template [[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t> { using D = std::decay_t; return detail::names_v; } // Returns std::array with pairs (enum value, string name), sorted by enum value. template [[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t> { using D = std::decay_t; return detail::entries_v; } // Obtains enum value from integer value. // Returns std::optional with enum value. template [[nodiscard]] constexpr auto enum_cast(underlying_type_t value) noexcept -> detail::enable_if_enum_t>> { using D = std::decay_t; if (detail::undex(value) != detail::invalid_index_v) { return static_cast(value); } return std::nullopt; // Invalid value or out of range. } // Obtains enum value from string name. // Returns std::optional with enum value. template [[nodiscard]] constexpr auto enum_cast(std::string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_t>> { using D = std::decay_t; static_assert(std::is_invocable_r_v, "magic_enum::enum_cast requires bool(char, char) invocable predicate."); for (std::size_t i = 0; i < detail::count_v; ++i) { if (detail::cmp_equal(value, detail::names_v[i], p)) { return enum_value(i); } } return std::nullopt; // Invalid value or out of range. } // Obtains enum value from string name. // Returns std::optional with enum value. template [[nodiscard]] constexpr auto enum_cast(std::string_view value) noexcept -> detail::enable_if_enum_t>> { using D = std::decay_t; return enum_cast(value, detail::char_equal_to{}); } // Returns integer value from enum value. template [[nodiscard]] constexpr underlying_type_t enum_integer(E value) noexcept { return static_cast>(value); } // Obtains index in enum values from enum value. // Returns std::optional with index. template [[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t> { using D = std::decay_t; if (const auto i = detail::endex(value); i != detail::invalid_index_v) { return i; } return std::nullopt; // Value out of range. } // Checks whether enum contains enumerator with such enum value. template [[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_t { using D = std::decay_t; return detail::endex(value) != detail::invalid_index_v; } // Checks whether enum contains enumerator with such integer value. template [[nodiscard]] constexpr auto enum_contains(underlying_type_t value) noexcept -> detail::enable_if_enum_t { using D = std::decay_t; return detail::undex(value) != detail::invalid_index_v; } // Checks whether enum contains enumerator with such string name. template [[nodiscard]] constexpr auto enum_contains(std::string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_t { using D = std::decay_t; static_assert(std::is_invocable_r_v, "magic_enum::enum_contains requires bool(char, char) invocable predicate."); return enum_cast(value, std::move_if_noexcept(p)).has_value(); } // Checks whether enum contains enumerator with such string name. template [[nodiscard]] constexpr auto enum_contains(std::string_view value) noexcept -> detail::enable_if_enum_t { using D = std::decay_t; return enum_cast(value).has_value(); } namespace ostream_operators { template , int> = 0> std::basic_ostream& operator<<(std::basic_ostream& os, E value) { using D = std::decay_t; using U = underlying_type_t; #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED using namespace magic_enum; if (const auto name = enum_name(value); !name.empty()) { for (const auto c : name) { os.put(c); } return os; } #endif return os << static_cast(value); } template , int> = 0> std::basic_ostream& operator<<(std::basic_ostream& os, std::optional value) { if (value.has_value()) { os << value.value(); } return os; } } // namespace magic_enum::ostream_operators namespace bitwise_operators { template , int> = 0> constexpr E operator~(E rhs) noexcept { return static_cast(~static_cast>(rhs)); } template , int> = 0> constexpr E operator|(E lhs, E rhs) noexcept { return static_cast(static_cast>(lhs) | static_cast>(rhs)); } template , int> = 0> constexpr E operator&(E lhs, E rhs) noexcept { return static_cast(static_cast>(lhs) & static_cast>(rhs)); } template , int> = 0> constexpr E operator^(E lhs, E rhs) noexcept { return static_cast(static_cast>(lhs) ^ static_cast>(rhs)); } template , int> = 0> constexpr E& operator|=(E& lhs, E rhs) noexcept { return lhs = lhs | rhs; } template , int> = 0> constexpr E& operator&=(E& lhs, E rhs) noexcept { return lhs = lhs & rhs; } template , int> = 0> constexpr E& operator^=(E& lhs, E rhs) noexcept { return lhs = lhs ^ rhs; } } // namespace magic_enum::bitwise_operators namespace flags { // Returns string name of enum type. using magic_enum::enum_type_name; // Returns number of enum-flags values. template [[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_flags_t { using D = std::decay_t; return detail::count_v; } // Returns enum-flags value at specified index. // No bounds checking is performed: the behavior is undefined if index >= number of enum-flags values. template [[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_flags_t> { using D = std::decay_t; if constexpr (detail::is_sparse_v) { return assert((index < detail::count_v)), detail::values_v[index]; } else { constexpr auto min = detail::log2(detail::min_v); return assert((index < detail::count_v)), detail::value(index); } } // Returns std::array with enum-flags values, sorted by enum-flags value. template [[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_flags_t> { using D = std::decay_t; return detail::values_v; } // Returns string name from enum-flags value. // If enum-flags value does not have name or value out of range, returns empty string. template [[nodiscard]] constexpr auto enum_name(E value) -> detail::enable_if_enum_flags_t> { using D = std::decay_t; using U = underlying_type_t; if constexpr (Strict) { for (std::size_t i = 0; i < detail::count_v; ++i) { if (enum_value(i) == value) { return detail::names_v[i]; } } } else { std::string name; auto check_value = U{0}; for (std::size_t i = 0; i < detail::count_v; ++i) { if (const auto v = enum_value(i); (static_cast(value) & static_cast(v)) != 0) { check_value |= static_cast(v); const auto n = detail::names_v[i]; if (!name.empty()) { name.append(1, '|'); } name.append(n.data(), n.size()); } } if (check_value == static_cast(value)) { return name; } } return {}; // Invalid value or out of range. } // Returns std::array with string names, sorted by enum-flags value. template [[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_flags_t> { using D = std::decay_t; return detail::names_v; } // Returns std::array with pairs (enum-flags value, string name), sorted by enum-flags value. template [[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_flags_t> { using D = std::decay_t; return detail::entries_v; } // Obtains enum-flags value from integer value. // Returns std::optional with enum-flags value. template [[nodiscard]] constexpr auto enum_cast(underlying_type_t value) noexcept -> detail::enable_if_enum_flags_t>> { using D = std::decay_t; using U = underlying_type_t; if constexpr (Strict) { for (std::size_t i = 0; i < detail::count_v; ++i) { if (value == static_cast(enum_value(i))) { return static_cast(value); } } } 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::value_ors(); if (value >= min && value <= max) { return static_cast(value); } } } return std::nullopt; } // Obtains enum-flags value from string name. // Returns std::optional with enum-flags value. template [[nodiscard]] constexpr auto enum_cast(std::string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_flags_t>> { static_assert(std::is_invocable_r_v, "magic_enum::flags::enum_cast requires bool(char, char) invocable predicate."); using D = std::decay_t; if constexpr (Strict) { for (std::size_t i = 0; i < detail::count_v; ++i) { if (detail::cmp_equal(value, detail::names_v[i], p)) { return enum_value(i); } } return std::nullopt; } else { using U = underlying_type_t; auto result = U{0}; while (!value.empty()) { const auto d = value.find_first_of('|'); const auto s = (d == std::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 == 0) { return std::nullopt; } else { result |= f; } value.remove_prefix((d == std::string_view::npos) ? value.size() : d + 1); } if (result == 0) { return std::nullopt; } else { return static_cast(result); } } } // Obtains enum-flags value from string name. // Returns std::optional with enum-flags value. template [[nodiscard]] constexpr auto enum_cast(std::string_view value) noexcept -> detail::enable_if_enum_flags_t>> { using D = std::decay_t; return enum_cast(value, detail::char_equal_to{}); } // Returns integer value from enum value. using magic_enum::enum_integer; // Obtains index in enum-flags values from enum-flags value. // Returns std::optional with index. template [[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_flags_t> { using D = std::decay_t; if (detail::is_pow2(value)) { for (std::size_t i = 0; i < detail::count_v; ++i) { auto v = enum_value(i); if (v == value) { return i; } } } return std::nullopt; // Value out of range. } // Checks whether enum-flags contains enumerator with such enum-flags value. template [[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_flags_t { using D = std::decay_t; using U = underlying_type_t; return enum_cast(static_cast(value)).has_value(); } // Checks whether enum-flags contains enumerator with such integer value. template [[nodiscard]] constexpr auto enum_contains(underlying_type_t value) noexcept -> detail::enable_if_enum_flags_t { using D = std::decay_t; return enum_cast(value).has_value(); } // Checks whether enum-flags contains enumerator with such string name. template [[nodiscard]] constexpr auto enum_contains(std::string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_flags_t { using D = std::decay_t; static_assert(std::is_invocable_r_v, "magic_enum::flags::enum_contains requires bool(char, char) invocable predicate."); return enum_cast(value, std::move_if_noexcept(p)).has_value(); } // Checks whether enum-flags contains enumerator with such string name. template [[nodiscard]] constexpr auto enum_contains(std::string_view value) noexcept -> detail::enable_if_enum_flags_t { using D = std::decay_t; return enum_cast(value).has_value(); } } // namespace magic_enum::flags namespace flags::ostream_operators { template = 0> std::basic_ostream& operator<<(std::basic_ostream& os, E value) { using D = std::decay_t; using U = underlying_type_t; #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED using namespace magic_enum::flags; if (const auto name = enum_name(value); !name.empty()) { for (const auto c : name) { os.put(c); } return os; } #endif return os << static_cast(value); } template = 0> std::basic_ostream& operator<<(std::basic_ostream& os, std::optional value) { if (value.has_value()) { os << value.value(); } return os; } } // namespace magic_enum::flags::ostream_operators namespace flags::bitwise_operators { using namespace magic_enum::bitwise_operators; } // namespace magic_enum::flags::bitwise_operators } // namespace magic_enum #if defined(__clang__) # pragma clang diagnostic pop #elif defined(__GNUC__) # pragma GCC diagnostic pop #elif defined(_MSC_VER) # pragma warning(pop) #endif #endif // NEARGYE_MAGIC_ENUM_HPP