This commit is contained in:
Mars 2024-06-08 15:53:06 -04:00
parent bd402f57f5
commit 3ea546fe08
Signed by: pupbrained
GPG key ID: 874E22DF2F9DFCB5
166 changed files with 18360 additions and 18199 deletions

View file

@ -29,63 +29,86 @@ namespace rfl {
constexpr static std::array<EnumType, N> enums_ =
std::array<EnumType, N> {_enums...};
static_assert(N == 0 || LiteralType::size() == N,
"Size of literal and enum do not match.");
static_assert(
N == 0 || LiteralType::size() == N,
"Size of literal and enum do not match."
);
template <class NewLiteral, auto _new_enum>
using AddOneType =
std::conditional_t<N == 0,
Names<EnumType, NewLiteral, 1, _new_enum>,
Names<EnumType,
define_literal_t<LiteralType, NewLiteral>,
N + 1,
_enums...,
_new_enum>>;
using AddOneType = std::conditional_t<
N == 0,
Names<EnumType, NewLiteral, 1, _new_enum>,
Names<
EnumType,
define_literal_t<LiteralType, NewLiteral>,
N + 1,
_enums...,
_new_enum>>;
};
template <class EnumType,
size_t N,
StringLiteral... _names,
auto... _enums>
auto names_to_enumerator_named_tuple(
Names<EnumType, Literal<_names...>, N, _enums...>) {
template <
class EnumType,
size_t N,
StringLiteral... _names,
auto... _enums>
auto names_to_enumerator_named_tuple(Names<
EnumType,
Literal<_names...>,
N,
_enums...>) {
return make_named_tuple(Field<_names, EnumType> {_enums}...);
}
template <class EnumType,
size_t N,
StringLiteral... _names,
auto... _enums>
auto names_to_underlying_enumerator_named_tuple(
Names<EnumType, Literal<_names...>, N, _enums...>) {
template <
class EnumType,
size_t N,
StringLiteral... _names,
auto... _enums>
auto names_to_underlying_enumerator_named_tuple(Names<
EnumType,
Literal<_names...>,
N,
_enums...>) {
return make_named_tuple(
Field<_names, std::underlying_type_t<EnumType>> {
static_cast<std::underlying_type_t<EnumType>>(_enums)}...);
static_cast<std::underlying_type_t<EnumType>>(_enums)
}...
);
}
template <class EnumType,
size_t N,
StringLiteral... _names,
auto... _enums>
template <
class EnumType,
size_t N,
StringLiteral... _names,
auto... _enums>
constexpr std::array<std::pair<std::string_view, EnumType>, N>
names_to_enumerator_array(
Names<EnumType, Literal<_names...>, N, _enums...>) {
return {std::make_pair(LiteralHelper<_names>::field_.string_view(),
_enums)...};
names_to_enumerator_array(Names<
EnumType,
Literal<_names...>,
N,
_enums...>) {
return {std::make_pair(
LiteralHelper<_names>::field_.string_view(), _enums
)...};
}
template <class EnumType,
size_t N,
StringLiteral... _names,
auto... _enums>
template <
class EnumType,
size_t N,
StringLiteral... _names,
auto... _enums>
constexpr std::array<
std::pair<std::string_view, std::underlying_type_t<EnumType>>,
N>
names_to_underlying_enumerator_array(
Names<EnumType, Literal<_names...>, N, _enums...>) {
names_to_underlying_enumerator_array(Names<
EnumType,
Literal<_names...>,
N,
_enums...>) {
return {std::make_pair(
LiteralHelper<_names>::field_.string_view(),
static_cast<std::underlying_type_t<EnumType>>(_enums))...};
static_cast<std::underlying_type_t<EnumType>>(_enums)
)...};
}
} // namespace enums

View file

