mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-10 23:44:29 +00:00
add is_unscoped_enum/is_scoped_enum
This commit is contained in:
parent
11a0366421
commit
d24d38ea50
3 changed files with 70 additions and 2 deletions
28
README.md
28
README.md
|
|
@ -27,6 +27,8 @@ Header-only C++17 library provides static reflection on enums, work with any enu
|
|||
* `enum_count` returns number of enum values.
|
||||
* `enum_name` obtains string name from enum value.
|
||||
* `enum_names` obtains string enum name sequence.
|
||||
* `is_unscoped_enum` checks whether type is an Unscoped enumeration.
|
||||
* `is_scoped_enum` checks whether type is an Scoped enumeration.
|
||||
|
||||
## Features
|
||||
|
||||
|
|
@ -137,6 +139,32 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
std::cout << color << std::endl; // "BLUE"
|
||||
```
|
||||
|
||||
* Checks whether type is an Unscoped enumeration.
|
||||
```cpp
|
||||
enum сolor { red, green, blue };
|
||||
enum class direction { left, right };
|
||||
|
||||
magic_enum::is_unscoped_enum<color>::value -> true
|
||||
magic_enum::is_unscoped_enum<direction>::value -> false
|
||||
magic_enum::is_unscoped_enum<int>::value -> false
|
||||
|
||||
// Helper variable template.
|
||||
magic_enum::is_unscoped_enum_v<color> -> true
|
||||
```
|
||||
|
||||
* Checks whether type is an Scoped enumeration.
|
||||
```cpp
|
||||
enum сolor { red, green, blue };
|
||||
enum class direction { left, right };
|
||||
|
||||
magic_enum::is_scoped_enum<color>::value -> false
|
||||
magic_enum::is_scoped_enum<direction>::value -> true
|
||||
magic_enum::is_scoped_enum<int>::value -> false
|
||||
|
||||
// Helper variable template.
|
||||
magic_enum::is_scoped_enum_v<direction> -> true
|
||||
```
|
||||
|
||||
## Remarks
|
||||
|
||||
* `magic_enum::enum_cast` returns `std::optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value.
|
||||
|
|
|
|||
|
|
@ -180,11 +180,39 @@ template <typename E>
|
|||
return std::nullopt; // Invalid value or out of range.
|
||||
}
|
||||
|
||||
template<class E>
|
||||
using enable_if_enum_t = typename std::enable_if<std::is_enum_v<std::decay_t<E>>>::type;
|
||||
template<typename T>
|
||||
using enable_if_enum_t = typename std::enable_if<std::is_enum_v<std::decay_t<T>>>::type;
|
||||
|
||||
template<typename T, bool = std::is_enum_v<T>>
|
||||
struct is_scoped_enum_impl : std::false_type {};
|
||||
|
||||
template<typename T>
|
||||
struct is_scoped_enum_impl<T, true> : std::bool_constant<!std::is_convertible_v<T, std::underlying_type_t<T>>> {};
|
||||
|
||||
template<typename T, bool = std::is_enum_v<T>>
|
||||
struct is_unscoped_enum_impl : std::false_type {};
|
||||
|
||||
template<typename T>
|
||||
struct is_unscoped_enum_impl<T, true> : std::bool_constant<std::is_convertible_v<T, std::underlying_type_t<T>>> {};
|
||||
|
||||
} // namespace magic_enum::detail
|
||||
|
||||
// Checks whether T is an Unscoped enumeration type. Provides the member constant value which is equal to true, if T is an Unscoped enumeration type. Otherwise, value is equal to false.
|
||||
// https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration
|
||||
template <typename T>
|
||||
struct is_unscoped_enum : detail::is_unscoped_enum_impl<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 type. Otherwise, value is equal to false.
|
||||
// https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations
|
||||
template <typename T>
|
||||
struct is_scoped_enum : detail::is_scoped_enum_impl<T> {};
|
||||
|
||||
template <typename T>
|
||||
inline constexpr bool is_scoped_enum_v = is_scoped_enum<T>::value;
|
||||
|
||||
// Obtains enum value from enum string name.
|
||||
template <typename E, typename = detail::enable_if_enum_t<E>>
|
||||
[[nodiscard]] constexpr std::optional<E> enum_cast(std::string_view value) noexcept {
|
||||
|
|
|
|||
|
|
@ -233,3 +233,15 @@ TEST_CASE("operator<<") {
|
|||
test_ostream(number::three, "three");
|
||||
test_ostream((number)0, "");
|
||||
}
|
||||
|
||||
TEST_CASE("type_traits") {
|
||||
REQUIRE_FALSE(magic_enum::is_unscoped_enum_v<Color>);
|
||||
REQUIRE_FALSE(magic_enum::is_unscoped_enum_v<Numbers>);
|
||||
REQUIRE(magic_enum::is_unscoped_enum_v<Directions>);
|
||||
REQUIRE(magic_enum::is_unscoped_enum_v<number>);
|
||||
|
||||
REQUIRE(magic_enum::is_scoped_enum_v<Color>);
|
||||
REQUIRE(magic_enum::is_scoped_enum_v<Numbers>);
|
||||
REQUIRE_FALSE(magic_enum::is_scoped_enum_v<Directions>);
|
||||
REQUIRE_FALSE(magic_enum::is_scoped_enum_v<number>);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue