C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
enum.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "../common_types.hpp"
26#include "../enum.hpp"
27#include "../naming_convention.hpp"
28
29#include <optional>
30#include <ranges>
31#include <span>
32#include <string>
33#include <type_traits>
34#include <unordered_map>
35#include <utility>
36#include <vector>
37
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,
53 };
54
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>();
58
59 for (auto&& [name, value] : names) {
60 result.emplace_back(std::string{name}, value);
61
62 // Adds extra names according to the naming conventions.
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; });
67
68 for (auto&& item : extra_names) {
69 result.emplace_back(std::move(item), value);
70 }
71 }
72 }
73
74 return result;
75 };
76
77 static const auto short_cache = make_cache(true);
78 static const auto cache = make_cache(false);
79
80 if (short_name) {
81 return short_cache;
82 }
83
84 return cache;
85 }
86
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;
99
100 for (auto&& [name, value] : get_enum_names<T, Tag>(short_name)) {
101 result.emplace_back(name);
102 }
103
104 return result;
105 };
106
107 static const auto short_cache = make_cache(true);
108 static const auto cache = make_cache(false);
109
110 if (short_name) {
111 return short_cache;
112 }
113
114 return cache;
115 }
116
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;
128
129 for (auto&& [name, value] : get_enum_names<T>()) {
130 result.insert_or_assign(value, name);
131 }
132
133 return result;
134 }();
135
136 if (auto iter = mapping.find(value); iter != mapping.end()) {
137 return iter->second;
138 }
139
140 return {};
141 }
142
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;
154
155 for (auto&& [name, value] : get_enum_names<T, all_naming_conventions_tag>()) {
156 result.insert_or_assign(name, value);
157 }
158
159 return result;
160 }();
161
162 if (auto iter = mapping.find(name); iter != mapping.end()) {
163 return iter->second;
164 }
165
166 return std::nullopt;
167 }
168} // namespace essence::meta::runtime
@ value
the parser finished reading a JSON value