Mercurial > hg > orthanc-stone
comparison Deprecated/Resources/CodeGeneration/template.in.h.j2 @ 1401:f6a2d46d2b76
moved CodeGeneration into Deprecated
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Wed, 29 Apr 2020 20:48:18 +0200 |
parents | Resources/CodeGeneration/template.in.h.j2@8a0a62189f46 |
children |
comparison
equal
deleted
inserted
replaced
1400:419d0320c344 | 1401:f6a2d46d2b76 |
---|---|
1 /* | |
2 1 2 3 4 5 6 7 | |
3 12345678901234567890123456789012345678901234567890123456789012345678901234567890 | |
4 | |
5 Generated on {{currentDatetime}} by stonegentool | |
6 | |
7 */ | |
8 #pragma once | |
9 | |
10 #include <exception> | |
11 #include <iostream> | |
12 #include <string> | |
13 #include <sstream> | |
14 #include <assert.h> | |
15 #include <memory> | |
16 #include <set> | |
17 #include <json/json.h> | |
18 | |
19 //#define STONEGEN_NO_CPP11 1 | |
20 | |
21 #ifdef STONEGEN_NO_CPP11 | |
22 #define StoneSmartPtr std::unique_ptr | |
23 #else | |
24 #define StoneSmartPtr std::unique_ptr | |
25 #endif | |
26 | |
27 namespace {{rootName}} | |
28 { | |
29 /** Throws in case of problem */ | |
30 inline void _StoneDeserializeValue(int32_t& destValue, const Json::Value& jsonValue) | |
31 { | |
32 if (!jsonValue.isNull()) | |
33 { | |
34 destValue = jsonValue.asInt(); | |
35 } | |
36 } | |
37 | |
38 inline Json::Value _StoneSerializeValue(int32_t value) | |
39 { | |
40 Json::Value result(value); | |
41 return result; | |
42 } | |
43 | |
44 inline void _StoneDeserializeValue(int64_t& destValue, const Json::Value& jsonValue) | |
45 { | |
46 if (!jsonValue.isNull()) | |
47 { | |
48 destValue = jsonValue.asInt64(); | |
49 } | |
50 } | |
51 | |
52 inline Json::Value _StoneSerializeValue(int64_t value) | |
53 { | |
54 Json::Value result(static_cast<Json::Value::Int64>(value)); | |
55 return result; | |
56 } | |
57 | |
58 inline void _StoneDeserializeValue(uint32_t& destValue, const Json::Value& jsonValue) | |
59 { | |
60 if (!jsonValue.isNull()) | |
61 { | |
62 destValue = jsonValue.asUInt(); | |
63 } | |
64 } | |
65 | |
66 inline Json::Value _StoneSerializeValue(uint32_t value) | |
67 { | |
68 Json::Value result(value); | |
69 return result; | |
70 } | |
71 | |
72 inline void _StoneDeserializeValue(uint64_t& destValue, const Json::Value& jsonValue) | |
73 { | |
74 if (!jsonValue.isNull()) | |
75 { | |
76 destValue = jsonValue.asUInt64(); | |
77 } | |
78 } | |
79 | |
80 inline Json::Value _StoneSerializeValue(uint64_t value) | |
81 { | |
82 Json::Value result(static_cast<Json::Value::UInt64>(value)); | |
83 return result; | |
84 } | |
85 | |
86 inline void _StoneDeserializeValue(Json::Value& destValue, const Json::Value& jsonValue) | |
87 { | |
88 destValue = jsonValue; | |
89 } | |
90 | |
91 inline Json::Value _StoneSerializeValue(Json::Value value) | |
92 { | |
93 return value; | |
94 } | |
95 | |
96 /** Throws in case of problem */ | |
97 inline void _StoneDeserializeValue(double& destValue, const Json::Value& jsonValue) | |
98 { | |
99 if (!jsonValue.isNull()) | |
100 { | |
101 destValue = jsonValue.asDouble(); | |
102 } | |
103 } | |
104 | |
105 inline Json::Value _StoneSerializeValue(double value) | |
106 { | |
107 Json::Value result(value); | |
108 return result; | |
109 } | |
110 | |
111 /** Throws in case of problem */ | |
112 inline void _StoneDeserializeValue(float& destValue, const Json::Value& jsonValue) | |
113 { | |
114 if (!jsonValue.isNull()) | |
115 { | |
116 destValue = jsonValue.asFloat(); | |
117 } | |
118 } | |
119 | |
120 inline Json::Value _StoneSerializeValue(float value) | |
121 { | |
122 Json::Value result(value); | |
123 return result; | |
124 } | |
125 | |
126 /** Throws in case of problem */ | |
127 inline void _StoneDeserializeValue(bool& destValue, const Json::Value& jsonValue) | |
128 { | |
129 if (!jsonValue.isNull()) | |
130 { | |
131 destValue = jsonValue.asBool(); | |
132 } | |
133 } | |
134 | |
135 inline Json::Value _StoneSerializeValue(bool value) | |
136 { | |
137 Json::Value result(value); | |
138 return result; | |
139 } | |
140 | |
141 /** Throws in case of problem */ | |
142 inline void _StoneDeserializeValue( | |
143 std::string& destValue | |
144 , const Json::Value& jsonValue) | |
145 { | |
146 if (!jsonValue.isNull()) | |
147 { | |
148 destValue = jsonValue.asString(); | |
149 } | |
150 } | |
151 | |
152 inline Json::Value _StoneSerializeValue(const std::string& value) | |
153 { | |
154 // the following is better than | |
155 Json::Value result(value.data(),value.data()+value.size()); | |
156 return result; | |
157 } | |
158 | |
159 inline std::string MakeIndent(size_t indent) | |
160 { | |
161 char* txt = reinterpret_cast<char*>(malloc(indent+1)); // NO EXCEPTION BELOW!!!!!!!!!!!! | |
162 for(size_t i = 0; i < indent; ++i) | |
163 txt[i] = ' '; | |
164 txt[indent] = 0; | |
165 std::string retVal(txt); | |
166 free(txt); // NO EXCEPTION ABOVE !!!!!!!!!! | |
167 return retVal; | |
168 } | |
169 | |
170 // generic dumper | |
171 template<typename T> | |
172 std::ostream& StoneDumpValue(std::ostream& out, const T& value, size_t indent) | |
173 { | |
174 out << MakeIndent(indent) << value; | |
175 return out; | |
176 } | |
177 | |
178 // string dumper | |
179 inline std::ostream& StoneDumpValue(std::ostream& out, const std::string& value, size_t indent) | |
180 { | |
181 out << MakeIndent(indent) << "\"" << value << "\""; | |
182 return out; | |
183 } | |
184 | |
185 inline std::string ToString(const std::string& str) | |
186 { | |
187 return str; | |
188 } | |
189 | |
190 inline void FromString(std::string& value, std::string strValue) | |
191 { | |
192 value = strValue; | |
193 } | |
194 | |
195 | |
196 /** Throws in case of problem */ | |
197 template<typename TK, typename TV> | |
198 void _StoneDeserializeValue( | |
199 std::map<TK, TV>& destValue, const Json::Value& jsonValue) | |
200 { | |
201 if (!jsonValue.isNull()) | |
202 { | |
203 destValue.clear(); | |
204 for ( | |
205 Json::Value::const_iterator itr = jsonValue.begin(); | |
206 itr != jsonValue.end(); | |
207 itr++) | |
208 { | |
209 std::string strKey; | |
210 _StoneDeserializeValue(strKey, itr.key()); | |
211 TK key; | |
212 FromString(key, strKey); // if you have a compile error here, it means that your type is not suitable to be the key of a map (or you should overwrite the FromString/ToString in template.in.h.j2) | |
213 | |
214 TV innerDestValue; | |
215 _StoneDeserializeValue(innerDestValue, *itr); | |
216 | |
217 destValue[key] = innerDestValue; | |
218 } | |
219 } | |
220 } | |
221 | |
222 template<typename TK, typename TV> | |
223 Json::Value _StoneSerializeValue(const std::map<TK, TV>& value) | |
224 { | |
225 Json::Value result(Json::objectValue); | |
226 | |
227 for (typename std::map<TK, TV>::const_iterator it = value.cbegin(); | |
228 it != value.cend(); ++it) | |
229 { | |
230 // it->first it->second | |
231 result[ToString(it->first)] = _StoneSerializeValue(it->second); | |
232 } | |
233 return result; | |
234 } | |
235 | |
236 template<typename TK, typename TV> | |
237 std::ostream& StoneDumpValue(std::ostream& out, const std::map<TK, TV>& value, size_t indent) | |
238 { | |
239 out << MakeIndent(indent) << "{\n"; | |
240 for (typename std::map<TK, TV>::const_iterator it = value.cbegin(); | |
241 it != value.cend(); ++it) | |
242 { | |
243 out << MakeIndent(indent+2) << "\"" << it->first << "\" : "; | |
244 StoneDumpValue(out, it->second, indent+2); | |
245 out << ", \n"; | |
246 } | |
247 out << MakeIndent(indent) << "}\n"; | |
248 return out; | |
249 } | |
250 | |
251 /** Throws in case of problem */ | |
252 template<typename T> | |
253 void _StoneDeserializeValue( | |
254 std::vector<T>& destValue, const Json::Value& jsonValue) | |
255 { | |
256 if (!jsonValue.isNull() && jsonValue.isArray()) | |
257 { | |
258 destValue.clear(); | |
259 destValue.reserve(jsonValue.size()); | |
260 for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++) | |
261 { | |
262 T innerDestValue; | |
263 _StoneDeserializeValue(innerDestValue, jsonValue[i]); | |
264 destValue.push_back(innerDestValue); | |
265 } | |
266 } | |
267 } | |
268 | |
269 template<typename T> | |
270 Json::Value _StoneSerializeValue(const std::vector<T>& value) | |
271 { | |
272 Json::Value result(Json::arrayValue); | |
273 for (size_t i = 0; i < value.size(); ++i) | |
274 { | |
275 result.append(_StoneSerializeValue(value[i])); | |
276 } | |
277 return result; | |
278 } | |
279 | |
280 template<typename T> | |
281 std::ostream& StoneDumpValue(std::ostream& out, const std::vector<T>& value, size_t indent) | |
282 { | |
283 out << MakeIndent(indent) << "[\n"; | |
284 for (size_t i = 0; i < value.size(); ++i) | |
285 { | |
286 StoneDumpValue(out, value[i], indent+2); | |
287 out << ", \n"; | |
288 } | |
289 out << MakeIndent(indent) << "]\n"; | |
290 return out; | |
291 } | |
292 | |
293 /** Throws in case of problem */ | |
294 template<typename T> | |
295 void _StoneDeserializeValue( | |
296 std::set<T>& destValue, const Json::Value& jsonValue) | |
297 { | |
298 if (!jsonValue.isNull() && jsonValue.isArray()) | |
299 { | |
300 destValue.clear(); | |
301 for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++) | |
302 { | |
303 T innerDestValue; | |
304 _StoneDeserializeValue(innerDestValue, jsonValue[i]); | |
305 destValue.insert(innerDestValue); | |
306 } | |
307 } | |
308 } | |
309 | |
310 template<typename T> | |
311 Json::Value _StoneSerializeValue(const std::set<T>& value) | |
312 { | |
313 Json::Value result(Json::arrayValue); | |
314 for (typename std::set<T>::const_iterator it = value.begin(); it != value.end(); ++it) | |
315 { | |
316 result.append(_StoneSerializeValue(*it)); | |
317 } | |
318 return result; | |
319 } | |
320 | |
321 template<typename T> | |
322 std::ostream& StoneDumpValue(std::ostream& out, const std::set<T>& value, size_t indent) | |
323 { | |
324 out << MakeIndent(indent) << "[\n"; | |
325 for (typename std::set<T>::const_iterator it = value.begin(); it != value.end(); ++it) | |
326 { | |
327 StoneDumpValue(out, *it, indent+2); | |
328 out << ", \n"; | |
329 } | |
330 out << MakeIndent(indent) << "]\n"; | |
331 return out; | |
332 } | |
333 | |
334 inline void StoneCheckSerializedValueTypeGeneric(const Json::Value& value) | |
335 { | |
336 if ((!value.isMember("type")) || (!value["type"].isString())) | |
337 { | |
338 std::stringstream ss; | |
339 ss << "Cannot deserialize value ('type' key invalid)"; | |
340 throw std::runtime_error(ss.str()); | |
341 } | |
342 } | |
343 | |
344 inline void StoneCheckSerializedValueType( | |
345 const Json::Value& value, std::string typeStr) | |
346 { | |
347 StoneCheckSerializedValueTypeGeneric(value); | |
348 | |
349 std::string actTypeStr = value["type"].asString(); | |
350 if (actTypeStr != typeStr) | |
351 { | |
352 std::stringstream ss; | |
353 ss << "Cannot deserialize type" << actTypeStr | |
354 << "into " << typeStr; | |
355 throw std::runtime_error(ss.str()); | |
356 } | |
357 } | |
358 | |
359 // end of generic methods | |
360 | |
361 // end of generic methods | |
362 {% for enum in enums%} | |
363 enum {{enum['name']}} { | |
364 {% for key in enum['fields']%} {{enum['name']}}_{{key}}, | |
365 {%endfor%} }; | |
366 | |
367 inline std::string ToString(const {{enum['name']}}& value) | |
368 { | |
369 {% for key in enum['fields']%} if( value == {{enum['name']}}_{{key}}) | |
370 { | |
371 return std::string("{{key}}"); | |
372 } | |
373 {%endfor%} std::stringstream ss; | |
374 ss << "Value \"" << value << "\" cannot be converted to {{enum['name']}}. Possible values are: " | |
375 {% for key in enum['fields']%} << " {{key}} = " << static_cast<int64_t>({{enum['name']}}_{{key}}) << ", " | |
376 {% endfor %} << std::endl; | |
377 std::string msg = ss.str(); | |
378 throw std::runtime_error(msg); | |
379 } | |
380 | |
381 inline void FromString({{enum['name']}}& value, std::string strValue) | |
382 { | |
383 {% for key in enum['fields']%} if( strValue == std::string("{{key}}") ) | |
384 { | |
385 value = {{enum['name']}}_{{key}}; | |
386 return; | |
387 } | |
388 {%endfor%} | |
389 std::stringstream ss; | |
390 ss << "String \"" << strValue << "\" cannot be converted to {{enum['name']}}. Possible values are: {% for key in enum['fields']%}{{key}} {% endfor %}"; | |
391 std::string msg = ss.str(); | |
392 throw std::runtime_error(msg); | |
393 } | |
394 | |
395 | |
396 inline void _StoneDeserializeValue( | |
397 {{enum['name']}}& destValue, const Json::Value& jsonValue) | |
398 { | |
399 if (!jsonValue.isNull()) | |
400 { | |
401 FromString(destValue, jsonValue.asString()); | |
402 } | |
403 } | |
404 | |
405 inline Json::Value _StoneSerializeValue(const {{enum['name']}}& value) | |
406 { | |
407 std::string strValue = ToString(value); | |
408 return Json::Value(strValue); | |
409 } | |
410 | |
411 inline std::ostream& StoneDumpValue(std::ostream& out, const {{enum['name']}}& value, size_t indent = 0) | |
412 { | |
413 {% for key in enum['fields']%} if( value == {{enum['name']}}_{{key}}) | |
414 { | |
415 out << MakeIndent(indent) << "{{key}}"; | |
416 } | |
417 {%endfor%} return out; | |
418 } | |
419 | |
420 {%endfor%} | |
421 {% for struct in structs%} | |
422 #ifdef _MSC_VER | |
423 #pragma region {{struct['name']}} | |
424 #endif //_MSC_VER | |
425 | |
426 struct {{struct['name']}} | |
427 { | |
428 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields'] %} {{CanonToCpp(struct['fields'][key]['type'])}} {{key}}; | |
429 {% endfor %}{% endif %}{% endif %} | |
430 {{struct['name']}}({% if struct %}{% if struct['fields'] %}{% for key in struct['fields'] %}{{CanonToCpp(struct['fields'][key]['type'])}} {{key}} = {% if struct['fields'][key]['defaultValue'] %}{{DefaultValueToCpp(rootName,enums,struct['fields'][key])}} {%else%} {{CanonToCpp(struct['fields'][key]['type'])}}() {%endif%} {{ ", " if not loop.last }}{% endfor %}{% endif %}{% endif %}) | |
431 { | |
432 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} this->{{key}} = {{key}}; | |
433 {% endfor %}{% endif %}{% endif %} } | |
434 }; | |
435 | |
436 inline void _StoneDeserializeValue({{struct['name']}}& destValue, const Json::Value& value) | |
437 { | |
438 if (!value.isNull()) | |
439 { | |
440 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} _StoneDeserializeValue(destValue.{{key}}, value["{{key}}"]); | |
441 {% endfor %}{% endif %}{% endif %} } | |
442 } | |
443 | |
444 inline Json::Value _StoneSerializeValue(const {{struct['name']}}& value) | |
445 { | |
446 Json::Value result(Json::objectValue); | |
447 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} result["{{key}}"] = _StoneSerializeValue(value.{{key}}); | |
448 {% endfor %}{% endif %}{% endif %} | |
449 return result; | |
450 } | |
451 | |
452 inline std::ostream& StoneDumpValue(std::ostream& out, const {{struct['name']}}& value, size_t indent = 0) | |
453 { | |
454 out << MakeIndent(indent) << "{\n"; | |
455 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} out << MakeIndent(indent+2) << "{{key}}: "; | |
456 StoneDumpValue(out, value.{{key}},indent+2); | |
457 out << ", \n"; | |
458 {% endfor %}{% endif %}{% endif %} | |
459 out << MakeIndent(indent) << "}"; | |
460 return out; | |
461 } | |
462 | |
463 inline void StoneDeserialize({{struct['name']}}& destValue, const Json::Value& value) | |
464 { | |
465 StoneCheckSerializedValueType(value, "{{rootName}}.{{struct['name']}}"); | |
466 _StoneDeserializeValue(destValue, value["value"]); | |
467 } | |
468 | |
469 inline Json::Value StoneSerializeToJson(const {{struct['name']}}& value) | |
470 { | |
471 Json::Value result(Json::objectValue); | |
472 result["type"] = "{{rootName}}.{{struct['name']}}"; | |
473 result["value"] = _StoneSerializeValue(value); | |
474 return result; | |
475 } | |
476 | |
477 inline std::string StoneSerialize(const {{struct['name']}}& value) | |
478 { | |
479 Json::Value resultJson = StoneSerializeToJson(value); | |
480 std::string resultStr = resultJson.toStyledString(); | |
481 return resultStr; | |
482 } | |
483 | |
484 #ifdef _MSC_VER | |
485 #pragma endregion {{struct['name']}} | |
486 #endif //_MSC_VER | |
487 {% endfor %} | |
488 #ifdef _MSC_VER | |
489 #pragma region Dispatching code | |
490 #endif //_MSC_VER | |
491 | |
492 class IHandler | |
493 { | |
494 public: | |
495 {% for struct in structs%}{% if struct['__meta__'].handleInCpp %} virtual bool Handle(const {{struct['name']}}& value) = 0; | |
496 {% endif %}{% endfor %} }; | |
497 | |
498 /** Service function for StoneDispatchToHandler */ | |
499 inline bool StoneDispatchJsonToHandler( | |
500 const Json::Value& jsonValue, IHandler* handler) | |
501 { | |
502 StoneCheckSerializedValueTypeGeneric(jsonValue); | |
503 std::string type = jsonValue["type"].asString(); | |
504 if (type == "") | |
505 { | |
506 // this should never ever happen | |
507 throw std::runtime_error("Caught empty type while dispatching"); | |
508 } | |
509 {% for struct in structs%}{% if struct['__meta__'].handleInCpp %} else if (type == "{{rootName}}.{{struct['name']}}") | |
510 { | |
511 {{struct['name']}} value; | |
512 _StoneDeserializeValue(value, jsonValue["value"]); | |
513 return handler->Handle(value); | |
514 } | |
515 {% endif %}{% endfor %} else | |
516 { | |
517 return false; | |
518 } | |
519 } | |
520 | |
521 /** Takes a serialized type and passes this to the handler */ | |
522 inline bool StoneDispatchToHandler(std::string strValue, IHandler* handler) | |
523 { | |
524 Json::Value readValue; | |
525 | |
526 Json::CharReaderBuilder builder; | |
527 Json::CharReader* reader = builder.newCharReader(); | |
528 | |
529 StoneSmartPtr<Json::CharReader> ptr(reader); | |
530 | |
531 std::string errors; | |
532 | |
533 bool ok = reader->parse( | |
534 strValue.c_str(), | |
535 strValue.c_str() + strValue.size(), | |
536 &readValue, | |
537 &errors | |
538 ); | |
539 if (!ok) | |
540 { | |
541 std::stringstream ss; | |
542 ss << "Jsoncpp parsing error: " << errors; | |
543 throw std::runtime_error(ss.str()); | |
544 } | |
545 return StoneDispatchJsonToHandler(readValue, handler); | |
546 } | |
547 | |
548 #ifdef _MSC_VER | |
549 #pragma endregion Dispatching code | |
550 #endif //_MSC_VER | |
551 } |