35 lines
846 B
C++
35 lines
846 B
C++
#pragma once
|
|
|
|
#include <magic_enum/magic_enum.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <type_traits>
|
|
|
|
namespace renderive::web {
|
|
|
|
template <class Enum>
|
|
requires std::is_enum_v<Enum>
|
|
std::string gallery_enum_id(Enum value) {
|
|
std::string result(magic_enum::enum_name(value));
|
|
std::ranges::transform(result, result.begin(), [](unsigned char character) {
|
|
return static_cast<char>(std::tolower(character));
|
|
});
|
|
return result;
|
|
}
|
|
|
|
template <class Enum>
|
|
requires std::is_enum_v<Enum>
|
|
std::optional<Enum> gallery_enum_cast(std::string_view identifier) {
|
|
for (const Enum value : magic_enum::enum_values<Enum>()) {
|
|
if (gallery_enum_id(value) == identifier)
|
|
return value;
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
} // namespace renderive::web
|