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

Add adl_ranges (#413)

Co-authored-by: lsemprini <17140216+lsemprini@users.noreply.github.com>
This commit is contained in:
ZXShady 2025-06-11 20:00:01 +03:00 committed by GitHub
parent 513e606d7b
commit d642b05dcb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 174 additions and 14 deletions

View file

@ -99,6 +99,29 @@ struct magic_enum::customize::enum_range<Binary> {
static constexpr int max = 64;
};
namespace We::Need::To::Go::Deeper {
enum class Dimension : short { Overworld = 1000, Nether, TheEnd = Overworld + 128 };
enum class Flaggy : std::uint64_t { Flag0 = 1 << 0, Flag32 = std::uint64_t(1) << 32 };
auto adl_magic_enum_define_range(Dimension)
{
enum {
min = 1000,
max = 1000 + 128
} e{};
return e;
}
struct FlaggyData {
static constexpr bool is_flags = true;
};
// not defined!
FlaggyData adl_magic_enum_define_range(Flaggy);
}
using We::Need::To::Go::Deeper::Dimension;
using We::Need::To::Go::Deeper::Flaggy;
enum class BoolTest : bool { Yay, Nay };
using namespace magic_enum;
@ -113,6 +136,12 @@ TEST_CASE("enum_cast") {
REQUIRE(enum_cast<Color>("blue", [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }).value() == Color::BLUE);
REQUIRE_FALSE(enum_cast<Color>("None").has_value());
constexpr auto dim = enum_cast<Dimension>("Nether");
REQUIRE(dim.value() == Dimension::Nether);
REQUIRE(enum_cast<Dimension&>("Nether").value() == Dimension::Nether);
REQUIRE(enum_cast<Dimension>("theend", [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }).value() == Dimension::TheEnd);
REQUIRE_FALSE(enum_cast<Dimension>("Aether").has_value());
constexpr auto no = enum_cast<Numbers>("one");
REQUIRE(no.value() == Numbers::one);
REQUIRE(enum_cast<Numbers>("two").value() == Numbers::two);
@ -427,6 +456,13 @@ TEST_CASE("enum_values") {
constexpr auto& s6 = enum_values<MaxUsedAsInvalid>();
REQUIRE(s6 == std::array<MaxUsedAsInvalid, 2>{{MaxUsedAsInvalid::ONE, MaxUsedAsInvalid::TWO}});
constexpr auto& s7 = enum_values<Dimension>();
REQUIRE(s7 == std::array<Dimension, 3>{{Dimension::Overworld, Dimension::Nether, Dimension::TheEnd}});
constexpr auto& s8 = enum_values<Flaggy>();
REQUIRE(s8 == std::array<Flaggy, 2>{{Flaggy::Flag0, Flaggy::Flag32}});
}
TEST_CASE("enum_count") {
@ -932,6 +968,10 @@ TEST_CASE("extrema") {
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<Dimension>::min == 1000);
REQUIRE(magic_enum::customize::enum_range<Dimension>::max == 1000 + 128);
REQUIRE_FALSE(magic_enum::customize::enum_range<Dimension>::is_flags);
REQUIRE(magic_enum::customize::enum_range<Color>::min == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::reflected_min<Color, as_common<>>() == MAGIC_ENUM_RANGE_MIN);
REQUIRE(magic_enum::detail::min_v<Color, as_common<>> == -12);

View file

@ -49,17 +49,17 @@ struct magic_enum::customize::enum_range<Color> {
static constexpr bool is_flags = true;
};
enum class Numbers : int {
none = 0,
one = 1 << 1,
two = 1 << 2,
three = 1 << 3,
many = 1 << 30,
};
template <>
struct magic_enum::customize::enum_range<Numbers> {
static constexpr bool is_flags = true;
};
namespace Namespace {
enum class Numbers : int {
none = 0,
one = 1 << 1,
two = 1 << 2,
three = 1 << 3,
many = 1 << 30,
};
magic_enum::customize::adl_info<true> adl_magic_enum_define_range(Numbers);
}
using Namespace::Numbers;
enum Directions : std::uint64_t {
NoDirection = 0,