Repository navigation
Expand file tree
/
Copy pathMapping.mpp
More file actions
284 lines (258 loc) · 10.1 KB
/
Copy pathMapping.mpp
File metadata and controls
284 lines (258 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
export module CppUtils.Language.JSON.Mapping;
import std;
import CppUtils.String;
import CppUtils.Type.Mapping;
export namespace CppUtils::Language::JSON
{
struct Value;
using Object = std::map<std::string, Value, std::less<>>;
using Array = std::vector<Value>;
template<std::output_iterator<char> Iterator>
[[nodiscard]] inline auto escapeString(std::string_view string, Iterator out) -> Iterator
{
using namespace std::literals;
using EscapeMapping = Type::Mapping<
Type::Pair<'"', String::Hasher{R"(\")"}>,
Type::Pair<'\\', String::Hasher{R"(\)"}>,
Type::Pair<'\b', String::Hasher{R"(\b)"}>,
Type::Pair<'\f', String::Hasher{R"(\f)"}>,
Type::Pair<'\n', String::Hasher{R"(\n)"}>,
Type::Pair<'\r', String::Hasher{R"(\r)"}>,
Type::Pair<'\t', String::Hasher{R"(\t)"}>>;
*out++ = '"';
for (const auto character : string)
if (const auto escaped = EscapeMapping::toRhs(character, std::string_view{}); not std::ranges::empty(escaped))
out = std::ranges::copy(escaped, out).out;
else if (static_cast<unsigned char>(character) < 0x20)
out = std::format_to(out, R"(\u{:04x})", static_cast<int>(character));
else
*out++ = character;
*out++ = '"';
return out;
}
struct Value final
{
std::variant<std::monostate, bool, double, std::string, Array, Object> data;
constexpr Value() = default;
constexpr Value(bool value): data{value} {}
constexpr Value(double value): data{value} {}
inline Value(std::string value): data{std::move(value)} {}
inline Value(const char* value): data{std::string{value}} {}
inline Value(Array value): data{std::move(value)} {}
inline Value(Object value): data{std::move(value)} {}
[[nodiscard]] constexpr auto isNull() const -> bool { return std::holds_alternative<std::monostate>(data); }
[[nodiscard]] constexpr auto isBool() const -> bool { return std::holds_alternative<bool>(data); }
[[nodiscard]] constexpr auto isNumber() const -> bool { return std::holds_alternative<double>(data); }
[[nodiscard]] constexpr auto isString() const -> bool { return std::holds_alternative<std::string>(data); }
[[nodiscard]] constexpr auto isArray() const -> bool { return std::holds_alternative<Array>(data); }
[[nodiscard]] constexpr auto isObject() const -> bool { return std::holds_alternative<Object>(data); }
template<class T>
[[nodiscard]] constexpr auto is() const -> bool
{
return std::holds_alternative<T>(data);
}
template<class T>
[[nodiscard]] constexpr auto&& as(this auto&& self)
{
return std::get<T>(std::forward<decltype(self)>(self).data);
}
template<class T>
[[nodiscard]] inline auto getIf() const -> std::optional<std::reference_wrapper<const T>>
{
if (const auto* value = std::get_if<T>(std::addressof(data)))
return std::ref(*value);
return std::nullopt;
}
[[nodiscard]] constexpr auto operator==(const Value&) const -> bool = default;
[[nodiscard]] inline operator std::string() const
{
auto result = std::string{};
dump(std::back_inserter(result));
return result;
}
[[nodiscard]] inline auto operator[](this auto&& self [[clang::lifetimebound, msvc::lifetimebound]], std::string_view key) -> const Value&;
[[nodiscard]] inline auto operator[](this auto&& self [[clang::lifetimebound, msvc::lifetimebound]], std::size_t index) -> const Value&;
template<std::output_iterator<char> Iterator>
inline auto dump(Iterator out) const -> Iterator
{
using namespace std::literals;
return std::visit([&](const auto& value) -> Iterator {
using T = std::remove_cvref_t<decltype(value)>;
if constexpr (std::same_as<T, std::monostate>)
return std::ranges::copy("null"sv, out).out;
if constexpr (std::same_as<T, bool>)
return std::ranges::copy(value ? "true"sv : "false"sv, out).out;
if constexpr (std::same_as<T, double>)
return std::ranges::copy(String::formatValue(value), out).out;
if constexpr (std::same_as<T, std::string>)
return escapeString(value, out);
if constexpr (std::same_as<T, Array>)
{
*out++ = '[';
for (auto first = true; const auto& item : value)
{
if (not first)
*out++ = ',';
out = item.dump(out);
first = false;
}
*out++ = ']';
return out;
}
if constexpr (std::same_as<T, Object>)
{
*out++ = '{';
for (auto first = true; const auto& [key, val] : value)
{
if (not first)
*out++ = ',';
out = escapeString(key, out);
*out++ = ':';
out = val.dump(out);
first = false;
}
*out++ = '}';
return out;
}
std::unreachable();
}, data);
}
};
inline constinit const auto nullValue = Value{};
[[nodiscard]] inline auto Value::operator[](this auto&& self [[clang::lifetimebound, msvc::lifetimebound]], std::string_view key) -> const Value&
{
if (auto object = self.template getIf<Object>())
{
const auto& objectRef = object->get();
if (auto it = objectRef.find(key); it != std::ranges::end(objectRef))
return it->second;
}
return nullValue;
}
[[nodiscard]] inline auto Value::operator[](this auto&& self [[clang::lifetimebound, msvc::lifetimebound]], std::size_t index) -> const Value&
{
if (auto array = self.template getIf<Array>())
{
const auto& arrayRef = array->get();
if (index < std::ranges::size(arrayRef))
return arrayRef[index];
}
return nullValue;
}
inline auto operator<<(std::ostream& os, const Value& value) -> std::ostream&
{
return os << static_cast<std::string>(value);
}
template<class ObjectType, class Mapping>
requires (std::is_default_constructible_v<ObjectType>)
[[nodiscard]] inline auto toStruct(const Value& jsonValue) -> ObjectType
{
using JSONMapping = typename Mapping::JSONMapping;
using StructMapping = typename Mapping::StructMapping;
using FunctionMapping = typename Mapping::FunctionMapping;
auto objectValue = ObjectType{};
std::apply([&](auto&&... jsonPairs) -> void {
([&](auto&& jsonPair) -> void {
using JSONPair = std::remove_cvref_t<decltype(jsonPair)>;
constexpr auto token = JSONPair::lhs;
constexpr auto jsonKeyValue = std::get<0>(JSONPair::rhs);
auto getKeyString = [&]() -> std::string {
using KeyType = std::remove_cvref_t<decltype(jsonKeyValue)>;
if constexpr (std::convertible_to<KeyType, std::string_view>)
return static_cast<std::string>(static_cast<std::string_view>(jsonKeyValue));
else if constexpr (std::convertible_to<KeyType, std::string>)
return static_cast<std::string>(jsonKeyValue);
else
return {};
};
const auto key = getKeyString();
const auto& value = jsonValue[key];
if (not value.isNull())
{
constexpr auto memberPointer = std::get<0>(StructMapping::template toRhs<token>());
auto& member = objectValue.*memberPointer;
using MemberType = std::remove_cvref_t<decltype(member)>;
if constexpr (FunctionMapping::template containsLhs<token>())
FunctionMapping::template toSingleRhs<token>()(member, value);
else
std::visit([&](const auto& value) {
using ValueType = std::remove_cvref_t<decltype(value)>;
if constexpr (std::same_as<ValueType, std::monostate>)
return;
if constexpr (std::same_as<MemberType, bool>)
{
if constexpr (std::same_as<ValueType, bool>)
member = value;
}
else if constexpr (std::same_as<MemberType, std::string_view> or std::same_as<MemberType, std::string>)
{
if constexpr (std::same_as<ValueType, std::string>)
member = value;
}
else if constexpr (std::integral<MemberType> or std::floating_point<MemberType>)
{
if constexpr (std::same_as<ValueType, double> or std::same_as<ValueType, bool>)
member = static_cast<MemberType>(value);
}
}, value.data);
}
}(jsonPairs), ...);
}, typename JSONMapping::Values{});
return objectValue;
}
template<class ObjectType, class Mapping>
[[nodiscard]] inline auto toStructs(
const Value& jsonValue,
std::vector<ObjectType>&& objects = {}) -> std::vector<ObjectType>
{
if (jsonValue.isArray())
for (const auto& item : jsonValue.as<Array>())
objects.push_back(toStruct<ObjectType, Mapping>(item));
return std::move(objects);
}
template<class ObjectType, class Mapping>
requires (std::is_default_constructible_v<ObjectType>)
[[nodiscard]] inline auto toJSON(const ObjectType& objectValue) -> Value
{
using JSONMapping = typename Mapping::JSONMapping;
using StructMapping = typename Mapping::StructMapping;
auto object = Object{};
std::apply([&](auto&&... jsonPairs) -> void {
([&](auto&& jsonPair) -> void {
using JSONPair = std::remove_cvref_t<decltype(jsonPair)>;
constexpr auto token = JSONPair::lhs;
constexpr auto jsonKeyValue = std::get<0>(JSONPair::rhs);
auto getKeyString = [&]() -> std::string {
using KeyType = std::remove_cvref_t<decltype(jsonKeyValue)>;
if constexpr (std::convertible_to<KeyType, std::string_view>)
return static_cast<std::string>(static_cast<std::string_view>(jsonKeyValue));
else if constexpr (std::convertible_to<KeyType, std::string>)
return static_cast<std::string>(jsonKeyValue);
else
return {};
};
const auto jsonKey = getKeyString();
constexpr auto memberPointer = std::get<0>(StructMapping::template toRhs<token>());
const auto& member = objectValue.*memberPointer;
using MemberType = std::remove_cvref_t<decltype(member)>;
if constexpr (std::same_as<MemberType, bool>)
object[jsonKey] = Value{member};
else if constexpr (std::same_as<MemberType, std::string_view>)
object[jsonKey] = Value{std::string{member}};
else if constexpr (std::same_as<MemberType, std::string>)
object[jsonKey] = Value{member};
else if constexpr (std::integral<MemberType> or std::floating_point<MemberType>)
object[jsonKey] = Value{static_cast<double>(member)};
}(jsonPairs), ...);
}, typename JSONMapping::Values{});
return Value{object};
}
}
template<>
struct std::formatter<CppUtils::Language::JSON::Value>: std::formatter<std::string>
{
inline auto format(const CppUtils::Language::JSON::Value& value, std::format_context& context) const
{
return value.dump(context.out());
}
};