1111
1212namespace rfl {
1313
14+ // Returns a named tuple mapping names of enumerators of the given enum type to
15+ // their values.
16+ template <enchantum::Enum EnumType>
17+ auto get_enumerators () {
18+ return internal::enums::names_to_enumerator_named_tuple (
19+ internal::enums::get_enum_names<EnumType>());
20+ }
21+
22+ // Returns a named tuple mapping names of enumerators of the given enum type to
23+ // their underlying values.
24+ template <enchantum::Enum EnumType>
25+ auto get_underlying_enumerators () {
26+ return internal::enums::names_to_underlying_enumerator_named_tuple (
27+ internal::enums::get_enum_names<EnumType>());
28+ }
29+
30+ // Returns an std::array containing pairs of enumerator names (as
31+ // std::string_view) and values.
32+ template <enchantum::Enum EnumType>
33+ constexpr auto get_enumerator_array () {
34+ return internal::enums::names_to_enumerator_array (
35+ internal::enums::get_enum_names<EnumType>());
36+ }
37+
38+ // Returns an std::array containing pairs of enumerator names (as
39+ // std::string_view) and underlying values.
40+ template <enchantum::Enum EnumType>
41+ constexpr auto get_underlying_enumerator_array () {
42+ return internal::enums::names_to_underlying_enumerator_array (
43+ internal::enums::get_enum_names<EnumType>());
44+ }
45+
46+ // Returns the range of the given enum type as a pair of the minimum and maximum
47+ template <enchantum::Enum EnumType>
48+ constexpr auto get_enum_range () {
49+ return std::make_pair (enchantum::enum_traits<EnumType>::min,
50+ enchantum::enum_traits<EnumType>::max);
51+ }
52+
53+ // Converts an enum value to tis string representation.
1454template <enchantum::Enum EnumType>
1555std::string enum_to_string (const EnumType _enum) {
1656 const auto to_string_or_number = [](const EnumType e) {
@@ -52,7 +92,18 @@ Result<EnumType> string_to_enum(const std::string& _str) {
5292 try {
5393 return static_cast <EnumType>(std::stoi (name));
5494 } catch (std::exception& exp) {
55- return error (exp.what ());
95+ std::ostringstream oss;
96+ oss << " Invalid enum value: '" << name << " '. Must be one of [" ;
97+ bool first = true ;
98+ for (const auto & [name, value] : get_enumerator_array<EnumType>()) {
99+ if (!first) {
100+ oss << " , " ;
101+ }
102+ first = false ;
103+ oss << name;
104+ }
105+ oss << " ]." ;
106+ return error (oss.str ());
56107 }
57108 };
58109
@@ -74,45 +125,6 @@ Result<EnumType> string_to_enum(const std::string& _str) {
74125 }
75126}
76127
77- // Returns a named tuple mapping names of enumerators of the given enum type to
78- // their values.
79- template <enchantum::Enum EnumType>
80- auto get_enumerators () {
81- return internal::enums::names_to_enumerator_named_tuple (
82- internal::enums::get_enum_names<EnumType>());
83- }
84-
85- // Returns a named tuple mapping names of enumerators of the given enum type to
86- // their underlying values.
87- template <enchantum::Enum EnumType>
88- auto get_underlying_enumerators () {
89- return internal::enums::names_to_underlying_enumerator_named_tuple (
90- internal::enums::get_enum_names<EnumType>());
91- }
92-
93- // Returns an std::array containing pairs of enumerator names (as
94- // std::string_view) and values.
95- template <enchantum::Enum EnumType>
96- constexpr auto get_enumerator_array () {
97- return internal::enums::names_to_enumerator_array (
98- internal::enums::get_enum_names<EnumType>());
99- }
100-
101- // Returns an std::array containing pairs of enumerator names (as
102- // std::string_view) and underlying values.
103- template <enchantum::Enum EnumType>
104- constexpr auto get_underlying_enumerator_array () {
105- return internal::enums::names_to_underlying_enumerator_array (
106- internal::enums::get_enum_names<EnumType>());
107- }
108-
109- // Returns the range of the given enum type as a pair of the minimum and maximum
110- template <enchantum::Enum EnumType>
111- constexpr auto get_enum_range () {
112- return std::make_pair (enchantum::enum_traits<EnumType>::min,
113- enchantum::enum_traits<EnumType>::max);
114- }
115-
116128} // namespace rfl
117129
118130#endif // RFL_ENUMS_HPP_
0 commit comments