mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
v0.3.0
This commit is contained in:
parent
bbda253f8b
commit
c425706289
5 changed files with 128 additions and 127 deletions
|
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.6)
|
||||
|
||||
project(magic_enum VERSION "0.2.0" LANGUAGES CXX)
|
||||
project(magic_enum VERSION "0.3.0" LANGUAGES CXX)
|
||||
|
||||
option(MAGIC_ENUM_OPT_BUILD_EXAMPLES "Build magic_enum examples" ON)
|
||||
option(MAGIC_ENUM_OPT_BUILD_TESTS "Build and perform magic_enum tests" ON)
|
||||
|
|
|
|||
51
README.md
51
README.md
|
|
@ -16,17 +16,17 @@
|
|||
[](https://travis-ci.org/Neargye/magic_enum)
|
||||
[](https://ci.appveyor.com/project/Neargye/magic-enum-hf8vk/branch/master)
|
||||
[](https://www.codacy.com/app/Neargye/magic_enum?utm_source=github.com&utm_medium=referral&utm_content=Neargye/magic_enum&utm_campaign=Badge_Grade)
|
||||
[](https://wandbox.org/permlink/KOo6s4qJ9wUSxRGG)
|
||||
[](https://wandbox.org/permlink/kXdow0AxI1Dss18p)
|
||||
|
||||
## What is Magic Enum?
|
||||
|
||||
Header-only C++17 library provides Enum-to-String and String-to-Enum and other useful functions, without any macro or boilerplate code.
|
||||
* `magic_enum::enum_cast` obtains enum value from string or integer.
|
||||
* `magic_enum::enum_value` obtains indexed access to enum value.
|
||||
* `magic_enum::enum_values` obtains enum value sequence.
|
||||
* `magic_enum::enum_count` obtains number of enum values.
|
||||
* `magic_enum::enum_name` obtains string name from enum value.
|
||||
* `magic_enum::enum_names` obtains string enum name sequence.
|
||||
Header-only C++17 library provides Enum-to-String and String-to-Enum and other useful, functions work with any enum type without any macro or boilerplate code.
|
||||
* `yae::enum_cast` obtains enum value from string or integer.
|
||||
* `yae::enum_value` obtains indexed access to enum value.
|
||||
* `yae::enum_values` obtains enum value sequence.
|
||||
* `yae::enum_count` obtains number of enum values.
|
||||
* `yae::enum_name` obtains string name from enum value.
|
||||
* `yae::enum_names` obtains string enum name sequence.
|
||||
|
||||
## Features
|
||||
|
||||
|
|
@ -36,7 +36,8 @@ Header-only C++17 library provides Enum-to-String and String-to-Enum and other u
|
|||
* Compile-time
|
||||
* Enum to string
|
||||
* String to enum
|
||||
* Works with any enum type
|
||||
* Work with any enum type
|
||||
* Without any macro or boilerplate code
|
||||
|
||||
## [Examples](example/example.cpp)
|
||||
|
||||
|
|
@ -48,7 +49,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* Enum value to string
|
||||
```cpp
|
||||
Color color = Color::RED;
|
||||
auto color_name = magic_enum::enum_name(color);
|
||||
auto color_name = yae::enum_name(color);
|
||||
if (color_name.has_value()) {
|
||||
// color_name.value() -> "RED"
|
||||
}
|
||||
|
|
@ -57,7 +58,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* Static storage enum variable to string
|
||||
```cpp
|
||||
constexpr Color color = Color::BLUE;
|
||||
constexpr auto color_name = magic_enum::enum_name(color);
|
||||
constexpr auto color_name = yae::enum_name(color);
|
||||
if (color_name.has_value()) {
|
||||
// color_name.value() -> "BLUE"
|
||||
}
|
||||
|
|
@ -66,7 +67,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* String to enum value
|
||||
```cpp
|
||||
std::string color_name{"GREEN"};
|
||||
auto color = magic_enum::enum_cast<Color>(color_name);
|
||||
auto color = yae::enum_cast<Color>(color_name);
|
||||
if (color.has_value()) {
|
||||
// color.value() -> Color::GREEN
|
||||
}
|
||||
|
|
@ -74,7 +75,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
|
||||
* Static storage string to enum value
|
||||
```cpp
|
||||
constexpr auto color = magic_enum::enum_cast<Color>("BLUE");
|
||||
constexpr auto color = yae::enum_cast<Color>("BLUE");
|
||||
if (color.has_value()) {
|
||||
// color.value() -> Color::BLUE
|
||||
}
|
||||
|
|
@ -83,7 +84,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* Integer to enum value
|
||||
```cpp
|
||||
int color_value = 2;
|
||||
auto color = magic_enum::enum_cast<Color>(color_value);
|
||||
auto color = yae::enum_cast<Color>(color_value);
|
||||
if (colo.has_value()) {
|
||||
// color.value() -> Color::RED
|
||||
}
|
||||
|
|
@ -91,7 +92,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
|
||||
* Static storage integer to enum value
|
||||
```cpp
|
||||
constexpr auto color = magic_enum::enum_cast<Color>(4);
|
||||
constexpr auto color = yae::enum_cast<Color>(4);
|
||||
if (color.has_value()) {
|
||||
// color.value() -> Color::BLUE
|
||||
}
|
||||
|
|
@ -100,50 +101,50 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* Indexed access to enum value
|
||||
```cpp
|
||||
int i = 1;
|
||||
Color colo = magic_enum::enum_value<Color>(i);
|
||||
Color colo = yae::enum_value<Color>(i);
|
||||
// color -> Color::BLUE
|
||||
```
|
||||
|
||||
* Compile-time indexed access.
|
||||
```cpp
|
||||
constexpr Color color = magic_enum::enum_value<Color>(0);
|
||||
constexpr Color color = yae::enum_value<Color>(0);
|
||||
// color -> Color::RED
|
||||
```
|
||||
|
||||
* Enum value sequence
|
||||
```cpp
|
||||
constexpr auto colors = magic_enum::enum_values<Color>();
|
||||
constexpr auto colors = yae::enum_values<Color>();
|
||||
// colors -> {Color::RED, Color::BLUE, Color::GREEN}
|
||||
```
|
||||
|
||||
* Number of enum elements
|
||||
```cpp
|
||||
constexpr std::size_t color_count = magic_enum::enum_count<Color>();
|
||||
constexpr std::size_t color_count = yae::enum_count<Color>();
|
||||
// color_count -> 3
|
||||
```
|
||||
|
||||
* Enum names sequence
|
||||
```cpp
|
||||
constexpr auto color_names = magic_enum::enum_names<Color>();
|
||||
constexpr auto color_names = yae::enum_names<Color>();
|
||||
// color_names -> {"RED", "BLUE", "GREEN"}
|
||||
```
|
||||
|
||||
* Stream operator for enum
|
||||
```cpp
|
||||
using namespace magic_enum::ops; // out-of-the-box stream operator for enums.
|
||||
using namespace yae::ops; // out-of-the-box stream operator for enums.
|
||||
Color color = Color::BLUE;
|
||||
std::cout << color << std::endl; // "BLUE"
|
||||
```
|
||||
|
||||
## Remarks
|
||||
|
||||
* `magic_enum::enum_cast` returns `std::optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value.
|
||||
* `yae::enum_cast` returns `std::optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value.
|
||||
|
||||
* `magic_enum::enum_values` returns `std::array<E, N>` with all enum value where `N = number of enum values`, sorted by enum value.
|
||||
* `yae::enum_values` returns `std::array<E, N>` with all enum value where `N = number of enum values`, sorted by enum value.
|
||||
|
||||
* `magic_enum::enum_name` returns `std::optional<std::string_view>`, using `has_value()` to check contains enum name and `value()` to get the enum name.
|
||||
* `yae::enum_name` returns `std::optional<std::string_view>`, using `has_value()` to check contains enum name and `value()` to get the enum name.
|
||||
|
||||
* `magic_enum::enum_names` returns `std::array<std::string_view, N>` with all string enum name where `N = number of enum values`, sorted by enum value.
|
||||
* `yae::enum_names` returns `std::array<std::string_view, N>` with all string enum name where `N = number of enum values`, sorted by enum value.
|
||||
|
||||
* Enum value must be in range `(-MAGIC_ENUM_RANGE, MAGIC_ENUM_RANGE)`. By default `MAGIC_ENUM_RANGE = 256`. If you need a larger range, redefine the macro `MAGIC_ENUM_RANGE`.
|
||||
```cpp
|
||||
|
|
|
|||
|
|
@ -29,13 +29,13 @@ enum Color { RED = -10, BLUE = 0, GREEN = 10 };
|
|||
int main() {
|
||||
// Enum variable to string name.
|
||||
Color c1 = Color::RED;
|
||||
auto c1_name = magic_enum::enum_name(c1);
|
||||
auto c1_name = yae::enum_name(c1);
|
||||
if (c1_name.has_value()) {
|
||||
std::cout << c1_name.value() << std::endl; // RED
|
||||
}
|
||||
|
||||
// String enum name sequence.
|
||||
constexpr auto color_names = magic_enum::enum_names<Color>();
|
||||
constexpr auto color_names = yae::enum_names<Color>();
|
||||
std::cout << "Color names:";
|
||||
for (auto n : color_names) {
|
||||
std::cout << " " << n;
|
||||
|
|
@ -44,29 +44,29 @@ int main() {
|
|||
// Color names: RED BLUE GREEN
|
||||
|
||||
// String name to enum value.
|
||||
auto c2 = magic_enum::enum_cast<Color>("BLUE");
|
||||
auto c2 = yae::enum_cast<Color>("BLUE");
|
||||
if (c2.has_value() && c2.value() == Color::BLUE) {
|
||||
std::cout << "BLUE = " << c2.value() << std::endl; // BLUE = 0
|
||||
}
|
||||
|
||||
// Integer value to enum value.
|
||||
auto c3 = magic_enum::enum_cast<Color>(10);
|
||||
auto c3 = yae::enum_cast<Color>(10);
|
||||
if (c3.has_value() && c3.value() == Color::GREEN) {
|
||||
std::cout << "GREEN = " << c3.value() << std::endl; // GREEN = 10
|
||||
}
|
||||
|
||||
using namespace magic_enum::ops; // out-of-the-box stream operator for enums.
|
||||
using namespace yae::ops; // out-of-the-box stream operator for enums.
|
||||
// ostream operator for enum.
|
||||
std::cout << "Color: " << c1 << " " << c2 << " " << c3 << std::endl; // Color: RED BLUE GREEN
|
||||
|
||||
// Number of enum values.
|
||||
std::cout << "Color enum size: " << magic_enum::enum_count<Color>() << std::endl; // Color enum size: 3
|
||||
std::cout << "Color enum size: " << yae::enum_count<Color>() << std::endl; // Color enum size: 3
|
||||
|
||||
// Indexed access to enum value.
|
||||
std::cout << "Color[0] = " << magic_enum::enum_value<Color>(0) << std::endl; // Color[0] = RED
|
||||
std::cout << "Color[0] = " << yae::enum_value<Color>(0) << std::endl; // Color[0] = RED
|
||||
|
||||
// Enum value sequence.
|
||||
constexpr auto colors = magic_enum::enum_values<Color>();
|
||||
constexpr auto colors = yae::enum_values<Color>();
|
||||
std::cout << "Colors sequence:";
|
||||
for (Color c : colors) {
|
||||
std::cout << " " << c; // ostream operator for enum.
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
// | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
|
||||
// |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
|
||||
// __/ | https://github.com/Neargye/magic_enum
|
||||
// |___/ vesion 0.3.0-dev
|
||||
// |___/ vesion 0.3.0
|
||||
//
|
||||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
|
@ -47,7 +47,7 @@
|
|||
# define MAGIC_ENUM_RANGE 256
|
||||
#endif
|
||||
|
||||
namespace magic_enum {
|
||||
namespace yae { // aka Yet Another Enums.
|
||||
|
||||
static_assert(MAGIC_ENUM_RANGE > 0,
|
||||
"MAGIC_ENUM_RANGE must be positive and greater than zero.");
|
||||
|
|
@ -59,7 +59,7 @@ namespace detail {
|
|||
template <typename E>
|
||||
struct enum_range final {
|
||||
using D = std::decay_t<E>;
|
||||
static_assert(std::is_enum_v<D>, "magic_enum::detail::enum_range require enum type.");
|
||||
static_assert(std::is_enum_v<D>, "yae::detail::enum_range require enum type.");
|
||||
using U = std::underlying_type_t<D>;
|
||||
using C = std::common_type_t<int, U>;
|
||||
static constexpr int min = std::max<C>(std::is_signed_v<U> ? -MAGIC_ENUM_RANGE : 0, std::numeric_limits<U>::min());
|
||||
|
|
@ -77,7 +77,7 @@ struct enum_range final {
|
|||
|
||||
template <typename E, E V>
|
||||
[[nodiscard]] constexpr std::string_view name_impl() noexcept {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::name_impl require enum type.");
|
||||
static_assert(std::is_enum_v<E>, "yae::detail::name_impl require enum type.");
|
||||
#if defined(__clang__)
|
||||
std::string_view name{__PRETTY_FUNCTION__};
|
||||
constexpr auto suffix = sizeof("]") - 1;
|
||||
|
|
@ -110,7 +110,7 @@ template <typename E, E V>
|
|||
|
||||
template <typename E, int... I>
|
||||
[[nodiscard]] constexpr decltype(auto) strings_impl(std::integer_sequence<int, I...>) noexcept {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::strings_impl require enum type.");
|
||||
static_assert(std::is_enum_v<E>, "yae::detail::strings_impl require enum type.");
|
||||
constexpr std::array<std::string_view, sizeof...(I)> names{{name_impl<E, static_cast<E>(I + enum_range<E>::min)>()...}};
|
||||
|
||||
return names;
|
||||
|
|
@ -118,7 +118,7 @@ template <typename E, int... I>
|
|||
|
||||
template <typename E>
|
||||
[[nodiscard]] constexpr std::string_view name_impl(int value) noexcept {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::name_impl require enum type.");
|
||||
static_assert(std::is_enum_v<E>, "yae::detail::name_impl require enum type.");
|
||||
constexpr auto names = strings_impl<E>(enum_range<E>::sequence);
|
||||
|
||||
return enum_range<E>::contains(value) ? names[value - enum_range<E>::min] : std::string_view{};
|
||||
|
|
@ -126,7 +126,7 @@ template <typename E>
|
|||
|
||||
template <typename E, int... I>
|
||||
[[nodiscard]] constexpr decltype(auto) values_impl(std::integer_sequence<int, I...>) noexcept {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::values_impl require enum type.");
|
||||
static_assert(std::is_enum_v<E>, "yae::detail::values_impl require enum type.");
|
||||
constexpr int n = sizeof...(I);
|
||||
constexpr std::array<bool, n> valid{{!name_impl<E, static_cast<E>(I + enum_range<E>::min)>().empty()...}};
|
||||
constexpr int num_valid = ((valid[I] ? 1 : 0) + ...);
|
||||
|
|
@ -143,7 +143,7 @@ template <typename E, int... I>
|
|||
|
||||
template <typename E, std::size_t... I>
|
||||
[[nodiscard]] constexpr decltype(auto) names_impl(std::integer_sequence<std::size_t, I...>) noexcept {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::names_impl require enum type.");
|
||||
static_assert(std::is_enum_v<E>, "yae::detail::names_impl require enum type.");
|
||||
constexpr auto enums = values_impl<E>(enum_range<E>::sequence);
|
||||
constexpr std::array<std::string_view, sizeof...(I)> names{{name_impl<E, enums[I]>()...}};
|
||||
|
||||
|
|
@ -152,7 +152,7 @@ template <typename E, std::size_t... I>
|
|||
|
||||
template <typename E>
|
||||
[[nodiscard]] constexpr std::optional<E> enum_cast_impl(std::string_view value) noexcept {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::enum_cast_impl require enum type.");
|
||||
static_assert(std::is_enum_v<E>, "yae::detail::enum_cast_impl require enum type.");
|
||||
constexpr auto values = values_impl<E>(enum_range<E>::sequence);
|
||||
constexpr auto count = values.size();
|
||||
constexpr auto names = names_impl<E>(std::make_index_sequence<count>{});
|
||||
|
|
@ -169,7 +169,7 @@ template <typename E>
|
|||
template<class E>
|
||||
using enable_if_enum_t = typename std::enable_if<std::is_enum_v<std::decay_t<E>>>::type;
|
||||
|
||||
} // namespace magic_enum::detail
|
||||
} // namespace yae::detail
|
||||
|
||||
// Obtains enum value from enum string name.
|
||||
template <typename E, typename = detail::enable_if_enum_t<E>>
|
||||
|
|
@ -269,6 +269,6 @@ std::ostream& operator<<(std::ostream& os, std::optional<E> value) {
|
|||
return os;
|
||||
}
|
||||
|
||||
} // namespace magic_enum::ops
|
||||
} // namespace yae::ops
|
||||
|
||||
} // namespace magic_enum
|
||||
} // namespace yae
|
||||
|
|
|
|||
162
test/test.cpp
162
test/test.cpp
|
|
@ -44,166 +44,166 @@ TEST_CASE("enum_cast") {
|
|||
# define constexpr // Visual Studio 2017 have bug with string_view constexpr compare.
|
||||
#endif
|
||||
|
||||
constexpr auto cr = magic_enum::enum_cast<Color>("RED");
|
||||
constexpr auto cr = yae::enum_cast<Color>("RED");
|
||||
REQUIRE(cr.value() == Color::RED);
|
||||
REQUIRE(magic_enum::enum_cast<Color>("GREEN").value() == Color::GREEN);
|
||||
REQUIRE(magic_enum::enum_cast<Color>("BLUE").value() == Color::BLUE);
|
||||
REQUIRE_FALSE(magic_enum::enum_cast<Color>("None").has_value());
|
||||
REQUIRE(yae::enum_cast<Color>("GREEN").value() == Color::GREEN);
|
||||
REQUIRE(yae::enum_cast<Color>("BLUE").value() == Color::BLUE);
|
||||
REQUIRE_FALSE(yae::enum_cast<Color>("None").has_value());
|
||||
|
||||
constexpr auto no = magic_enum::enum_cast<Numbers>("one");
|
||||
constexpr auto no = yae::enum_cast<Numbers>("one");
|
||||
REQUIRE(no.value() == Numbers::one);
|
||||
REQUIRE(magic_enum::enum_cast<Numbers>("two").value() == Numbers::two);
|
||||
REQUIRE(magic_enum::enum_cast<Numbers>("three").value() == Numbers::three);
|
||||
REQUIRE_FALSE(magic_enum::enum_cast<Numbers>("None").has_value());
|
||||
REQUIRE(yae::enum_cast<Numbers>("two").value() == Numbers::two);
|
||||
REQUIRE(yae::enum_cast<Numbers>("three").value() == Numbers::three);
|
||||
REQUIRE_FALSE(yae::enum_cast<Numbers>("None").has_value());
|
||||
|
||||
constexpr auto dr = magic_enum::enum_cast<Directions>("Right");
|
||||
REQUIRE(magic_enum::enum_cast<Directions>("Up").value() == Directions::Up);
|
||||
REQUIRE(magic_enum::enum_cast<Directions>("Down").value() == Directions::Down);
|
||||
constexpr auto dr = yae::enum_cast<Directions>("Right");
|
||||
REQUIRE(yae::enum_cast<Directions>("Up").value() == Directions::Up);
|
||||
REQUIRE(yae::enum_cast<Directions>("Down").value() == Directions::Down);
|
||||
REQUIRE(dr.value() == Directions::Right);
|
||||
REQUIRE(magic_enum::enum_cast<Directions>("Left").value() == Directions::Left);
|
||||
REQUIRE_FALSE(magic_enum::enum_cast<Directions>("None").has_value());
|
||||
REQUIRE(yae::enum_cast<Directions>("Left").value() == Directions::Left);
|
||||
REQUIRE_FALSE(yae::enum_cast<Directions>("None").has_value());
|
||||
|
||||
constexpr auto nt = magic_enum::enum_cast<number>("three");
|
||||
REQUIRE(magic_enum::enum_cast<number>("one").value() == number::one);
|
||||
REQUIRE(magic_enum::enum_cast<number>("two").value() == number::two);
|
||||
constexpr auto nt = yae::enum_cast<number>("three");
|
||||
REQUIRE(yae::enum_cast<number>("one").value() == number::one);
|
||||
REQUIRE(yae::enum_cast<number>("two").value() == number::two);
|
||||
REQUIRE(nt.value() == number::three);
|
||||
REQUIRE_FALSE(magic_enum::enum_cast<number>("None").has_value());
|
||||
REQUIRE_FALSE(yae::enum_cast<number>("None").has_value());
|
||||
|
||||
#undef constexpr
|
||||
}
|
||||
|
||||
SECTION("string") {
|
||||
constexpr auto cr = magic_enum::enum_cast<Color>(-12);
|
||||
constexpr auto cr = yae::enum_cast<Color>(-12);
|
||||
REQUIRE(cr.value() == Color::RED);
|
||||
REQUIRE(magic_enum::enum_cast<Color>(7).value() == Color::GREEN);
|
||||
REQUIRE(magic_enum::enum_cast<Color>(15).value() == Color::BLUE);
|
||||
REQUIRE_FALSE(magic_enum::enum_cast<Color>(0).has_value());
|
||||
REQUIRE(yae::enum_cast<Color>(7).value() == Color::GREEN);
|
||||
REQUIRE(yae::enum_cast<Color>(15).value() == Color::BLUE);
|
||||
REQUIRE_FALSE(yae::enum_cast<Color>(0).has_value());
|
||||
|
||||
constexpr auto no = magic_enum::enum_cast<Numbers>(10);
|
||||
constexpr auto no = yae::enum_cast<Numbers>(10);
|
||||
REQUIRE(no.value() == Numbers::one);
|
||||
REQUIRE(magic_enum::enum_cast<Numbers>(20).value() == Numbers::two);
|
||||
REQUIRE(magic_enum::enum_cast<Numbers>(30).value() == Numbers::three);
|
||||
REQUIRE_FALSE(magic_enum::enum_cast<Numbers>(0).has_value());
|
||||
REQUIRE(yae::enum_cast<Numbers>(20).value() == Numbers::two);
|
||||
REQUIRE(yae::enum_cast<Numbers>(30).value() == Numbers::three);
|
||||
REQUIRE_FALSE(yae::enum_cast<Numbers>(0).has_value());
|
||||
|
||||
constexpr auto dr = magic_enum::enum_cast<Directions>(119);
|
||||
REQUIRE(magic_enum::enum_cast<Directions>(85).value() == Directions::Up);
|
||||
REQUIRE(magic_enum::enum_cast<Directions>(-42).value() == Directions::Down);
|
||||
constexpr auto dr = yae::enum_cast<Directions>(119);
|
||||
REQUIRE(yae::enum_cast<Directions>(85).value() == Directions::Up);
|
||||
REQUIRE(yae::enum_cast<Directions>(-42).value() == Directions::Down);
|
||||
REQUIRE(dr.value() == Directions::Right);
|
||||
REQUIRE(magic_enum::enum_cast<Directions>(-119).value() == Directions::Left);
|
||||
REQUIRE_FALSE(magic_enum::enum_cast<Directions>(0).has_value());
|
||||
REQUIRE(yae::enum_cast<Directions>(-119).value() == Directions::Left);
|
||||
REQUIRE_FALSE(yae::enum_cast<Directions>(0).has_value());
|
||||
|
||||
constexpr auto nt = magic_enum::enum_cast<number>(30);
|
||||
REQUIRE(magic_enum::enum_cast<number>(10).value() == number::one);
|
||||
REQUIRE(magic_enum::enum_cast<number>(20).value() == number::two);
|
||||
constexpr auto nt = yae::enum_cast<number>(30);
|
||||
REQUIRE(yae::enum_cast<number>(10).value() == number::one);
|
||||
REQUIRE(yae::enum_cast<number>(20).value() == number::two);
|
||||
REQUIRE(nt.value() == number::three);
|
||||
REQUIRE_FALSE(magic_enum::enum_cast<number>(0).has_value());
|
||||
REQUIRE_FALSE(yae::enum_cast<number>(0).has_value());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("enum_value") {
|
||||
constexpr auto cr = magic_enum::enum_value<Color>(0);
|
||||
constexpr auto cr = yae::enum_value<Color>(0);
|
||||
REQUIRE(cr == Color::RED);
|
||||
REQUIRE(magic_enum::enum_value<Color>(1) == Color::GREEN);
|
||||
REQUIRE(magic_enum::enum_value<Color>(2) == Color::BLUE);
|
||||
REQUIRE(yae::enum_value<Color>(1) == Color::GREEN);
|
||||
REQUIRE(yae::enum_value<Color>(2) == Color::BLUE);
|
||||
|
||||
constexpr auto no = magic_enum::enum_value<Numbers>(0);
|
||||
constexpr auto no = yae::enum_value<Numbers>(0);
|
||||
REQUIRE(no == Numbers::one);
|
||||
REQUIRE(magic_enum::enum_value<Numbers>(1) == Numbers::two);
|
||||
REQUIRE(magic_enum::enum_value<Numbers>(2) == Numbers::three);
|
||||
REQUIRE(yae::enum_value<Numbers>(1) == Numbers::two);
|
||||
REQUIRE(yae::enum_value<Numbers>(2) == Numbers::three);
|
||||
|
||||
constexpr auto dr = magic_enum::enum_value<Directions>(3);
|
||||
REQUIRE(magic_enum::enum_value<Directions>(0) == Directions::Left);
|
||||
REQUIRE(magic_enum::enum_value<Directions>(1) == Directions::Down);
|
||||
REQUIRE(magic_enum::enum_value<Directions>(2) == Directions::Up);
|
||||
constexpr auto dr = yae::enum_value<Directions>(3);
|
||||
REQUIRE(yae::enum_value<Directions>(0) == Directions::Left);
|
||||
REQUIRE(yae::enum_value<Directions>(1) == Directions::Down);
|
||||
REQUIRE(yae::enum_value<Directions>(2) == Directions::Up);
|
||||
REQUIRE(dr == Directions::Right);
|
||||
|
||||
constexpr auto nt = magic_enum::enum_value<number>(2);
|
||||
REQUIRE(magic_enum::enum_value<number>(0) == number::one);
|
||||
REQUIRE(magic_enum::enum_value<number>(1) == number::two);
|
||||
constexpr auto nt = yae::enum_value<number>(2);
|
||||
REQUIRE(yae::enum_value<number>(0) == number::one);
|
||||
REQUIRE(yae::enum_value<number>(1) == number::two);
|
||||
REQUIRE(nt == number::three);
|
||||
}
|
||||
|
||||
TEST_CASE("enum_values") {
|
||||
constexpr auto mge_s1 = magic_enum::enum_values<Color>();
|
||||
constexpr auto mge_s1 = yae::enum_values<Color>();
|
||||
REQUIRE(mge_s1 == std::array<Color, 3>{Color::RED, Color::GREEN, Color::BLUE});
|
||||
|
||||
constexpr auto mge_s2 = magic_enum::enum_values<Numbers>();
|
||||
constexpr auto mge_s2 = yae::enum_values<Numbers>();
|
||||
REQUIRE(mge_s2 == std::array<Numbers, 3>{Numbers::one, Numbers::two, Numbers::three});
|
||||
|
||||
constexpr auto mge_s3 = magic_enum::enum_values<Directions>();
|
||||
constexpr auto mge_s3 = yae::enum_values<Directions>();
|
||||
REQUIRE(mge_s3 == std::array<Directions, 4>{Directions::Left, Directions::Down, Directions::Up, Directions::Right});
|
||||
|
||||
constexpr auto mge_s4 = magic_enum::enum_values<number>();
|
||||
constexpr auto mge_s4 = yae::enum_values<number>();
|
||||
REQUIRE(mge_s4 == std::array<number, 3>{number::one, number::two, number::three});
|
||||
}
|
||||
|
||||
TEST_CASE("enum_count") {
|
||||
constexpr auto mge_s1 = magic_enum::enum_count<Color>();
|
||||
constexpr auto mge_s1 = yae::enum_count<Color>();
|
||||
REQUIRE(mge_s1 == 3);
|
||||
|
||||
constexpr auto mge_s2 = magic_enum::enum_count<Numbers>();
|
||||
constexpr auto mge_s2 = yae::enum_count<Numbers>();
|
||||
REQUIRE(mge_s2 == 3);
|
||||
|
||||
constexpr auto mge_s3 = magic_enum::enum_count<Directions>();
|
||||
constexpr auto mge_s3 = yae::enum_count<Directions>();
|
||||
REQUIRE(mge_s3 == 4);
|
||||
|
||||
constexpr auto mge_s4 = magic_enum::enum_count<number>();
|
||||
constexpr auto mge_s4 = yae::enum_count<number>();
|
||||
REQUIRE(mge_s4 == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("enum_name") {
|
||||
constexpr Color cr = Color::RED;
|
||||
constexpr auto cr_name = magic_enum::enum_name(cr);
|
||||
constexpr auto cr_name = yae::enum_name(cr);
|
||||
Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
|
||||
REQUIRE(cr_name.value() == "RED");
|
||||
REQUIRE(magic_enum::enum_name(Color::BLUE).value() == "BLUE");
|
||||
REQUIRE(magic_enum::enum_name(cm[1]).value() == "GREEN");
|
||||
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Color>(MAGIC_ENUM_RANGE)).has_value());
|
||||
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Color>(-MAGIC_ENUM_RANGE)).has_value());
|
||||
REQUIRE(yae::enum_name(Color::BLUE).value() == "BLUE");
|
||||
REQUIRE(yae::enum_name(cm[1]).value() == "GREEN");
|
||||
REQUIRE_FALSE(yae::enum_name(static_cast<Color>(MAGIC_ENUM_RANGE)).has_value());
|
||||
REQUIRE_FALSE(yae::enum_name(static_cast<Color>(-MAGIC_ENUM_RANGE)).has_value());
|
||||
|
||||
constexpr Numbers no = Numbers::one;
|
||||
constexpr auto no_name = magic_enum::enum_name(no);
|
||||
constexpr auto no_name = yae::enum_name(no);
|
||||
REQUIRE(no_name.value() == "one");
|
||||
REQUIRE(magic_enum::enum_name(Numbers::two).value() == "two");
|
||||
REQUIRE(magic_enum::enum_name(Numbers::three).value() == "three");
|
||||
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Numbers>(MAGIC_ENUM_RANGE)).has_value());
|
||||
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Numbers>(-MAGIC_ENUM_RANGE)).has_value());
|
||||
REQUIRE(yae::enum_name(Numbers::two).value() == "two");
|
||||
REQUIRE(yae::enum_name(Numbers::three).value() == "three");
|
||||
REQUIRE_FALSE(yae::enum_name(static_cast<Numbers>(MAGIC_ENUM_RANGE)).has_value());
|
||||
REQUIRE_FALSE(yae::enum_name(static_cast<Numbers>(-MAGIC_ENUM_RANGE)).has_value());
|
||||
|
||||
constexpr Directions dr = Directions::Right;
|
||||
constexpr auto dr_name = magic_enum::enum_name(dr);
|
||||
REQUIRE(magic_enum::enum_name(Directions::Up).value() == "Up");
|
||||
REQUIRE(magic_enum::enum_name(Directions::Down).value() == "Down");
|
||||
constexpr auto dr_name = yae::enum_name(dr);
|
||||
REQUIRE(yae::enum_name(Directions::Up).value() == "Up");
|
||||
REQUIRE(yae::enum_name(Directions::Down).value() == "Down");
|
||||
REQUIRE(dr_name.value() == "Right");
|
||||
REQUIRE(magic_enum::enum_name(Directions::Left).value() == "Left");
|
||||
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Directions>(MAGIC_ENUM_RANGE)).has_value());
|
||||
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Directions>(-MAGIC_ENUM_RANGE)).has_value());
|
||||
REQUIRE(yae::enum_name(Directions::Left).value() == "Left");
|
||||
REQUIRE_FALSE(yae::enum_name(static_cast<Directions>(MAGIC_ENUM_RANGE)).has_value());
|
||||
REQUIRE_FALSE(yae::enum_name(static_cast<Directions>(-MAGIC_ENUM_RANGE)).has_value());
|
||||
|
||||
constexpr number nt = number::three;
|
||||
constexpr auto nt_name = magic_enum::enum_name(nt);
|
||||
REQUIRE(magic_enum::enum_name(number::one).value() == "one");
|
||||
REQUIRE(magic_enum::enum_name(number::two).value() == "two");
|
||||
constexpr auto nt_name = yae::enum_name(nt);
|
||||
REQUIRE(yae::enum_name(number::one).value() == "one");
|
||||
REQUIRE(yae::enum_name(number::two).value() == "two");
|
||||
REQUIRE(nt_name.value() == "three");
|
||||
REQUIRE_FALSE(magic_enum::enum_name(static_cast<number>(MAGIC_ENUM_RANGE)).has_value());
|
||||
REQUIRE_FALSE(magic_enum::enum_name(static_cast<number>(-MAGIC_ENUM_RANGE)).has_value());
|
||||
REQUIRE_FALSE(yae::enum_name(static_cast<number>(MAGIC_ENUM_RANGE)).has_value());
|
||||
REQUIRE_FALSE(yae::enum_name(static_cast<number>(-MAGIC_ENUM_RANGE)).has_value());
|
||||
}
|
||||
|
||||
TEST_CASE("enum_names") {
|
||||
constexpr auto mge_s1 = magic_enum::enum_names<Color>();
|
||||
constexpr auto mge_s1 = yae::enum_names<Color>();
|
||||
REQUIRE(mge_s1 == std::array<std::string_view, 3>{"RED", "GREEN", "BLUE"});
|
||||
|
||||
constexpr auto mge_s2 = magic_enum::enum_names<Numbers>();
|
||||
constexpr auto mge_s2 = yae::enum_names<Numbers>();
|
||||
REQUIRE(mge_s2 == std::array<std::string_view, 3>{"one", "two", "three"});
|
||||
|
||||
constexpr auto mge_s3 = magic_enum::enum_names<Directions>();
|
||||
constexpr auto mge_s3 = yae::enum_names<Directions>();
|
||||
REQUIRE(mge_s3 == std::array<std::string_view, 4>{"Left", "Down", "Up", "Right"});
|
||||
|
||||
constexpr auto mge_s4 = magic_enum::enum_names<number>();
|
||||
constexpr auto mge_s4 = yae::enum_names<number>();
|
||||
REQUIRE(mge_s4 == std::array<std::string_view, 3>{"one", "two", "three"});
|
||||
}
|
||||
|
||||
TEST_CASE("operator<<") {
|
||||
auto test_ostream = [](auto e, std::string_view name) {
|
||||
using namespace magic_enum::ops;
|
||||
using namespace yae::ops;
|
||||
std::stringstream ss;
|
||||
ss << e;
|
||||
REQUIRE(ss.str() == name);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue