mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-10 23:44:29 +00:00
v0.4.0
This commit is contained in:
parent
82a326876d
commit
11a0366421
5 changed files with 198 additions and 168 deletions
|
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.6)
|
||||
|
||||
project(magic_enum VERSION "0.3.0" LANGUAGES CXX)
|
||||
project(magic_enum VERSION "0.4.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)
|
||||
|
|
|
|||
65
README.md
65
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/kXdow0AxI1Dss18p)
|
||||
[](https://wandbox.org/permlink/jPWeZxV1UcqvudZr)
|
||||
|
||||
## What is Magic Enum?
|
||||
|
||||
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.
|
||||
Header-only C++17 library provides static reflection on enums, work with any enum type without any macro or boilerplate code.
|
||||
* `enum_cast` obtains enum value from string or integer.
|
||||
* `enum_value` returns enum value at specified index.
|
||||
* `enum_values` obtains enum value sequence.
|
||||
* `enum_count` returns number of enum values.
|
||||
* `enum_name` obtains string name from enum value.
|
||||
* `enum_names` obtains string enum name sequence.
|
||||
|
||||
## Features
|
||||
|
||||
|
|
@ -34,6 +34,7 @@ Header-only C++17 library provides Enum-to-String and String-to-Enum and other u
|
|||
* Header-only
|
||||
* Dependency-free
|
||||
* Compile-time
|
||||
* Static reflection on enums
|
||||
* Enum to string
|
||||
* String to enum
|
||||
* Work with any enum type
|
||||
|
|
@ -49,7 +50,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* Enum value to string
|
||||
```cpp
|
||||
Color color = Color::RED;
|
||||
auto color_name = yae::enum_name(color);
|
||||
auto color_name = magic_enum::enum_name(color);
|
||||
if (color_name.has_value()) {
|
||||
// color_name.value() -> "RED"
|
||||
}
|
||||
|
|
@ -58,7 +59,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 = yae::enum_name(color);
|
||||
constexpr auto color_name = magic_enum::enum_name(color);
|
||||
if (color_name.has_value()) {
|
||||
// color_name.value() -> "BLUE"
|
||||
}
|
||||
|
|
@ -67,7 +68,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* String to enum value
|
||||
```cpp
|
||||
std::string color_name{"GREEN"};
|
||||
auto color = yae::enum_cast<Color>(color_name);
|
||||
auto color = magic_enum::enum_cast<Color>(color_name);
|
||||
if (color.has_value()) {
|
||||
// color.value() -> Color::GREEN
|
||||
}
|
||||
|
|
@ -75,7 +76,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
|
||||
* Static storage string to enum value
|
||||
```cpp
|
||||
constexpr auto color = yae::enum_cast<Color>("BLUE");
|
||||
constexpr auto color = magic_enum::enum_cast<Color>("BLUE");
|
||||
if (color.has_value()) {
|
||||
// color.value() -> Color::BLUE
|
||||
}
|
||||
|
|
@ -84,7 +85,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* Integer to enum value
|
||||
```cpp
|
||||
int color_value = 2;
|
||||
auto color = yae::enum_cast<Color>(color_value);
|
||||
auto color = magic_enum::enum_cast<Color>(color_value);
|
||||
if (colo.has_value()) {
|
||||
// color.value() -> Color::RED
|
||||
}
|
||||
|
|
@ -92,7 +93,7 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
|
||||
* Static storage integer to enum value
|
||||
```cpp
|
||||
constexpr auto color = yae::enum_cast<Color>(4);
|
||||
constexpr auto color = magic_enum::enum_cast<Color>(4);
|
||||
if (color.has_value()) {
|
||||
// color.value() -> Color::BLUE
|
||||
}
|
||||
|
|
@ -101,55 +102,67 @@ enum Color { RED = 2, BLUE = 4, GREEN = 8 };
|
|||
* Indexed access to enum value
|
||||
```cpp
|
||||
int i = 1;
|
||||
Color colo = yae::enum_value<Color>(i);
|
||||
Color colo = magic_enum::enum_value<Color>(i);
|
||||
// color -> Color::BLUE
|
||||
```
|
||||
|
||||
* Compile-time indexed access.
|
||||
```cpp
|
||||
constexpr Color color = yae::enum_value<Color>(0);
|
||||
constexpr Color color = magic_enum::enum_value<Color>(0);
|
||||
// color -> Color::RED
|
||||
```
|
||||
|
||||
* Enum value sequence
|
||||
```cpp
|
||||
constexpr auto colors = yae::enum_values<Color>();
|
||||
constexpr auto colors = magic_enum::enum_values<Color>();
|
||||
// colors -> {Color::RED, Color::BLUE, Color::GREEN}
|
||||
```
|
||||
|
||||
* Number of enum elements
|
||||
```cpp
|
||||
constexpr std::size_t color_count = yae::enum_count<Color>();
|
||||
constexpr std::size_t color_count = magic_enum::enum_count<Color>();
|
||||
// color_count -> 3
|
||||
```
|
||||
|
||||
* Enum names sequence
|
||||
```cpp
|
||||
constexpr auto color_names = yae::enum_names<Color>();
|
||||
constexpr auto color_names = magic_enum::enum_names<Color>();
|
||||
// color_names -> {"RED", "BLUE", "GREEN"}
|
||||
```
|
||||
|
||||
* Stream operator for enum
|
||||
```cpp
|
||||
using namespace yae::ops; // out-of-the-box stream operator for enums.
|
||||
using namespace magic_enum::ops; // out-of-the-box stream operator for enums.
|
||||
Color color = Color::BLUE;
|
||||
std::cout << color << std::endl; // "BLUE"
|
||||
```
|
||||
|
||||
## Remarks
|
||||
|
||||
* `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_cast` returns `std::optional<E>`, using `has_value()` to check contains enum value and `value()` to get the 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_value` no bounds checking is performed: the behavior is undefined if `index >= number of enum values`.
|
||||
|
||||
* `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_values` returns `std::array<E, N>` with all enum value 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.
|
||||
* `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.
|
||||
|
||||
* 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`.
|
||||
* `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.
|
||||
|
||||
* Enum value must be in range `[-256, 256]`. If you need another range, add specialization enum_range for necessary enum type.
|
||||
```cpp
|
||||
#define MAGIC_ENUM_RANGE 1024 // Redefine MAGIC_ENUM_RANGE for larger range.
|
||||
#include <magic_enum.hpp>
|
||||
|
||||
enum number { one = 100, two = 200, three = 300 };
|
||||
|
||||
namespace magic_enum {
|
||||
template <>
|
||||
struct enum_range<number> {
|
||||
static constexpr int min = 100;
|
||||
static constexpr int max = 300;
|
||||
};
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Integration
|
||||
|
|
|
|||
|
|
@ -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 = yae::enum_name(c1);
|
||||
auto c1_name = magic_enum::enum_name(c1);
|
||||
if (c1_name.has_value()) {
|
||||
std::cout << c1_name.value() << std::endl; // RED
|
||||
}
|
||||
|
||||
// String enum name sequence.
|
||||
constexpr auto color_names = yae::enum_names<Color>();
|
||||
constexpr auto color_names = magic_enum::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 = yae::enum_cast<Color>("BLUE");
|
||||
auto c2 = magic_enum::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 = yae::enum_cast<Color>(10);
|
||||
auto c3 = magic_enum::enum_cast<Color>(10);
|
||||
if (c3.has_value() && c3.value() == Color::GREEN) {
|
||||
std::cout << "GREEN = " << c3.value() << std::endl; // GREEN = 10
|
||||
}
|
||||
|
||||
using namespace yae::ops; // out-of-the-box stream operator for enums.
|
||||
using namespace magic_enum::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: " << yae::enum_count<Color>() << std::endl; // Color enum size: 3
|
||||
std::cout << "Color enum size: " << magic_enum::enum_count<Color>() << std::endl; // Color enum size: 3
|
||||
|
||||
// Indexed access to enum value.
|
||||
std::cout << "Color[0] = " << yae::enum_value<Color>(0) << std::endl; // Color[0] = RED
|
||||
std::cout << "Color[0] = " << magic_enum::enum_value<Color>(0) << std::endl; // Color[0] = RED
|
||||
|
||||
// Enum value sequence.
|
||||
constexpr auto colors = yae::enum_values<Color>();
|
||||
constexpr auto colors = magic_enum::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
|
||||
// |___/ vesion 0.4.0
|
||||
//
|
||||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
|
@ -31,7 +31,6 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
|
|
@ -42,34 +41,44 @@
|
|||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
// Enum value must be in range (-MAGIC_ENUM_RANGE, MAGIC_ENUM_RANGE). If you need a larger range, redefine the macro MAGIC_ENUM_RANGE.
|
||||
#if !defined(MAGIC_ENUM_RANGE)
|
||||
# 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.");
|
||||
static_assert(MAGIC_ENUM_RANGE < std::numeric_limits<int>::max(),
|
||||
"MAGIC_ENUM_RANGE must be less INT_MAX.");
|
||||
// Enum value must be in range [-256, 256]. If you need another range, add specialization enum_range for necessary enum type.
|
||||
template <typename E>
|
||||
struct enum_range final {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::enum_range requires enum type.");
|
||||
static constexpr int min = std::is_signed_v<std::underlying_type_t<E>> ? -256 : 0;
|
||||
static constexpr int max = 256;
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename E>
|
||||
struct enum_range final {
|
||||
using D = std::decay_t<E>;
|
||||
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());
|
||||
static constexpr int max = std::min<C>(MAGIC_ENUM_RANGE, std::numeric_limits<U>::max());
|
||||
static constexpr auto sequence = std::make_integer_sequence<int, max - min + 1>{};
|
||||
[[nodiscard]] constexpr int min_impl() {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::min_impl requires enum type.");
|
||||
using U = std::underlying_type_t<E>;
|
||||
constexpr int min = (enum_range<E>::min > std::numeric_limits<U>::min()) ? enum_range<E>::min : std::numeric_limits<U>::min();
|
||||
|
||||
return min;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
[[nodiscard]] constexpr int max_impl() {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::max_impl requires enum type.");
|
||||
using U = std::underlying_type_t<E>;
|
||||
constexpr int max = (enum_range<E>::max < std::numeric_limits<U>::max()) ? enum_range<E>::max : std::numeric_limits<U>::max();
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
[[nodiscard]] constexpr decltype(auto) range_impl() {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::range_impl requires enum type.");
|
||||
constexpr auto range = std::make_integer_sequence<int, max_impl<E>() - min_impl<E>() + 1>{};
|
||||
|
||||
return range;
|
||||
}
|
||||
|
||||
static constexpr bool contains(int value) noexcept {
|
||||
return static_cast<int>(value) <= enum_range<D>::max && static_cast<int>(value) >= enum_range<D>::min;
|
||||
}
|
||||
};
|
||||
|
||||
[[nodiscard]] constexpr bool is_name_char(char c, bool front) noexcept {
|
||||
return (!front && c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
|
||||
|
|
@ -77,7 +86,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>, "yae::detail::name_impl require enum type.");
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::name_impl requires enum type.");
|
||||
#if defined(__clang__)
|
||||
std::string_view name{__PRETTY_FUNCTION__};
|
||||
constexpr auto suffix = sizeof("]") - 1;
|
||||
|
|
@ -110,31 +119,36 @@ 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>, "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)>()...}};
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::strings_impl requires enum type.");
|
||||
constexpr std::array<std::string_view, sizeof...(I)> names{{name_impl<E, static_cast<E>(I + min_impl<E>())>()...}};
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
[[nodiscard]] constexpr std::string_view name_impl(int value) noexcept {
|
||||
static_assert(std::is_enum_v<E>, "yae::detail::name_impl require enum type.");
|
||||
constexpr auto names = strings_impl<E>(enum_range<E>::sequence);
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::name_impl requires enum type.");
|
||||
constexpr auto names = strings_impl<E>(range_impl<E>());
|
||||
const int i = value - min_impl<E>();
|
||||
|
||||
return enum_range<E>::contains(value) ? names[value - enum_range<E>::min] : std::string_view{};
|
||||
if (i >= 0 && static_cast<std::size_t>(i) < names.size()) {
|
||||
return names[i];
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E, int... I>
|
||||
[[nodiscard]] constexpr decltype(auto) values_impl(std::integer_sequence<int, I...>) noexcept {
|
||||
static_assert(std::is_enum_v<E>, "yae::detail::values_impl require enum type.");
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::values_impl requires 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 std::array<bool, n> valid{{!name_impl<E, static_cast<E>(I + min_impl<E>())>().empty()...}};
|
||||
constexpr int num_valid = ((valid[I] ? 1 : 0) + ...);
|
||||
|
||||
std::array<E, num_valid> enums{};
|
||||
for (int i = 0, v = 0; i < n && v < num_valid; ++i) {
|
||||
if (valid[i]) {
|
||||
enums[v++] = static_cast<E>(i + enum_range<E>::min);
|
||||
enums[v++] = static_cast<E>(i + min_impl<E>());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -143,8 +157,8 @@ 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>, "yae::detail::names_impl require enum type.");
|
||||
constexpr auto enums = values_impl<E>(enum_range<E>::sequence);
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::names_impl requires enum type.");
|
||||
constexpr auto enums = values_impl<E>(range_impl<E>());
|
||||
constexpr std::array<std::string_view, sizeof...(I)> names{{name_impl<E, enums[I]>()...}};
|
||||
|
||||
return names;
|
||||
|
|
@ -152,8 +166,8 @@ 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>, "yae::detail::enum_cast_impl require enum type.");
|
||||
constexpr auto values = values_impl<E>(enum_range<E>::sequence);
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::enum_cast_impl requires enum type.");
|
||||
constexpr auto values = values_impl<E>(range_impl<E>());
|
||||
constexpr auto count = values.size();
|
||||
constexpr auto names = names_impl<E>(std::make_index_sequence<count>{});
|
||||
|
||||
|
|
@ -169,7 +183,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 yae::detail
|
||||
} // namespace magic_enum::detail
|
||||
|
||||
// Obtains enum value from enum string name.
|
||||
template <typename E, typename = detail::enable_if_enum_t<E>>
|
||||
|
|
@ -191,29 +205,29 @@ template <typename E, typename = detail::enable_if_enum_t<E>>
|
|||
}
|
||||
}
|
||||
|
||||
// Obtains indexed access to enum element.
|
||||
// Returns enum value at specified index. No bounds checking is performed: the behavior is undefined if index >= number of enum values.
|
||||
template<typename E, typename = detail::enable_if_enum_t<E>>
|
||||
[[nodiscard]] constexpr E enum_value(std::size_t i) noexcept {
|
||||
[[nodiscard]] constexpr E enum_value(std::size_t index) {
|
||||
using D = std::decay_t<E>;
|
||||
constexpr auto values = detail::values_impl<D>(detail::enum_range<D>::sequence);
|
||||
constexpr auto values = detail::values_impl<D>(detail::range_impl<E>());
|
||||
|
||||
return assert(i < values.size()), values[i];
|
||||
return assert(index < values.size()), values[index];
|
||||
}
|
||||
|
||||
// Obtains value enum sequence.
|
||||
template <typename E, typename = detail::enable_if_enum_t<E>>
|
||||
[[nodiscard]] constexpr decltype(auto) enum_values() noexcept {
|
||||
using D = std::decay_t<E>;
|
||||
constexpr auto values = detail::values_impl<D>(detail::enum_range<D>::sequence);
|
||||
constexpr auto values = detail::values_impl<D>(detail::range_impl<E>());
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
// Obtains number of enum elements.
|
||||
// Returns number of enum values.
|
||||
template <typename E, typename = detail::enable_if_enum_t<E>>
|
||||
[[nodiscard]] constexpr std::size_t enum_count() noexcept {
|
||||
using D = std::decay_t<E>;
|
||||
constexpr auto count = detail::values_impl<D>(detail::enum_range<D>::sequence).size();
|
||||
constexpr auto count = detail::values_impl<D>(detail::range_impl<E>()).size();
|
||||
|
||||
return count;
|
||||
}
|
||||
|
|
@ -225,9 +239,9 @@ template <typename E, typename = detail::enable_if_enum_t<E>>
|
|||
const auto name = detail::name_impl<D>(static_cast<int>(value));
|
||||
|
||||
if (name.empty()) {
|
||||
return std::nullopt;
|
||||
return std::nullopt; // Invalid value or out of range.
|
||||
} else {
|
||||
return name; // Enum value does not have name or out of range.
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -235,7 +249,7 @@ template <typename E, typename = detail::enable_if_enum_t<E>>
|
|||
template <typename E, typename = detail::enable_if_enum_t<E>>
|
||||
[[nodiscard]] constexpr decltype(auto) enum_names() noexcept {
|
||||
using D = std::decay_t<E>;
|
||||
constexpr auto count = detail::values_impl<D>(detail::enum_range<D>::sequence).size();
|
||||
constexpr auto count = detail::values_impl<D>(detail::range_impl<E>()).size();
|
||||
constexpr auto names = detail::names_impl<D>(std::make_index_sequence<count>{});
|
||||
|
||||
return names;
|
||||
|
|
@ -269,6 +283,6 @@ std::ostream& operator<<(std::ostream& os, std::optional<E> value) {
|
|||
return os;
|
||||
}
|
||||
|
||||
} // namespace yae::ops
|
||||
} // namespace magic_enum::ops
|
||||
|
||||
} // namespace yae
|
||||
} // namespace magic_enum
|
||||
|
|
|
|||
169
test/test.cpp
169
test/test.cpp
|
|
@ -23,7 +23,6 @@
|
|||
#define CATCH_CONFIG_MAIN
|
||||
#include <catch.hpp>
|
||||
|
||||
#define MAGIC_ENUM_RANGE 120
|
||||
#include <magic_enum.hpp>
|
||||
|
||||
#include <array>
|
||||
|
|
@ -36,7 +35,15 @@ enum class Numbers : char { one = 10, two = 20, three = 30 };
|
|||
|
||||
enum Directions { Up = 85, Down = -42, Right = 119, Left = -119 };
|
||||
|
||||
enum number : unsigned long { one = 10, two = 20, three = 30 };
|
||||
enum number : unsigned long { one = 100, two = 200, three = 300 };
|
||||
|
||||
namespace magic_enum {
|
||||
template <>
|
||||
struct enum_range<number> {
|
||||
static constexpr int min = 100;
|
||||
static constexpr int max = 300;
|
||||
};
|
||||
}
|
||||
|
||||
TEST_CASE("enum_cast") {
|
||||
SECTION("string") {
|
||||
|
|
@ -44,166 +51,162 @@ TEST_CASE("enum_cast") {
|
|||
# define constexpr // Visual Studio 2017 have bug with string_view constexpr compare.
|
||||
#endif
|
||||
|
||||
constexpr auto cr = yae::enum_cast<Color>("RED");
|
||||
constexpr auto cr = magic_enum::enum_cast<Color>("RED");
|
||||
REQUIRE(cr.value() == Color::RED);
|
||||
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());
|
||||
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());
|
||||
|
||||
constexpr auto no = yae::enum_cast<Numbers>("one");
|
||||
constexpr auto no = magic_enum::enum_cast<Numbers>("one");
|
||||
REQUIRE(no.value() == Numbers::one);
|
||||
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());
|
||||
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());
|
||||
|
||||
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);
|
||||
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);
|
||||
REQUIRE(dr.value() == Directions::Right);
|
||||
REQUIRE(yae::enum_cast<Directions>("Left").value() == Directions::Left);
|
||||
REQUIRE_FALSE(yae::enum_cast<Directions>("None").has_value());
|
||||
REQUIRE(magic_enum::enum_cast<Directions>("Left").value() == Directions::Left);
|
||||
REQUIRE_FALSE(magic_enum::enum_cast<Directions>("None").has_value());
|
||||
|
||||
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);
|
||||
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);
|
||||
REQUIRE(nt.value() == number::three);
|
||||
REQUIRE_FALSE(yae::enum_cast<number>("None").has_value());
|
||||
REQUIRE_FALSE(magic_enum::enum_cast<number>("None").has_value());
|
||||
|
||||
#undef constexpr
|
||||
}
|
||||
|
||||
SECTION("integer") {
|
||||
constexpr auto cr = yae::enum_cast<Color>(-12);
|
||||
constexpr auto cr = magic_enum::enum_cast<Color>(-12);
|
||||
REQUIRE(cr.value() == Color::RED);
|
||||
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());
|
||||
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());
|
||||
|
||||
constexpr auto no = yae::enum_cast<Numbers>(10);
|
||||
constexpr auto no = magic_enum::enum_cast<Numbers>(10);
|
||||
REQUIRE(no.value() == Numbers::one);
|
||||
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());
|
||||
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());
|
||||
|
||||
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);
|
||||
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);
|
||||
REQUIRE(dr.value() == Directions::Right);
|
||||
REQUIRE(yae::enum_cast<Directions>(-119).value() == Directions::Left);
|
||||
REQUIRE_FALSE(yae::enum_cast<Directions>(0).has_value());
|
||||
REQUIRE(magic_enum::enum_cast<Directions>(-119).value() == Directions::Left);
|
||||
REQUIRE_FALSE(magic_enum::enum_cast<Directions>(0).has_value());
|
||||
|
||||
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);
|
||||
constexpr auto nt = magic_enum::enum_cast<number>(300);
|
||||
REQUIRE(magic_enum::enum_cast<number>(100).value() == number::one);
|
||||
REQUIRE(magic_enum::enum_cast<number>(200).value() == number::two);
|
||||
REQUIRE(nt.value() == number::three);
|
||||
REQUIRE_FALSE(yae::enum_cast<number>(0).has_value());
|
||||
REQUIRE_FALSE(magic_enum::enum_cast<number>(0).has_value());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("enum_value") {
|
||||
constexpr auto cr = yae::enum_value<Color>(0);
|
||||
constexpr auto cr = magic_enum::enum_value<Color>(0);
|
||||
REQUIRE(cr == Color::RED);
|
||||
REQUIRE(yae::enum_value<Color>(1) == Color::GREEN);
|
||||
REQUIRE(yae::enum_value<Color>(2) == Color::BLUE);
|
||||
REQUIRE(magic_enum::enum_value<Color>(1) == Color::GREEN);
|
||||
REQUIRE(magic_enum::enum_value<Color>(2) == Color::BLUE);
|
||||
|
||||
constexpr auto no = yae::enum_value<Numbers>(0);
|
||||
constexpr auto no = magic_enum::enum_value<Numbers>(0);
|
||||
REQUIRE(no == Numbers::one);
|
||||
REQUIRE(yae::enum_value<Numbers>(1) == Numbers::two);
|
||||
REQUIRE(yae::enum_value<Numbers>(2) == Numbers::three);
|
||||
REQUIRE(magic_enum::enum_value<Numbers>(1) == Numbers::two);
|
||||
REQUIRE(magic_enum::enum_value<Numbers>(2) == Numbers::three);
|
||||
|
||||
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);
|
||||
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);
|
||||
REQUIRE(dr == Directions::Right);
|
||||
|
||||
constexpr auto nt = yae::enum_value<number>(2);
|
||||
REQUIRE(yae::enum_value<number>(0) == number::one);
|
||||
REQUIRE(yae::enum_value<number>(1) == number::two);
|
||||
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);
|
||||
REQUIRE(nt == number::three);
|
||||
}
|
||||
|
||||
TEST_CASE("enum_values") {
|
||||
constexpr auto s1 = yae::enum_values<Color>();
|
||||
constexpr auto s1 = magic_enum::enum_values<Color>();
|
||||
REQUIRE(s1 == std::array<Color, 3>{{Color::RED, Color::GREEN, Color::BLUE}});
|
||||
|
||||
constexpr auto s2 = yae::enum_values<Numbers>();
|
||||
constexpr auto s2 = magic_enum::enum_values<Numbers>();
|
||||
REQUIRE(s2 == std::array<Numbers, 3>{{Numbers::one, Numbers::two, Numbers::three}});
|
||||
|
||||
constexpr auto s3 = yae::enum_values<Directions>();
|
||||
constexpr auto s3 = magic_enum::enum_values<Directions>();
|
||||
REQUIRE(s3 == std::array<Directions, 4>{{Directions::Left, Directions::Down, Directions::Up, Directions::Right}});
|
||||
|
||||
constexpr auto s4 = yae::enum_values<number>();
|
||||
constexpr auto s4 = magic_enum::enum_values<number>();
|
||||
REQUIRE(s4 == std::array<number, 3>{{number::one, number::two, number::three}});
|
||||
}
|
||||
|
||||
TEST_CASE("enum_count") {
|
||||
constexpr auto s1 = yae::enum_count<Color>();
|
||||
constexpr auto s1 = magic_enum::enum_count<Color>();
|
||||
REQUIRE(s1 == 3);
|
||||
|
||||
constexpr auto s2 = yae::enum_count<Numbers>();
|
||||
constexpr auto s2 = magic_enum::enum_count<Numbers>();
|
||||
REQUIRE(s2 == 3);
|
||||
|
||||
constexpr auto s3 = yae::enum_count<Directions>();
|
||||
constexpr auto s3 = magic_enum::enum_count<Directions>();
|
||||
REQUIRE(s3 == 4);
|
||||
|
||||
constexpr auto s4 = yae::enum_count<number>();
|
||||
constexpr auto s4 = magic_enum::enum_count<number>();
|
||||
REQUIRE(s4 == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("enum_name") {
|
||||
constexpr Color cr = Color::RED;
|
||||
constexpr auto cr_name = yae::enum_name(cr);
|
||||
constexpr auto cr_name = magic_enum::enum_name(cr);
|
||||
Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
|
||||
REQUIRE(cr_name.value() == "RED");
|
||||
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());
|
||||
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>(0)).has_value());
|
||||
|
||||
constexpr Numbers no = Numbers::one;
|
||||
constexpr auto no_name = yae::enum_name(no);
|
||||
constexpr auto no_name = magic_enum::enum_name(no);
|
||||
REQUIRE(no_name.value() == "one");
|
||||
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());
|
||||
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>(0)).has_value());
|
||||
|
||||
constexpr Directions dr = Directions::Right;
|
||||
constexpr auto dr_name = yae::enum_name(dr);
|
||||
REQUIRE(yae::enum_name(Directions::Up).value() == "Up");
|
||||
REQUIRE(yae::enum_name(Directions::Down).value() == "Down");
|
||||
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");
|
||||
REQUIRE(dr_name.value() == "Right");
|
||||
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());
|
||||
REQUIRE(magic_enum::enum_name(Directions::Left).value() == "Left");
|
||||
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Directions>(0)).has_value());
|
||||
|
||||
constexpr number nt = number::three;
|
||||
constexpr auto nt_name = yae::enum_name(nt);
|
||||
REQUIRE(yae::enum_name(number::one).value() == "one");
|
||||
REQUIRE(yae::enum_name(number::two).value() == "two");
|
||||
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");
|
||||
REQUIRE(nt_name.value() == "three");
|
||||
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());
|
||||
REQUIRE_FALSE(magic_enum::enum_name(static_cast<number>(0)).has_value());
|
||||
}
|
||||
|
||||
TEST_CASE("enum_names") {
|
||||
constexpr auto s1 = yae::enum_names<Color>();
|
||||
constexpr auto s1 = magic_enum::enum_names<Color>();
|
||||
REQUIRE(s1 == std::array<std::string_view, 3>{{"RED", "GREEN", "BLUE"}});
|
||||
|
||||
constexpr auto s2 = yae::enum_names<Numbers>();
|
||||
constexpr auto s2 = magic_enum::enum_names<Numbers>();
|
||||
REQUIRE(s2 == std::array<std::string_view, 3>{{"one", "two", "three"}});
|
||||
|
||||
constexpr auto s3 = yae::enum_names<Directions>();
|
||||
constexpr auto s3 = magic_enum::enum_names<Directions>();
|
||||
REQUIRE(s3 == std::array<std::string_view, 4>{{"Left", "Down", "Up", "Right"}});
|
||||
|
||||
constexpr auto s4 = yae::enum_names<number>();
|
||||
constexpr auto s4 = magic_enum::enum_names<number>();
|
||||
REQUIRE(s4 == std::array<std::string_view, 3>{{"one", "two", "three"}});
|
||||
}
|
||||
|
||||
TEST_CASE("operator<<") {
|
||||
auto test_ostream = [](auto e, std::string_view name) {
|
||||
using namespace yae::ops;
|
||||
using namespace magic_enum::ops;
|
||||
std::stringstream ss;
|
||||
ss << e;
|
||||
REQUIRE(ss.str() == name);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue