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

* Remove MAGIC_ENUM_ENABLE_NONASCII

* Optimize template instantiations
* Remove auto is_flags
* Change flags API
This commit is contained in:
neargye 2023-05-21 03:39:52 +04:00
parent 737ed4fc7f
commit 427a47394f
27 changed files with 515 additions and 875 deletions

View file

@ -12,8 +12,6 @@ cc_library(
_TESTS = [
"test",
"test_aliases",
"test_containers",
"test_flags",
]

View file

@ -18,10 +18,6 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
check_cxx_compiler_flag(-std=c++23 HAS_CPP23_FLAG)
endif()
if(MAGIC_ENUM_OPT_ENABLE_NONASCII)
set(OPTIONS ${OPTIONS} -DMAGIC_ENUM_ENABLE_NONASCII)
endif()
function(make_test src target std)
add_executable(${target} ${src})
target_compile_options(${target} PRIVATE ${OPTIONS})
@ -41,7 +37,7 @@ endfunction()
make_test(test.cpp test-cpp17 c++17)
make_test(test_flags.cpp test_flags-cpp17 c++17)
make_test(test_aliases.cpp test_aliases-cpp17 c++17)
make_test(test_containers.cpp test_containers-cpp17 c++17)
#make_test(test_containers.cpp test_containers-cpp17 c++17) TODO
if(MAGIC_ENUM_OPT_ENABLE_NONASCII)
make_test(test_nonascii.cpp test_nonascii-cpp17 c++17)
@ -51,7 +47,7 @@ if(HAS_CPP20_FLAG)
make_test(test.cpp test-cpp20 c++20)
make_test(test_flags.cpp test_flags-cpp20 c++20)
make_test(test_aliases.cpp test_aliases-cpp20 c++20)
make_test(test_containers.cpp test_containers-cpp20 c++20)
#make_test(test_containers.cpp test_containers-cpp20 c++20) TODO
if(MAGIC_ENUM_OPT_ENABLE_NONASCII)
make_test(test_nonascii.cpp test_nonascii-cpp20 c++20)
endif()
@ -61,7 +57,7 @@ if(HAS_CPP23_FLAG)
make_test(test.cpp test-cpp23 c++23)
make_test(test_flags.cpp test_flags-cpp23 c++23)
make_test(test_aliases.cpp test_aliases-cpp23 c++23)
make_test(test_containers.cpp test_containers-cpp23 c++23)
#make_test(test_containers.cpp test_containers-cpp23 c++23) TODO
if(MAGIC_ENUM_OPT_ENABLE_NONASCII)
make_test(test_nonascii.cpp test_nonascii-cpp23 c++23)
endif()
@ -71,7 +67,7 @@ if(HAS_CPPLATEST_FLAG)
make_test(test.cpp test-cpplatest c++latest)
make_test(test_flags.cpp test_flags-cpplatest c++latest)
make_test(test_aliases.cpp test_aliases-cpplatest c++latest)
make_test(test_containers.cpp test_containers-cpplatest c++latest)
#make_test(test_containers.cpp test_containers-cpplatest c++latest) TODO
if(MAGIC_ENUM_OPT_ENABLE_NONASCII)
make_test(test_nonascii.cpp test_nonascii-cpplatest c++latest)
endif()

View file

@ -5,8 +5,6 @@ catch2_dep = declare_dependency(
test_files = {
'basic test' : files('test.cpp'),
'flags test' : files('test_flags.cpp'),
'aliases test' : files('test_aliases.cpp'),
'containers test' : files('test_containers.cpp'),
}
foreach test_name, test_src : test_files

View file

@ -1,6 +1,6 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 - 2022 Daniil Goncharov <neargye@gmail.com>.
// Copyright (c) 2019 - 2023 Daniil Goncharov <neargye@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@ -55,7 +55,6 @@ enum number : unsigned long {
two = 200,
three = 300,
four = 400,
#if defined(MAGIC_ENUM_SUPPORTED_ALIASES)
_1 = one,
_2 = two,
@ -63,6 +62,11 @@ enum number : unsigned long {
_4 = four
#endif
};
template <>
struct magic_enum::customize::enum_range<number> {
static constexpr int min = 100;
static constexpr int max = 300;
};
enum class crc_hack {
b5a7b602ab754d7ab30fb42c4fb28d82
@ -73,12 +77,6 @@ enum class crc_hack_2 {
d19f2e9e82d14b96be4fa12b8a27ee9f
};
template <>
struct magic_enum::customize::enum_range<number> {
static constexpr int min = 100;
static constexpr int max = 300;
};
enum class MaxUsedAsInvalid : std::uint8_t {
ONE,
TWO = 63,
@ -460,7 +458,8 @@ TEST_CASE("enum_name") {
REQUIRE(cr_name == "red");
REQUIRE(enum_name<Color&>(cb) == "BLUE");
REQUIRE(enum_name<as_flags<false>>(cm[1]) == "GREEN");
REQUIRE(enum_name<detail::value_type::default_value>(static_cast<Color>(0)).empty());
REQUIRE(enum_name<as_common<true>>(cm[1]) == "GREEN");
REQUIRE(enum_name<as_flags<false>>(static_cast<Color>(0)).empty());
constexpr Numbers no = Numbers::one;
constexpr auto no_name = enum_name(no);
@ -762,58 +761,58 @@ TEST_CASE("extrema") {
SECTION("min") {
REQUIRE(magic_enum::customize::enum_range<BadColor>::min == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::reflected_min_v<BadColor, false> == 0);
REQUIRE(magic_enum::detail::min_v<BadColor> == 0);
REQUIRE(magic_enum::detail::reflected_min<BadColor, as_common<>>() == 0);
REQUIRE(magic_enum::detail::min_v<BadColor, as_common<>> == 0);
REQUIRE(magic_enum::customize::enum_range<Color>::min == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::reflected_min_v<Color, false> == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::min_v<Color> == -12);
REQUIRE(magic_enum::detail::reflected_min<Color, as_common<>>() == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::min_v<Color, as_common<>> == -12);
REQUIRE(magic_enum::customize::enum_range<Numbers>::min == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::reflected_min_v<Numbers, false> == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::min_v<Numbers> == 1);
REQUIRE(magic_enum::detail::reflected_min<Numbers, as_common<>>() == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::min_v<Numbers, as_common<>> == 1);
REQUIRE(magic_enum::customize::enum_range<Directions>::min == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::reflected_min_v<Directions, false> == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::min_v<Directions> == -120);
REQUIRE(magic_enum::detail::reflected_min<Directions, as_common<>>() == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::min_v<Directions, as_common<>> == -120);
REQUIRE(magic_enum::customize::enum_range<number>::min == 100);
REQUIRE(magic_enum::detail::reflected_min_v<number, false> == 100);
REQUIRE(magic_enum::detail::min_v<number> == 100);
REQUIRE(magic_enum::detail::reflected_min<number, as_common<>>() == 100);
REQUIRE(magic_enum::detail::min_v<number, as_common<>> == 100);
REQUIRE(magic_enum::detail::reflected_min_v<Binary, false> == 0);
REQUIRE(magic_enum::detail::min_v<Binary> == false);
REQUIRE(magic_enum::detail::reflected_min<Binary, as_common<>>() == 0);
REQUIRE(magic_enum::detail::min_v<Binary, as_common<>> == false);
REQUIRE(magic_enum::detail::reflected_min_v<MaxUsedAsInvalid,false> == 0);
REQUIRE(magic_enum::detail::min_v<MaxUsedAsInvalid> == 0);
REQUIRE(magic_enum::detail::reflected_min<MaxUsedAsInvalid, as_common<>>() == 0);
REQUIRE(magic_enum::detail::min_v<MaxUsedAsInvalid, as_common<>> == 0);
}
SECTION("max") {
REQUIRE(magic_enum::customize::enum_range<BadColor>::max == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::reflected_max_v<BadColor, false> == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::max_v<BadColor> == 2);
REQUIRE(magic_enum::detail::reflected_max<BadColor, as_common<>>() == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::max_v<BadColor, as_common<>> == 2);
REQUIRE(magic_enum::customize::enum_range<Color>::max == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::reflected_max_v<Color, false> == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::max_v<Color> == 15);
REQUIRE(magic_enum::detail::reflected_max<Color, as_common<>>() == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::max_v<Color, as_common<>> == 15);
REQUIRE(magic_enum::customize::enum_range<Numbers>::max == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::reflected_max_v<Numbers, false> == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::max_v<Numbers> == 3);
REQUIRE(magic_enum::detail::reflected_max<Numbers, as_common<>>() == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::max_v<Numbers, as_common<>> == 3);
REQUIRE(magic_enum::customize::enum_range<Directions>::max == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::reflected_max_v<Directions, false> == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::max_v<Directions> == 120);
REQUIRE(magic_enum::detail::reflected_max<Directions, as_common<>>() == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::max_v<Directions, as_common<>> == 120);
REQUIRE(magic_enum::customize::enum_range<number>::max == 300);
REQUIRE(magic_enum::detail::reflected_max_v<number, false> == 300);
REQUIRE(magic_enum::detail::max_v<number> == 300);
REQUIRE(magic_enum::detail::reflected_max<number, as_common<>>() == 300);
REQUIRE(magic_enum::detail::max_v<number, as_common<>> == 300);
REQUIRE(magic_enum::detail::reflected_max_v<Binary, false> == 1);
REQUIRE(magic_enum::detail::max_v<Binary> == true);
REQUIRE(magic_enum::detail::reflected_max<Binary, as_common<>>() == 1);
REQUIRE(magic_enum::detail::max_v<Binary, as_common<>> == true);
REQUIRE(magic_enum::detail::reflected_max_v<MaxUsedAsInvalid, false> == 64);
REQUIRE(magic_enum::detail::max_v<MaxUsedAsInvalid> == 63);
REQUIRE(magic_enum::detail::reflected_max<MaxUsedAsInvalid, as_common<>>() == 64);
REQUIRE(magic_enum::detail::max_v<MaxUsedAsInvalid, as_common<>> == 63);
}
}

View file

@ -1,6 +1,6 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 - 2022 Daniil Goncharov <neargye@gmail.com>.
// Copyright (c) 2019 - 2023 Daniil Goncharov <neargye@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal

View file

@ -1,6 +1,6 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 - 2022 Daniil Goncharov <neargye@gmail.com>.
// Copyright (c) 2019 - 2023 Daniil Goncharov <neargye@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal

View file

@ -1,6 +1,6 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 - 2022 Daniil Goncharov <neargye@gmail.com>.
// Copyright (c) 2019 - 2023 Daniil Goncharov <neargye@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@ -41,6 +41,10 @@
#include <sstream>
enum class Color { RED = 1, GREEN = 2, BLUE = 4 };
template <>
struct magic_enum::customize::enum_range<Color> {
static constexpr bool is_flags = true;
};
enum class Numbers : int {
one = 1 << 1,
@ -48,6 +52,10 @@ enum class Numbers : int {
three = 1 << 3,
many = 1 << 30,
};
template <>
struct magic_enum::customize::enum_range<Numbers> {
static constexpr bool is_flags = true;
};
enum Directions : std::uint64_t {
Left = std::uint64_t{1} << 10,
@ -55,13 +63,16 @@ enum Directions : std::uint64_t {
Up = std::uint64_t{1} << 31,
Right = std::uint64_t{1} << 63,
};
template <>
struct magic_enum::customize::enum_range<Directions> {
static constexpr bool is_flags = true;
};
enum number : unsigned long {
one = 1 << 1,
two = 1 << 2,
three = 1 << 3,
four = 1 << 4,
#if defined(MAGIC_ENUM_SUPPORTED_ALIASES)
_1 = one,
_2 = two,
@ -71,10 +82,12 @@ enum number : unsigned long {
};
template <>
struct magic_enum::customize::enum_range<number> {
static constexpr int min = 100;
static constexpr int max = 300;
static constexpr bool is_flags = true;
};
#include <magic_enum.hpp>
#include <magic_enum_fuse.hpp>
using namespace magic_enum;
using namespace magic_enum::bitwise_operators;
@ -208,11 +221,6 @@ TEST_CASE("enum_contains") {
REQUIRE(cr);
REQUIRE(enum_contains<Color&>(cg));
REQUIRE(enum_contains(cm[2]));
REQUIRE(enum_contains<Color, as_flags<>>(Color::RED | Color::GREEN));
REQUIRE(enum_contains<Color, as_flags<true>>(Color::RED | Color::GREEN | Color::GREEN));
REQUIRE_FALSE(enum_contains<Color>(Color::RED | Color::GREEN));
REQUIRE_FALSE(enum_contains<Color>(Color::RED | Color::GREEN | Color::GREEN));
REQUIRE_FALSE(enum_contains<Color>(Color::RED | Color{8}));
REQUIRE_FALSE(enum_contains(static_cast<Color>(0)));
REQUIRE(enum_flags_contains<Color&>(cg));
@ -252,11 +260,6 @@ TEST_CASE("enum_contains") {
REQUIRE(enum_contains<Color>(1));
REQUIRE(enum_contains<Color&>(2));
REQUIRE(enum_contains<const Color>(4));
REQUIRE(enum_contains<Color, as_flags<>>(1 | 2));
REQUIRE(enum_contains<Color, as_flags<true>>(1 | 2 | 1));
REQUIRE_FALSE(enum_contains<Color>(1 | 2));
REQUIRE_FALSE(enum_contains<Color>(1 | 2 | 1));
REQUIRE_FALSE(enum_contains<Color>(1 | 2 | 8));
REQUIRE_FALSE(enum_contains<Color>(0));
REQUIRE(enum_flags_contains<Color>(1));
@ -297,9 +300,6 @@ TEST_CASE("enum_contains") {
REQUIRE(enum_contains<Color>(cr));
REQUIRE(enum_contains<Color&>("GREEN"));
REQUIRE(enum_contains<const Color>("blue", [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }));
REQUIRE(enum_contains<Color&, as_flags<>>("blue|RED", [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }));
REQUIRE(enum_contains<Color&, as_flags<true>>("GREEN|RED"));
REQUIRE(enum_contains<Color, as_flags<true>>("GREEN|RED|RED"));
REQUIRE_FALSE(enum_contains<Color&>("blue|RED", [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }));
REQUIRE_FALSE(enum_contains<Color&>("GREEN|RED"));
REQUIRE_FALSE(enum_contains<Color&>("GREEN|RED|RED"));

View file

@ -1,6 +1,6 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 - 2022 Daniil Goncharov <neargye@gmail.com>.
// Copyright (c) 2019 - 2023 Daniil Goncharov <neargye@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@ -35,11 +35,7 @@
#include <string_view>
#include <sstream>
#if !defined(MAGIC_ENUM_ENABLE_NONASCII)
#error ENABLE_NONASCII must be defined to run nonascii tests
#endif
enum class Language : int { = 10, = 20, English = 30, 😃 = 40 };
enum class Language : int { = 10, = 20, English = 30, 😃 = 40, TVÅ = 50 };
enum class LanguageFlag : int {
= 1 << 1,
@ -59,6 +55,7 @@ TEST_CASE("enum_cast") {
REQUIRE(enum_cast<const Language>("English").value() == Language::English);
REQUIRE(lang.value() == Language::);
REQUIRE(enum_cast<Language>("😃").value() == Language::😃);
REQUIRE(enum_cast<Language>("TVÅ").value() == Language::TVÅ);
REQUIRE_FALSE(enum_cast<Language>("Französisch").has_value());
}
@ -130,12 +127,12 @@ TEST_CASE("enum_value") {
TEST_CASE("enum_values") {
constexpr auto& s7 = enum_values<const Language>();
REQUIRE(s7 == std::array<Language, 4>{{Language::, Language::, Language::English, Language::😃}});
REQUIRE(s7 == std::array<Language, 5>{{Language::, Language::, Language::English, Language::😃, Language::TVÅ}});
}
TEST_CASE("enum_count") {
constexpr auto s7 = enum_count<Language>();
REQUIRE(s7 == 4);
REQUIRE(s7 == 5);
}
TEST_CASE("enum_name") {
@ -147,6 +144,7 @@ TEST_CASE("enum_name") {
REQUIRE(enum_name<const Language>(Language::English) == "English");
REQUIRE(lang_name == "日本語");
REQUIRE(enum_name(Language::😃) == "😃");
REQUIRE(enum_name(Language::TVÅ) == "TVÅ");
REQUIRE(enum_name(static_cast<Language>(0)).empty());
}
@ -162,12 +160,12 @@ TEST_CASE("enum_name") {
TEST_CASE("enum_names") {
constexpr auto& s5 = enum_names<const Language>();
REQUIRE(s5 == std::array<std::string_view, 4>{{"日本語", "한국어", "English", "😃"}});
REQUIRE(s5 == std::array<std::string_view, 5>{{"日本語", "한국어", "English", "😃", "TVÅ"}});
}
TEST_CASE("enum_entries") {
constexpr auto& s5 = enum_entries<const Language>();
REQUIRE(s5 == std::array<std::pair<Language, std::string_view>, 4>{{{Language::, "日本語"}, {Language::, "한국어"}, {Language::English, "English"}, {Language::😃, "😃"}}});
REQUIRE(s5 == std::array<std::pair<Language, std::string_view>, 5>{{{Language::, "日本語"}, {Language::, "한국어"}, {Language::English, "English"}, {Language::😃, "😃"}, {Language::TVÅ, "TVÅ"}}});
}
TEST_CASE("ostream_operators") {
@ -251,14 +249,14 @@ TEST_CASE("enum_type_name") {
TEST_CASE("extrema") {
SECTION("min") {
REQUIRE(magic_enum::customize::enum_range<Language>::min == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::reflected_min_v<Language, false> == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::min_v<Language> == 10);
REQUIRE(magic_enum::detail::reflected_min<Language, as_common<>>() == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::min_v<Language, as_common<>> == 10);
}
SECTION("max") {
REQUIRE(magic_enum::customize::enum_range<Language>::max == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::reflected_max_v<Language, false> == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::max_v<Language> == 40);
REQUIRE(magic_enum::detail::reflected_max<Language, as_common<>>() == MAGIC_ENUM_RANGE_MAX);
REQUIRE(magic_enum::detail::max_v<Language, as_common<>> == 50);
}
}