1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-09 23:34:23 +00:00

improving

This commit is contained in:
terik23 2019-07-14 21:51:17 +05:00
parent 58c0b9ac59
commit ef5ab60f37

View file

@ -62,20 +62,23 @@ namespace magic_enum {
template <typename E>
struct enum_range final {
static_assert(std::is_enum_v<E>, "magic_enum::enum_range requires enum type.");
static constexpr int min = std::is_signed_v<std::underlying_type_t<E>> ? MAGIC_ENUM_RANGE_MIN : 0;
static constexpr int min = MAGIC_ENUM_RANGE_MIN;
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<int>::min)(),
"MAGIC_ENUM_RANGE_MIN must be greater than INT_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<int>::max)(),
"MAGIC_ENUM_RANGE_MAX must be less than INT_MAX.");
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<int>::min)(),
"MAGIC_ENUM_RANGE_MIN must be greater than INT_MIN.");
static_assert(MAGIC_ENUM_RANGE_MAX > MAGIC_ENUM_RANGE_MIN,
"MAGIC_ENUM_RANGE_MAX must be greater than MAGIC_ENUM_RANGE_MIN.");
namespace detail {
@ -91,6 +94,8 @@ template <typename E>
template <typename E>
[[nodiscard]] constexpr auto range_impl() {
static_assert(std::is_enum_v<E>, "magic_enum::detail::range_impl requires enum type.");
static_assert(enum_range<E>::min > (std::numeric_limits<int>::min)(), "magic_enum::enum_range requires min must be greater than INT_MIN.");
static_assert(enum_range<E>::max < (std::numeric_limits<int>::max)(), "magic_enum::enum_range requires max must be less than INT_MAX.");
static_assert(enum_range<E>::max > enum_range<E>::min, "magic_enum::enum_range requires max > min.");
using U = std::underlying_type_t<E>;
constexpr int max = enum_range<E>::max < (std::numeric_limits<U>::max)() ? enum_range<E>::max : (std::numeric_limits<U>::max)();
@ -148,7 +153,7 @@ template <typename E>
static_assert(std::is_enum_v<E>, "magic_enum::detail::name_impl requires enum type.");
constexpr auto names = strings_impl<E>(range_impl<E>());
if (int i = value - min_impl<E>(); i >= 0 && static_cast<std::size_t>(i) < names.size()) {
if (auto i = static_cast<std::size_t>(value - min_impl<E>()); i < names.size()) {
return names[i];
} else {
return {}; // Value out of range.
@ -158,14 +163,13 @@ template <typename E>
template <typename E, int... I>
[[nodiscard]] constexpr auto values_impl(std::integer_sequence<int, I...>) noexcept {
static_assert(std::is_enum_v<E>, "magic_enum::detail::values_impl requires enum type.");
constexpr int n = sizeof...(I);
constexpr std::array<bool, n> valid{{!name_impl<E, static_cast<E>(I + min_impl<E>())>().empty()...}};
constexpr int num_valid = ((valid[I] ? 1 : 0) + ...);
constexpr std::array<bool, sizeof...(I)> valid{{!name_impl<E, static_cast<E>(I + min_impl<E>())>().empty()...}};
constexpr auto num_valid = ((valid[I] ? 1 : 0) + ...);
std::array<E, num_valid> values{};
for (int i = 0, v = 0; i < n && v < num_valid; ++i) {
for (std::size_t i = 0, v = 0; i < valid.size() && v < num_valid; ++i) {
if (valid[i]) {
values[v++] = static_cast<E>(i + min_impl<E>());
values[v++] = static_cast<E>(static_cast<int>(i) + min_impl<E>());
}
}