From b9450675bbbd8779da5088353cf52417c567eb85 Mon Sep 17 00:00:00 2001 From: neargye Date: Sun, 24 May 2020 16:23:01 +0500 Subject: [PATCH] clean-up --- include/magic_enum.hpp | 90 +++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index d5db725..acbd6ee 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -331,14 +331,14 @@ template inline constexpr auto entries_v = entries(std::make_index_sequence>{}); template -inline static constexpr bool is_sparse = range_size_v != count_v; +inline constexpr bool is_sparse_v = range_size_v != count_v; template > constexpr int 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 >= static_cast(min_v) && value <= static_cast(max_v)) { - if constexpr (is_sparse) { + if constexpr (is_sparse_v) { if (const auto idx = indexes_v[i]; idx != invalid_index_v) { return idx; } @@ -416,7 +416,8 @@ using underlying_type_t = typename underlying_type::type; // Returns string name of enum type. template [[nodiscard]] constexpr auto enum_type_name() noexcept -> detail::enable_if_enum_t { - constexpr std::string_view name = detail::type_name_v>; + using D = std::decay_t; + constexpr std::string_view name = detail::type_name_v; static_assert(name.size() > 0, "Enum type does not have a name."); return name; @@ -425,21 +426,22 @@ template // Returns number of enum values. template [[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); + using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); - return detail::count_v>; + 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> { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); - if constexpr (detail::is_sparse) { + if constexpr (detail::is_sparse_v) { return assert(index < detail::count_v), detail::values_v[index]; } else { return assert(index < detail::count_v), static_cast(index + detail::min_v); @@ -450,10 +452,11 @@ template // Returns std::array with enum values, sorted by enum value. template [[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t>)&> { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); + using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); - return detail::values_v>; + return detail::values_v; } // Returns string enum name from static storage enum variable. @@ -470,9 +473,9 @@ template // 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 { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); if (const auto i = detail::endex(value); i != -1) { return detail::names_v[i]; @@ -485,30 +488,32 @@ template // Returns std::array with string enum names, sorted by enum value. template [[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t>)&> { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); + using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); - return detail::names_v>; + return detail::names_v; } // 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>)&> { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); + using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); - return detail::entries_v>; + return detail::entries_v; } // Obtains enum value from enum 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>> { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); - static_assert(std::is_invocable_r_v, "magic_enum::enum_cast requires bool(char, char) invocable predicate."); using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); + static_assert(std::is_invocable_r_v, "magic_enum::enum_cast requires bool(char, char) invocable predicate."); if constexpr (detail::range_size_v > detail::count_v * 2) { for (std::size_t i = 0; i < detail::count_v; ++i) { @@ -529,19 +534,20 @@ template template [[nodiscard]] constexpr auto enum_cast(std::string_view value) noexcept -> detail::enable_if_enum_t>> { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); + using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); - return enum_cast(value, detail::char_equal{}); + return enum_cast(value, detail::char_equal{}); } // 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>> { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); if (detail::undex(value) != -1) { return static_cast(value); @@ -560,10 +566,11 @@ template // Returns std::optional with index. template [[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t> { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); + using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); - if (const auto i = detail::endex>(value); i != -1) { + if (const auto i = detail::endex(value); i != -1) { return i; } @@ -573,28 +580,31 @@ template // Checks whether enum contains enumerator with such value. template [[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_t { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); + using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); - return detail::endex>(value) != -1; + return detail::endex(value) != -1; } // 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 { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); + using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); - return detail::undex>(value) != -1; + return detail::undex(value) != -1; } // Checks whether enum contains enumerator with such string enum name. template [[nodiscard]] constexpr auto enum_contains(std::string_view value) noexcept -> detail::enable_if_enum_t { - static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - static_assert(detail::count_v> > 0, "magic_enum requires enum implementation and valid max and min."); + using D = std::decay_t; + static_assert(detail::supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); - return enum_cast(value).has_value(); + return enum_cast(value).has_value(); } namespace ostream_operators {