mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-10 23:44:29 +00:00
Add prefix trimming (#414)
This commit is contained in:
parent
d468f23408
commit
aaf57737d5
5 changed files with 180 additions and 110 deletions
|
|
@ -105,23 +105,37 @@ namespace We::Need::To::Go::Deeper {
|
|||
|
||||
auto adl_magic_enum_define_range(Dimension)
|
||||
{
|
||||
enum {
|
||||
min = 1000,
|
||||
max = 1000 + 128
|
||||
} e{};
|
||||
return e;
|
||||
return magic_enum::customize::adl_info().minmax<1000,1000+128>();
|
||||
}
|
||||
|
||||
struct FlaggyData {
|
||||
static constexpr bool is_flags = true;
|
||||
};
|
||||
|
||||
// not defined!
|
||||
FlaggyData adl_magic_enum_define_range(Flaggy);
|
||||
auto adl_magic_enum_define_range(Flaggy)
|
||||
{
|
||||
return magic_enum::customize::adl_info().flag<true>();
|
||||
}
|
||||
}
|
||||
using We::Need::To::Go::Deeper::Dimension;
|
||||
using We::Need::To::Go::Deeper::Flaggy;
|
||||
|
||||
enum CStyleEnum {
|
||||
CStyleEnum_A = -36,
|
||||
CStyleEnum_B,
|
||||
CStyleEnum_C,
|
||||
CStyleEnum_D,
|
||||
CStyleEnum_F,
|
||||
CStyleEnum_G,
|
||||
CStyleEnum_H = 36
|
||||
};
|
||||
|
||||
template <>
|
||||
struct magic_enum::customize::enum_range<CStyleEnum> {
|
||||
static constexpr auto prefix_length = sizeof("CStyleEnum_") - 1;
|
||||
static constexpr int min = -100;
|
||||
static constexpr int max = 100;
|
||||
};
|
||||
|
||||
|
||||
|
||||
enum class BoolTest : bool { Yay, Nay };
|
||||
|
||||
using namespace magic_enum;
|
||||
|
|
@ -142,6 +156,13 @@ TEST_CASE("enum_cast") {
|
|||
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 cstyle = enum_cast<CStyleEnum>("A");
|
||||
REQUIRE(cstyle.value() == CStyleEnum_A);
|
||||
REQUIRE(enum_cast<const CStyleEnum&>("H").value() == CStyleEnum_H);
|
||||
REQUIRE_FALSE(enum_cast<CStyleEnum>("CStyleEnum_H").has_value());
|
||||
REQUIRE(enum_cast<CStyleEnum>("d", [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }) == CStyleEnum_D);
|
||||
REQUIRE_FALSE(enum_cast<CStyleEnum>("Q").has_value());
|
||||
|
||||
constexpr auto no = enum_cast<Numbers>("one");
|
||||
REQUIRE(no.value() == Numbers::one);
|
||||
REQUIRE(enum_cast<Numbers>("two").value() == Numbers::two);
|
||||
|
|
|
|||
|
|
@ -57,7 +57,10 @@ namespace Namespace {
|
|||
three = 1 << 3,
|
||||
many = 1 << 30,
|
||||
};
|
||||
magic_enum::customize::adl_info<true> adl_magic_enum_define_range(Numbers);
|
||||
auto adl_magic_enum_define_range(Numbers)
|
||||
{
|
||||
return magic_enum::customize::adl_info().flag<true>();
|
||||
}
|
||||
}
|
||||
using Namespace::Numbers;
|
||||
|
||||
|
|
@ -91,6 +94,24 @@ struct magic_enum::customize::enum_range<number> {
|
|||
static constexpr bool is_flags = true;
|
||||
};
|
||||
|
||||
enum CStyleFlags {
|
||||
CStyleFlags_A = 1 << 0,
|
||||
CStyleFlags_B = 1 << 1,
|
||||
CStyleFlags_C = 1 << 2,
|
||||
CStyleFlags_D = 1 << 3,
|
||||
CStyleFlags_E = 1 << 4,
|
||||
CStyleFlags_F = 1 << 5,
|
||||
CStyleFlags_G = 1 << 6,
|
||||
CStyleFlags_H = 1 << 7,
|
||||
CStyleFlags_I = 1 << 8,
|
||||
};
|
||||
|
||||
template <>
|
||||
struct magic_enum::customize::enum_range<CStyleFlags> {
|
||||
static constexpr bool is_flags = true;
|
||||
static constexpr auto prefix_length = sizeof("CStyleFlags_")-1;
|
||||
};
|
||||
|
||||
#include <magic_enum/magic_enum.hpp>
|
||||
#include <magic_enum/magic_enum_fuse.hpp>
|
||||
|
||||
|
|
@ -117,6 +138,13 @@ TEST_CASE("enum_cast") {
|
|||
REQUIRE_FALSE(enum_flags_cast<Color&>("GREEN|RED|None").has_value());
|
||||
REQUIRE_FALSE(enum_flags_cast<Color>("None").has_value());
|
||||
|
||||
REQUIRE(enum_flags_cast<CStyleFlags>("A|B|C|D").value() == (CStyleFlags_A | CStyleFlags_B | CStyleFlags_C | CStyleFlags_D));
|
||||
REQUIRE(enum_flags_cast<CStyleFlags>("a|e|f", [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }).value() == (CStyleFlags_A | CStyleFlags_E | CStyleFlags_F));
|
||||
REQUIRE_FALSE(enum_flags_cast<CStyleFlags>("blue|E|F|C", [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }).has_value());
|
||||
REQUIRE(enum_flags_cast<CStyleFlags>("H|I|F|F|F").value() == (CStyleFlags_H | CStyleFlags_I | CStyleFlags_F));
|
||||
REQUIRE(enum_flags_cast<CStyleFlags>("E|B|C|A").value() == (CStyleFlags_A | CStyleFlags_B | CStyleFlags_C | CStyleFlags_E));
|
||||
|
||||
|
||||
constexpr auto no = enum_cast<Numbers>("one");
|
||||
REQUIRE(no.value() == Numbers::one);
|
||||
REQUIRE(enum_cast<Numbers>("two").value() == Numbers::two);
|
||||
|
|
@ -504,6 +532,8 @@ TEST_CASE("enum_flags_name") {
|
|||
REQUIRE(enum_flags_name(number::four) == "four");
|
||||
REQUIRE(nto_name == "one|three");
|
||||
REQUIRE(enum_flags_name(static_cast<number>(0)).empty());
|
||||
|
||||
REQUIRE(enum_flags_name(CStyleFlags_A | CStyleFlags_B | CStyleFlags_C | CStyleFlags_D) == "A|B|C|D");
|
||||
}
|
||||
|
||||
TEST_CASE("enum_names") {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue