-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreflection.cpp
More file actions
307 lines (241 loc) · 7.97 KB
/
reflection.cpp
File metadata and controls
307 lines (241 loc) · 7.97 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// This file is part of the "edge" library, available at https://github.com/adigostin/edge
// Copyright (c) 2011-2020 Adi Gostin, distributed under Apache License v2.0.
#include "reflection.h"
#include <ctype.h>
namespace edge
{
extern const property_group misc_group = { 0, "Misc" };
//static
std::string string_convert_exception::make_string (std::string_view str, const char* type_name)
{
auto res = std::string("Cannot convert \"");
res += str;
res += "\" to type \"";
res += type_name;
res += "\".";
return res;
}
string_convert_exception::string_convert_exception (const char* str)
: _message(str)
{ }
string_convert_exception::string_convert_exception (std::string_view str, const char* type_name)
: _message(make_string(str, type_name))
{ }
// ========================================================================
const char bool_property_traits::type_name[] = "bool";
void bool_property_traits::to_string (value_t from, std::string& to)
{
to = from ? "True" : "False";
}
void bool_property_traits::from_string (std::string_view from, value_t& to)
{
if ((from.size() == 4) && (tolower(from[0]) == 't') && (tolower(from[1]) == 'r') && (tolower(from[2]) == 'u') && (tolower(from[3]) == 'e'))
{
to = true;
return;
}
if ((from.size() == 5) && (tolower(from[0]) == 'f') && (tolower(from[1]) == 'a') && (tolower(from[2]) == 'l') && (tolower(from[3]) == 's') && (tolower(from[4]) == 'e'))
{
to = false;
return;
}
throw string_convert_exception(from, type_name);
}
// ========================================================================
extern const char int32_type_name[] = "int32";
template<> void int32_property_traits::to_string (value_t from, std::string& to)
{
char buffer[16];
#ifdef _MSC_VER
sprintf_s (buffer, "%d", from);
#else
sprintf (buffer, "%d", from);
#endif
to = buffer;
}
template<> void int32_property_traits::from_string (std::string_view from, int32_t& to)
{
if (from.empty())
throw string_convert_exception(from, type_name);
char* endPtr;
long value = strtol (from.data(), &endPtr, 10);
if (endPtr != from.data() + from.size())
throw string_convert_exception(from, type_name);
to = value;
}
template<> void int32_property_traits::serialize (value_t from, out_stream_i* to)
{
assert(false); // not implemented
}
template<> void int32_property_traits::deserialize (binary_reader& from, value_t& to)
{
assert(false); // not implemented
}
// ========================================================================
extern const char uint32_type_name[] = "uint32";
template<> void uint32_property_traits::to_string (value_t from, std::string& to)
{
char buffer[16];
#ifdef _MSC_VER
sprintf_s (buffer, "%u", from);
#else
sprintf (buffer, "%u", from);
#endif
to = buffer;
}
template<> void uint32_property_traits::from_string (std::string_view from, uint32_t& to)
{
if (from.empty())
throw string_convert_exception(from, type_name);
char* endPtr;
unsigned long value = std::strtoul (from.data(), &endPtr, 10);
if (endPtr != from.data() + from.size())
throw string_convert_exception(from, type_name);
to = value;
}
template<> void uint32_property_traits::serialize (value_t from, out_stream_i* to)
{
assert(false); // not implemented
}
template<> void uint32_property_traits::deserialize (binary_reader& from, value_t& to)
{
assert(false); // not implemented
}
// ========================================================================
extern const char uint64_type_name[] = "uint64";
template<> void uint64_property_traits::to_string (value_t from, std::string& to)
{
char buffer[32];
#ifdef _MSC_VER
sprintf_s (buffer, "%llu", from);
#else
sprintf (buffer, "%llu", from);
#endif
to = buffer;
}
template<> void uint64_property_traits::from_string (std::string_view from, uint64_t& to)
{
if (from.empty())
throw string_convert_exception(from, type_name);
char* endPtr;
unsigned long long value = std::strtoull (from.data(), &endPtr, 10);
if (endPtr != from.data() + from.size())
throw string_convert_exception(from, type_name);
to = value;
}
template<> void uint64_property_traits::serialize (value_t from, out_stream_i* to)
{
assert(false); // not implemented
}
template<> void uint64_property_traits::deserialize (binary_reader& from, value_t& to)
{
assert(false); // not implemented
}
// ========================================================================
extern const char size_t_type_name[] = "size_t";
template<> void size_t_property_traits::to_string (value_t from, std::string& to)
{
uint32_property_traits::to_string((uint32_t)from, to);
}
template<> void size_t_property_traits::from_string (std::string_view from, size_t&to)
{
uint32_t val;
uint32_property_traits::from_string (from, val);
to = val;
}
template<> void size_t_property_traits::serialize (value_t from, out_stream_i* to)
{
assert(false); // not implemented
}
template<> void size_t_property_traits::deserialize (binary_reader& from, value_t& to)
{
assert(false); // not implemented
}
// ========================================================================
extern const char float_type_name[] = "float";
template<> void float_property_traits::to_string (value_t from, std::string& to)
{
char buffer[32];
#ifdef _MSC_VER
sprintf_s (buffer, "%f", from);
#else
sprintf (buffer, "%f", from);
#endif
to = buffer;
}
template<> void float_property_traits::from_string (std::string_view from, float& to)
{
if (from.empty())
throw string_convert_exception(from, type_name);
char* end_ptr;
float value = strtof (from.data(), &end_ptr);
if (end_ptr != from.data() + from.size())
throw string_convert_exception(from, type_name);
to = value;
}
template<> void float_property_traits::serialize (value_t from, out_stream_i* to)
{
assert(false); // not implemented
}
template<> void float_property_traits::deserialize (binary_reader& from, value_t& to)
{
assert(false); // not implemented
}
// ========================================================================
const char backed_string_property_traits::type_name[] = "backed_string";
void backed_string_property_traits::serialize (value_t from, out_stream_i* to)
{
if (from.size() < 254)
{
to->write((uint8_t)from.size());
}
else if (from.size() < 65536)
{
to->write((uint8_t)254);
to->write ((uint8_t)(from.size()));
to->write ((uint8_t)(from.size() >> 8));
}
else
{
to->write((uint8_t)255);
to->write ((uint8_t)(from.size()));
to->write ((uint8_t)(from.size() >> 8));
to->write ((uint8_t)(from.size() >> 16));
to->write ((uint8_t)(from.size() >> 24));
}
to->write (from.data(), from.size());
}
void backed_string_property_traits::deserialize (binary_reader& from, value_t& to)
{
assert (from.ptr + 1 <= from.end);
size_t len = *from.ptr++;
if (len == 254)
{
assert (from.ptr + 2 <= from.end);
len = *from.ptr++;
len |= (*from.ptr++ << 8);
}
else if (len == 255)
{
assert (from.ptr + 4 <= from.end);
len = *from.ptr++;
len |= (*from.ptr++ << 8);
len |= (*from.ptr++ << 16);
len |= (*from.ptr++ << 24);
}
assert (from.ptr + len <= from.end);
to = std::string_view((const char*)from.ptr, len);
from.ptr += len;
}
// ========================================================================
void temp_string_property_traits::serialize (value_t from, out_stream_i* to)
{
assert(false); // not implemented
}
void temp_string_property_traits::deserialize (binary_reader& from, value_t& to)
{
assert(false); // not implemented
}
// ========================================================================
const char unknown_enum_value_str[] = "(unknown)";
}