mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-15 00:24:20 +00:00
v0.2.0
This commit is contained in:
parent
122aa8028d
commit
5a3d4b53e6
5 changed files with 105 additions and 70 deletions
|
|
@ -5,7 +5,7 @@
|
|||
// | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
|
||||
// |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
|
||||
// __/ | https://github.com/Neargye/magic_enum
|
||||
// |___/ vesion 0.1.2
|
||||
// |___/ vesion 0.2.0
|
||||
//
|
||||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
|
@ -39,7 +39,7 @@
|
|||
#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 value 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)
|
||||
# define MAGIC_ENUM_RANGE 128
|
||||
#endif
|
||||
|
|
@ -60,14 +60,17 @@ namespace detail {
|
|||
template <typename E, E V>
|
||||
[[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.");
|
||||
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9)
|
||||
#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<char>]") - 1;
|
||||
#elif defined(_MSC_VER)
|
||||
std::string_view name{__FUNCSIG__};
|
||||
constexpr auto suffix = sizeof(">(void) noexcept") - 1;
|
||||
#else
|
||||
return std::nullopt; // Unsupported compiler.
|
||||
return {}; // Unsupported compiler.
|
||||
#endif
|
||||
|
||||
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER)
|
||||
|
|
@ -82,34 +85,38 @@ template <typename E, E V>
|
|||
if (name.length() > 0 && is_name_char(name.front(), true)) {
|
||||
return name;
|
||||
} else {
|
||||
return {}; // Enum variable does not have name.
|
||||
return {}; // Enum value does not have name.
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename E, int O, int... I>
|
||||
[[nodiscard]] constexpr std::string_view enum_to_string_impl(int value, std::integer_sequence<int, I...>) noexcept {
|
||||
constexpr std::size_t n = sizeof...(I);
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::enum_to_string require enum type.");
|
||||
constexpr int n = sizeof...(I);
|
||||
constexpr std::array<std::string_view, n> names{{enum_to_string_impl<E, static_cast<E>(I + O)>()...}};
|
||||
|
||||
return names[value - O];
|
||||
}
|
||||
|
||||
template <typename E, int O, int... I>
|
||||
[[nodiscard]] constexpr std::optional<E> enum_from_string_impl(std::string_view name, std::integer_sequence<int, I...>) noexcept {
|
||||
std::optional<E> value;
|
||||
(((enum_to_string_impl<E, static_cast<E>(I + O)>() == name) ? (value = static_cast<E>(I + O), false) : true) && ...);
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::enum_from_string require enum type.");
|
||||
std::optional<E> value{std::nullopt};
|
||||
(void)(((enum_to_string_impl<E, static_cast<E>(I + O)>() == name) ? (value = static_cast<E>(I + O), false) : true) && ...);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
template <typename E, int O, int... I>
|
||||
[[nodiscard]] constexpr decltype(auto) enum_sequence_impl(std::integer_sequence<int, I...>) noexcept {
|
||||
constexpr std::size_t n = sizeof...(I);
|
||||
[[nodiscard]] constexpr decltype(auto) enum_to_sequence_impl(std::integer_sequence<int, I...>) noexcept {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::enum_to_sequence require enum type.");
|
||||
constexpr int n = sizeof...(I);
|
||||
constexpr std::array<bool, n> valid{{!enum_to_string_impl<E, static_cast<E>(I + O)>().empty()...}};
|
||||
constexpr std::size_t num_valid = ((valid[I] ? 1 : 0) + ...);
|
||||
constexpr int num_valid = ((valid[I] ? 1 : 0) + ...);
|
||||
|
||||
std::array<E, num_valid> sequence{};
|
||||
int v = 0;
|
||||
for (int i = 0; i < n && v < num_valid; ++i) {
|
||||
for (int i = 0, v = 0; i < n && v < num_valid; ++i) {
|
||||
if (valid[i]) {
|
||||
sequence[v++] = static_cast<E>(i + O);
|
||||
}
|
||||
|
|
@ -120,13 +127,13 @@ template <typename E, int O, int... I>
|
|||
|
||||
template <typename E, int O, int... I>
|
||||
[[nodiscard]] constexpr decltype(auto) enum_to_string_sequence_impl(std::integer_sequence<int, I...> s) noexcept {
|
||||
constexpr std::size_t n = sizeof...(I);
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::enum_to_string_sequence require enum type.");
|
||||
constexpr int n = sizeof...(I);
|
||||
constexpr std::array<bool, n> valid{{!enum_to_string_impl<E, static_cast<E>(I + O)>().empty()...}};
|
||||
constexpr std::size_t num_valid = ((valid[I] ? 1 : 0) + ...);
|
||||
constexpr int num_valid = ((valid[I] ? 1 : 0) + ...);
|
||||
|
||||
std::array<std::string_view, num_valid> sequence{};
|
||||
int v = 0;
|
||||
for (int i = 0; i < n && v < num_valid; ++i) {
|
||||
for (int i = 0, v = 0; i < n && v < num_valid; ++i) {
|
||||
if (valid[i]) {
|
||||
sequence[v++] = enum_to_string_impl<E, O>(i + O, s);
|
||||
}
|
||||
|
|
@ -142,19 +149,20 @@ 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 {
|
||||
using D = std::decay_t<T>;
|
||||
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;
|
||||
using C = std::common_type_t<int, U>;
|
||||
constexpr int min = std::max<C>(std::is_signed_v<U> ? -MAGIC_ENUM_RANGE : 0, std::numeric_limits<U>::min());
|
||||
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) {
|
||||
return std::nullopt; // Enum variable out of range MAGIC_ENUM_RANGE.
|
||||
constexpr int range = max - min + 1;
|
||||
|
||||
if (static_cast<int>(value) > max || static_cast<int>(value) < min) {
|
||||
return std::nullopt; // Enum value out of range.
|
||||
}
|
||||
auto name = detail::enum_to_string_impl<D, min>(static_cast<int>(value), std::make_integer_sequence<int, range>{});
|
||||
|
||||
const 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.
|
||||
return name; // Enum value does not have name.
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -162,46 +170,47 @@ template <typename T, typename = std::enable_if_t<std::is_enum_v<std::decay_t<T>
|
|||
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 {
|
||||
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.
|
||||
return name; // Enum value does not have name.
|
||||
}
|
||||
}
|
||||
|
||||
// enum_from_string(name) obtains enum variable from enum string name.
|
||||
// enum_from_string(name) obtains enum value from enum string name.
|
||||
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 {
|
||||
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;
|
||||
using C = std::common_type_t<int, U>;
|
||||
constexpr int min = std::max<C>(std::is_signed_v<U> ? -MAGIC_ENUM_RANGE : 0, std::numeric_limits<U>::min());
|
||||
constexpr int max = std::min<C>(MAGIC_ENUM_RANGE, std::numeric_limits<U>::max());
|
||||
constexpr int range = max - min;
|
||||
constexpr int range = max - min + 1;
|
||||
|
||||
return detail::enum_from_string_impl<E, min>(name, std::make_integer_sequence<int, range>{});
|
||||
}
|
||||
|
||||
// enum_sequence<enum>() obtains string enum sequence.
|
||||
// enum_to_sequence<enum>() obtains value enum sequence.
|
||||
template <typename E, typename = std::enable_if_t<std::is_enum_v<E>>>
|
||||
[[nodiscard]] constexpr decltype(auto) enum_sequence() noexcept {
|
||||
[[nodiscard]] constexpr decltype(auto) enum_to_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;
|
||||
using C = std::common_type_t<int, U>;
|
||||
constexpr int min = std::max<C>(std::is_signed_v<U> ? -MAGIC_ENUM_RANGE : 0, std::numeric_limits<U>::min());
|
||||
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>{});
|
||||
constexpr int range = max - min + 1;
|
||||
|
||||
return detail::enum_to_sequence_impl<E, min>(std::make_integer_sequence<int, range>{});
|
||||
}
|
||||
|
||||
// enum_to_string_sequence<enum>() obtains string enum name sequence.
|
||||
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;
|
||||
using C = std::common_type_t<int, U>;
|
||||
constexpr int min = std::max<C>(std::is_signed_v<U> ? -MAGIC_ENUM_RANGE : 0, std::numeric_limits<U>::min());
|
||||
constexpr int max = std::min<C>(MAGIC_ENUM_RANGE, std::numeric_limits<U>::max());
|
||||
constexpr int range = max - min;
|
||||
constexpr int range = max - min + 1;
|
||||
|
||||
return detail::enum_to_string_sequence_impl<E, min>(std::make_integer_sequence<int, range>{});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue