1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-09 23:34:23 +00:00

dev v0.3.0

This commit is contained in:
terik23 2019-04-07 04:02:53 +05:00
parent 5a3d4b53e6
commit 74eac12c11
5 changed files with 399 additions and 219 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
build/
.vscode/
### C++ gitignore ###
# Prerequisites

View file

@ -21,10 +21,12 @@
## What is Magic Enum?
Header-only C++17 library provides Enum-to-String and String-to-Enum functions.
* `magic_enum::enum_to_string` obtains string enum name from enum variable.
* `magic_enum::enum_from_string` obtains enum value from enum string name.
* `magic_enum::enum_to_sequence` obtains value enum sequence.
* `magic_enum::enum_to_string_sequence` obtains string enum name sequence.
* `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.
## Features
@ -34,56 +36,84 @@ Header-only C++17 library provides Enum-to-String and String-to-Enum functions.
* Compile-time
* Enum to string
* String to enum
* Works with any enum type
## [Examples](example/example.cpp)
* Enum variable to string enum name
* Enum value to string
```cpp
auto color = Color::RED;
auto color_name = magic_enum::enum_to_string(color);
Color color = Color::RED;
auto color_name = magic_enum::enum_name(color);
if (color_name.has_value()) {
// color_name.value() -> "RED"
}
```
* Static storage enum variable to string enum name
* Static storage enum variable to string
```cpp
constexpr auto color = Color::BLUE;
constexpr auto color_name = magic_enum::enum_to_string<color>();
constexpr Color color = Color::BLUE;
constexpr auto color_name = magic_enum::enum_name(color);
if (color_name.has_value()) {
// color_name.value() -> "BLUE"
}
```
* String enum name to enum value
* String to enum value
```cpp
constexpr auto color = magic_enum::enum_from_string<Color>("GREEN");
constexpr auto color = magic_enum::enum_cast<Color>("GREEN");
if (color.has_value()) {
// color.value() -> Color::GREEN
}
```
* Enum to value sequence
* Integer to enum value
```cpp
constexpr auto colors = magic_enum::enum_to_sequence<Color>();
constexpr auto color = magic_enum::enum_cast<Color>(0);
if (color.has_value()) {
// color.value() -> Color::RED
}
```
* Indexed access to enum value
```cpp
constexpr Color color_value = magic_enum::enum_value<Color>(0);
// color_element -> Color::RED
```
* Enum value sequence
```cpp
constexpr auto colors = magic_enum::enum_values<Color>();
// colors -> {Color::RED, Color::BLUE, Color::GREEN}
```
* Enum to string enum name sequence
* Number of enum elements
```cpp
constexpr auto color_names = magic_enum::enum_to_string_sequence<Color>();
constexpr std::size_t color_size = magic_enum::enum_count<Color>();
// color_size -> 3
```
* Enum string sequence
```cpp
constexpr auto color_names = magic_enum::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.
Color color = Color::BLUE;
std::cout << color << std::endl; // "BLUE"
```
## Remarks
* `magic_enum::enum_to_string` returns `std::optional<std::string_view>`, using `has_value()` to check contains enum name and `value()` to get the enum name. If enum value does not have name or out of range `MAGIC_ENUM_RANGE`, returns `std::nullopt`.
* `magic_enum::enum_cast` returns `std::optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value.
* `magic_enum::enum_from_string` returns `std::optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value. If enum value does not have name or out of range `MAGIC_ENUM_RANGE`, returns `std::nullopt`.
* `magic_enum::enum_values` returns `std::array<E, N>` with all enum value, sorted by enum value.
* `magic_enum::enum_to_sequence` returns `std::array<E, N>` with all value enum, 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.
* `magic_enum::enum_to_string_sequence` returns `std::array<std::string_view, N>` with all string enum name, sorted by enum value.
* `magic_enum::enum_names` returns `std::array<std::string_view, N>` with all string enum name, sorted by enum value.
* Enum value must be in range `(-MAGIC_ENUM_RANGE, MAGIC_ENUM_RANGE)`. By default `MAGIC_ENUM_RANGE = 128`. If you need larger range, redefine the macro `MAGIC_ENUM_RANGE`.
```cpp

View file

