1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-10 23:44:29 +00:00
This commit is contained in:
terik23 2019-04-05 20:53:42 +05:00
parent 48e70234b5
commit 0ac07739f6
3 changed files with 106 additions and 80 deletions

View file

@ -47,5 +47,19 @@ int main() {
std::cout << c3_name.value() << std::endl; // BLUE std::cout << c3_name.value() << std::endl; // BLUE
} }
constexpr auto colors = magic_enum::enum_sequence<Color>();
std::cout << "Color sequence:";
for (auto e : colors) {
std::cout << " " << magic_enum::enum_to_string(e).value();
}
std::cout << std::endl;
constexpr auto colors_name = magic_enum::enum_to_string_sequence<Color>();
std::cout << "Color sequence:";
for (auto e : colors_name) {
std::cout << " " << e;
}
std::cout << std::endl;
return 0; return 0;
} }

View file

@ -5,7 +5,7 @@
// | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_| // | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
// |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____| // |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
// __/ | https://github.com/Neargye/magic_enum // __/ | https://github.com/Neargye/magic_enum
// |___/ vesion 0.1.1 // |___/ vesion 0.1.2
// //
// Licensed under the MIT License <http://opensource.org/licenses/MIT>. // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
@ -36,6 +36,8 @@
#include <limits> #include <limits>
#include <string_view> #include <string_view>
#include <optional> #include <optional>
#include <array>
#include <algorithm>
// Enum variable must be in range (-MAGIC_ENUM_RANGE, MAGIC_ENUM_RANGE). If you need a larger range, redefine the macro MAGIC_ENUM_RANGE. // Enum variable must be in range (-MAGIC_ENUM_RANGE, MAGIC_ENUM_RANGE). If you need a larger range, redefine the macro MAGIC_ENUM_RANGE.
#if !defined(MAGIC_ENUM_RANGE) #if !defined(MAGIC_ENUM_RANGE)
@ -46,8 +48,6 @@ namespace magic_enum {
static_assert(MAGIC_ENUM_RANGE > 0, static_assert(MAGIC_ENUM_RANGE > 0,
"MAGIC_ENUM_RANGE must be positive and greater than zero."); "MAGIC_ENUM_RANGE must be positive and greater than zero.");
static_assert(MAGIC_ENUM_RANGE % 8 == 0,
"MAGIC_ENUM_RANGE must be a multiple of 8.");
static_assert(MAGIC_ENUM_RANGE < std::numeric_limits<int>::max(), static_assert(MAGIC_ENUM_RANGE < std::numeric_limits<int>::max(),
"MAGIC_ENUM_RANGE must be less INT_MAX."); "MAGIC_ENUM_RANGE must be less INT_MAX.");
@ -58,7 +58,7 @@ namespace detail {
} }
template <typename E, E V> template <typename E, E V>
[[nodiscard]] constexpr std::optional<std::string_view> enum_to_string_impl() noexcept { [[nodiscard]] constexpr std::string_view enum_to_string_impl() noexcept {
static_assert(std::is_enum_v<E>, "magic_enum::enum_to_string require enum type."); static_assert(std::is_enum_v<E>, "magic_enum::enum_to_string require enum type.");
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9)
std::string_view name{__PRETTY_FUNCTION__}; std::string_view name{__PRETTY_FUNCTION__};
@ -82,113 +82,125 @@ template <typename E, E V>
if (name.length() > 0 && is_name_char(name.front(), true)) { if (name.length() > 0 && is_name_char(name.front(), true)) {
return name; return name;
} else { } else {
return std::nullopt; // Enum variable does not have name. return {}; // Enum variable does not have name.
} }
#endif #endif
} }
template <typename E, int V> template <typename E, int O, int... I>
struct enum_to_string_impl_t final { [[nodiscard]] constexpr std::string_view enum_to_string_impl(int value, std::integer_sequence<int, I...>) noexcept {
[[nodiscard]] constexpr std::optional<std::string_view> operator()(int value) const noexcept { constexpr std::size_t n = sizeof...(I);
static_assert(std::is_enum_v<E>, "magic_enum::enum_to_string require enum type."); constexpr std::array<std::string_view, n> names{{enum_to_string_impl<E, static_cast<E>(I + O)>()...}};
if constexpr (V > std::numeric_limits<std::underlying_type_t<E>>::max()) { return names[value - O];
return std::nullopt; // Enum variable out of range. }
}
switch (value - V) { template <typename E, int O, int... I>
case 0: [[nodiscard]] constexpr std::optional<E> enum_from_string_impl(std::string_view name, std::integer_sequence<int, I...>) noexcept {
return enum_to_string_impl<E, static_cast<E>(V)>(); std::optional<E> value;
case 1: (((enum_to_string_impl<E, static_cast<E>(I + O)>() == name) ? (value = static_cast<E>(I + O), false) : true) && ...);
return enum_to_string_impl<E, static_cast<E>(V + 1)>(); return value;
case 2: }
return enum_to_string_impl<E, static_cast<E>(V + 2)>();
case 3: template <typename E, int O, int... I>
return enum_to_string_impl<E, static_cast<E>(V + 3)>(); [[nodiscard]] constexpr decltype(auto) enum_sequence_impl(std::integer_sequence<int, I...>) noexcept {
case 4: constexpr std::size_t n = sizeof...(I);
return enum_to_string_impl<E, static_cast<E>(V + 4)>(); constexpr std::array<bool, n> valid{{!enum_to_string_impl<E, static_cast<E>(I + O)>().empty()...}};
case 5: constexpr std::size_t num_valid = ((valid[I] ? 1 : 0) + ...);
return enum_to_string_impl<E, static_cast<E>(V + 5)>();
case 6: std::array<E, num_valid> sequence{};
return enum_to_string_impl<E, static_cast<E>(V + 6)>(); int v = 0;
case 7: for (int i = 0; i < n && v < num_valid; ++i) {
return enum_to_string_impl<E, static_cast<E>(V + 7)>(); if (valid[i]) {
default: sequence[v++] = static_cast<E>(i + O);
return enum_to_string_impl_t<E, V + 8>{}(value);
} }
} }
};
template <typename E> return sequence;
struct enum_to_string_impl_t<E, MAGIC_ENUM_RANGE> final { }
[[nodiscard]] constexpr std::optional<std::string_view> operator()(int) const noexcept {
static_assert(std::is_enum_v<E>, "magic_enum::enum_to_string require enum type.");
return std::nullopt; // Enum variable out of range MAGIC_ENUM_RANGE.
}
};
template <typename E, int V> template <typename E, int O, int... I>
struct enum_from_string_impl_t final { [[nodiscard]] constexpr decltype(auto) enum_to_string_sequence_impl(std::integer_sequence<int, I...> s) noexcept {
[[nodiscard]] constexpr std::optional<E> operator()(std::string_view name) const noexcept { constexpr std::size_t n = sizeof...(I);
static_assert(std::is_enum_v<E>, "magic_enum::enum_from_string require enum type."); constexpr std::array<bool, n> valid{{!enum_to_string_impl<E, static_cast<E>(I + O)>().empty()...}};
if constexpr (V > std::numeric_limits<std::underlying_type_t<E>>::max()) { constexpr std::size_t num_valid = ((valid[I] ? 1 : 0) + ...);
return std::nullopt; // Enum variable out of range.
}
if (enum_to_string_impl<E, static_cast<E>(V)>() == name) { std::array<std::string_view, num_valid> sequence{};
return static_cast<E>(V); int v = 0;
} else if (enum_to_string_impl<E, static_cast<E>(V + 1)>() == name) { for (int i = 0; i < n && v < num_valid; ++i) {
return static_cast<E>(V + 1); if (valid[i]) {
} else if (enum_to_string_impl<E, static_cast<E>(V + 2)>() == name) { sequence[v++] = enum_to_string_impl<E, O>(i + O, s);
return static_cast<E>(V + 2);
} else if (enum_to_string_impl<E, static_cast<E>(V + 3)>() == name) {
return static_cast<E>(V + 3);
} else if (enum_to_string_impl<E, static_cast<E>(V + 4)>() == name) {
return static_cast<E>(V + 4);
} else if (enum_to_string_impl<E, static_cast<E>(V + 5)>() == name) {
return static_cast<E>(V + 5);
} else if (enum_to_string_impl<E, static_cast<E>(V + 6)>() == name) {
return static_cast<E>(V + 6);
} else if (enum_to_string_impl<E, static_cast<E>(V + 7)>() == name) {
return static_cast<E>(V + 7);
} else {
return enum_from_string_impl_t<E, V + 8>{}(name);
} }
} }
};
template <typename E> return sequence;
struct enum_from_string_impl_t<E, MAGIC_ENUM_RANGE> final { }
[[nodiscard]] constexpr std::optional<E> operator()(std::string_view) const noexcept {
static_assert(std::is_enum_v<E>, "magic_enum::enum_from_string require enum type.");
return std::nullopt; // Enum variable out of range MAGIC_ENUM_RANGE.
}
};
} // namespace detail } // namespace detail
// enum_to_string(enum) obtains string enum name from enum variable. // enum_to_string(enum) obtains string enum name from enum variable.
template <typename T, typename = std::enable_if_t<std::is_enum_v<std::decay_t<T>>>> template <typename T, typename = std::enable_if_t<std::is_enum_v<std::decay_t<T>>>>
[[nodiscard]] constexpr std::optional<std::string_view> enum_to_string(T value) noexcept { [[nodiscard]] constexpr std::optional<std::string_view> enum_to_string(T value) noexcept {
constexpr bool s = std::is_signed_v<std::underlying_type_t<std::decay_t<T>>>; using D = std::decay_t<T>;
constexpr int min = s ? -MAGIC_ENUM_RANGE : 0; using U = std::underlying_type_t<D>;
using C = std::common_type_t<decltype(MAGIC_ENUM_RANGE), U>;
constexpr bool s = std::is_signed_v<U>;
constexpr int min = s ? std::max<C>(-MAGIC_ENUM_RANGE, std::numeric_limits<U>::min()) : 0;
constexpr int max = std::min<C>(MAGIC_ENUM_RANGE, std::numeric_limits<U>::max());
constexpr int range = max - min;
if (static_cast<int>(value) >= MAGIC_ENUM_RANGE || static_cast<int>(value) <= -MAGIC_ENUM_RANGE) { if (static_cast<int>(value) >= MAGIC_ENUM_RANGE || static_cast<int>(value) <= -MAGIC_ENUM_RANGE) {
return std::nullopt; // Enum variable out of range MAGIC_ENUM_RANGE. return std::nullopt; // Enum variable out of range MAGIC_ENUM_RANGE.
} }
return detail::enum_to_string_impl_t<std::decay_t<T>, min>{}(static_cast<int>(value)); auto name = detail::enum_to_string_impl<D, min>(static_cast<int>(value), std::make_integer_sequence<int, range>{});
if (name.empty()) {
return std::nullopt;
} else {
return name; // Enum variable does not have name.
}
} }
// enum_to_string<enum>() obtains string enum name from static storage enum variable. // enum_to_string<enum>() obtains string enum name from static storage enum variable.
template <auto V, typename = std::enable_if_t<std::is_enum_v<std::decay_t<decltype(V)>>>> template <auto V, typename = std::enable_if_t<std::is_enum_v<std::decay_t<decltype(V)>>>>
[[nodiscard]] constexpr std::optional<std::string_view> enum_to_string() noexcept { [[nodiscard]] constexpr std::optional<std::string_view> enum_to_string() noexcept {
return detail::enum_to_string_impl<decltype(V), V>(); constexpr auto name = detail::enum_to_string_impl<decltype(V), V>();
if (name.empty()) {
return std::nullopt;
} else {
return name; // Enum variable does not have name.
}
} }
// enum_from_string(name) obtains enum variable from enum string name. // enum_from_string(name) obtains enum variable from enum string name.
template <typename E, typename = std::enable_if_t<std::is_enum_v<E>>> template <typename E, typename = std::enable_if_t<std::is_enum_v<E>>>
[[nodiscard]] constexpr std::optional<E> enum_from_string(std::string_view name) noexcept { [[nodiscard]] constexpr std::optional<E> enum_from_string(std::string_view name) noexcept {
constexpr bool s = std::is_signed_v<std::underlying_type_t<E>>; using U = std::underlying_type_t<E>;
constexpr int min = s ? -MAGIC_ENUM_RANGE : 0; using C = std::common_type_t<decltype(MAGIC_ENUM_RANGE), U>;
return detail::enum_from_string_impl_t<E, min>{}(name); constexpr bool s = std::is_signed_v<U>;
constexpr int min = s ? std::max<C>(-MAGIC_ENUM_RANGE, std::numeric_limits<U>::min()) : 0;
constexpr int max = std::min<C>(MAGIC_ENUM_RANGE, std::numeric_limits<U>::max());
constexpr int range = max - min;
return detail::enum_from_string_impl<E, min>(name, std::make_integer_sequence<int, range>{});
}
template <typename E, typename = std::enable_if_t<std::is_enum_v<E>>>
[[nodiscard]] constexpr decltype(auto) enum_sequence() noexcept {
using U = std::underlying_type_t<E>;
using C = std::common_type_t<decltype(MAGIC_ENUM_RANGE), U>;
constexpr bool s = std::is_signed_v<U>;
constexpr int min = s ? std::max<C>(-MAGIC_ENUM_RANGE, std::numeric_limits<U>::min()) : 0;
constexpr int max = std::min<C>(MAGIC_ENUM_RANGE, std::numeric_limits<U>::max());
constexpr int range = max - min;
return detail::enum_sequence_impl<E, min>(std::make_integer_sequence<int, range>{});
}
template <typename E, typename = std::enable_if_t<std::is_enum_v<E>>>
[[nodiscard]] constexpr decltype(auto) enum_to_string_sequence() noexcept {
using U = std::underlying_type_t<E>;
using C = std::common_type_t<decltype(MAGIC_ENUM_RANGE), U>;
constexpr bool s = std::is_signed_v<U>;
constexpr int min = s ? std::max<C>(-MAGIC_ENUM_RANGE, std::numeric_limits<U>::min()) : 0;
constexpr int max = std::min<C>(MAGIC_ENUM_RANGE, std::numeric_limits<U>::max());
constexpr int range = max - min;
return detail::enum_to_string_sequence_impl<E, min>(std::make_integer_sequence<int, range>{});
} }
} // namespace magic_enum } // namespace magic_enum

View file

@ -32,7 +32,7 @@ enum class Numbers : char { one = 10, two = 20, three = 30 };
enum Directions { Up = 85, Down = -42, Right = 119, Left = -119 }; enum Directions { Up = 85, Down = -42, Right = 119, Left = -119 };
enum number : int { one = 10, two = 20, three = 30 }; enum number : long { one = 10, two = 20, three = 30 };
TEST_CASE("magic_enum::enum_to_string(enum)") { TEST_CASE("magic_enum::enum_to_string(enum)") {
Color cr = Color::RED; Color cr = Color::RED;