1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-02-06 04:00:09 +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

@ -23,6 +23,32 @@
* 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);
}
```
* 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 { ... };
// - 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);
}
```
## Enum Range
* Enum values must be in the range `[MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]`.
@ -52,6 +78,32 @@
};
```
* 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.