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>
72 auto direct_result = magic_enum::enum_cast<EnumType>(str);
73 if (direct_result.has_value()) {
79 auto upper_result = magic_enum::enum_cast<EnumType>(upper_str);
80 if (upper_result.has_value()) {
93template <
typename EnumType>
94constexpr std::optional<EnumType>
string_to_enum(std::string_view str)
noexcept
96 return magic_enum::enum_cast<EnumType>(str);
104template <
typename EnumType>
107 auto names = magic_enum::enum_names<EnumType>();
108 std::vector<std::string> lowercase_names;
109 lowercase_names.reserve(names.size());
111 for (
const auto& name : names) {
115 return lowercase_names;
123template <
typename EnumType>
126 return magic_enum::enum_names<EnumType>();
134template <
typename EnumType>
137 return magic_enum::enum_values<EnumType>();
146template <
typename EnumType>
149 return string_to_enum_case_insensitive<EnumType>(str).has_value();
157template <
typename EnumType>
160 return magic_enum::enum_count<EnumType>();
171template <
typename EnumType>
173 std::string_view context =
"")
175 auto result = string_to_enum_case_insensitive<EnumType>(str);
176 if (!result.has_value()) {
177 std::string error_msg =
"Invalid enum value: '" + std::string(str) +
"'";
178 if (!context.empty()) {
179 error_msg +=
" for " + std::string(context);
182 error_msg +=
". Valid values are: ";
183 auto names = get_enum_names_lowercase<EnumType>();
184 for (
size_t i = 0; i < names.size(); ++i) {
187 error_msg += names[i];
190 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)