|
4 | 4 | #include <string>
|
5 | 5 |
|
6 | 6 | #include "Result.hpp"
|
7 |
| -#include "internal/enums/StringConverter.hpp" |
8 | 7 | #include "internal/enums/get_enum_names.hpp"
|
9 |
| -#include "internal/enums/is_flag_enum.hpp" |
10 |
| -#include "internal/enums/is_scoped_enum.hpp" |
| 8 | +#include "internal/strings/strings.hpp" |
| 9 | +#include "thirdparty/enchantum/enchantum.hpp" |
| 10 | +#include "thirdparty/enchantum/bitflags.hpp" |
11 | 11 |
|
12 | 12 | namespace rfl {
|
13 | 13 |
|
14 |
| -// Converts an enum value to a string. |
15 |
| -template <internal::enums::is_scoped_enum EnumType> |
16 |
| -std::string enum_to_string(EnumType _enum) { |
17 |
| - return rfl::internal::enums::StringConverter<EnumType>::enum_to_string(_enum); |
| 14 | +template <enchantum::Enum EnumType> |
| 15 | +std::string enum_to_string(const EnumType _enum) { |
| 16 | + const auto to_string_or_number = [](const EnumType e) { |
| 17 | + const auto s = enchantum::to_string(e); |
| 18 | + return s.empty() ? std::to_string( |
| 19 | + static_cast<std::underlying_type_t<EnumType>>(e)) |
| 20 | + : std::string(s); |
| 21 | + }; |
| 22 | + |
| 23 | + if constexpr (enchantum::is_bitflag<EnumType>) { |
| 24 | + // Iterates through the enum bit by bit and matches it against the flags. |
| 25 | + using T = std::underlying_type_t<EnumType>; |
| 26 | + auto val = static_cast<T>(_enum); |
| 27 | + int i = 0; |
| 28 | + std::vector<std::string> flags; |
| 29 | + while (val != 0) { |
| 30 | + const auto bit = val & static_cast<T>(1); |
| 31 | + if (bit == 1) { |
| 32 | + auto str = |
| 33 | + to_string_or_number(static_cast<EnumType>(static_cast<T>(1) << i)); |
| 34 | + flags.emplace_back(std::move(str)); |
| 35 | + } |
| 36 | + ++i; |
| 37 | + val >>= 1; |
| 38 | + } |
| 39 | + return internal::strings::join("|", flags); |
| 40 | + } else { |
| 41 | + return to_string_or_number(_enum); |
| 42 | + } |
18 | 43 | }
|
19 | 44 |
|
20 | 45 | // Converts a string to a value of the given enum type.
|
21 |
| -template <internal::enums::is_scoped_enum EnumType> |
22 |
| -rfl::Result<EnumType> string_to_enum(const std::string& _str) { |
23 |
| - return rfl::internal::enums::StringConverter<EnumType>::string_to_enum(_str); |
| 46 | +template <enchantum::Enum EnumType> |
| 47 | +Result<EnumType> string_to_enum(const std::string& _str) { |
| 48 | + const auto cast_numbers_or_names = |
| 49 | + [](const std::string& name) -> Result<EnumType> { |
| 50 | + const auto r = enchantum::cast<EnumType>(name); |
| 51 | + if (r) return *r; |
| 52 | + try { |
| 53 | + return static_cast<EnumType>(std::stoi(name)); |
| 54 | + } catch (std::exception& exp) { |
| 55 | + return error(exp.what()); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + if constexpr (enchantum::is_bitflag<EnumType>) { |
| 60 | + using T = std::underlying_type_t<EnumType>; |
| 61 | + const auto split = internal::strings::split(_str, "|"); |
| 62 | + auto res = static_cast<T>(0); |
| 63 | + for (const auto& s : split) { |
| 64 | + const auto r = cast_numbers_or_names(s); |
| 65 | + if (r) { |
| 66 | + res |= static_cast<T>(*r); |
| 67 | + } else { |
| 68 | + return r; |
| 69 | + } |
| 70 | + } |
| 71 | + return static_cast<EnumType>(res); |
| 72 | + } else { |
| 73 | + return cast_numbers_or_names(_str); |
| 74 | + } |
24 | 75 | }
|
25 | 76 |
|
26 | 77 | // Returns a named tuple mapping names of enumerators of the given enum type to
|
27 | 78 | // their values.
|
28 |
| -template <internal::enums::is_scoped_enum EnumType> |
| 79 | +template <enchantum::Enum EnumType> |
29 | 80 | auto get_enumerators() {
|
30 |
| - constexpr auto names = internal::enums::get_enum_names< |
31 |
| - EnumType, internal::enums::is_flag_enum<EnumType>>(); |
32 |
| - return internal::enums::names_to_enumerator_named_tuple(names); |
| 81 | + return internal::enums::names_to_enumerator_named_tuple( |
| 82 | + internal::enums::get_enum_names<EnumType>()); |
33 | 83 | }
|
34 | 84 |
|
35 | 85 | // Returns a named tuple mapping names of enumerators of the given enum type to
|
36 | 86 | // their underlying values.
|
37 |
| -template <internal::enums::is_scoped_enum EnumType> |
| 87 | +template <enchantum::Enum EnumType> |
38 | 88 | auto get_underlying_enumerators() {
|
39 |
| - constexpr auto names = internal::enums::get_enum_names< |
40 |
| - EnumType, internal::enums::is_flag_enum<EnumType>>(); |
41 |
| - return internal::enums::names_to_underlying_enumerator_named_tuple(names); |
| 89 | + return internal::enums::names_to_underlying_enumerator_named_tuple( |
| 90 | + internal::enums::get_enum_names<EnumType>()); |
42 | 91 | }
|
43 | 92 |
|
44 | 93 | // Returns an std::array containing pairs of enumerator names (as
|
45 | 94 | // std::string_view) and values.
|
46 |
| -template <internal::enums::is_scoped_enum EnumType> |
| 95 | +template <enchantum::Enum EnumType> |
47 | 96 | constexpr auto get_enumerator_array() {
|
48 |
| - constexpr auto names = internal::enums::get_enum_names< |
49 |
| - EnumType, internal::enums::is_flag_enum<EnumType>>(); |
50 |
| - return internal::enums::names_to_enumerator_array(names); |
| 97 | + return internal::enums::names_to_enumerator_array( |
| 98 | + internal::enums::get_enum_names<EnumType>()); |
51 | 99 | }
|
52 | 100 |
|
53 | 101 | // Returns an std::array containing pairs of enumerator names (as
|
54 | 102 | // std::string_view) and underlying values.
|
55 |
| -template <internal::enums::is_scoped_enum EnumType> |
| 103 | +template <enchantum::Enum EnumType> |
56 | 104 | constexpr auto get_underlying_enumerator_array() {
|
57 |
| - constexpr auto names = internal::enums::get_enum_names< |
58 |
| - EnumType, internal::enums::is_flag_enum<EnumType>>(); |
59 |
| - return internal::enums::names_to_underlying_enumerator_array(names); |
| 105 | + return internal::enums::names_to_underlying_enumerator_array( |
| 106 | + internal::enums::get_enum_names<EnumType>()); |
60 | 107 | }
|
61 | 108 |
|
62 | 109 | // Returns the range of the given enum type as a pair of the minimum and maximum
|
63 |
| -template <internal::enums::is_scoped_enum EnumType> |
| 110 | +template <enchantum::Enum EnumType> |
64 | 111 | constexpr auto get_enum_range() {
|
65 |
| - return std::make_pair( |
66 |
| - internal::enums::get_range_min<EnumType, |
67 |
| - internal::enums::is_flag_enum<EnumType>>(), |
68 |
| - internal::enums::get_range_max< |
69 |
| - EnumType, internal::enums::is_flag_enum<EnumType>>()); |
| 112 | + return std::make_pair(enchantum::enum_traits<EnumType>::min, |
| 113 | + enchantum::enum_traits<EnumType>::max); |
70 | 114 | }
|
71 | 115 |
|
72 | 116 | } // namespace rfl
|
|
0 commit comments