@ -20,45 +20,57 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <magic_enum.hpp>
#include <iostream>
#include <sstream>
#include <magic_enum.hpp>
enum Color { RED = -10, BLUE = 0, GREEN = 10 };
int main() {
// Enum variable to string enum name.
auto c1 = Color::RED;
auto c1_name = magic_enum::enum_to_string(c1);
// Enum variable to string name.
Color c1 = Color::RED;
auto c1_name = magic_enum::enum_name(c1);
if (c1_name.has_value()) {
std::cout << c1_name.value() << std::endl; // RED
}
// String enum name to enum variable.
auto c2 = magic_enum::enum_from_string<Color>("GREEN");
if (c2.has_value() && c2.value() == Color::GREEN) {
std::cout << "GREEN = " << c2.value() << std::endl; // GREEN = 10
}
// Static storage enum variable to string enum name.
constexpr auto c3 = Color::BLUE;
constexpr auto c3_name = magic_enum::enum_to_string<c3>();
if (c3_name.has_value()) {
std::cout << c3_name.value() << std::endl; // BLUE
}
constexpr auto colors = magic_enum::enum_to_sequence<Color>();
std::cout << "Colors:";
for (auto e : colors) {
std::cout << " " << magic_enum::enum_to_string(e).value();
// String enum name sequence.
constexpr auto color_names = magic_enum::enum_names<Color>();
std::cout << "Color names:";
for (auto n : color_names) {
std::cout << " " << n;
}
std::cout << std::endl;
// Color sequence: RED BLUE GREEN
// Color names: RED BLUE GREEN
constexpr auto color_names = magic_enum::enum_to_string_sequence<Color>();
std::cout << "Color names:";
for (auto e : color_names) {
std::cout << " " << e;
// String name to enum value.
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 = 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 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: " << magic_enum::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
// Enum value sequence.
constexpr auto colors = magic_enum::enum_values<Color>();
std::cout << "Colors sequence:";
for (Color c : colors) {
std::cout << " " << c; // ostream operator for enum.
}
std::cout << std::endl;
// Color sequence: RED BLUE GREEN

View file

@ -5,7 +5,7 @@
// | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
// |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
// __/ | https://github.com/Neargye/magic_enum
// |___/ vesion 0.2.0
// |___/ vesion 0.3.0-dev
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
@ -31,13 +31,16 @@
#pragma once
#include <algorithm>
#include <array>
#include <cassert>
#include <cstddef>
#include <type_traits>
#include <iosfwd>
#include <limits>
#include <string_view>
#include <optional>
#include <array>
#include <algorithm>
#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)
@ -53,13 +56,28 @@ static_assert(MAGIC_ENUM_RANGE < std::numeric_limits<int>::max(),
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.");
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>{};
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 == '_';
}
template <typename E, E V>
[[nodiscard]] constexpr std::string_view enum_to_string_impl() noexcept {
static_assert(std::is_enum_v<E>, "magic_enum::enum_to_string require enum type.");
[[nodiscard]] constexpr std::string_view name_impl() noexcept {
static_assert(std::is_enum_v<E>, "magic_enum::detail::name_impl require enum type.");
#if defined(__clang__)
std::string_view name{__PRETTY_FUNCTION__};
constexpr auto suffix = sizeof("]") - 1;
@ -90,128 +108,167 @@ template <typename E, E V>
#endif
}
template <typename E, int O, int... I>
[[nodiscard]] constexpr std::string_view enum_to_string_impl(int value, std::integer_sequence<int, I...>) noexcept {
static_assert(std::is_enum_v<E>, "magic_enum::enum_to_string require enum type.");
constexpr int n = sizeof...(I);
constexpr std::array<std::string_view, n> names{{enum_to_string_impl<E, static_cast<E>(I + O)>()...}};
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.");
constexpr std::array<std::string_view, sizeof...(I)> names{{name_impl<E, static_cast<E>(I + enum_range<E>::min)>()...}};
return names[value - O];
return names;
}
template <typename E, int O, int... I>
[[nodiscard]] constexpr std::optional<E> enum_from_string_impl(std::string_view name, std::integer_sequence<int, I...>) noexcept {
static_assert(std::is_enum_v<E>, "magic_enum::enum_from_string require enum type.");
std::optional<E> value{std::nullopt};
(void)(((enum_to_string_impl<E, static_cast<E>(I + O)>() == name) ? (value = static_cast<E>(I + O), false) : true) && ...);
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.");
constexpr auto names = strings_impl<E>(enum_range<E>::sequence);
return value;
return enum_range<E>::contains(value) ? names[value - enum_range<E>::min] : std::string_view{};
}
template <typename E, int O, int... I>
[[nodiscard]] constexpr decltype(auto) enum_to_sequence_impl(std::integer_sequence<int, I...>) noexcept {
static_assert(std::is_enum_v<E>, "magic_enum::enum_to_sequence require enum type.");
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.");
constexpr int n = sizeof...(I);
constexpr std::array<bool, n> valid{{!enum_to_string_impl<E, static_cast<E>(I + O)>().empty()...}};
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) + ...);
std::array<E, num_valid> sequence{};
std::array<E, num_valid> enums{};
for (int i = 0, v = 0; i < n && v < num_valid; ++i) {
if (valid[i]) {
sequence[v++] = static_cast<E>(i + O);
enums[v++] = static_cast<E>(i + enum_range<E>::min);
}
}
return sequence;
return enums;
}
template <typename E, int O, int... I>
[[nodiscard]] constexpr decltype(auto) enum_to_string_sequence_impl(std::integer_sequence<int, I...> s) noexcept {
static_assert(std::is_enum_v<E>, "magic_enum::enum_to_string_sequence require enum type.");
constexpr int n = sizeof...(I);
constexpr std::array<bool, n> valid{{!enum_to_string_impl<E, static_cast<E>(I + O)>().empty()...}};
constexpr int num_valid = ((valid[I] ? 1 : 0) + ...);
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.");
constexpr auto enums = values_impl<E>(enum_range<E>::sequence);
constexpr std::array<std::string_view, sizeof...(I)> names{{name_impl<E, enums[I]>()...}};
std::array<std::string_view, num_valid> sequence{};
for (int i = 0, v = 0; i < n && v < num_valid; ++i) {
if (valid[i]) {
sequence[v++] = enum_to_string_impl<E, O>(i + O, s);
return names;
}
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.");
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>{});
for (std::size_t i = 0; i < count; ++i) {
if (names[i] == value) {
return values[i];
}
}
return sequence;
return std::nullopt; // Invalid value or out of range.
}
} // namespace detail
template<class E>
using enable_if_enum_t = typename std::enable_if<std::is_enum_v<std::decay_t<E>>>::type;
// enum_to_string(enum) obtains string enum name from enum variable.
template <typename T, typename = std::enable_if_t<std::is_enum_v<std::decay_t<T>>>>
[[nodiscard]] constexpr std::optional<std::string_view> enum_to_string(T value) noexcept {
using D = std::decay_t<T>;
using U = std::underlying_type_t<D>;
using C = std::common_type_t<int, U>;
constexpr int min = std::max<C>(std::is_signed_v<U> ? -MAGIC_ENUM_RANGE : 0, std::numeric_limits<U>::min());
constexpr int max = std::min<C>(MAGIC_ENUM_RANGE, std::numeric_limits<U>::max());
constexpr int range = max - min + 1;
} // namespace magic_enum::detail
if (static_cast<int>(value) > max || static_cast<int>(value) < min) {
return std::nullopt; // Enum value out of range.
// 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 {
using D = std::decay_t<E>;
return detail::enum_cast_impl<D>(value);
}
const auto name = detail::enum_to_string_impl<D, min>(static_cast<int>(value), std::make_integer_sequence<int, range>{});
if (name.empty()) {
return std::nullopt;
// Obtains enum value from integer value.
template <typename E, typename = detail::enable_if_enum_t<E>>
[[nodiscard]] constexpr std::optional<E> enum_cast(std::underlying_type_t<E> value) noexcept {
using D = std::decay_t<E>;
if (detail::name_impl<D>(static_cast<int>(value)).empty()) {
return std::nullopt; // Invalid value or out of range.
} else {
return name; // Enum value does not have name.
return static_cast<D>(value);
}
}
// enum_to_string<enum>() obtains string enum name from static storage enum variable.
template <auto V, typename = std::enable_if_t<std::is_enum_v<std::decay_t<decltype(V)>>>>
[[nodiscard]] constexpr std::optional<std::string_view> enum_to_string() noexcept {
constexpr auto name = detail::enum_to_string_impl<decltype(V), V>();
// Obtains indexed access to enum element.
template<typename E, typename = detail::enable_if_enum_t<E>>
[[nodiscard]] constexpr E enum_value(std::size_t i) noexcept {
using D = std::decay_t<E>;
constexpr auto values = detail::values_impl<D>(detail::enum_range<D>::sequence);
return assert(i < values.size()), values[i];
}
// 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);
return values;
}
// Obtains number of enum elements.
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();
return count;
}
// Obtains string enum name from enum value.
template <typename E, typename = detail::enable_if_enum_t<E>>
[[nodiscard]] constexpr std::optional<std::string_view> enum_name(E value) noexcept {
using D = std::decay_t<E>;
const auto name = detail::name_impl<D>(static_cast<int>(value));
if (name.empty()) {
return std::nullopt;
} else {
return name; // Enum value does not have name.
return name; // Enum value does not have name or out of range.
}
}
// enum_from_string(name) obtains enum value from enum string name.
template <typename E, typename = std::enable_if_t<std::is_enum_v<E>>>
[[nodiscard]] constexpr std::optional<E> enum_from_string(std::string_view name) noexcept {
using U = std::underlying_type_t<E>;
using C = std::common_type_t<int, U>;
constexpr int min = std::max<C>(std::is_signed_v<U> ? -MAGIC_ENUM_RANGE : 0, std::numeric_limits<U>::min());
constexpr int max = std::min<C>(MAGIC_ENUM_RANGE, std::numeric_limits<U>::max());
constexpr int range = max - min + 1;
// Obtains string enum name sequence.
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 names = detail::names_impl<D>(std::make_index_sequence<count>{});
return detail::enum_from_string_impl<E, min>(name, std::make_integer_sequence<int, range>{});
return names;
}
// enum_to_sequence<enum>() obtains value enum sequence.
template <typename E, typename = std::enable_if_t<std::is_enum_v<E>>>
[[nodiscard]] constexpr decltype(auto) enum_to_sequence() noexcept {
using U = std::underlying_type_t<E>;
using C = std::common_type_t<int, U>;
constexpr int min = std::max<C>(std::is_signed_v<U> ? -MAGIC_ENUM_RANGE : 0, std::numeric_limits<U>::min());
constexpr int max = std::min<C>(MAGIC_ENUM_RANGE, std::numeric_limits<U>::max());
constexpr int range = max - min + 1;
namespace ops {
return detail::enum_to_sequence_impl<E, min>(std::make_integer_sequence<int, range>{});
template <typename E, typename = detail::enable_if_enum_t<E>>
std::ostream& operator<<(std::ostream& os, E value) {
using D = std::decay_t<E>;
const auto name = detail::name_impl<D>(static_cast<int>(value));
if (!name.empty()) {
os << name;
}
// enum_to_string_sequence<enum>() obtains string enum name sequence.
template <typename E, typename = std::enable_if_t<std::is_enum_v<E>>>
[[nodiscard]] constexpr decltype(auto) enum_to_string_sequence() noexcept {
using U = std::underlying_type_t<E>;
using C = std::common_type_t<int, U>;
constexpr int min = std::max<C>(std::is_signed_v<U> ? -MAGIC_ENUM_RANGE : 0, std::numeric_limits<U>::min());
constexpr int max = std::min<C>(MAGIC_ENUM_RANGE, std::numeric_limits<U>::max());
constexpr int range = max - min + 1;
return detail::enum_to_string_sequence_impl<E, min>(std::make_integer_sequence<int, range>{});
return os;
}
template <typename E, typename = detail::enable_if_enum_t<E>>
std::ostream& operator<<(std::ostream& os, std::optional<E> value) {
using D = std::decay_t<E>;
if (value.has_value()) {
const auto name = detail::name_impl<D>(static_cast<int>(value.value()));
if (!name.empty()) {
os << name;
}
}
return os;
}
} // namespace magic_enum::ops
} // namespace magic_enum

View file

@ -27,6 +27,8 @@
#include <magic_enum.hpp>
#include <array>
#include <string>
#include <sstream>
enum class Color { RED = -12, GREEN = 7, BLUE = 15 };
@ -36,117 +38,195 @@ enum Directions { Up = 85, Down = -42, Right = 119, Left = -119 };
enum number : unsigned long { one = 10, two = 20, three = 30 };
TEST_CASE("magic_enum::enum_to_string(enum)") {
Color cr = Color::RED;
Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
REQUIRE(magic_enum::enum_to_string(cr).value() == "RED");
REQUIRE(magic_enum::enum_to_string(Color::BLUE).value() == "BLUE");
REQUIRE(magic_enum::enum_to_string(cm[1]).value() == "GREEN");
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<Color>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<Color>(-MAGIC_ENUM_RANGE)).has_value());
TEST_CASE("enum_cast") {
SECTION("string") {
#if defined(_MSC_VER) && _MSC_VER < 1920
# define constexpr // Visual Studio 2017 have bug with string_view constexpr compare.
#endif
Numbers no = Numbers::one;
REQUIRE(magic_enum::enum_to_string(no).value() == "one");
REQUIRE(magic_enum::enum_to_string(Numbers::two).value() == "two");
REQUIRE(magic_enum::enum_to_string(Numbers::three).value() == "three");
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<Numbers>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<Numbers>(-MAGIC_ENUM_RANGE)).has_value());
constexpr auto cr = magic_enum::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());
Directions dr = Directions::Right;
REQUIRE(magic_enum::enum_to_string(Directions::Up).value() == "Up");
REQUIRE(magic_enum::enum_to_string(Directions::Down).value() == "Down");
REQUIRE(magic_enum::enum_to_string(dr).value() == "Right");
REQUIRE(magic_enum::enum_to_string(Directions::Left).value() == "Left");
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<Directions>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<Directions>(-MAGIC_ENUM_RANGE)).has_value());
constexpr auto no = magic_enum::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());
number nt = number::three;
REQUIRE(magic_enum::enum_to_string(number::one).value() == "one");
REQUIRE(magic_enum::enum_to_string(number::two).value() == "two");
REQUIRE(magic_enum::enum_to_string(nt).value() == "three");
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<number>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<number>(-MAGIC_ENUM_RANGE)).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);
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());
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(magic_enum::enum_cast<number>("None").has_value());
#undef constexpr
}
TEST_CASE("magic_enum::enum_to_string<enum>()") {
constexpr Color cr = Color::RED;
constexpr Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
REQUIRE(magic_enum::enum_to_string<cr>().value() == "RED");
REQUIRE(magic_enum::enum_to_string<Color::BLUE>().value() == "BLUE");
REQUIRE(magic_enum::enum_to_string<cm[1]>().value() == "GREEN");
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<Color>(MAGIC_ENUM_RANGE)>().has_value());
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<Color>(-MAGIC_ENUM_RANGE)>().has_value());
SECTION("string") {
constexpr auto cr = magic_enum::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());
constexpr Numbers no = Numbers::one;
REQUIRE(magic_enum::enum_to_string<no>().value() == "one");
REQUIRE(magic_enum::enum_to_string<Numbers::two>().value() == "two");
REQUIRE(magic_enum::enum_to_string<Numbers::three>().value() == "three");
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<Numbers>(MAGIC_ENUM_RANGE)>().has_value());
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<Numbers>(-MAGIC_ENUM_RANGE)>().has_value());
constexpr auto no = magic_enum::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());
constexpr Directions dr = Directions::Right;
REQUIRE(magic_enum::enum_to_string<Directions::Up>().value() == "Up");
REQUIRE(magic_enum::enum_to_string<Directions::Down>().value() == "Down");
REQUIRE(magic_enum::enum_to_string<dr>().value() == "Right");
REQUIRE(magic_enum::enum_to_string<Directions::Left>().value() == "Left");
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<Directions>(MAGIC_ENUM_RANGE)>().has_value());
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<Directions>(-MAGIC_ENUM_RANGE)>().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);
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());
constexpr number nt = number::three;
REQUIRE(magic_enum::enum_to_string<number::one>().value() == "one");
REQUIRE(magic_enum::enum_to_string<number::two>().value() == "two");
REQUIRE(magic_enum::enum_to_string<nt>().value() == "three");
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<number>(MAGIC_ENUM_RANGE)>().has_value());
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<number>(-MAGIC_ENUM_RANGE)>().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);
REQUIRE(nt.value() == number::three);
REQUIRE_FALSE(magic_enum::enum_cast<number>(0).has_value());
}
}
TEST_CASE("magic_enum::enum_from_string(name)") {
REQUIRE(magic_enum::enum_from_string<Color>("RED").value() == Color::RED);
REQUIRE(magic_enum::enum_from_string<Color>("GREEN").value() == Color::GREEN);
REQUIRE(magic_enum::enum_from_string<Color>("BLUE").value() == Color::BLUE);
REQUIRE_FALSE(magic_enum::enum_from_string<Color>("None").has_value());
TEST_CASE("enum_value") {
constexpr auto cr = magic_enum::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(magic_enum::enum_from_string<Numbers>("one").value() == Numbers::one);
REQUIRE(magic_enum::enum_from_string<Numbers>("two").value() == Numbers::two);
REQUIRE(magic_enum::enum_from_string<Numbers>("three").value() == Numbers::three);
REQUIRE_FALSE(magic_enum::enum_from_string<Numbers>("None").has_value());
constexpr auto no = magic_enum::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(magic_enum::enum_from_string<Directions>("Up").value() == Directions::Up);
REQUIRE(magic_enum::enum_from_string<Directions>("Down").value() == Directions::Down);
REQUIRE(magic_enum::enum_from_string<Directions>("Right").value() == Directions::Right);
REQUIRE(magic_enum::enum_from_string<Directions>("Left").value() == Directions::Left);
REQUIRE_FALSE(magic_enum::enum_from_string<Directions>("None").has_value());
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);
REQUIRE(magic_enum::enum_from_string<number>("one").value() == number::one);
REQUIRE(magic_enum::enum_from_string<number>("two").value() == number::two);
REQUIRE(magic_enum::enum_from_string<number>("three").value() == number::three);
REQUIRE_FALSE(magic_enum::enum_from_string<number>("None").has_value());
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("magic_enum::enum_sequence<enum>()") {
constexpr auto mge_s1 = magic_enum::enum_to_sequence<Color>();
TEST_CASE("enum_values") {
constexpr auto mge_s1 = magic_enum::enum_values<Color>();
REQUIRE(mge_s1 == std::array<Color, 3>{Color::RED, Color::GREEN, Color::BLUE});
constexpr auto mge_s2 = magic_enum::enum_to_sequence<Numbers>();
constexpr auto mge_s2 = magic_enum::enum_values<Numbers>();
REQUIRE(mge_s2 == std::array<Numbers, 3>{Numbers::one, Numbers::two, Numbers::three});
constexpr auto mge_s3 = magic_enum::enum_to_sequence<Directions>();
constexpr auto mge_s3 = magic_enum::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_to_sequence<number>();
constexpr auto mge_s4 = magic_enum::enum_values<number>();
REQUIRE(mge_s4 == std::array<number, 3>{number::one, number::two, number::three});
}
TEST_CASE("magic_enum::enum_to_string_sequence<enum>()") {
constexpr auto mge_s1 = magic_enum::enum_to_string_sequence<Color>();
TEST_CASE("enum_count") {
constexpr auto mge_s1 = magic_enum::enum_count<Color>();
REQUIRE(mge_s1 == 3);
constexpr auto mge_s2 = magic_enum::enum_count<Numbers>();
REQUIRE(mge_s2 == 3);
constexpr auto mge_s3 = magic_enum::enum_count<Directions>();
REQUIRE(mge_s3 == 4);
constexpr auto mge_s4 = magic_enum::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);
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());
constexpr Numbers no = Numbers::one;
constexpr auto no_name = magic_enum::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());
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");
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());
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");
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());
}
TEST_CASE("enum_names") {
constexpr auto mge_s1 = magic_enum::enum_names<Color>();
REQUIRE(mge_s1 == std::array<std::string_view, 3>{"RED", "GREEN", "BLUE"});
constexpr auto mge_s2 = magic_enum::enum_to_string_sequence<Numbers>();
constexpr auto mge_s2 = magic_enum::enum_names<Numbers>();
REQUIRE(mge_s2 == std::array<std::string_view, 3>{"one", "two", "three"});
constexpr auto mge_s3 = magic_enum::enum_to_string_sequence<Directions>();
constexpr auto mge_s3 = magic_enum::enum_names<Directions>();
REQUIRE(mge_s3 == std::array<std::string_view, 4>{"Left", "Down", "Up", "Right"});
constexpr auto mge_s4 = magic_enum::enum_to_string_sequence<number>();
constexpr auto mge_s4 = magic_enum::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;
std::stringstream ss;
ss << e;
REQUIRE(ss.str() == name);
};
test_ostream(Color::RED, "RED");
test_ostream(Color::GREEN, "GREEN");
test_ostream(Color::BLUE, "BLUE");
test_ostream((Color)0, "");
test_ostream(Numbers::one, "one");
test_ostream(Numbers::two, "two");
test_ostream(Numbers::three, "three");
test_ostream((Numbers)0, "");
test_ostream(Directions::Up, "Up");
test_ostream(Directions::Down, "Down");
test_ostream(Directions::Right, "Right");
test_ostream(Directions::Left, "Left");
test_ostream((Directions)0, "");
test_ostream(number::one, "one");
test_ostream(number::two, "two");
test_ostream(number::three, "three");
test_ostream((number)0, "");
}