@ -43,7 +43,8 @@ namespace rfl {
static_assert(
names_.size != 0,
"No enum could be identified. Please choose enum values "
"between 0 to 127 or for flag enums choose 1,2,4,8,16,...");
"between 0 to 127 or for flag enums choose 1,2,4,8,16,..."
);
if constexpr (is_flag_enum_) {
return string_to_flag_enum(_str);
} else {
@ -55,15 +56,16 @@ namespace rfl {
/// Iterates through the enum bit by bit and matches it against the
/// flags.
static std::string flag_enum_to_string(const EnumType _e) {
using T = std::underlying_type_t<EnumType>;
auto val = static_cast<T>(_e);
int i = 0;
using T = std::underlying_type_t<EnumType>;
auto val = static_cast<T>(_e);
int i = 0;
std::vector<std::string> flags;
while (val != 0) {
const auto bit = val & static_cast<T>(1);
if (bit == 1) {
auto str = enum_to_single_string(
static_cast<EnumType>(static_cast<T>(1) << i));
static_cast<EnumType>(static_cast<T>(1) << i)
);
flags.emplace_back(std::move(str));
}
++i;
@ -80,14 +82,16 @@ namespace rfl {
for (size_t i = 0; i < names_.size; ++i) {
if (names_.enums_[i] == _enum) {
return NamesLiteral::from_value(
static_cast<typename NamesLiteral::ValueType>(i))
static_cast<typename NamesLiteral::ValueType>(i)
)
.transform(to_str)
.value();
}
}
return std::to_string(
static_cast<std::underlying_type_t<EnumType>>(_enum));
static_cast<std::underlying_type_t<EnumType>>(_enum)
);
}
/// Finds the enum matching the literal.
@ -111,11 +115,11 @@ namespace rfl {
/// Only relevant if this is a flag enum - combines the different
/// matches using |.
static Result<EnumType> string_to_flag_enum(
const std::string& _str) noexcept {
static Result<EnumType> string_to_flag_enum(const std::string& _str
) noexcept {
using T = std::underlying_type_t<EnumType>;
const auto split = strings::split(_str, "|");
auto res = static_cast<T>(0);
auto res = static_cast<T>(0);
for (const auto& s : split) {
const auto r = single_string_to_enum(s);
if (r) {

View file

@ -63,7 +63,8 @@ namespace rfl {
static_assert(
false,
"You are using an unsupported compiler. Please use GCC, Clang "
"or MSVC or use rfl::Literal.");
"or MSVC or use rfl::Literal."
);
#endif
}
@ -105,11 +106,12 @@ namespace rfl {
}
}
template <class EnumType,
class NamesType,
auto _max,
bool _is_flag,
int _i>
template <
class EnumType,
class NamesType,
auto _max,
bool _is_flag,
int _i>
consteval auto get_enum_names_impl() {
using T = std::underlying_type_t<EnumType>;
@ -121,8 +123,8 @@ namespace rfl {
if constexpr (j == _max) {
return NamesType {};
} else {
return get_enum_names_impl<EnumType, NamesType, _max, _is_flag,
_i + 1>();
return get_enum_names_impl<
EnumType, NamesType, _max, _is_flag, _i + 1>();
}
} else {
using NewNames = typename NamesType::template AddOneType<
@ -131,8 +133,8 @@ namespace rfl {
if constexpr (j == _max) {
return NewNames {};
} else {
return get_enum_names_impl<EnumType, NewNames, _max, _is_flag,
_i + 1>();
return get_enum_names_impl<
EnumType, NewNames, _max, _is_flag, _i + 1>();
}
}
}
@ -142,10 +144,13 @@ namespace rfl {
static_assert(
is_scoped_enum<EnumType>,
"You must use scoped enums (using class or struct) for the "
"parsing to work!");
"parsing to work!"
);
static_assert(std::is_integral_v<std::underlying_type_t<EnumType>>,
"The underlying type of any Enum must be integral!");
static_assert(
std::is_integral_v<std::underlying_type_t<EnumType>>,
"The underlying type of any Enum must be integral!"
);
constexpr auto max =
get_max<std::underlying_type_t<EnumType>, _is_flag>();