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