3#include "magic_enum/magic_enum.hpp"
13 result.reserve(str.size());
14 std::ranges::transform(str, std::back_inserter(result),
15 [](
char c) {
return std::tolower(c); });
25 result.reserve(str.size());
26 std::ranges::transform(str, std::back_inserter(result),
27 [](
char c) {
return std::toupper(c); });
37template <
typename EnumType>
40 auto name = magic_enum::enum_name(value);
50template <
typename EnumType>
53 return magic_enum::enum_name(value);
62template <
typename EnumType>
66 auto direct_result = magic_enum::enum_cast<EnumType>(str);
67 if (direct_result.has_value()) {
73 auto upper_result = magic_enum::enum_cast<EnumType>(upper_str);
74 if (upper_result.has_value()) {
87template <
typename EnumType>
88constexpr std::optional<EnumType>
string_to_enum(std::string_view str)
noexcept
90 return magic_enum::enum_cast<EnumType>(str);
98template <
typename EnumType>
101 auto names = magic_enum::enum_names<EnumType>();
102 std::vector<std::string> lowercase_names;
103 lowercase_names.reserve(names.size());
105 for (
const auto& name : names) {
109 return lowercase_names;
117template <
typename EnumType>
120 return magic_enum::enum_names<EnumType>();
128template <
typename EnumType>
131 return magic_enum::enum_values<EnumType>();
140template <
typename EnumType>
143 return string_to_enum_case_insensitive<EnumType>(str).has_value();
151template <
typename EnumType>
154 return magic_enum::enum_count<EnumType>();
165template <
typename EnumType>
167 std::string_view context =
"")
169 auto result = string_to_enum_case_insensitive<EnumType>(str);
170 if (!result.has_value()) {
171 std::string error_msg =
"Invalid enum value: '" + std::string(str) +
"'";
172 if (!context.empty()) {
173 error_msg +=
" for " + std::string(context);
176 error_msg +=
". Valid values are: ";
177 auto names = get_enum_names_lowercase<EnumType>();
178 for (
size_t i = 0; i < names.size(); ++i) {
181 error_msg += names[i];
184 throw std::invalid_argument(error_msg);
EnumType string_to_enum_or_throw_case_insensitive(std::string_view str, std::string_view context="")
Convert string to enum with exception on failure (case-insensitive)
constexpr size_t enum_count() noexcept
Get enum count.
std::string enum_to_lowercase_string(EnumType value) noexcept
Universal enum to lowercase string converter using magic_enum.
std::vector< std::string > get_enum_names_lowercase() noexcept
Get all enum values as lowercase strings.
constexpr std::string_view enum_to_string(EnumType value) noexcept
Universal enum to string converter using magic_enum (original case)
constexpr std::optional< EnumType > string_to_enum(std::string_view str) noexcept
Universal string to enum converter using magic_enum (exact case match)
constexpr auto get_enum_values() noexcept
Get all enum values.
std::optional< EnumType > string_to_enum_case_insensitive(std::string_view str) noexcept
Universal case-insensitive string to enum converter using magic_enum.
bool is_valid_enum_string_case_insensitive(std::string_view str) noexcept
Validate if string is a valid enum value (case-insensitive)
std::string to_uppercase(std::string_view str)
Convert string to uppercase.
std::string to_lowercase(std::string_view str)
Convert string to lowercase.
constexpr auto get_enum_names() noexcept
Get all enum values as strings (original case)