Mercurial > hg > orthanc-stone
comparison Resources/CodeGeneration/template.in.h.j2 @ 628:84af39146e76 am-dev
CodeGeneration: support default values
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Wed, 08 May 2019 16:32:57 +0200 |
parents | 8432926e9db9 |
children | 5dd496343fad |
comparison
equal
deleted
inserted
replaced
627:b7fd0471281c | 628:84af39146e76 |
---|---|
27 namespace {{rootName}} | 27 namespace {{rootName}} |
28 { | 28 { |
29 /** Throws in case of problem */ | 29 /** Throws in case of problem */ |
30 inline void _StoneDeserializeValue(int32_t& destValue, const Json::Value& jsonValue) | 30 inline void _StoneDeserializeValue(int32_t& destValue, const Json::Value& jsonValue) |
31 { | 31 { |
32 destValue = jsonValue.asInt(); | 32 if (!jsonValue.isNull()) |
33 { | |
34 destValue = jsonValue.asInt(); | |
35 } | |
33 } | 36 } |
34 | 37 |
35 inline Json::Value _StoneSerializeValue(int32_t value) | 38 inline Json::Value _StoneSerializeValue(int32_t value) |
36 { | 39 { |
37 Json::Value result(value); | 40 Json::Value result(value); |
38 return result; | 41 return result; |
39 } | 42 } |
40 | 43 |
41 inline void _StoneDeserializeValue(int64_t& destValue, const Json::Value& jsonValue) | 44 inline void _StoneDeserializeValue(int64_t& destValue, const Json::Value& jsonValue) |
42 { | 45 { |
43 destValue = jsonValue.asInt64(); | 46 if (!jsonValue.isNull()) |
47 { | |
48 destValue = jsonValue.asInt64(); | |
49 } | |
44 } | 50 } |
45 | 51 |
46 inline Json::Value _StoneSerializeValue(int64_t value) | 52 inline Json::Value _StoneSerializeValue(int64_t value) |
47 { | 53 { |
48 Json::Value result(value); | 54 Json::Value result(value); |
49 return result; | 55 return result; |
50 } | 56 } |
51 | 57 |
52 inline void _StoneDeserializeValue(uint32_t& destValue, const Json::Value& jsonValue) | 58 inline void _StoneDeserializeValue(uint32_t& destValue, const Json::Value& jsonValue) |
53 { | 59 { |
54 destValue = jsonValue.asUInt(); | 60 if (!jsonValue.isNull()) |
61 { | |
62 destValue = jsonValue.asUInt(); | |
63 } | |
55 } | 64 } |
56 | 65 |
57 inline Json::Value _StoneSerializeValue(uint32_t value) | 66 inline Json::Value _StoneSerializeValue(uint32_t value) |
58 { | 67 { |
59 Json::Value result(value); | 68 Json::Value result(value); |
60 return result; | 69 return result; |
61 } | 70 } |
62 | 71 |
63 inline void _StoneDeserializeValue(uint64_t& destValue, const Json::Value& jsonValue) | 72 inline void _StoneDeserializeValue(uint64_t& destValue, const Json::Value& jsonValue) |
64 { | 73 { |
65 destValue = jsonValue.asUInt64(); | 74 if (!jsonValue.isNull()) |
75 { | |
76 destValue = jsonValue.asUInt64(); | |
77 } | |
66 } | 78 } |
67 | 79 |
68 inline Json::Value _StoneSerializeValue(uint64_t value) | 80 inline Json::Value _StoneSerializeValue(uint64_t value) |
69 { | 81 { |
70 Json::Value result(value); | 82 Json::Value result(value); |
82 } | 94 } |
83 | 95 |
84 /** Throws in case of problem */ | 96 /** Throws in case of problem */ |
85 inline void _StoneDeserializeValue(double& destValue, const Json::Value& jsonValue) | 97 inline void _StoneDeserializeValue(double& destValue, const Json::Value& jsonValue) |
86 { | 98 { |
87 destValue = jsonValue.asDouble(); | 99 if (!jsonValue.isNull()) |
100 { | |
101 destValue = jsonValue.asDouble(); | |
102 } | |
88 } | 103 } |
89 | 104 |
90 inline Json::Value _StoneSerializeValue(double value) | 105 inline Json::Value _StoneSerializeValue(double value) |
91 { | 106 { |
92 Json::Value result(value); | 107 Json::Value result(value); |
94 } | 109 } |
95 | 110 |
96 /** Throws in case of problem */ | 111 /** Throws in case of problem */ |
97 inline void _StoneDeserializeValue(bool& destValue, const Json::Value& jsonValue) | 112 inline void _StoneDeserializeValue(bool& destValue, const Json::Value& jsonValue) |
98 { | 113 { |
99 destValue = jsonValue.asBool(); | 114 if (!jsonValue.isNull()) |
115 { | |
116 destValue = jsonValue.asBool(); | |
117 } | |
100 } | 118 } |
101 | 119 |
102 inline Json::Value _StoneSerializeValue(bool value) | 120 inline Json::Value _StoneSerializeValue(bool value) |
103 { | 121 { |
104 Json::Value result(value); | 122 Json::Value result(value); |
108 /** Throws in case of problem */ | 126 /** Throws in case of problem */ |
109 inline void _StoneDeserializeValue( | 127 inline void _StoneDeserializeValue( |
110 std::string& destValue | 128 std::string& destValue |
111 , const Json::Value& jsonValue) | 129 , const Json::Value& jsonValue) |
112 { | 130 { |
113 destValue = jsonValue.asString(); | 131 if (!jsonValue.isNull()) |
132 { | |
133 destValue = jsonValue.asString(); | |
134 } | |
114 } | 135 } |
115 | 136 |
116 inline Json::Value _StoneSerializeValue(const std::string& value) | 137 inline Json::Value _StoneSerializeValue(const std::string& value) |
117 { | 138 { |
118 // the following is better than | 139 // the following is better than |
149 /** Throws in case of problem */ | 170 /** Throws in case of problem */ |
150 template<typename T> | 171 template<typename T> |
151 void _StoneDeserializeValue( | 172 void _StoneDeserializeValue( |
152 std::map<std::string, T>& destValue, const Json::Value& jsonValue) | 173 std::map<std::string, T>& destValue, const Json::Value& jsonValue) |
153 { | 174 { |
154 destValue.clear(); | 175 if (!jsonValue.isNull()) |
155 for ( | 176 { |
156 Json::Value::const_iterator itr = jsonValue.begin(); | 177 destValue.clear(); |
157 itr != jsonValue.end(); | 178 for ( |
158 itr++) | 179 Json::Value::const_iterator itr = jsonValue.begin(); |
159 { | 180 itr != jsonValue.end(); |
160 std::string key; | 181 itr++) |
161 _StoneDeserializeValue(key, itr.key()); | 182 { |
162 | 183 std::string key; |
163 T innerDestValue; | 184 _StoneDeserializeValue(key, itr.key()); |
164 _StoneDeserializeValue(innerDestValue, *itr); | 185 |
165 | 186 T innerDestValue; |
166 destValue[key] = innerDestValue; | 187 _StoneDeserializeValue(innerDestValue, *itr); |
188 | |
189 destValue[key] = innerDestValue; | |
190 } | |
167 } | 191 } |
168 } | 192 } |
169 | 193 |
170 template<typename T> | 194 template<typename T> |
171 Json::Value _StoneSerializeValue(const std::map<std::string,T>& value) | 195 Json::Value _StoneSerializeValue(const std::map<std::string,T>& value) |
198 /** Throws in case of problem */ | 222 /** Throws in case of problem */ |
199 template<typename T> | 223 template<typename T> |
200 void _StoneDeserializeValue( | 224 void _StoneDeserializeValue( |
201 std::vector<T>& destValue, const Json::Value& jsonValue) | 225 std::vector<T>& destValue, const Json::Value& jsonValue) |
202 { | 226 { |
203 destValue.clear(); | 227 if (!jsonValue.isNull()) |
204 destValue.reserve(jsonValue.size()); | 228 { |
205 for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++) | 229 destValue.clear(); |
206 { | 230 destValue.reserve(jsonValue.size()); |
207 T innerDestValue; | 231 for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++) |
208 _StoneDeserializeValue(innerDestValue, jsonValue[i]); | 232 { |
209 destValue.push_back(innerDestValue); | 233 T innerDestValue; |
234 _StoneDeserializeValue(innerDestValue, jsonValue[i]); | |
235 destValue.push_back(innerDestValue); | |
236 } | |
210 } | 237 } |
211 } | 238 } |
212 | 239 |
213 template<typename T> | 240 template<typename T> |
214 Json::Value _StoneSerializeValue(const std::vector<T>& value) | 241 Json::Value _StoneSerializeValue(const std::vector<T>& value) |
236 /** Throws in case of problem */ | 263 /** Throws in case of problem */ |
237 template<typename T> | 264 template<typename T> |
238 void _StoneDeserializeValue( | 265 void _StoneDeserializeValue( |
239 std::set<T>& destValue, const Json::Value& jsonValue) | 266 std::set<T>& destValue, const Json::Value& jsonValue) |
240 { | 267 { |
241 destValue.clear(); | 268 if (!jsonValue.isNull()) |
242 for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++) | 269 { |
243 { | 270 destValue.clear(); |
244 T innerDestValue; | 271 for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++) |
245 _StoneDeserializeValue(innerDestValue, jsonValue[i]); | 272 { |
246 destValue.insert(innerDestValue); | 273 T innerDestValue; |
274 _StoneDeserializeValue(innerDestValue, jsonValue[i]); | |
275 destValue.insert(innerDestValue); | |
276 } | |
247 } | 277 } |
248 } | 278 } |
249 | 279 |
250 template<typename T> | 280 template<typename T> |
251 Json::Value _StoneSerializeValue(const std::set<T>& value) | 281 Json::Value _StoneSerializeValue(const std::set<T>& value) |
333 | 363 |
334 | 364 |
335 inline void _StoneDeserializeValue( | 365 inline void _StoneDeserializeValue( |
336 {{enum['name']}}& destValue, const Json::Value& jsonValue) | 366 {{enum['name']}}& destValue, const Json::Value& jsonValue) |
337 { | 367 { |
338 FromString(destValue, jsonValue.asString()); | 368 if (!jsonValue.isNull()) |
369 { | |
370 FromString(destValue, jsonValue.asString()); | |
371 } | |
339 } | 372 } |
340 | 373 |
341 inline Json::Value _StoneSerializeValue(const {{enum['name']}}& value) | 374 inline Json::Value _StoneSerializeValue(const {{enum['name']}}& value) |
342 { | 375 { |
343 std::string strValue = ToString(value); | 376 std::string strValue = ToString(value); |
359 #pragma region {{struct['name']}} | 392 #pragma region {{struct['name']}} |
360 #endif //_MSC_VER | 393 #endif //_MSC_VER |
361 | 394 |
362 struct {{struct['name']}} | 395 struct {{struct['name']}} |
363 { | 396 { |
364 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} {{CanonToCpp(struct['fields'][key])}} {{key}}; | 397 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} {{CanonToCpp(struct['fields'][key]['type'])}} {{key}}; |
365 {% endfor %}{% endif %}{% endif %} | 398 {% endfor %}{% endif %}{% endif %} |
366 {{struct['name']}}({% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}{{CanonToCpp(struct['fields'][key])}} {{key}} = {{CanonToCpp(struct['fields'][key])}}(){{ ", " if not loop.last }}{% endfor %}{% endif %}{% endif %}) | 399 {{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 %}) |
367 { | 400 { |
368 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} this->{{key}} = {{key}}; | 401 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} this->{{key}} = {{key}}; |
369 {% endfor %}{% endif %}{% endif %} } | 402 {% endfor %}{% endif %}{% endif %} } |
370 }; | 403 }; |
371 | 404 |
372 inline void _StoneDeserializeValue({{struct['name']}}& destValue, const Json::Value& value) | 405 inline void _StoneDeserializeValue({{struct['name']}}& destValue, const Json::Value& value) |
373 { | 406 { |
374 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} _StoneDeserializeValue(destValue.{{key}}, value["{{key}}"]); | 407 if (!value.isNull()) |
408 { | |
409 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} _StoneDeserializeValue(destValue.{{key}}, value["{{key}}"]); | |
375 {% endfor %}{% endif %}{% endif %} } | 410 {% endfor %}{% endif %}{% endif %} } |
411 } | |
376 | 412 |
377 inline Json::Value _StoneSerializeValue(const {{struct['name']}}& value) | 413 inline Json::Value _StoneSerializeValue(const {{struct['name']}}& value) |
378 { | 414 { |
379 Json::Value result(Json::objectValue); | 415 Json::Value result(Json::objectValue); |
380 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} result["{{key}}"] = _StoneSerializeValue(value.{{key}}); | 416 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} result["{{key}}"] = _StoneSerializeValue(value.{{key}}); |