// __ __ _ ______ _____ // | \/ | (_) | ____| / ____|_ _ // | \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_ // | |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _| // | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_| // |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____| // __/ | https://github.com/Neargye/magic_enum // |___/ version 0.6.5 // // Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2019 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 #include #include #include #include #include #include #include #include #include #include #if defined(_MSC_VER) # pragma warning(push) # pragma warning(disable : 26495) // Variable 'magic_enum::detail::static_string::chars' is uninitialized. # pragma warning(disable : 26451) // Arithmetic overflow: 'indexes[static_cast(value) - min_v]' using operator '-' on a 4 byte value and then casting the result to a 8 byte value. #endif // Checks magic_enum compiler compatibility. #if defined(__clang__) || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) # undef MAGIC_ENUM_SUPPORTED # define MAGIC_ENUM_SUPPORTED 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 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 { 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>; 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 chars.size(); } 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]...}} {} 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 pretty_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 bool mixed_sign_less(L lhs, R rhs) noexcept { static_assert(std::is_integral_v && std::is_integral_v, "magic_enum::detail::mixed_sign_less requires integral type."); if constexpr (std::is_signed_v && std::is_unsigned_v) { // If 'left' is negative, then result is 'true', otherwise cast & compare. return lhs < 0 || static_cast>(lhs) < rhs; } else if constexpr (std::is_unsigned_v && std::is_signed_v) { // If 'right' is negative, then result is 'false', otherwise cast & compare. return rhs >= 0 && lhs < static_cast>(rhs); } else { // If same signedness (both signed or both unsigned). return lhs < rhs; } } template constexpr auto n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED # if defined(__clang__) constexpr std::string_view name{__PRETTY_FUNCTION__ + 34, sizeof(__PRETTY_FUNCTION__) - 36}; # elif defined(__GNUC__) constexpr std::string_view name{__PRETTY_FUNCTION__ + 49, sizeof(__PRETTY_FUNCTION__) - 51}; # elif defined(_MSC_VER) constexpr std::string_view name{__FUNCSIG__ + 40, sizeof(__FUNCSIG__) - 57}; # endif return static_string{name}; #else return std::string_view{}; // Unsupported compiler. #endif } template inline constexpr auto type_name_v = n(); template constexpr auto n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED # if defined(__clang__) || defined(__GNUC__) constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); # elif defined(_MSC_VER) constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); # endif return static_string{name}; #else return std::string_view{}; // Unsupported compiler. #endif } template inline constexpr auto name_v = n(); template constexpr int reflected_min() noexcept { static_assert(is_enum_v, "magic_enum::detail::reflected_min requires enum type."); 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)(); return mixed_sign_less(lhs, rhs) ? rhs : lhs; } template constexpr int reflected_max() noexcept { static_assert(is_enum_v, "magic_enum::detail::reflected_max requires enum type."); 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)(); return mixed_sign_less(lhs, rhs) ? lhs : rhs; } template inline constexpr int reflected_min_v = reflected_min(); template inline constexpr int reflected_max_v = reflected_max(); template constexpr std::size_t reflected_size() noexcept { static_assert(is_enum_v, "magic_enum::detail::reflected_size requires enum type."); static_assert(reflected_max_v > reflected_min_v, "magic_enum::enum_range requires max > min."); constexpr auto size = reflected_max_v - reflected_min_v + 1; static_assert(size > 0, "magic_enum::enum_range requires valid size."); static_assert(size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); return static_cast(size); } template constexpr auto values(std::integer_sequence) noexcept { static_assert(is_enum_v, "magic_enum::detail::values requires enum type."); constexpr std::array valid{{(n(I + reflected_min_v)>().size() != 0)...}}; constexpr int count = ((valid[I] ? 1 : 0) + ...); std::array values{}; for (int i = 0, v = 0; v < count; ++i) { if (valid[i]) { values[v++] = static_cast(i + reflected_min_v); } } return values; } template inline constexpr auto values_v = values(std::make_integer_sequence()>{}); template inline constexpr std::size_t count_v = values_v.size(); template inline constexpr int min_v = values_v.empty() ? 0 : static_cast(values_v.front()); template inline constexpr int max_v = values_v.empty() ? 0 : 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."); constexpr auto size = max_v - min_v + 1; static_assert(size > 0, "magic_enum::enum_range requires valid size."); static_assert(size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); return static_cast(size); } template inline constexpr std::size_t 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."); index_t i = 0; return std::array, sizeof...(I)>{{((n(I + min_v)>().size() != 0) ? i++ : invalid_index_v)...}}; } template constexpr auto names(std::index_sequence) noexcept { static_assert(is_enum_v, "magic_enum::detail::names requires enum type."); return std::array{{name_v[I]>...}}; } 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], name_v[I]>}...}}; } template using enable_if_enum_t = std::enable_if_t>, R>; 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> {}; template > struct enum_traits {}; template struct enum_traits { using type = E; using underlying_type = typename detail::underlying_type::type; inline static constexpr std::string_view type_name = detail::type_name_v; inline static constexpr bool is_unscoped = detail::is_unscoped_enum::value; inline static constexpr bool is_scoped = detail::is_scoped_enum::value; inline static constexpr bool is_dense = detail::range_size_v == detail::count_v; inline static constexpr bool is_sparse = detail::range_size_v != detail::count_v; inline static constexpr std::size_t count = detail::count_v; inline static constexpr std::array values = detail::values_v; inline static constexpr std::array names = detail::names(std::make_index_sequence>{}); inline static constexpr std::array, count> entries = detail::entries(std::make_index_sequence>{}); [[nodiscard]] static constexpr bool reflected(E value) noexcept { return static_cast(value) >= static_cast(reflected_min_v) && static_cast(value) <= static_cast(reflected_max_v); } [[nodiscard]] static constexpr int index(E value) noexcept { if (static_cast(value) >= static_cast(min_v) && static_cast(value) <= static_cast(max_v)) { if constexpr (is_sparse) { if (const auto i = indexes[static_cast(value) - min_v]; i != invalid_index_v) { return i; } } else { return static_cast(value) - min_v; } } return -1; // Value out of range. } [[nodiscard]] static constexpr E value(std::size_t index) noexcept { if constexpr (is_sparse) { return assert(index < count), values[index]; } else { return assert(index < count), static_cast(index + min_v); } } [[nodiscard]] static constexpr std::string_view name(E value) noexcept { if (const auto i = index(value); i != -1) { return names[i]; } return {}; // Value out of range. } private: static_assert(is_enum_v, "magic_enum::enum_traits requires enum type."); static_assert(supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); static_assert(count > 0, "magic_enum::enum_range requires enum implementation and valid max and min."); using U = underlying_type; inline static constexpr auto indexes = detail::indexes(std::make_integer_sequence>{}); }; } // 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; // Enum traits defines a compile-time template-based interface to query the properties of enum. template using enum_traits = detail::enum_traits>; // Obtains enum value from enum 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; if constexpr (detail::range_size_v > detail::count_v * 2) { for (std::size_t i = 0; i < enum_traits::count; ++i) { if (value == enum_traits::names[i]) { return enum_traits::values[i]; } } } else { for (auto i = detail::min_v; i <= detail::max_v; ++i) { if (value == enum_traits::name(static_cast(i))) { return static_cast(i); } } } return std::nullopt; // Invalid value or out of range. } // 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 (enum_traits::index(static_cast(value)) != -1) { return static_cast(value); } return std::nullopt; // Invalid value or out of range. } // Returns integer value from enum value. template [[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_enum_t> { return static_cast>(value); } // Obtains index in enum value sequence from enum value. // Returns std::optional with index. template [[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t> { if (const auto i = enum_traits::index(value); i != -1) { return i; } return std::nullopt; // Value out of range. } // 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> { return enum_traits::value(index); } // Obtains value enum sequence. // Returns std::array with enum values, sorted by enum value. template [[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t::values)&> { return enum_traits::values; } // Returns number of enum values. template [[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t { return enum_traits::count; } // Returns string enum 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 auto enum_name() noexcept -> detail::enable_if_enum_t { constexpr std::string_view name = detail::name_v, V>; static_assert(name.size() > 0, "Enum value does not have a name."); return name; } // Returns string enum 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 { return enum_traits::name(value); } // Obtains string enum name sequence. // Returns std::array with string enum names, sorted by enum value. template [[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t::names)&> { return enum_traits::names; } // Obtains pair (value enum, string enum name) sequence. // Returns std::array with std::pair (value enum, string enum name), sorted by enum value. template [[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t::entries)&> { return enum_traits::entries; } namespace ostream_operators { template auto operator<<(std::basic_ostream& os, E value) -> detail::enable_if_enum_t&> { if (const auto name = enum_name(value); !name.empty()) { for (const auto c : name) { os.put(c); } } else { os << enum_integer(value); } return os; } template auto operator<<(std::basic_ostream& os, std::optional value) -> detail::enable_if_enum_t&> { if (value.has_value()) { os << value.value(); } return os; } } // namespace magic_enum::ostream_operators namespace bitwise_operators { template constexpr auto operator~(E rhs) noexcept -> detail::enable_if_enum_t { return static_cast(~static_cast>(rhs)); } template constexpr auto operator|(E lhs, E rhs) noexcept -> detail::enable_if_enum_t { return static_cast(static_cast>(lhs) | static_cast>(rhs)); } template constexpr auto operator&(E lhs, E rhs) noexcept -> detail::enable_if_enum_t { return static_cast(static_cast>(lhs) & static_cast>(rhs)); } template constexpr auto operator^(E lhs, E rhs) noexcept -> detail::enable_if_enum_t { return static_cast(static_cast>(lhs) ^ static_cast>(rhs)); } template constexpr auto operator|=(E& lhs, E rhs) noexcept -> detail::enable_if_enum_t { return lhs = lhs | rhs; } template constexpr auto operator&=(E& lhs, E rhs) noexcept -> detail::enable_if_enum_t { return lhs = lhs & rhs; } template constexpr auto operator^=(E& lhs, E rhs) noexcept -> detail::enable_if_enum_t { return lhs = lhs ^ rhs; } } // namespace magic_enum::bitwise_operators } // namespace magic_enum #if defined(_MSC_VER) # pragma warning(pop) #endif #endif // NEARGYE_MAGIC_ENUM_HPP