draconisplusplus/include/rfl/enums.hpp

65 lines
2.4 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_ENUMS_HPP_
#define RFL_ENUMS_HPP_
#include <string>
#include "Result.hpp"
#include "internal/enums/StringConverter.hpp"
#include "internal/enums/get_enum_names.hpp"
#include "internal/enums/is_flag_enum.hpp"
#include "internal/enums/is_scoped_enum.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
// Converts an enum value to a string.
template <internal::enums::is_scoped_enum EnumType>
std::string enum_to_string(EnumType _enum) {
2024-06-16 00:13:15 -04:00
return rfl::internal::enums::StringConverter<EnumType>::enum_to_string(_enum);
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
// Converts a string to a value of the given enum type.
template <internal::enums::is_scoped_enum EnumType>
rfl::Result<EnumType> string_to_enum(const std::string& _str) {
2024-06-16 00:13:15 -04:00
return rfl::internal::enums::StringConverter<EnumType>::string_to_enum(_str);
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
// Returns a named tuple mapping names of enumerators of the given enum type
// to their values.
template <internal::enums::is_scoped_enum EnumType>
auto get_enumerators() {
2024-06-16 00:13:15 -04:00
constexpr auto names =
internal::enums::get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
2024-06-08 14:10:59 -04:00
return internal::enums::names_to_enumerator_named_tuple(names);
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
// Returns a named tuple mapping names of enumerators of the given enum type
// to their underlying values.
template <internal::enums::is_scoped_enum EnumType>
auto get_underlying_enumerators() {
2024-06-16 00:13:15 -04:00
constexpr auto names =
internal::enums::get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
2024-06-08 14:10:59 -04:00
return internal::enums::names_to_underlying_enumerator_named_tuple(names);
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
// Returns an std::array containing pairs of enumerator names (as
// std::string_view) and values.
template <internal::enums::is_scoped_enum EnumType>
constexpr auto get_enumerator_array() {
2024-06-16 00:13:15 -04:00
constexpr auto names =
internal::enums::get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
2024-06-08 14:10:59 -04:00
return internal::enums::names_to_enumerator_array(names);
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
// Returns an std::array containing pairs of enumerator names (as
// std::string_view) and underlying values.
template <internal::enums::is_scoped_enum EnumType>
constexpr auto get_underlying_enumerator_array() {
2024-06-16 00:13:15 -04:00
constexpr auto names =
internal::enums::get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
2024-06-08 14:10:59 -04:00
return internal::enums::names_to_underlying_enumerator_array(names);
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
} // namespace rfl
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
#endif // RFL_ENUMS_HPP_