mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-10 23:44:29 +00:00
add enum concept
This commit is contained in:
parent
e325a70663
commit
3b2ebf7e55
2 changed files with 18 additions and 4 deletions
|
|
@ -26,6 +26,12 @@
|
||||||
|
|
||||||
enum class Color : int { RED = -10, BLUE = 0, GREEN = 10 };
|
enum class Color : int { RED = -10, BLUE = 0, GREEN = 10 };
|
||||||
|
|
||||||
|
template <typename E>
|
||||||
|
auto to_integer(magic_enum::Enum<E> value) {
|
||||||
|
// magic_enum::Enum<E> - C++17 Concept for enum type.
|
||||||
|
return static_cast<std::underlying_type_t<E>>(value);
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// Enum variable to string name.
|
// Enum variable to string name.
|
||||||
Color c1 = Color::RED;
|
Color c1 = Color::RED;
|
||||||
|
|
@ -44,7 +50,7 @@ int main() {
|
||||||
// String name to enum value.
|
// String name to enum value.
|
||||||
auto c2 = magic_enum::enum_cast<Color>("BLUE");
|
auto c2 = magic_enum::enum_cast<Color>("BLUE");
|
||||||
if (c2.has_value() && c2.value() == Color::BLUE) {
|
if (c2.has_value() && c2.value() == Color::BLUE) {
|
||||||
std::cout << "BLUE = " << static_cast<int>(c2.value()) << std::endl; // BLUE = 0
|
std::cout << "BLUE = " << to_integer(c2.value()) << std::endl; // BLUE = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Integer value to enum value.
|
// Integer value to enum value.
|
||||||
|
|
@ -55,7 +61,7 @@ int main() {
|
||||||
|
|
||||||
// Enum value to integer value.
|
// Enum value to integer value.
|
||||||
auto color_integer = magic_enum::enum_integer(Color::RED);
|
auto color_integer = magic_enum::enum_integer(Color::RED);
|
||||||
if (color_integer == static_cast<std::underlying_type_t<Color>>(Color::RED)) {
|
if (color_integer == to_integer(Color::RED)) {
|
||||||
std::cout << "RED = " << color_integer << std::endl; // RED = -10
|
std::cout << "RED = " << color_integer << std::endl; // RED = -10
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -97,7 +97,9 @@ inline constexpr bool is_enum_v = std::is_enum_v<T> && std::is_same_v<T, std::de
|
||||||
|
|
||||||
template <std::size_t N>
|
template <std::size_t N>
|
||||||
struct static_string {
|
struct static_string {
|
||||||
constexpr static_string(std::string_view str) noexcept : static_string{str, std::make_index_sequence<N>{}} {}
|
constexpr static_string(std::string_view str) noexcept : static_string{str, std::make_index_sequence<N>{}} {
|
||||||
|
assert(str.size() == N);
|
||||||
|
}
|
||||||
|
|
||||||
constexpr const char* data() const noexcept { return chars.data(); }
|
constexpr const char* data() const noexcept { return chars.data(); }
|
||||||
|
|
||||||
|
|
@ -109,7 +111,7 @@ struct static_string {
|
||||||
template <std::size_t... I>
|
template <std::size_t... I>
|
||||||
constexpr static_string(std::string_view str, std::index_sequence<I...>) noexcept : chars{{str[I]...}} {}
|
constexpr static_string(std::string_view str, std::index_sequence<I...>) noexcept : chars{{str[I]...}} {}
|
||||||
|
|
||||||
const std::array<char, N> chars;
|
const std::array<const char, N> chars;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
|
|
@ -308,6 +310,9 @@ constexpr auto entries(std::index_sequence<I...>) noexcept {
|
||||||
template <typename T, typename R>
|
template <typename T, typename R>
|
||||||
using enable_if_enum_t = std::enable_if_t<std::is_enum_v<std::decay_t<T>>, R>;
|
using enable_if_enum_t = std::enable_if_t<std::is_enum_v<std::decay_t<T>>, R>;
|
||||||
|
|
||||||
|
template <typename T, typename Enable = std::enable_if_t<std::is_enum_v<std::decay_t<T>>>>
|
||||||
|
using enum_concept = T;
|
||||||
|
|
||||||
template <typename T, bool = std::is_enum_v<T>>
|
template <typename T, bool = std::is_enum_v<T>>
|
||||||
struct is_scoped_enum : std::false_type {};
|
struct is_scoped_enum : std::false_type {};
|
||||||
|
|
||||||
|
|
@ -392,6 +397,9 @@ struct enum_traits<E, std::enable_if_t<is_enum_v<E>>> {
|
||||||
// Checks is magic_enum supported compiler.
|
// Checks is magic_enum supported compiler.
|
||||||
inline constexpr bool is_magic_enum_supported = detail::supported<void>::value;
|
inline constexpr bool is_magic_enum_supported = detail::supported<void>::value;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using Enum = detail::enum_concept<T>;
|
||||||
|
|
||||||
// Checks whether T is an Unscoped enumeration type.
|
// 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.
|
// 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>
|
template <typename T>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue