diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index 59387f8..fc1e079 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -142,31 +142,33 @@ constexpr std::string_view pretty_name(std::string_view name) noexcept { } template -constexpr bool mixed_sign_less(L left, R right) noexcept { - static_assert(std::is_integral_v, "L must be an integral value"); - static_assert(std::is_integral_v, "R must be an integral value"); +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_signed_v) { - // If same signedness (both signed or both unsigned) - return left < right; - } - else if constexpr(std::is_signed_v) { - // if 'left' is negative, then result is 'true', otherwise cast & compare - return left < 0 || static_cast>(left) < right; - } - else { // std::is_signed_v - // if 'right' is negative, then result is 'false', otherwise cast & compare - return right >= 0 && left < static_cast>(right); + // If same signedness (both signed or both unsigned). + return lhs < rhs; + } else if constexpr(std::is_signed_v) { + // If 'left' is negative, then result is 'true', otherwise cast & compare. + return lhs < 0 || static_cast>(lhs) < rhs; + } else { // std::is_signed_v + // If 'right' is negative, then result is 'false', otherwise cast & compare. + return rhs >= 0 && lhs < static_cast>(rhs); } } template -constexpr int mixed_sign_min_as_int(L left, R right) noexcept { - return mixed_sign_less(left, right) ? static_cast(left) : static_cast(right); +constexpr int mixed_sign_min_as_int(L lhs, R rhs) noexcept { + static_assert(std::is_integral_v && std::is_integral_v, "magic_enum::detail::mixed_sign_min_as_int requires integral type."); + + return mixed_sign_less(lhs, rhs) ? static_cast(lhs) : static_cast(rhs); } template -constexpr int mixed_sign_max_as_int(L left, R right) noexcept { - return mixed_sign_less(left, right) ? static_cast(right) : static_cast(left); +constexpr int mixed_sign_max_as_int(L lhs, R rhs) noexcept { + static_assert(std::is_integral_v && std::is_integral_v, "magic_enum::detail::mixed_sign_max_as_int requires integral type."); + + return mixed_sign_less(lhs, rhs) ? static_cast(rhs) : static_cast(lhs); } template @@ -210,10 +212,10 @@ template inline constexpr auto name_v = n(); template -inline constexpr int reflected_min_v = mixed_sign_max_as_int(enum_range::min, std::numeric_limits>::min()); +inline constexpr int reflected_min_v = mixed_sign_max_as_int(enum_range::min, (std::numeric_limits>::min)()); template -inline constexpr int reflected_max_v = mixed_sign_min_as_int(enum_range::max, std::numeric_limits>::max()); +inline constexpr int reflected_max_v = mixed_sign_min_as_int(enum_range::max, (std::numeric_limits>::max)()); template constexpr std::size_t reflected_size() { @@ -228,49 +230,33 @@ constexpr std::size_t reflected_size() { return static_cast(size); } -template -inline constexpr std::size_t reflected_size_v = reflected_size(); - template -constexpr std::array> reflected_set_v_helper(std::integer_sequence) { - return {{(n(I + reflected_min_v)>().size() != 0)...}}; +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 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++] = static_cast(static_cast(i) + reflected_min_v); + } + } + + return values; } + template -inline constexpr std::array> reflected_set_v = reflected_set_v_helper(std::make_integer_sequence>{}); +inline constexpr auto values_v = values(std::make_integer_sequence()>{}); template -constexpr int range_min() noexcept { - static_assert(is_enum_v, "magic_enum::detail::range_min requires enum type."); - - // Find leftmost value. - for (int i = 0; i < static_cast(reflected_size_v); ++i) { - if (reflected_set_v[i]) { - return i + reflected_min_v; - } - } - - return reflected_max_v; -} +inline constexpr std::size_t count_v = values_v.size(); template -constexpr int range_max() noexcept { - static_assert(is_enum_v, "magic_enum::detail::range_max requires enum type."); - - // Find rightmost value - for (int i = static_cast(reflected_size_v) - 1; i >= 0; --i) { - if (reflected_set_v[i]) { - return i + reflected_min_v; - } - } - - return reflected_min_v; -} +inline constexpr int min_v = values_v.empty() ? 0 : static_cast(values_v.front()); template -inline constexpr int min_v = range_min(); - -template -inline constexpr int max_v = range_max(); +inline constexpr int max_v = values_v.empty() ? 0 : static_cast(values_v.back()); template constexpr std::size_t range_size() noexcept { @@ -288,34 +274,9 @@ inline constexpr std::size_t size_v = range_size(); template inline constexpr auto range_v = std::make_integer_sequence>{}; -template -constexpr std::size_t count(std::integer_sequence) noexcept { - static_assert(is_enum_v, "magic_enum::detail::count requires enum type."); - - return (((n(I + min_v)>().size() != 0) ? 1 : 0) + ...); -} - -template -inline constexpr std::size_t count_v = count(range_v); - template inline constexpr auto sequence_v = std::make_index_sequence>{}; -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 + min_v)>().size() != 0)...}}; - - std::array> values{}; - for (std::size_t i = 0, v = 0; v < count_v; ++i) { - if (valid[i]) { - values[v++] = static_cast(static_cast(i) + min_v); - } - } - - return values; -} - template using index_t = std::conditional_t < (std::numeric_limits::max)(), std::uint8_t, std::uint16_t>; @@ -333,17 +294,15 @@ constexpr auto indexes(std::integer_sequence) noexcept { template constexpr auto names(std::index_sequence) noexcept { static_assert(is_enum_v, "magic_enum::detail::names requires enum type."); - constexpr auto vals = values(range_v); - return std::array>{{name_v...}}; + 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."); - constexpr auto vals = values(range_v); - return std::array, count_v>{{{vals[I], name_v}...}}; + return std::array, count_v>{{{values_v[I], name_v[I]>}...}}; } template @@ -381,7 +340,7 @@ struct enum_traits>> { inline static constexpr bool is_scoped_enum = detail::is_scoped_enum::value; inline static constexpr std::size_t count = detail::count_v; - inline static constexpr std::array values = detail::values(range_v); + inline static constexpr std::array values = detail::values_v; inline static constexpr std::array names = detail::names(sequence_v); inline static constexpr std::array, count> entries = detail::entries(sequence_v);