MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
EnumReflect.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
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 return magic_enum::enum_cast<EnumType>(str, magic_enum::case_insensitive);
72}
73
74/**
75 * @brief Universal string to enum converter using magic_enum (exact case match)
76 * @tparam EnumType Any enum type
77 * @param str String to convert
78 * @return Optional enum value if valid, nullopt otherwise
79 */
80template <typename EnumType>
81constexpr std::optional<EnumType> string_to_enum(std::string_view str) noexcept
82{
83 return magic_enum::enum_cast<EnumType>(str);
84}
85
86/**
87 * @brief Get all enum values as lowercase strings
88 * @tparam EnumType Any enum type
89 * @return Vector of all enum value names in lowercase
90 */
91template <typename EnumType>
92std::vector<std::string> get_enum_names_lowercase() noexcept
93{
94 auto names = magic_enum::enum_names<EnumType>();
95 std::vector<std::string> lowercase_names;
96 lowercase_names.reserve(names.size());
97
98 for (const auto& name : names) {
99 lowercase_names.push_back(to_lowercase(name));
100 }
101
102 return lowercase_names;
103}
104
105/**
106 * @brief Get all enum values as strings (original case)
107 * @tparam EnumType Any enum type
108 * @return Vector of all enum value names
109 */
110template <typename EnumType>
111constexpr auto get_enum_names() noexcept
112{
113 return magic_enum::enum_names<EnumType>();
114}
115
116/**
117 * @brief Get all enum values
118 * @tparam EnumType Any enum type
119 * @return Array of all enum values
120 */
121template <typename EnumType>
122constexpr auto get_enum_values() noexcept
123{
124 return magic_enum::enum_values<EnumType>();
125}
126
127/**
128 * @brief Validate if string is a valid enum value (case-insensitive)
129 * @tparam EnumType Any enum type
130 * @param str String to validate
131 * @return True if string represents a valid enum value
132 */
133template <typename EnumType>
134bool is_valid_enum_string_case_insensitive(std::string_view str) noexcept
135{
136 return string_to_enum_case_insensitive<EnumType>(str).has_value();
137}
138
139/**
140 * @brief Get enum count
141 * @tparam EnumType Any enum type
142 * @return Number of enum values
143 */
144template <typename EnumType>
145constexpr size_t enum_count() noexcept
146{
147 return magic_enum::enum_count<EnumType>();
148}
149
150/**
151 * @brief Convert string to enum with exception on failure (case-insensitive)
152 * @tparam EnumType Any enum type
153 * @param str String to convert
154 * @param context Optional context for error message
155 * @return Enum value
156 * @throws std::invalid_argument if string is not valid enum
157 */
158template <typename EnumType>
159EnumType string_to_enum_or_throw_case_insensitive(std::string_view str,
160 std::string_view context = "")
161{
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);
167 }
168
169 error_msg += ". Valid values are: ";
170 auto names = get_enum_names_lowercase<EnumType>();
171 for (size_t i = 0; i < names.size(); ++i) {
172 if (i > 0)
173 error_msg += ", ";
174 error_msg += names[i];
175 }
176
177 throw std::invalid_argument(error_msg);
178 }
179 return *result;
180}
181}
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.