1
0
Fork 0
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:
ZXShady 2025-08-31 21:56:19 +03:00 committed by GitHub
parent d468f23408
commit aaf57737d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 180 additions and 110 deletions

View file

@ -23,29 +23,23 @@
* If an enum is declared as a flag enum, its zero value will not be reflected.
* Or, for enum types that are deeply nested in classes and/or namespaces, declare a function called `my_adl_info_struct adl_magic_enum_define_range(my_enum_type)` in the same namespace as `my_enum_type`, which magic_enum will find by ADL (because the function is in the same class/namespace as `my_enum_type`), and whose return type is a struct with `static constexpr` data members containing the same parameters as `magic_enum::customize::enum_range<my_enum_type>`
```cpp
namespace Deeply::Nested::Namespace {
enum class my_enum_type { ... };
struct my_adl_info_struct {
static constexpr bool is_flags = true;
// you can also set min and max here (see Enum Range below)
// static constexpr int min = ...;
// static constexpr int max = ...;
};
// - magic_enum will find this function by ADL
// - no need to ever define this function
my_adl_info_struct adl_magic_enum_define_range(my_enum_type);
}
```
* Or, for enum types that are deeply nested in classes and/or namespaces, declare a function called `adl_magic_enum_define_range(my_enum_type)` in the same namespace as `my_enum_type`, which magic_enum will find by ADL (because the function is in the same class/namespace as `my_enum_type`), and whose return type is a `magic_enum::customize::adl_info`.
* As a shorthand, if you only want to set `is_flags` and not `min` or `max`, you can also use `magic_enum::customize::adl_info<is_flags_bool>` to avoid having to define `my_adl_info_struct` in your code:
```cpp
namespace Deeply::Nested::Namespace {
enum class my_enum_type { ... };
enum class my_enum_type { my_enum_value1,my_enum_value2 };
// - magic_enum will find this function by ADL
// - no need to ever define this function
magic_enum::customize::adl_info<true> adl_magic_enum_define_range(my_enum_type);
// - uses builder pattern
// - use auto to not have to name the type yourself
auto adl_magic_enum_define_range(my_enum_type)
{
return magic_enum::customize::adl_info()
.minmax<10,10>() // the min max search range
.flag<true>() // whether it is a flag enum
.prefix<sizeof("my_enum_")-1>(); // how many characters to trim from the start of each enum entry.
}
}
```
@ -78,32 +72,6 @@
};
```
* Or, for enum types that are deeply nested in classes and/or namespaces, declare a function called `my_adl_info_struct adl_magic_enum_define_range(my_enum_type)` in the same namespace as `my_enum_type`, which magic_enum will find by ADL (because the function is in the same class/namespace as `my_enum_type`), and whose return type is a struct with `static constexpr` data members containing the same parameters as `magic_enum::customize::enum_range<my_enum_type>`
```cpp
namespace Deeply::Nested::Namespace {
enum class my_enum_type { ... };
struct my_adl_info_struct {
static constexpr int min = 100;
static constexpr int max = 300;
// you can also set is_flags here
// static constexpr bool is_flags = true;
};
// - magic_enum will find this function by ADL
// - no need to ever define this function
my_adl_info_struct adl_magic_enum_define_range(my_enum_type);
}
```
* As a shorthand, if you only want to set `min` and `max` and not `is_flags`, you can also use `magic_enum::customize::adl_info<min_int, max_int>` to avoid having to define `my_adl_info_struct` in your code:
```cpp
namespace Deeply::Nested::Namespace {
enum class my_enum_type { ... };
// - magic_enum will find this function by ADL
// - no need to ever define this function
magic_enum::customize::adl_info<100 /*min*/, 300 /*max*/> adl_magic_enum_define_range(my_enum_type);
}
```
## Aliasing
magic_enum [won't work if a value is aliased](https://github.com/Neargye/magic_enum/issues/68). How magic_enum works with aliases is compiler-implementation-defined.