From 8f31ad7cfaa0588a759adaaf9ef45eef192081a4 Mon Sep 17 00:00:00 2001 From: neargye Date: Sat, 15 Aug 2020 19:52:37 +0500 Subject: [PATCH] wip --- README.md | 2 ++ doc/reference.md | 16 ++++++++++++++-- include/magic_enum.hpp | 16 ++++++++++++++-- test/test.cpp | 16 +++++++++------- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index cafca07..71f39d3 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/doc/reference.md b/doc/reference.md index 7d2c1fe..a4a8566 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -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`.
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 -constexpr optional enum_cast(string_view value) noexcept; +constexpr optional enum_cast(underlying_type_t value) noexcept; template -constexpr optional enum_cast(underlying_type_t value) noexcept; +constexpr optional enum_cast(string_view value) noexcept; + +template +constexpr optional enum_cast(string_view value, BinaryPredicate p) noexcept(is_nothrow_invocable_v); ``` * Obtains enum value from string or integer. @@ -224,8 +229,15 @@ constexpr optional enum_index() noexcept; ```cpp template constexpr bool enum_contains(E value) noexcept; + +template constexpr bool enum_contains(underlying_type_t value) noexcept; + +template constexpr bool enum_contains(string_view value) noexcept; + +template +constexpr optional enum_contains(string_view value, BinaryPredicate p) noexcept(is_nothrow_invocable_v); ``` * Checks whether enum contains enumerator with such value. diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index dad0d7b..5698dff 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -738,6 +738,15 @@ template return detail::undex(value) != detail::invalid_index_v; } +// Checks whether enum contains enumerator with such string name. +template +[[nodiscard]] constexpr auto enum_contains(std::string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_t { + using D = std::decay_t; + static_assert(std::is_invocable_r_v, "magic_enum::enum_contains requires bool(char, char) invocable predicate."); + + return enum_cast(value, std::move(p)).has_value(); +} + // Checks whether enum contains enumerator with such string name. template [[nodiscard]] constexpr auto enum_contains(std::string_view value) noexcept -> detail::enable_if_enum_t { @@ -980,8 +989,11 @@ template 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 { diff --git a/test/test.cpp b/test/test.cpp index 1c3d87c..cbad4cf 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -27,6 +27,10 @@ #define MAGIC_ENUM_RANGE_MAX 120 #include +#if defined(_MSC_VER) && _MSC_VER >= 1920 || defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 +# define MAGIC_ENUM_SUPPORTED_ALIASES +#endif + #include #include #include @@ -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(cr)); REQUIRE(enum_contains("GREEN")); - REQUIRE(enum_contains("BLUE")); + REQUIRE(enum_contains("blue", [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); })); REQUIRE_FALSE(enum_contains("None")); constexpr auto no = std::string_view{"one"}; @@ -573,16 +575,16 @@ TEST_CASE("type_traits") { REQUIRE_FALSE(is_scoped_enum_v); } - +/* TODO: https://github.com/Neargye/nameof/issues/22 TEST_CASE("enum_type_name") { REQUIRE(enum_type_name() == "Color"); REQUIRE(enum_type_name() == "Numbers"); REQUIRE(enum_type_name() == "Directions"); REQUIRE(enum_type_name() == "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() == 3);