25#include "../common_types.hpp"
27#include "../naming_convention.hpp"
34#include <unordered_map>
38namespace essence::meta::runtime {
46 template <
typename T,
typename Tag =
void>
47 requires std::is_enum_v<T>
48 std::span<const std::pair<std::string, T>> get_enum_names(
bool short_name =
true) {
49 static constexpr std::array naming_conventions{
50 naming_convention::camel_case,
51 naming_convention::pascal_case,
52 naming_convention::snake_case,
55 static constexpr auto make_cache = [](
bool short_name) {
56 std::vector<std::pair<std::string, T>> result;
57 auto names = short_name ? meta::probe_enum_names<T, true>() : meta::probe_enum_names<T, false>();
59 for (
auto&& [name, value] : names) {
60 result.emplace_back(std::string{name},
value);
63 if constexpr (std::same_as<Tag, all_naming_conventions_tag>) {
64 auto extra_names = naming_conventions | std::views::transform([&](
const auto& inner) {
65 return convert_naming_convention(name, inner);
66 }) | std::views::filter([&](
const auto& inner) {
return inner != name; });
68 for (
auto&& item : extra_names) {
69 result.emplace_back(std::move(item), value);
77 static const auto short_cache = make_cache(
true);
78 static const auto cache = make_cache(
false);
94 template <
typename T,
typename Tag =
void>
95 requires std::is_enum_v<T>
96 std::span<const std::string> get_enum_names_only(
bool short_name =
true) {
97 static const auto make_cache = [](
bool short_name) {
98 std::vector<std::string> result;
100 for (
auto&& [name, value] : get_enum_names<T, Tag>(short_name)) {
101 result.emplace_back(name);
107 static const auto short_cache = make_cache(
true);
108 static const auto cache = make_cache(
false);
123 template <
typename T>
124 requires std::is_enum_v<T>
125 std::string to_string(T value) {
126 static const auto mapping = [] {
127 std::unordered_map<T, std::string> result;
129 for (
auto&& [name, value] : get_enum_names<T>()) {
130 result.insert_or_assign(value, name);
136 if (
auto iter = mapping.find(value); iter != mapping.end()) {
149 template <
typename T>
150 requires std::is_enum_v<T>
151 std::optional<T> from_string(std::string_view name) {
152 static const auto mapping = [] {
153 std::unordered_map<std::string_view, T> result;
155 for (
auto&& [name, value] : get_enum_names<T, all_naming_conventions_tag>()) {
156 result.insert_or_assign(name, value);
162 if (
auto iter = mapping.find(name); iter != mapping.end()) {
@ value
the parser finished reading a JSON value