|
1 | 1 | #ifndef RFL_INTERNAL_ENUMS_GET_ENUM_NAMES_HPP_ |
2 | 2 | #define RFL_INTERNAL_ENUMS_GET_ENUM_NAMES_HPP_ |
3 | 3 |
|
4 | | -// Enum values must be greater than or equal to RFL_ENUM_RANGE_MIN. |
5 | | -// By default, RFL_ENUM_RANGE_MIN is set to -256. |
6 | | -// To change the default minimum range for all enum types, redefine the macro |
7 | | -// RFL_ENUM_RANGE_MIN. |
8 | | -#if !defined(RFL_ENUM_RANGE_MIN) |
9 | | -#define RFL_ENUM_RANGE_MIN -256 |
| 4 | +#ifndef REFLECTCPP_USE_CPP_REFLECTION |
| 5 | +#include "cpp20/get_enum_names.hpp" |
| 6 | +#else |
| 7 | +#include "cpp26/get_enum_names.hpp" |
10 | 8 | #endif |
11 | 9 |
|
12 | | -// Enum values must be less than or equal to RFL_ENUM_RANGE_MAX. |
13 | | -// By default, RFL_ENUM_RANGE_MAX is set to 256. |
14 | | -// To change the default maximum range for all enum types, redefine the macro |
15 | | -// RFL_ENUM_RANGE_MAX. |
16 | | -#if !defined(RFL_ENUM_RANGE_MAX) |
17 | | -#define RFL_ENUM_RANGE_MAX 256 |
18 | | -#endif |
19 | | - |
20 | | -#ifdef ENCHANTUM_MIN_RANGE |
21 | | -#undef ENCHANTUM_MIN_RANGE |
22 | | -#endif |
23 | | -#define ENCHANTUM_MIN_RANGE RFL_ENUM_RANGE_MIN |
24 | | - |
25 | | -#ifdef ENCHANTUM_MAX_RANGE |
26 | | -#undef ENCHANTUM_MAX_RANGE |
27 | | -#endif |
28 | | -#define ENCHANTUM_MAX_RANGE RFL_ENUM_RANGE_MAX |
29 | | - |
30 | | -#include <concepts> |
31 | | -#include <utility> |
32 | | - |
33 | | -#include "../../thirdparty/enchantum/enchantum.hpp"// NOLINT(unused-includes) |
34 | | - |
35 | | -#include "../../Literal.hpp" |
36 | | -#include "Names.hpp" |
37 | | -#include "range_defined.hpp" |
38 | | - |
39 | | -// https://en.cppreference.com/w/cpp/language/static_cast: |
40 | | - |
41 | | -// 8) A value of integer or enumeration type can be converted to any complete |
42 | | -// enumeration type. |
43 | | - |
44 | | -// If the underlying type is not fixed, the behavior is undefined if the value |
45 | | -// of expression is out of range (the range is all values possible for the |
46 | | -// smallest bit-field large enough to hold all enumerators of the target |
47 | | -// enumeration). If the underlying type is fixed, the result is the same as |
48 | | -// converting the original value first to the underlying type of the enumeration |
49 | | -// and then to the enumeration type. |
50 | | - |
51 | | -// https://en.cppreference.com/w/cpp/language/enum |
52 | | - |
53 | | -// enum struct|class name { enumerator = constexpr , enumerator = constexpr , |
54 | | -// ... } (1) |
55 | | -// ... |
56 | | -// 1) declares a scoped enumeration type whose underlying type is int (the |
57 | | -// keywords class and struct are exactly equivalent) |
58 | | -// |
59 | | -// --> These rules taken together imply that if you EITHER fix the type OR you |
60 | | -// use a scoped integer, static_cast<MyEnum>(some_integer_value) will always be |
61 | | -// defined. |
62 | | - |
63 | | -template <enchantum::Enum E> |
64 | | - requires requires(E e) { |
65 | | - { e | e } -> std::same_as<E>; |
66 | | - } |
67 | | -constexpr inline bool enchantum::is_bitflag<E> = true; |
68 | | - |
69 | | -// Specialize the enchantum EnumTraits further, so rfl::config::enum_range |
70 | | -// works. |
71 | | -namespace enchantum { |
72 | | - |
73 | | -template <SignedEnum E> |
74 | | - requires rfl::internal::enums::range_defined<E> |
75 | | -struct enum_traits<E> { |
76 | | - static constexpr std::size_t prefix_length = 0; |
77 | | - |
78 | | - static constexpr auto min = rfl::config::enum_range<E>::min; |
79 | | - static constexpr auto max = rfl::config::enum_range<E>::max; |
80 | | -}; |
81 | | - |
82 | | -template <UnsignedEnum E> |
83 | | - requires rfl::internal::enums::range_defined<E> |
84 | | -struct enum_traits<E> { |
85 | | - static constexpr std::size_t prefix_length = 0; |
86 | | - |
87 | | - static constexpr auto min = rfl::config::enum_range<E>::min; |
88 | | - static constexpr auto max = rfl::config::enum_range<E>::max; |
89 | | -}; |
90 | | - |
91 | | -template <UnscopedEnum E> |
92 | | - requires SignedEnum<E> && |
93 | | - (!EnumFixedUnderlying<E>) && rfl::internal::enums::range_defined<E> |
94 | | -struct enum_traits<E> { |
95 | | - static constexpr auto min = rfl::config::enum_range<E>::min; |
96 | | - static constexpr auto max = rfl::config::enum_range<E>::max; |
97 | | -}; |
98 | | - |
99 | | -template <UnscopedEnum E> |
100 | | - requires UnsignedEnum<E> && |
101 | | - (!EnumFixedUnderlying<E>) && rfl::internal::enums::range_defined<E> |
102 | | -struct enum_traits<E> { |
103 | | - static constexpr auto min = rfl::config::enum_range<E>::min; |
104 | | - static constexpr auto max = rfl::config::enum_range<E>::max; |
105 | | -}; |
106 | | - |
107 | | -} // namespace enchantum |
108 | | - |
109 | 10 | namespace rfl::internal::enums { |
110 | 11 |
|
111 | | -template <enchantum::Enum EnumType> |
| 12 | +template <class EnumType> |
112 | 13 | consteval auto get_enum_names() { |
113 | | - return []<std::size_t... Is>(std::index_sequence<Is...>) { |
114 | | - constexpr auto& entries = enchantum::entries<EnumType>; |
115 | | - constexpr auto to_str_lit = |
116 | | - []<std::size_t... Js>(const char* name, std::index_sequence<Js...>) { |
117 | | - return StringLiteral<sizeof...(Js) + 1>{name[Js]...}; |
118 | | - }; |
119 | | - return Names<EnumType, |
120 | | - Literal<to_str_lit( |
121 | | - entries[Is].second.data(), |
122 | | - std::make_index_sequence<entries[Is].second.size()>{})...>, |
123 | | - entries.size(), enchantum::is_bitflag<EnumType>, |
124 | | - entries[Is].first...>{}; |
125 | | - }(std::make_index_sequence<enchantum::count<EnumType>>{}); |
| 14 | +#ifndef REFLECTCPP_USE_CPP_REFLECTION |
| 15 | + return cpp20::get_enum_names<EnumType>(); |
| 16 | +#else |
| 17 | + return cpp26::get_enum_names<EnumType>(); |
| 18 | +#endif |
126 | 19 | } |
| 20 | + |
127 | 21 | } // namespace rfl::internal::enums |
128 | 22 |
|
129 | 23 | #endif |
0 commit comments