mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
wip
This commit is contained in:
parent
619164687f
commit
8f31ad7cfa
4 changed files with 39 additions and 11 deletions
|
|
@ -199,6 +199,8 @@ enum class Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
|
||||
* Before use, read the [limitations](doc/limitations.md) of functionality.
|
||||
|
||||
* For the small enum use the API from the namespace `magic_enum`, and for enum-flags use the API from the namespace `magic_enum::flags`.
|
||||
|
||||
## Integration
|
||||
|
||||
You should add the required file [magic_enum.hpp](include/magic_enum.hpp).
|
||||
|
|
|
|||
|
|
@ -24,14 +24,19 @@
|
|||
* To check is magic_enum supported compiler use macro `MAGIC_ENUM_SUPPORTED` or constexpr constant `magic_enum::is_magic_enum_supported`.</br>
|
||||
If magic_enum used on unsupported compiler, occurs the compilation error. To suppress error define macro `MAGIC_ENUM_NO_CHECK_SUPPORT`.
|
||||
|
||||
* For the small enum use the API from the namespace `magic_enum`, and for enum-flags use the API from the namespace `magic_enum::flags`.
|
||||
|
||||
## `enum_cast`
|
||||
|
||||
```cpp
|
||||
template <typename E>
|
||||
constexpr optional<E> enum_cast(string_view value) noexcept;
|
||||
constexpr optional<E> enum_cast(underlying_type_t<E> value) noexcept;
|
||||
|
||||
template <typename E>
|
||||
constexpr optional<E> enum_cast(underlying_type_t<E> value) noexcept;
|
||||
constexpr optional<E> enum_cast(string_view value) noexcept;
|
||||
|
||||
template <typename E, typename BinaryPredicate>
|
||||
constexpr optional<E> enum_cast(string_view value, BinaryPredicate p) noexcept(is_nothrow_invocable_v<BinaryPredicate>);
|
||||
```
|
||||
|
||||
* Obtains enum value from string or integer.
|
||||
|
|
@ -224,8 +229,15 @@ constexpr optional<size_t> enum_index() noexcept;
|
|||
```cpp
|
||||
template <typename E>
|
||||
constexpr bool enum_contains(E value) noexcept;
|
||||
|
||||
template <typename E>
|
||||
constexpr bool enum_contains(underlying_type_t<E> value) noexcept;
|
||||
|
||||
template <typename E>
|
||||
constexpr bool enum_contains(string_view value) noexcept;
|
||||
|
||||
template <typename E, typename BinaryPredicate>
|
||||
constexpr optional<E> enum_contains(string_view value, BinaryPredicate p) noexcept(is_nothrow_invocable_v<BinaryPredicate>);
|
||||
```
|
||||
|
||||
* Checks whether enum contains enumerator with such value.
|
||||
|
|
|
|||
|
|
@ -738,6 +738,15 @@ template <typename E>
|
|||
return detail::undex<D>(value) != detail::invalid_index_v<D>;
|
||||
}
|
||||
|
||||
// Checks whether enum contains enumerator with such string name.
|
||||
template <typename E, typename BinaryPredicate>
|
||||
[[nodiscard]] constexpr auto enum_contains(std::string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) -> detail::enable_if_enum_t<E, bool> {
|
||||
using D = std::decay_t<E>;
|
||||
static_assert(std::is_invocable_r_v<bool, BinaryPredicate, char, char>, "magic_enum::enum_contains requires bool(char, char) invocable predicate.");
|
||||
|
||||
return enum_cast<D>(value, std::move(p)).has_value();
|
||||
}
|
||||
|
||||
// Checks whether enum contains enumerator with such string name.
|
||||
template <typename E>
|
||||
[[nodiscard]] constexpr auto enum_contains(std::string_view value) noexcept -> detail::enable_if_enum_t<E, bool> {
|
||||
|
|
@ -980,8 +989,11 @@ template <typename E>
|
|||
return std::nullopt; // Value out of range.
|
||||
}
|
||||
|
||||
using magic_enum::enum_type_name; // TODO: impl
|
||||
using magic_enum::enum_integer; // TODO: impl
|
||||
// Returns string name of enum type.
|
||||
using magic_enum::enum_type_name;
|
||||
|
||||
// Returns integer value from enum value.
|
||||
using magic_enum::enum_integer;
|
||||
|
||||
namespace ostream_operators {
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,10 @@
|
|||
#define MAGIC_ENUM_RANGE_MAX 120
|
||||
#include <magic_enum.hpp>
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1920 || defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9
|
||||
# define MAGIC_ENUM_SUPPORTED_ALIASES
|
||||
#endif
|
||||
|
||||
#include <array>
|
||||
#include <cctype>
|
||||
#include <string_view>
|
||||
|
|
@ -44,9 +48,7 @@ enum number : unsigned long {
|
|||
three = 300,
|
||||
four = 400,
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1920
|
||||
// Aliases won't work on vs2017.
|
||||
|
||||
#if defined(MAGIC_ENUM_SUPPORTED_ALIASES)
|
||||
_1 = one,
|
||||
_2 = two,
|
||||
_3 = three,
|
||||
|
|
@ -260,7 +262,7 @@ TEST_CASE("enum_contains") {
|
|||
constexpr auto cr = "RED";
|
||||
REQUIRE(enum_contains<Color>(cr));
|
||||
REQUIRE(enum_contains<Color&>("GREEN"));
|
||||
REQUIRE(enum_contains<Color>("BLUE"));
|
||||
REQUIRE(enum_contains<Color>("blue", [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }));
|
||||
REQUIRE_FALSE(enum_contains<Color>("None"));
|
||||
|
||||
constexpr auto no = std::string_view{"one"};
|
||||
|
|
@ -573,16 +575,16 @@ TEST_CASE("type_traits") {
|
|||
REQUIRE_FALSE(is_scoped_enum_v<number>);
|
||||
}
|
||||
|
||||
|
||||
/* TODO: https://github.com/Neargye/nameof/issues/22
|
||||
TEST_CASE("enum_type_name") {
|
||||
REQUIRE(enum_type_name<Color&>() == "Color");
|
||||
REQUIRE(enum_type_name<Numbers>() == "Numbers");
|
||||
REQUIRE(enum_type_name<Directions&>() == "Directions");
|
||||
REQUIRE(enum_type_name<number>() == "number");
|
||||
}
|
||||
*/
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1920
|
||||
// Aliases won't work on vs2017.
|
||||
#if defined(MAGIC_ENUM_SUPPORTED_ALIASES)
|
||||
TEST_CASE("aliases") {
|
||||
REQUIRE(enum_count<number>() == 3);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue