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

fix cvref

This commit is contained in:
neargye 2019-10-03 14:50:09 +05:00
parent 3f0aad9f74
commit 0b262f7cc8
2 changed files with 92 additions and 79 deletions

View file

@ -301,7 +301,11 @@ constexpr auto entries(std::index_sequence<I...>) noexcept {
}
template <typename T, typename R>
using enable_if_enum_t = std::enable_if_t<std::is_enum_v<T>, R>;
using enable_if_enum_t = std::enable_if_t<std::is_enum_v<std::decay_t<T>>, R>;
} // namespace magic_enum::detail
namespace traits {
template <typename T, bool = std::is_enum_v<T>>
struct is_scoped_enum : std::false_type {};
@ -315,42 +319,13 @@ struct is_unscoped_enum : std::false_type {};
template <typename T>
struct is_unscoped_enum<T, true> : std::bool_constant<std::is_convertible_v<T, std::underlying_type_t<T>>> {};
template <typename T, bool = std::is_enum_v<T>>
template <typename T, bool = std::is_enum_v<std::decay_t<T>>>
struct underlying_type {};
template <typename T>
struct underlying_type<T, true> : std::underlying_type<T> {};
struct underlying_type<T, true> : std::underlying_type<std::decay_t<T>> {};
} // namespace magic_enum::detail
// Checks is magic_enum supported compiler.
inline constexpr bool is_magic_enum_supported = detail::supported<void>::value;
// Checks whether T is an Unscoped enumeration type.
// Provides the member constant value which is equal to true, if T is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration) type. Otherwise, value is equal to false.
template <typename T>
struct is_unscoped_enum : detail::is_unscoped_enum<T> {};
template <typename T>
inline constexpr bool is_unscoped_enum_v = is_unscoped_enum<T>::value;
// Checks whether T is an Scoped enumeration type.
// Provides the member constant value which is equal to true, if T is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations) type. Otherwise, value is equal to false.
template <typename T>
struct is_scoped_enum : detail::is_scoped_enum<T> {};
template <typename T>
inline constexpr bool is_scoped_enum_v = is_scoped_enum<T>::value;
// If T is a complete enumeration type, provides a member typedef type that names the underlying type of T.
// Otherwise, if T is not an enumeration type, there is no member type. Otherwise (T is an incomplete enumeration type), the program is ill-formed.
template <typename T>
struct underlying_type : detail::underlying_type<T> {};
template <typename T>
using underlying_type_t = typename underlying_type<T>::type;
template <typename E, typename EnableIfEnum = void>
template <typename E, typename = void>
struct enum_traits {};
template <typename E>
@ -360,8 +335,8 @@ struct enum_traits<E, std::enable_if_t<detail::is_enum_v<E>>> {
inline static constexpr std::string_view type_name = detail::type_name_v<E>;
inline static constexpr bool is_unscoped_enum = is_unscoped_enum_v<E>;
inline static constexpr bool is_scoped_enum = is_scoped_enum_v<E>;
inline static constexpr bool is_unscoped_enum = traits::is_unscoped_enum<E>::value;
inline static constexpr bool is_scoped_enum = traits::is_scoped_enum<E>::value;
inline static constexpr std::size_t count = detail::count_v<E>;
inline static constexpr std::array<E, count> values = detail::values<E>(detail::range_v<E>);
@ -375,9 +350,9 @@ struct enum_traits<E, std::enable_if_t<detail::is_enum_v<E>>> {
[[nodiscard]] static constexpr int index(E value) noexcept {
if (static_cast<U>(value) >= static_cast<U>(detail::min_v<E>) && static_cast<U>(value) <= static_cast<U>(detail::max_v<E>)) {
if constexpr (detail::size_v<E> != detail::count_v<E>) {
if (auto i = indexes[static_cast<U>(value) - detail::min_v<E>]; i != detail::invalid_index_v<E>) {
return i;
}
if (auto i = indexes[static_cast<U>(value) - detail::min_v<E>]; i != detail::invalid_index_v<E>) {
return i;
}
} else {
return static_cast<U>(value) - detail::min_v<E>;
}
@ -411,6 +386,39 @@ struct enum_traits<E, std::enable_if_t<detail::is_enum_v<E>>> {
inline static constexpr auto indexes = detail::indexes<E>(detail::range_v<E>);
};
} // namespace magic_enum::traits
// Checks is magic_enum supported compiler.
inline constexpr bool is_magic_enum_supported = detail::supported<void>::value;
// Checks whether T is an Unscoped enumeration type.
// Provides the member constant value which is equal to true, if T is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration) type. Otherwise, value is equal to false.
template <typename T>
struct is_unscoped_enum : traits::is_unscoped_enum<T> {};
template <typename T>
inline constexpr bool is_unscoped_enum_v = is_unscoped_enum<T>::value;
// Checks whether T is an Scoped enumeration type.
// Provides the member constant value which is equal to true, if T is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations) type. Otherwise, value is equal to false.
template <typename T>
struct is_scoped_enum : traits::is_scoped_enum<T> {};
template <typename T>
inline constexpr bool is_scoped_enum_v = is_scoped_enum<T>::value;
// If T is a complete enumeration type, provides a member typedef type that names the underlying type of T.
// Otherwise, if T is not an enumeration type, there is no member type. Otherwise (T is an incomplete enumeration type), the program is ill-formed.
template <typename T>
struct underlying_type : traits::underlying_type<T> {};
template <typename T>
using underlying_type_t = typename underlying_type<T>::type;
// Enum traits defines a compile-time template-based interface to query the properties of enum.
template <typename E>
using enum_traits = traits::enum_traits<std::decay_t<E>>;
// Obtains enum value from enum string name.
// Returns std::optional with enum value.
template <typename E>
@ -437,7 +445,7 @@ template <typename E>
// Obtains enum value from integer value.
// Returns std::optional with enum value.
template <typename E>
[[nodiscard]] constexpr auto enum_cast(std::underlying_type_t<std::decay_t<E>> value) noexcept -> detail::enable_if_enum_t<std::decay_t<E>, std::optional<std::decay_t<E>>> {
[[nodiscard]] constexpr auto enum_cast(underlying_type_t<E> value) noexcept -> detail::enable_if_enum_t<E, std::optional<std::decay_t<E>>> {
using D = std::decay_t<E>;
if (enum_traits<D>::index(static_cast<D>(value)) != -1) {
@ -449,15 +457,15 @@ template <typename E>
// Returns integer value from enum value.
template <typename E>
[[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_enum_t<E, std::underlying_type_t<E>> {
return static_cast<std::underlying_type_t<E>>(value);
[[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_enum_t<E, underlying_type_t<E>> {
return static_cast<underlying_type_t<E>>(value);
}
// Obtains index in enum value sequence from enum value.
// Returns std::optional with index.
template <typename E>
[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t<E, std::optional<std::size_t>> {
if (auto i = enum_traits<std::decay_t<E>>::index(value); i != -1) {
if (auto i = enum_traits<E>::index(value); i != -1) {
return i;
}
@ -468,20 +476,20 @@ template <typename E>
// No bounds checking is performed: the behavior is undefined if index >= number of enum values.
template<typename E>
[[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_t<E, std::decay_t<E>> {
return enum_traits<std::decay_t<E>>::value(index);
return enum_traits<E>::value(index);
}
// Obtains value enum sequence.
// Returns std::array with enum values, sorted by enum value.
template <typename E>
[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t<E, const decltype(enum_traits<std::decay_t<E>>::values)&> {
return enum_traits<std::decay_t<E>>::values;
[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t<E, decltype(enum_traits<E>::values)&> {
return enum_traits<E>::values;
}
// Returns number of enum values.
template <typename E>
[[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t<E, std::size_t> {
return enum_traits<std::decay_t<E>>::count;
return enum_traits<E>::count;
}
// Returns string enum name from static storage enum variable.
@ -495,33 +503,33 @@ template <auto V>
// If enum value does not have name or value out of range, returns empty string.
template <typename E>
[[nodiscard]] constexpr auto enum_name(E value) noexcept -> detail::enable_if_enum_t<E, std::string_view> {
return enum_traits<std::decay_t<E>>::name(value);
return enum_traits<E>::name(value);
}
// Obtains string enum name sequence.
// Returns std::array with string enum names, sorted by enum value.
template <typename E>
[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t<E, const decltype(enum_traits<std::decay_t<E>>::names)&> {
return enum_traits<std::decay_t<E>>::names;
[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t<E, decltype(enum_traits<E>::names)&> {
return enum_traits<E>::names;
}
// Obtains pair (value enum, string enum name) sequence.
// Returns std::array with std::pair (value enum, string enum name), sorted by enum value.
template <typename E>
[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t<E, const decltype(enum_traits<std::decay_t<E>>::entries)&> {
return enum_traits<std::decay_t<E>>::entries;
[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t<E, decltype(enum_traits<E>::entries)&> {
return enum_traits<E>::entries;
}
namespace ostream_operators {
template <typename Char, typename Traits, typename E>
auto operator<<(std::basic_ostream<Char, Traits>& os, E value) -> detail::enable_if_enum_t<E, std::basic_ostream<Char, Traits>&> {
if (auto name = enum_traits<std::decay_t<E>>::name(value); !name.empty()) {
if (auto name = enum_name(value); !name.empty()) {
for (auto c : name) {
os.put(c);
}
} else {
os << static_cast<std::underlying_type_t<E>>(value);
os << enum_integer(value);
}
return os;
@ -542,22 +550,22 @@ namespace bitwise_operators {
template <typename E>
constexpr auto operator~(E rhs) noexcept -> detail::enable_if_enum_t<E, E> {
return static_cast<E>(~static_cast<std::underlying_type_t<E>>(rhs));
return static_cast<E>(~static_cast<underlying_type_t<E>>(rhs));
}
template <typename E>
constexpr auto operator|(E lhs, E rhs) noexcept -> detail::enable_if_enum_t<E, E> {
return static_cast<E>(static_cast<std::underlying_type_t<E>>(lhs) | static_cast<std::underlying_type_t<E>>(rhs));
return static_cast<E>(static_cast<underlying_type_t<E>>(lhs) | static_cast<underlying_type_t<E>>(rhs));
}
template <typename E>
constexpr auto operator&(E lhs, E rhs) noexcept -> detail::enable_if_enum_t<E, E> {
return static_cast<E>(static_cast<std::underlying_type_t<E>>(lhs) & static_cast<std::underlying_type_t<E>>(rhs));
return static_cast<E>(static_cast<underlying_type_t<E>>(lhs) & static_cast<underlying_type_t<E>>(rhs));
}
template <typename E>
constexpr auto operator^(E lhs, E rhs) noexcept -> detail::enable_if_enum_t<E, E> {
return static_cast<E>(static_cast<std::underlying_type_t<E>>(lhs) ^ static_cast<std::underlying_type_t<E>>(rhs));
return static_cast<E>(static_cast<underlying_type_t<E>>(lhs) ^ static_cast<underlying_type_t<E>>(rhs));
}
template <typename E>

View file

@ -55,7 +55,7 @@ TEST_CASE("enum_cast") {
SECTION("string") {
constexpr auto cr = enum_cast<Color>("RED");
REQUIRE(cr.value() == Color::RED);
REQUIRE(enum_cast<Color>("GREEN").value() == Color::GREEN);
REQUIRE(enum_cast<Color&>("GREEN").value() == Color::GREEN);
REQUIRE(enum_cast<Color>("BLUE").value() == Color::BLUE);
REQUIRE_FALSE(enum_cast<Color>("None").has_value());
@ -67,7 +67,7 @@ TEST_CASE("enum_cast") {
REQUIRE_FALSE(enum_cast<Numbers>("None").has_value());
constexpr auto dr = enum_cast<Directions>("Right");
REQUIRE(enum_cast<Directions>("Up").value() == Directions::Up);
REQUIRE(enum_cast<Directions&>("Up").value() == Directions::Up);
REQUIRE(enum_cast<Directions>("Down").value() == Directions::Down);
REQUIRE(dr.value() == Directions::Right);
REQUIRE(enum_cast<Directions>("Left").value() == Directions::Left);
@ -85,7 +85,6 @@ TEST_CASE("enum_cast") {
Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
constexpr auto cr = enum_cast<Color>(-12);
REQUIRE(cr.value() == Color::RED);
REQUIRE(enum_cast<Color>(7).value() == Color::GREEN);
REQUIRE(enum_cast<Color&>(7).value() == Color::GREEN);
REQUIRE(enum_cast<Color>((int)cm[2]).value() == Color::BLUE);
REQUIRE_FALSE(enum_cast<Color>(0).has_value());
@ -98,7 +97,7 @@ TEST_CASE("enum_cast") {
REQUIRE_FALSE(enum_cast<Numbers>(0).has_value());
constexpr auto dr = enum_cast<Directions>(120);
REQUIRE(enum_cast<Directions>(85).value() == Directions::Up);
REQUIRE(enum_cast<Directions&>(85).value() == Directions::Up);
REQUIRE(enum_cast<Directions>(-42).value() == Directions::Down);
REQUIRE(dr.value() == Directions::Right);
REQUIRE(enum_cast<Directions>(-120).value() == Directions::Left);
@ -116,8 +115,9 @@ TEST_CASE("enum_cast") {
TEST_CASE("enum_integer") {
Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
constexpr auto cr = enum_integer(Color::RED);
Color cg = Color::GREEN;
REQUIRE(cr == -12);
REQUIRE(enum_integer(Color::GREEN) == 7);
REQUIRE(enum_integer<Color&>(cg) == 7);
REQUIRE(enum_integer(cm[2]) == 15);
REQUIRE(enum_integer(static_cast<Color>(0)) == 0);
@ -129,7 +129,8 @@ TEST_CASE("enum_integer") {
REQUIRE(enum_integer(static_cast<Numbers>(0)) == 0);
constexpr auto dr = enum_integer(Directions::Right);
REQUIRE(enum_integer(Directions::Left) == -120);
Directions dl = Directions::Left;
REQUIRE(enum_integer<Directions&>(dl) == -120);
REQUIRE(enum_integer(Directions::Down) == -42);
REQUIRE(enum_integer(Directions::Up) == 85);
REQUIRE(dr == 120);
@ -146,8 +147,9 @@ TEST_CASE("enum_integer") {
TEST_CASE("enum_index") {
Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
constexpr auto cr = enum_index(Color::RED);
Color cg = Color::GREEN;
REQUIRE(cr.value() == 0);
REQUIRE(enum_index(Color::GREEN).value() == 1);
REQUIRE(enum_index<Color&>(cg).value() == 1);
REQUIRE(enum_index(cm[2]).value() == 2);
REQUIRE_FALSE(enum_index(static_cast<Color>(0)).has_value());
@ -159,7 +161,8 @@ TEST_CASE("enum_index") {
REQUIRE_FALSE(enum_index(static_cast<Numbers>(0)).has_value());
constexpr auto dr = enum_index(Directions::Right);
REQUIRE(enum_index(Directions::Left).value() == 0);
Directions dl = Directions::Left;
REQUIRE(enum_index<Directions&>(dl).value() == 0);
REQUIRE(enum_index(Directions::Down).value() == 1);
REQUIRE(enum_index(Directions::Up).value() == 2);
REQUIRE(dr.value() == 3);
@ -176,7 +179,7 @@ TEST_CASE("enum_index") {
TEST_CASE("enum_value") {
constexpr auto cr = enum_value<Color>(0);
REQUIRE(cr == Color::RED);
REQUIRE(enum_value<Color>(1) == Color::GREEN);
REQUIRE(enum_value<Color&>(1) == Color::GREEN);
REQUIRE(enum_value<Color>(2) == Color::BLUE);
constexpr auto no = enum_value<Numbers>(0);
@ -185,7 +188,7 @@ TEST_CASE("enum_value") {
REQUIRE(enum_value<Numbers>(2) == Numbers::three);
constexpr auto dr = enum_value<Directions>(3);
REQUIRE(enum_value<Directions>(0) == Directions::Left);
REQUIRE(enum_value<Directions&>(0) == Directions::Left);
REQUIRE(enum_value<Directions>(1) == Directions::Down);
REQUIRE(enum_value<Directions>(2) == Directions::Up);
REQUIRE(dr == Directions::Right);
@ -197,13 +200,13 @@ TEST_CASE("enum_value") {
}
TEST_CASE("enum_values") {
constexpr auto s1 = enum_values<Color>();
constexpr auto s1 = enum_values<Color&>();
REQUIRE(s1 == std::array<Color, 3>{{Color::RED, Color::GREEN, Color::BLUE}});
auto s2 = enum_values<Numbers>();
REQUIRE(s2 == std::array<Numbers, 3>{{Numbers::one, Numbers::two, Numbers::three}});
constexpr auto s3 = enum_values<Directions>();
constexpr auto s3 = enum_values<Directions&>();
REQUIRE(s3 == std::array<Directions, 4>{{Directions::Left, Directions::Down, Directions::Up, Directions::Right}});
auto s4 = enum_values<number>();
@ -211,13 +214,13 @@ TEST_CASE("enum_values") {
}
TEST_CASE("enum_count") {
constexpr auto s1 = enum_count<Color>();
constexpr auto s1 = enum_count<Color&>();
REQUIRE(s1 == 3);
auto s2 = enum_count<Numbers>();
REQUIRE(s2 == 3);
constexpr auto s3 = enum_count<Directions>();
constexpr auto s3 = enum_count<Directions&>();
REQUIRE(s3 == 4);
auto s4 = enum_count<number>();
@ -229,8 +232,9 @@ TEST_CASE("enum_name") {
constexpr Color cr = Color::RED;
constexpr auto cr_name = enum_name(cr);
Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
Color cb = Color::BLUE;
REQUIRE(cr_name == "RED");
REQUIRE(enum_name(Color::BLUE) == "BLUE");
REQUIRE(enum_name<Color&>(cb) == "BLUE");
REQUIRE(enum_name(cm[1]) == "GREEN");
REQUIRE(enum_name(static_cast<Color>(0)).empty());
@ -244,7 +248,8 @@ TEST_CASE("enum_name") {
constexpr Directions dr = Directions::Right;
constexpr auto dr_name = enum_name(dr);
REQUIRE(enum_name(Directions::Up) == "Up");
Directions du = Directions::Up;
REQUIRE(enum_name<Directions&>(du) == "Up");
REQUIRE(enum_name(Directions::Down) == "Down");
REQUIRE(dr_name == "Right");
REQUIRE(enum_name(Directions::Left) == "Left");
@ -295,13 +300,13 @@ TEST_CASE("enum_name") {
}
TEST_CASE("enum_names") {
constexpr auto s1 = enum_names<Color>();
constexpr auto s1 = enum_names<Color&>();
REQUIRE(s1 == std::array<std::string_view, 3>{{"RED", "GREEN", "BLUE"}});
auto s2 = enum_names<Numbers>();
REQUIRE(s2 == std::array<std::string_view, 3>{{"one", "two", "three"}});
constexpr auto s3 = enum_names<Directions>();
constexpr auto s3 = enum_names<Directions&>();
REQUIRE(s3 == std::array<std::string_view, 4>{{"Left", "Down", "Up", "Right"}});
auto s4 = enum_names<number>();
@ -309,13 +314,13 @@ TEST_CASE("enum_names") {
}
TEST_CASE("enum_entries") {
constexpr auto s1 = enum_entries<Color>();
constexpr auto s1 = enum_entries<Color&>();
REQUIRE(s1 == std::array<std::pair<Color, std::string_view>, 3>{{{Color::RED, "RED"}, {Color::GREEN, "GREEN"}, {Color::BLUE, "BLUE"}}});
auto s2 = enum_entries<Numbers>();
REQUIRE(s2 == std::array<std::pair<Numbers, std::string_view>, 3>{{{Numbers::one, "one"}, {Numbers::two, "two"}, {Numbers::three, "three"}}});
constexpr auto s3 = enum_entries<Directions>();
constexpr auto s3 = enum_entries<Directions&>();
REQUIRE(s3 == std::array<std::pair<Directions, std::string_view>, 4>{{{Directions::Left, "Left"}, {Directions::Down, "Down"}, {Directions::Up, "Up"}, {Directions::Right, "Right"}}});
auto s4 = enum_entries<number>();
@ -458,9 +463,9 @@ TEST_CASE("type_traits") {
TEST_CASE("enum_traits") {
SECTION("type_name") {
REQUIRE(enum_traits<Color>::type_name == "Color");
REQUIRE(enum_traits<Color&>::type_name == "Color");
REQUIRE(enum_traits<Numbers>::type_name == "Numbers");
REQUIRE(enum_traits<Directions>::type_name == "Directions");
REQUIRE(enum_traits<Directions&>::type_name == "Directions");
REQUIRE(enum_traits<number>::type_name == "number");
}
}