Skip to content

Commit 0832143

Browse files
committed
Simplify code
1 parent a00caaa commit 0832143

5 files changed

Lines changed: 16 additions & 63 deletions

File tree

include/rfl/enums.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@
77
#include "internal/enums/StringConverter.hpp"
88
#include "internal/enums/get_enum_names.hpp"
99
#include "internal/enums/is_flag_enum.hpp"
10-
#include "internal/enums/is_scoped_enum.hpp"
10+
#include "thirdparty/enchantum.hpp"
1111

1212
namespace rfl {
1313

1414
// Converts an enum value to a string.
15-
template <internal::enums::is_scoped_enum EnumType>
15+
template <enchantum::Enum EnumType>
1616
std::string enum_to_string(EnumType _enum) {
1717
return rfl::internal::enums::StringConverter<EnumType>::enum_to_string(_enum);
1818
}
1919

2020
// Converts a string to a value of the given enum type.
21-
template <internal::enums::is_scoped_enum EnumType>
21+
template <enchantum::Enum EnumType>
2222
rfl::Result<EnumType> string_to_enum(const std::string& _str) {
2323
return rfl::internal::enums::StringConverter<EnumType>::string_to_enum(_str);
2424
}
2525

2626
// Returns a named tuple mapping names of enumerators of the given enum type to
2727
// their values.
28-
template <internal::enums::is_scoped_enum EnumType>
28+
template <enchantum::Enum EnumType>
2929
auto get_enumerators() {
3030
constexpr auto names = internal::enums::get_enum_names<
3131
EnumType, internal::enums::is_flag_enum<EnumType>>();
@@ -34,7 +34,7 @@ auto get_enumerators() {
3434

3535
// Returns a named tuple mapping names of enumerators of the given enum type to
3636
// their underlying values.
37-
template <internal::enums::is_scoped_enum EnumType>
37+
template <enchantum::Enum EnumType>
3838
auto get_underlying_enumerators() {
3939
constexpr auto names = internal::enums::get_enum_names<
4040
EnumType, internal::enums::is_flag_enum<EnumType>>();
@@ -43,7 +43,7 @@ auto get_underlying_enumerators() {
4343

4444
// Returns an std::array containing pairs of enumerator names (as
4545
// std::string_view) and values.
46-
template <internal::enums::is_scoped_enum EnumType>
46+
template <enchantum::Enum EnumType>
4747
constexpr auto get_enumerator_array() {
4848
constexpr auto names = internal::enums::get_enum_names<
4949
EnumType, internal::enums::is_flag_enum<EnumType>>();
@@ -52,15 +52,15 @@ constexpr auto get_enumerator_array() {
5252

5353
// Returns an std::array containing pairs of enumerator names (as
5454
// std::string_view) and underlying values.
55-
template <internal::enums::is_scoped_enum EnumType>
55+
template <enchantum::Enum EnumType>
5656
constexpr auto get_underlying_enumerator_array() {
5757
constexpr auto names = internal::enums::get_enum_names<
5858
EnumType, internal::enums::is_flag_enum<EnumType>>();
5959
return internal::enums::names_to_underlying_enumerator_array(names);
6060
}
6161

6262
// Returns the range of the given enum type as a pair of the minimum and maximum
63-
template <internal::enums::is_scoped_enum EnumType>
63+
template <enchantum::Enum EnumType>
6464
constexpr auto get_enum_range() {
6565
return std::make_pair(
6666
internal::enums::get_range_min<EnumType,

include/rfl/internal/enums/Names.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
#define RFL_INTERNAL_ENUMS_NAMES_HPP_
33

44
// Enum values must be greater than or equal to RFL_ENUM_RANGE_MIN.
5-
// By default, RFL_ENUM_RANGE_MIN is set to 0.
5+
// By default, RFL_ENUM_RANGE_MIN is set to -256.
66
// To change the default minimum range for all enum types, redefine the macro
77
// RFL_ENUM_RANGE_MIN.
88
#if !defined(RFL_ENUM_RANGE_MIN)
99
#define RFL_ENUM_RANGE_MIN -256
1010
#endif
1111

1212
// Enum values must be less than or equal to RFL_ENUM_RANGE_MAX.
13-
// By default, RFL_ENUM_RANGE_MAX is set to 127.
13+
// By default, RFL_ENUM_RANGE_MAX is set to 256.
1414
// To change the default maximum range for all enum types, redefine the macro
1515
// RFL_ENUM_RANGE_MAX.
1616
#if !defined(RFL_ENUM_RANGE_MAX)

include/rfl/internal/enums/get_enum_names.hpp

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,46 +66,19 @@ consteval auto get_range_max() {
6666
}
6767
}
6868

69-
struct str_view_agg {
70-
const char* data;
71-
std::size_t size;
72-
};
73-
7469
template <enchantum::Enum EnumType, bool _is_flag>
7570
consteval auto get_enum_names() {
76-
// constexpr auto max = get_range_max<EnumType, _is_flag>();
77-
// constexpr auto min = get_range_min<EnumType, _is_flag>();
78-
// constexpr auto range_size = max - min + 1;
79-
//
80-
// static_assert(range_size > 0,
81-
// "enum_range requires a valid range size. Ensure that max is "
82-
// "greater than min.");
83-
//
84-
// using EmptyNames = Names<EnumType, rfl::Literal<"">, 0, _is_flag>;
85-
86-
constexpr auto convert = []() {
87-
constexpr auto a = enchantum::entries<EnumType>;
88-
std::array<std::pair<EnumType, str_view_agg>, a.size()> b;
89-
for (std::size_t i = 0; i < a.size(); ++i) {
90-
b[i].first = a[i].first;
91-
b[i].second = {a[i].second.data(), a[i].second.size()};
92-
}
93-
return b;
94-
}();
95-
96-
// return Names<E, Literal<"">, sizeof...(entries), _is_flag,
97-
// entries.first...>{};
98-
99-
return [convert]<std::size_t... Is>(std::index_sequence<Is...>) {
71+
return []<std::size_t... Is>(std::index_sequence<Is...>) {
72+
constexpr auto& entries = enchantum::entries<EnumType>;
10073
constexpr auto to_str_lit =
10174
[]<std::size_t... Js>(const char* name, std::index_sequence<Js...>) {
10275
return StringLiteral<sizeof...(Js) + 1>{name[Js]...};
10376
};
10477
return Names<EnumType,
10578
Literal<to_str_lit(
106-
convert[Is].second.data,
107-
std::make_index_sequence<convert[Is].second.size>{})...>,
108-
convert.size(), _is_flag, convert[Is].first...>{};
79+
entries[Is].second.data(),
80+
std::make_index_sequence<entries[Is].second.size()>{})...>,
81+
entries.size(), _is_flag, entries[Is].first...>{};
10982
}(std::make_index_sequence<enchantum::count<EnumType>>{});
11083
}
11184
} // namespace rfl::internal::enums

include/rfl/internal/enums/is_flag_enum.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include <concepts>
55

6-
#include "is_scoped_enum.hpp"
76
#include "../../thirdparty/enchantum.hpp"
87
template<enchantum::Enum E>
98
requires requires(E e) {
@@ -17,7 +16,7 @@ namespace enums {
1716

1817
template <class EnumType>
1918
concept is_flag_enum =
20-
is_scoped_enum<EnumType> && enchantum::BitFlagEnum<EnumType>;
19+
enchantum::ScopedEnum<EnumType> && enchantum::BitFlagEnum<EnumType>;
2120

2221

2322
} // namespace enums

include/rfl/internal/enums/is_scoped_enum.hpp

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)