MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
EnumUtils.hpp
Go to the documentation of this file.
1#pragma once
2
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"
7#else
8#error "magic_enum.hpp not found"
9#endif
10
11namespace MayaFlux::Utils {
12
13/**
14 * @brief Convert string to lowercase
15 */
16inline std::string to_lowercase(std::string_view str)
17{
18 std::string result;
19 result.reserve(str.size());
20 std::ranges::transform(str, std::back_inserter(result),
21 [](char c) { return std::tolower(c); });
22 return result;
23}
24
25/**
26 * @brief Convert string to uppercase
27 */
28inline std::string to_uppercase(std::string_view str)
29{
30 std::string result;
31 result.reserve(str.size());
32 std::ranges::transform(str, std::back_inserter(result),
33 [](char c) { return std::toupper(c); });
34 return result;
35}
36
37/**
38 * @brief Universal enum to lowercase string converter using magic_enum
39 * @tparam EnumType Any enum type
40 * @param value Enum value to convert
41 * @return Lowercase string representation of the enum
42 */
43template <typename EnumType>
44std::string enum_to_lowercase_string(EnumType value) noexcept
45{
46 auto name = magic_enum::enum_name(value);
47 return to_lowercase(name);
48}
49
50/**
51 * @brief Universal enum to string converter using magic_enum (original case)
52 * @tparam EnumType Any enum type
53 * @param value Enum value to convert
54 * @return String representation of the enum
55 */
56template <typename EnumType>
57constexpr std::string_view enum_to_string(EnumType value) noexcept
58{
59 return magic_enum::enum_name(value);
60}
61
62/**
63 * @brief Universal case-insensitive string to enum converter using magic_enum
64 * @tparam EnumType Any enum type
65 * @param str String to convert (case-insensitive)
66 * @return Optional enum value if valid, nullopt otherwise
67 */
68template <typename EnumType>
69std::optional<EnumType> string_to_enum_case_insensitive(std::string_view str) noexcept
70{
71 // Try direct match first (most efficient)
72 auto direct_result = magic_enum::enum_cast<EnumType>(str);
73 if (direct_result.has_value()) {
74 return direct_result;
75 }
76
77 // Convert input to uppercase for comparison with enum names
78 std::string upper_str = to_uppercase(str);
79 auto upper_result = magic_enum::enum_cast<EnumType>(upper_str);
80 if (upper_result.has_value()) {
81 return upper_result;
82 }
83
84 return std::nullopt;
85}
86
87/**
88 * @brief Universal string to enum converter using magic_enum (exact case match)
89 * @tparam EnumType Any enum type
90 * @param str String to convert
91 * @return Optional enum value if valid, nullopt otherwise
92 */
93template <typename EnumType>
94constexpr std::optional<EnumType> string_to_enum(std::string_view str) noexcept
95{
96 return magic_enum::enum_cast<EnumType>(str);
97}
98
99/**
100 * @brief Get all enum values as lowercase strings
101 * @tparam EnumType Any enum type
102 * @return Vector of all enum value names in lowercase
103 */
104template <typename EnumType>
105std::vector<std::string> get_enum_names_lowercase() noexcept
106{
107 auto names = magic_enum::enum_names<EnumType>();
108 std::vector<std::string> lowercase_names;
109 lowercase_names.reserve(names.size());
110
111 for (const auto& name : names) {
112 lowercase_names.push_back(to_lowercase(name));
113 }
114
115 return lowercase_names;
116}
117
118/**
119 * @brief Get all enum values as strings (original case)
120 * @tparam EnumType Any enum type
121 * @return Vector of all enum value names
122 */
123template <typename EnumType>
124constexpr auto get_enum_names() noexcept
125{
126 return magic_enum::enum_names<EnumType>();
127}
128
129/**
130 * @brief Get all enum values
131 * @tparam EnumType Any enum type
132 * @return Array of all enum values
133 */
134template <typename EnumType>
135constexpr auto get_enum_values() noexcept
136{
137 return magic_enum::enum_values<EnumType>();
138}
139
140/**
141 * @brief Validate if string is a valid enum value (case-insensitive)
142 * @tparam EnumType Any enum type
143 * @param str String to validate
144 * @return True if string represents a valid enum value
145 */
146template <typename EnumType>
147bool is_valid_enum_string_case_insensitive(std::string_view str) noexcept
148{
149 return string_to_enum_case_insensitive<EnumType>(str).has_value();
150}
151
152/**
153 * @brief Get enum count
154 * @tparam EnumType Any enum type
155 * @return Number of enum values
156 */
157template <typename EnumType>
158constexpr size_t enum_count() noexcept
159{
160 return magic_enum::enum_count<EnumType>();
161}
162
163/**
164 * @brief Convert string to enum with exception on failure (case-insensitive)
165 * @tparam EnumType Any enum type
166 * @param str String to convert
167 * @param context Optional context for error message
168 * @return Enum value
169 * @throws std::invalid_argument if string is not valid enum
170 */
171template <typename EnumType>
172EnumType string_to_enum_or_throw_case_insensitive(std::string_view str,
173 std::string_view context = "")
174{
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);
180 }
181
182 error_msg += ". Valid values are: ";
183 auto names = get_enum_names_lowercase<EnumType>();
184 for (size_t i = 0; i < names.size(); ++i) {
185 if (i > 0)
186 error_msg += ", ";
187 error_msg += names[i];
188 }
189
190 throw std::invalid_argument(error_msg);
191 }
192 return *result;
193}
194}
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.
Definition EnumUtils.hpp:44
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)
Definition EnumUtils.hpp:57
constexpr std::optional< EnumType > string_to_enum(std::string_view str) noexcept
Universal string to enum converter using magic_enum (exact case match)
Definition EnumUtils.hpp:94
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.
Definition EnumUtils.hpp:69
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.
Definition EnumUtils.hpp:28
std::string to_lowercase(std::string_view str)
Convert string to lowercase.
Definition EnumUtils.hpp:16
constexpr auto get_enum_names() noexcept
Get all enum values as strings (original case)