mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
change check support compiler
This commit is contained in:
parent
75c9d9bce5
commit
c947afdbae
2 changed files with 25 additions and 23 deletions
|
|
@ -33,7 +33,7 @@ int main() {
|
||||||
std::cout << c1_name << std::endl; // RED
|
std::cout << c1_name << std::endl; // RED
|
||||||
|
|
||||||
// String enum name sequence.
|
// String enum name sequence.
|
||||||
constexpr auto& color_names = magic_enum::enum_names<Color>();
|
constexpr auto color_names = magic_enum::enum_names<Color>();
|
||||||
std::cout << "Color names:";
|
std::cout << "Color names:";
|
||||||
for (auto n : color_names) {
|
for (auto n : color_names) {
|
||||||
std::cout << " " << n;
|
std::cout << " " << n;
|
||||||
|
|
@ -70,7 +70,7 @@ int main() {
|
||||||
std::cout << "Color[0] = " << magic_enum::enum_value<Color>(0) << std::endl; // Color[0] = RED
|
std::cout << "Color[0] = " << magic_enum::enum_value<Color>(0) << std::endl; // Color[0] = RED
|
||||||
|
|
||||||
// Enum value sequence.
|
// Enum value sequence.
|
||||||
constexpr auto& colors = magic_enum::enum_values<Color>();
|
constexpr auto colors = magic_enum::enum_values<Color>();
|
||||||
std::cout << "Colors sequence:";
|
std::cout << "Colors sequence:";
|
||||||
for (Color c : colors) {
|
for (Color c : colors) {
|
||||||
std::cout << " " << c; // ostream operator for enum.
|
std::cout << " " << c; // ostream operator for enum.
|
||||||
|
|
@ -106,7 +106,7 @@ int main() {
|
||||||
// Enum pair (value enum, string enum name) sequence.
|
// Enum pair (value enum, string enum name) sequence.
|
||||||
constexpr auto color_entries = magic_enum::enum_entries<Color>();
|
constexpr auto color_entries = magic_enum::enum_entries<Color>();
|
||||||
std::cout << "Colors entries:";
|
std::cout << "Colors entries:";
|
||||||
for (auto& e : color_entries) {
|
for (auto e : color_entries) {
|
||||||
std::cout << " " << e.second << " = " << static_cast<int>(e.first);
|
std::cout << " " << e.second << " = " << static_cast<int>(e.first);
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,10 @@
|
||||||
|
|
||||||
namespace magic_enum {
|
namespace magic_enum {
|
||||||
|
|
||||||
|
#if defined(__clang__) || defined(__GNUC__) && __GNUC__>= 9 || defined(_MSC_VER)
|
||||||
|
# define MAGIC_ENUM_SUPPORTED 1
|
||||||
|
#endif
|
||||||
|
|
||||||
// Enum value must be in range [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]. By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 128.
|
// Enum value must be in range [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]. By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 128.
|
||||||
// If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX.
|
// If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX.
|
||||||
// If need another range for specific enum type, add specialization enum_range for necessary enum type.
|
// If need another range for specific enum type, add specialization enum_range for necessary enum type.
|
||||||
|
|
@ -79,7 +83,7 @@ namespace detail {
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct supported final
|
struct supported final
|
||||||
#if defined(__clang__) || defined(__GNUC__) && __GNUC__>= 9 || defined(_MSC_VER) || defined(MAGIC_ENUM_NO_CHECK_SUPPORT)
|
#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED
|
||||||
: std::true_type {};
|
: std::true_type {};
|
||||||
#else
|
#else
|
||||||
: std::false_type {};
|
: std::false_type {};
|
||||||
|
|
@ -139,20 +143,19 @@ constexpr std::string_view pretty_name(std::string_view name) noexcept {
|
||||||
template <typename E>
|
template <typename E>
|
||||||
constexpr auto n() noexcept {
|
constexpr auto n() noexcept {
|
||||||
static_assert(is_enum_v<E>, "magic_enum::detail::n requires enum type.");
|
static_assert(is_enum_v<E>, "magic_enum::detail::n requires enum type.");
|
||||||
#if defined(__clang__)
|
#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED
|
||||||
|
# if defined(__clang__)
|
||||||
constexpr std::string_view name{__PRETTY_FUNCTION__ + 34, sizeof(__PRETTY_FUNCTION__) - 36};
|
constexpr std::string_view name{__PRETTY_FUNCTION__ + 34, sizeof(__PRETTY_FUNCTION__) - 36};
|
||||||
#elif defined(__GNUC__)
|
# elif defined(__GNUC__)
|
||||||
constexpr std::string_view name{__PRETTY_FUNCTION__ + 49, sizeof(__PRETTY_FUNCTION__) - 51};
|
constexpr std::string_view name{__PRETTY_FUNCTION__ + 49, sizeof(__PRETTY_FUNCTION__) - 51};
|
||||||
#elif defined(_MSC_VER)
|
# elif defined(_MSC_VER)
|
||||||
constexpr std::string_view name{__FUNCSIG__ + 40, sizeof(__FUNCSIG__) - 57};
|
constexpr std::string_view name{__FUNCSIG__ + 40, sizeof(__FUNCSIG__) - 57};
|
||||||
|
# endif
|
||||||
|
return static_string<name.size()>{name};
|
||||||
|
#else
|
||||||
|
static_assert(supported<E>::value, "magic_enum: Unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility).");
|
||||||
|
return std::string_view{}; // Unsupported compiler.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if constexpr (supported<E>::value) {
|
|
||||||
return static_string<name.size()>{name};
|
|
||||||
} else {
|
|
||||||
static_assert(supported<E>::value, "magic_enum: Unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility).");
|
|
||||||
return std::string_view{}; // Unsupported compiler.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename E>
|
template <typename E>
|
||||||
|
|
@ -161,18 +164,17 @@ inline constexpr auto type_name_v = n<E>();
|
||||||
template <typename E, E V>
|
template <typename E, E V>
|
||||||
constexpr auto n() noexcept {
|
constexpr auto n() noexcept {
|
||||||
static_assert(is_enum_v<E>, "magic_enum::detail::n requires enum type.");
|
static_assert(is_enum_v<E>, "magic_enum::detail::n requires enum type.");
|
||||||
#if defined(__clang__) || defined(__GNUC__)
|
#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED
|
||||||
|
# if defined(__clang__) || defined(__GNUC__)
|
||||||
constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
|
constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
|
||||||
#elif defined(_MSC_VER)
|
# elif defined(_MSC_VER)
|
||||||
constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
|
constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
|
||||||
|
# endif
|
||||||
|
return static_string<name.size()>{name};
|
||||||
|
#else
|
||||||
|
static_assert(supported<E>::value, "magic_enum: Unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility).");
|
||||||
|
return std::string_view{}; // Unsupported compiler.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if constexpr (supported<E>::value) {
|
|
||||||
return static_string<name.size()>{name};
|
|
||||||
} else {
|
|
||||||
static_assert(supported<E>::value, "magic_enum: Unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility).");
|
|
||||||
return std::string_view{}; // Unsupported compiler.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename E, E V>
|
template <typename E, E V>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue