// __ __ _ ______ _____ // | \/ | (_) | ____| / ____|_ _ // | \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_ // | |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _| // | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_| // |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____| // __/ | https://github.com/Neargye/magic_enum // |___/ vesion 0.4.1 // // 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. #pragma once #include #include #include #include #include #include #include #include #include namespace magic_enum { // Enum value must be in range [-256, 256]. If you need another range, add specialization enum_range for necessary enum type. template struct enum_range final { static_assert(std::is_enum_v, "magic_enum::enum_range requires enum type."); static constexpr int min = std::is_signed_v> ? -256 : 0; static constexpr int max = 256; }; namespace detail { template > [[nodiscard]] constexpr int min_impl() { static_assert(std::is_enum_v, "magic_enum::detail::min_impl requires enum type."); constexpr int min = enum_range::min > (std::numeric_limits::min)() ? enum_range::min : (std::numeric_limits::min)(); return min; } template > [[nodiscard]] constexpr decltype(auto) range_impl() { static_assert(std::is_enum_v, "magic_enum::detail::range_impl requires enum type."); static_assert(enum_range::max > enum_range::min, "magic_enum::enum_range requires max > min."); constexpr int max = enum_range::max < (std::numeric_limits::max)() ? enum_range::max : (std::numeric_limits::max)(); constexpr auto range = std::make_integer_sequence() + 1>{}; return range; } [[nodiscard]] constexpr bool is_name_char(char c, bool front) noexcept { return (!front && c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; } template [[nodiscard]] constexpr std::string_view name_impl() noexcept { static_assert(std::is_enum_v, "magic_enum::detail::name_impl requires enum type."); #if defined(__clang__) std::string_view name{__PRETTY_FUNCTION__}; constexpr auto suffix = sizeof("]") - 1; #elif defined(__GNUC__) && __GNUC__ >= 9 std::string_view name{__PRETTY_FUNCTION__}; constexpr auto suffix = sizeof("; std::string_view = std::basic_string_view]") - 1; #elif defined(_MSC_VER) std::string_view name{__FUNCSIG__}; constexpr auto suffix = sizeof(">(void) noexcept") - 1; #else return {}; // Unsupported compiler. #endif #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) name.remove_suffix(suffix); for (std::size_t i = name.size(); i > 0; --i) { if (!is_name_char(name[i - 1], false)) { name.remove_prefix(i); break; } } if (name.length() > 0 && is_name_char(name.front(), true)) { return name; } else { return {}; // Value does not have name. } #endif } template [[nodiscard]] constexpr decltype(auto) strings_impl(std::integer_sequence) noexcept { static_assert(std::is_enum_v, "magic_enum::detail::strings_impl requires enum type."); constexpr std::array names{{name_impl(I + min_impl())>()...}}; return names; } template [[nodiscard]] constexpr std::string_view name_impl(int value) noexcept { static_assert(std::is_enum_v, "magic_enum::detail::name_impl requires enum type."); constexpr auto names = strings_impl(range_impl()); const int i = value - min_impl(); if (i >= 0 && static_cast(i) < names.size()) { return names[i]; } else { return {}; // Value out of range. } } template [[nodiscard]] constexpr decltype(auto) values_impl(std::integer_sequence) noexcept { static_assert(std::is_enum_v, "magic_enum::detail::values_impl requires enum type."); constexpr int n = sizeof...(I); constexpr std::array valid{{!name_impl(I + min_impl())>().empty()...}}; constexpr int num_valid = ((valid[I] ? 1 : 0) + ...); std::array enums{}; for (int i = 0, v = 0; i < n && v < num_valid; ++i) { if (valid[i]) { enums[v++] = static_cast(i + min_impl()); } } return enums; } template [[nodiscard]] constexpr decltype(auto) names_impl(std::integer_sequence) noexcept { static_assert(std::is_enum_v, "magic_enum::detail::names_impl requires enum type."); constexpr auto enums = values_impl(range_impl()); constexpr std::array names{{name_impl()...}}; return names; } template [[nodiscard]] constexpr std::optional enum_cast_impl(std::string_view value) noexcept { static_assert(std::is_enum_v, "magic_enum::detail::enum_cast_impl requires enum type."); constexpr auto values = values_impl(range_impl()); constexpr auto count = values.size(); constexpr auto names = names_impl(std::make_index_sequence{}); for (std::size_t i = 0; i < count; ++i) { if (names[i] == value) { return values[i]; } } return std::nullopt; // Invalid value or out of range. } template using enable_if_enum_t = typename std::enable_if>::type; template> struct is_scoped_enum_impl : std::false_type {}; template struct is_scoped_enum_impl : std::bool_constant>> {}; template> struct is_unscoped_enum_impl : std::false_type {}; template struct is_unscoped_enum_impl : std::bool_constant>> {}; } // namespace magic_enum::detail // 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_impl {}; 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_impl {}; template inline constexpr bool is_scoped_enum_v = is_scoped_enum::value; // Obtains enum value from enum string name. template > [[nodiscard]] constexpr std::optional enum_cast(std::string_view value) noexcept { static_assert(std::is_enum_v, "magic_enum::enum_cast requires enum type."); return detail::enum_cast_impl(value); } // Obtains enum value from integer value. template > [[nodiscard]] constexpr std::optional enum_cast(std::underlying_type_t value) noexcept { static_assert(std::is_enum_v, "magic_enum::enum_cast requires enum type."); if (detail::name_impl(static_cast(value)).empty()) { return std::nullopt; // Invalid value or out of range. } else { return static_cast(value); } } // Returns enum value at specified index. No bounds checking is performed: the behavior is undefined if index >= number of enum values. template> [[nodiscard]] constexpr E enum_value(std::size_t index) { static_assert(std::is_enum_v, "magic_enum::enum_value requires enum type."); constexpr auto values = detail::values_impl(detail::range_impl()); return assert(index < values.size()), values[index]; } // Obtains value enum sequence. template > [[nodiscard]] constexpr decltype(auto) enum_values() noexcept { static_assert(std::is_enum_v, "magic_enum::enum_values requires enum type."); constexpr auto values = detail::values_impl(detail::range_impl()); return values; } // Returns number of enum values. template > [[nodiscard]] constexpr std::size_t enum_count() noexcept { static_assert(std::is_enum_v, "magic_enum::enum_count requires enum type."); constexpr auto count = detail::values_impl(detail::range_impl()).size(); return count; } // Obtains string enum name from enum value. template , typename = detail::enable_if_enum_t> [[nodiscard]] constexpr std::optional enum_name(E value) noexcept { static_assert(std::is_enum_v, "magic_enum::enum_name requires enum type."); const auto name = detail::name_impl(static_cast(value)); if (name.empty()) { return std::nullopt; // Invalid value or out of range. } else { return name; } } // Obtains string enum name sequence. template > [[nodiscard]] constexpr decltype(auto) enum_names() noexcept { static_assert(std::is_enum_v, "magic_enum::enum_names requires enum type."); constexpr auto count = detail::values_impl(detail::range_impl()).size(); constexpr auto names = detail::names_impl(std::make_index_sequence{}); return names; } namespace ops { template , typename = detail::enable_if_enum_t> std::ostream& operator<<(std::ostream& os, E value) { static_assert(std::is_enum_v, "magic_enum::ops::operator<< requires enum type."); const auto name = detail::name_impl(static_cast(value)); if (!name.empty()) { os << name; } return os; } template > std::ostream& operator<<(std::ostream& os, std::optional value) { static_assert(std::is_enum_v, "magic_enum::ops::operator<< requires enum type."); if (value.has_value()) { const auto name = detail::name_impl(static_cast(value.value())); if (!name.empty()) { os << name; } } return os; } } // namespace magic_enum::ops } // namespace magic_enum