3#if __has_include("magic_enum/magic_enum.hpp")
4#include "magic_enum/magic_enum.hpp"
5#elif __has_include("magic_enum.hpp")
6#include "magic_enum.hpp"
8#error "magic_enum.hpp not found"
19 result.reserve(str.size());
20 std::ranges::transform(str, std::back_inserter(result),
21 [](
char c) {
return std::tolower(c); });
31 result.reserve(str.size());
32 std::ranges::transform(str, std::back_inserter(result),
33 [](
char c) {
return std::toupper(c); });
43template <
typename EnumType>
46 auto name = magic_enum::enum_name(value);
56template <
typename EnumType>
59 return magic_enum::enum_name(value);
68template <
typename EnumType>
71 return magic_enum::enum_cast<EnumType>(str, magic_enum::case_insensitive);
80template <
typename EnumType>
81constexpr std::optional<EnumType>
string_to_enum(std::string_view str)
noexcept
83 return magic_enum::enum_cast<EnumType>(str);
91template <
typename EnumType>
94 auto names = magic_enum::enum_names<EnumType>();
95 std::vector<std::string> lowercase_names;
96 lowercase_names.reserve(names.size());
98 for (
const auto& name : names) {
102 return lowercase_names;
110template <
typename EnumType>
113 return magic_enum::enum_names<EnumType>();
121template <
typename EnumType>
124 return magic_enum::enum_values<EnumType>();
133template <
typename EnumType>
136 return string_to_enum_case_insensitive<EnumType>(str).has_value();
144template <
typename EnumType>
147 return magic_enum::enum_count<EnumType>();
158template <
typename EnumType>
160 std::string_view context =
"")
162 auto result = string_to_enum_case_insensitive<EnumType>(str);
163 if (!result.has_value()) {
164 std::string error_msg =
"Invalid enum value: '" + std::string(str) +
"'";
165 if (!context.empty()) {
166 error_msg +=
" for " + std::string(context);
169 error_msg +=
". Valid values are: ";
170 auto names = get_enum_names_lowercase<EnumType>();
171 for (
size_t i = 0; i < names.size(); ++i) {
174 error_msg += names[i];
177 throw std::invalid_argument(error_msg);
constexpr auto get_enum_names() noexcept
Get all enum values as strings (original case)
std::string enum_to_lowercase_string(EnumType value) noexcept
Universal enum to lowercase string converter using magic_enum.
constexpr std::optional< EnumType > string_to_enum(std::string_view str) noexcept
Universal string to enum converter using magic_enum (exact case match)
std::optional< EnumType > string_to_enum_case_insensitive(std::string_view str) noexcept
Universal case-insensitive string to enum converter using magic_enum.
constexpr auto get_enum_values() noexcept
Get all enum values.
std::string to_uppercase(std::string_view str)
Convert string to uppercase.
constexpr std::string_view enum_to_string(EnumType value) noexcept
Universal enum to string converter using magic_enum (original case)
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)
std::string to_lowercase(std::string_view str)
Convert string to lowercase.
bool is_valid_enum_string_case_insensitive(std::string_view str) noexcept
Validate if string is a valid enum value (case-insensitive)
constexpr size_t enum_count() noexcept
Get enum count.
std::vector< std::string > get_enum_names_lowercase() noexcept
Get all enum values as lowercase strings.