comparison Resources/CodeGeneration/template.in.h @ 507:ce49eae4c887 bgo-commands-codegen

Codegen + Warning fixes
author Benjamin Golinvaux <bgo@osimis.io>
date Fri, 01 Mar 2019 16:18:38 +0100
parents baa9e1e932db
children 7105a0bad250
comparison
equal deleted inserted replaced
506:801d2697a1b1 507:ce49eae4c887
1 /* 1 /*
2 1 2 3 4 5 6 7 2 1 2 3 4 5 6 7
3 12345678901234567890123456789012345678901234567890123456789012345678901234567890 3 12345678901234567890123456789012345678901234567890123456789012345678901234567890
4 4
5 Generated on {{currentDatetime}} by stonegentool
6
5 */ 7 */
6 8 #pragma once
9
10 #include <exception>
7 #include <iostream> 11 #include <iostream>
8 #include <string> 12 #include <string>
9 #include <sstream> 13 #include <sstream>
10 #include <assert.h> 14 #include <assert.h>
11 #include <memory> 15 #include <memory>
21 #endif 25 #endif
22 26
23 namespace {{rootName}} 27 namespace {{rootName}}
24 { 28 {
25 /** Throws in case of problem */ 29 /** Throws in case of problem */
26 void _StoneDeserializeValue(int32_t& destValue, const Json::Value& jsonValue) 30 inline void _StoneDeserializeValue(int32_t& destValue, const Json::Value& jsonValue)
27 { 31 {
28 destValue = jsonValue.asInt(); 32 destValue = jsonValue.asInt();
29 } 33 }
30 34
31 Json::Value _StoneSerializeValue(int32_t value) 35 inline Json::Value _StoneSerializeValue(int32_t value)
32 { 36 {
33 Json::Value result(value); 37 Json::Value result(value);
34 return result; 38 return result;
35 } 39 }
36 40
37 /** Throws in case of problem */ 41 /** Throws in case of problem */
38 void _StoneDeserializeValue(double& destValue, const Json::Value& jsonValue) 42 inline void _StoneDeserializeValue(double& destValue, const Json::Value& jsonValue)
39 { 43 {
40 destValue = jsonValue.asDouble(); 44 destValue = jsonValue.asDouble();
41 } 45 }
42 46
43 Json::Value _StoneSerializeValue(double value) 47 inline Json::Value _StoneSerializeValue(double value)
44 { 48 {
45 Json::Value result(value); 49 Json::Value result(value);
46 return result; 50 return result;
47 } 51 }
48 52
49 /** Throws in case of problem */ 53 /** Throws in case of problem */
50 void _StoneDeserializeValue(bool& destValue, const Json::Value& jsonValue) 54 inline void _StoneDeserializeValue(bool& destValue, const Json::Value& jsonValue)
51 { 55 {
52 destValue = jsonValue.asBool(); 56 destValue = jsonValue.asBool();
53 } 57 }
54 58
55 Json::Value _StoneSerializeValue(bool value) 59 inline Json::Value _StoneSerializeValue(bool value)
56 { 60 {
57 Json::Value result(value); 61 Json::Value result(value);
58 return result; 62 return result;
59 } 63 }
60 64
61 /** Throws in case of problem */ 65 /** Throws in case of problem */
62 void _StoneDeserializeValue( 66 inline void _StoneDeserializeValue(
63 std::string& destValue 67 std::string& destValue
64 , const Json::Value& jsonValue) 68 , const Json::Value& jsonValue)
65 { 69 {
66 destValue = jsonValue.asString(); 70 destValue = jsonValue.asString();
67 } 71 }
68 72
69 Json::Value _StoneSerializeValue(const std::string& value) 73 inline Json::Value _StoneSerializeValue(const std::string& value)
70 { 74 {
71 // the following is better than 75 // the following is better than
72 Json::Value result(value.data(),value.data()+value.size()); 76 Json::Value result(value.data(),value.data()+value.size());
73 return result; 77 return result;
74 } 78 }
75 79
76 std::string MakeIndent(int indent) 80 inline std::string MakeIndent(int indent)
77 { 81 {
78 char* txt = reinterpret_cast<char*>(malloc(indent+1)); // NO EXCEPTION BELOW!!!!!!!!!!!! 82 char* txt = reinterpret_cast<char*>(malloc(indent+1)); // NO EXCEPTION BELOW!!!!!!!!!!!!
79 for(size_t i = 0; i < indent; ++i) 83 for(size_t i = 0; i < indent; ++i)
80 txt[i] = ' '; 84 txt[i] = ' ';
81 txt[indent] = 0; 85 txt[indent] = 0;
91 out << MakeIndent(indent) << value; 95 out << MakeIndent(indent) << value;
92 return out; 96 return out;
93 } 97 }
94 98
95 // string dumper 99 // string dumper
96 std::ostream& StoneDumpValue(std::ostream& out, const std::string& value, int indent) 100 inline std::ostream& StoneDumpValue(std::ostream& out, const std::string& value, int indent)
97 { 101 {
98 out << MakeIndent(indent) << "\"" << value << "\""; 102 out << MakeIndent(indent) << "\"" << value << "\"";
99 return out; 103 return out;
100 } 104 }
101 105
184 } 188 }
185 out << MakeIndent(indent) << "]\n"; 189 out << MakeIndent(indent) << "]\n";
186 return out; 190 return out;
187 } 191 }
188 192
189 void StoneCheckSerializedValueTypeGeneric(const Json::Value& value) 193 inline void StoneCheckSerializedValueTypeGeneric(const Json::Value& value)
190 { 194 {
191 if ((!value.isMember("type")) || (!value["type"].isString())) 195 if ((!value.isMember("type")) || (!value["type"].isString()))
192 { 196 {
193 std::stringstream ss; 197 std::stringstream ss;
194 ss << "Cannot deserialize value ('type' key invalid)"; 198 ss << "Cannot deserialize value ('type' key invalid)";
195 throw std::runtime_error(ss.str()); 199 throw std::runtime_error(ss.str());
196 } 200 }
197 } 201 }
198 202
199 void StoneCheckSerializedValueType( 203 inline void StoneCheckSerializedValueType(
200 const Json::Value& value, std::string typeStr) 204 const Json::Value& value, std::string typeStr)
201 { 205 {
202 StoneCheckSerializedValueTypeGeneric(value); 206 StoneCheckSerializedValueTypeGeneric(value);
203 207
204 std::string actTypeStr = value["type"].asString(); 208 std::string actTypeStr = value["type"].asString();
214 // end of generic methods 218 // end of generic methods
215 219
216 // end of generic methods 220 // end of generic methods
217 {% for enum in enums%} 221 {% for enum in enums%}
218 enum {{enum['name']}} { 222 enum {{enum['name']}} {
219 {% for key in enum['fields']%} {{key}}, 223 {% for key in enum['fields']%} {{enum['name']}}_{{key}},
220 {%endfor%} }; 224 {%endfor%} };
221 225
222 void _StoneDeserializeValue( 226 inline void _StoneDeserializeValue(
223 {{enum['name']}}& destValue, const Json::Value& jsonValue) 227 {{enum['name']}}& destValue, const Json::Value& jsonValue)
224 { 228 {
225 destValue = static_cast<{{enum['name']}}>(jsonValue.asInt64()); 229 destValue = static_cast<{{enum['name']}}>(jsonValue.asInt64());
226 } 230 }
227 231
228 Json::Value _StoneSerializeValue(const {{enum['name']}}& value) 232 inline Json::Value _StoneSerializeValue(const {{enum['name']}}& value)
229 { 233 {
230 return Json::Value(static_cast<int64_t>(value)); 234 return Json::Value(static_cast<int64_t>(value));
231 } 235 }
232 236
233 std::ostream& StoneDumpValue(std::ostream& out, const {{enum['name']}}& value, int indent = 0) 237 inline std::string ToString(const {{enum['name']}}& value)
234 { 238 {
235 {% for key in enum['fields']%} if( value == {{key}}) 239 {% for key in enum['fields']%} if( value == {{enum['name']}}_{{key}})
240 {
241 return std::string("{{key}}");
242 }
243 {%endfor%};
244 }
245
246 inline void FromString({{enum['name']}}& value, std::string strValue)
247 {
248 {% for key in enum['fields']%} if( strValue == std::string("{{key}}") )
249 {
250 value = {{enum['name']}}_{{key}};
251 }
252 {%endfor%}
253 std::stringstream ss;
254 ss << "String \"" << strValue << "\" cannot be converted to {{enum['name']}}. Possible values are: {% for key in enum['fields']%}{{key}}{% endfor %}";
255 std::string msg = ss.str();
256 throw std::runtime_error(msg);
257 }
258
259 inline std::ostream& StoneDumpValue(std::ostream& out, const {{enum['name']}}& value, int indent = 0)
260 {
261 {% for key in enum['fields']%} if( value == {{enum['name']}}_{{key}})
236 { 262 {
237 out << MakeIndent(indent) << "{{key}}" << std::endl; 263 out << MakeIndent(indent) << "{{key}}" << std::endl;
238 } 264 }
239 {%endfor%} return out; 265 {%endfor%} return out;
240 } 266 }
245 #pragma region {{struct['name']}} 271 #pragma region {{struct['name']}}
246 #endif //_MSC_VER 272 #endif //_MSC_VER
247 273
248 struct {{struct['name']}} 274 struct {{struct['name']}}
249 { 275 {
250 {% for key in struct['fields']%} {{CanonToCpp(struct['fields'][key])}} {{key}}; 276 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} {{CanonToCpp(struct['fields'][key])}} {{key}};
251 {% endfor %} 277 {% endfor %}{% endif %}{% endif %}
252 {{struct['name']}}() 278 {{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 %})
253 { 279 {
254 {% for key in struct['fields']%} {{key}} = {{CanonToCpp(struct['fields'][key])}}(); 280 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} this->{{key}} = {{key}};
255 {% endfor %} 281 {% endfor %}{% endif %}{% endif %} }
256 }
257 }; 282 };
258 283
259 void _StoneDeserializeValue({{struct['name']}}& destValue, const Json::Value& value) 284 inline void _StoneDeserializeValue({{struct['name']}}& destValue, const Json::Value& value)
260 { 285 {
261 {% for key in struct['fields']%} _StoneDeserializeValue(destValue.{{key}}, value["{{key}}"]); 286 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} _StoneDeserializeValue(destValue.{{key}}, value["{{key}}"]);
262 {% endfor %} 287 {% endfor %}{% endif %}{% endif %} }
263 } 288
264 289 inline Json::Value _StoneSerializeValue(const {{struct['name']}}& value)
265 Json::Value _StoneSerializeValue(const {{struct['name']}}& value)
266 { 290 {
267 Json::Value result(Json::objectValue); 291 Json::Value result(Json::objectValue);
268 {% for key in struct['fields']%} result["{{key}}"] = _StoneSerializeValue(value.{{key}}); 292 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} result["{{key}}"] = _StoneSerializeValue(value.{{key}});
269 {% endfor %} 293 {% endfor %}{% endif %}{% endif %}
270 return result; 294 return result;
271 } 295 }
272 296
273 std::ostream& StoneDumpValue(std::ostream& out, const {{struct['name']}}& value, int indent = 0) 297 inline std::ostream& StoneDumpValue(std::ostream& out, const {{struct['name']}}& value, int indent = 0)
274 { 298 {
275 out << MakeIndent(indent) << "{\n"; 299 out << MakeIndent(indent) << "{\n";
276 {% for key in struct['fields']%} out << MakeIndent(indent) << "{{key}}:\n"; 300 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} out << MakeIndent(indent) << "{{key}}:\n";
277 StoneDumpValue(out, value.{{key}},indent+2); 301 StoneDumpValue(out, value.{{key}},indent+2);
278 out << "\n"; 302 out << "\n";
279 {% endfor %} 303 {% endfor %}{% endif %}{% endif %}
280 out << MakeIndent(indent) << "}\n"; 304 out << MakeIndent(indent) << "}\n";
281 return out; 305 return out;
282 } 306 }
283 307
284 void StoneDeserialize({{struct['name']}}& destValue, const Json::Value& value) 308 inline void StoneDeserialize({{struct['name']}}& destValue, const Json::Value& value)
285 { 309 {
286 StoneCheckSerializedValueType(value, "{{rootName}}.{{struct['name']}}"); 310 StoneCheckSerializedValueType(value, "{{rootName}}.{{struct['name']}}");
287 _StoneDeserializeValue(destValue, value["value"]); 311 _StoneDeserializeValue(destValue, value["value"]);
288 } 312 }
289 313
290 Json::Value StoneSerialize(const {{struct['name']}}& value) 314 inline Json::Value StoneSerializeToJson(const {{struct['name']}}& value)
291 { 315 {
292 Json::Value result(Json::objectValue); 316 Json::Value result(Json::objectValue);
293 result["type"] = "{{rootName}}.{{struct['name']}}"; 317 result["type"] = "{{rootName}}.{{struct['name']}}";
294 result["value"] = _StoneSerializeValue(value); 318 result["value"] = _StoneSerializeValue(value);
295 return result; 319 return result;
320 }
321
322 inline std::string StoneSerialize(const {{struct['name']}}& value)
323 {
324 Json::Value resultJson = StoneSerializeToJson(value);
325 std::string resultStr = resultJson.toStyledString();
326 return resultStr;
296 } 327 }
297 328
298 #ifdef _MSC_VER 329 #ifdef _MSC_VER
299 #pragma endregion {{struct['name']}} 330 #pragma endregion {{struct['name']}}
300 #endif //_MSC_VER 331 #endif //_MSC_VER
308 public: 339 public:
309 {% for struct in structs%} virtual bool Handle(const {{struct['name']}}& value) = 0; 340 {% for struct in structs%} virtual bool Handle(const {{struct['name']}}& value) = 0;
310 {% endfor %} }; 341 {% endfor %} };
311 342
312 /** Service function for StoneDispatchToHandler */ 343 /** Service function for StoneDispatchToHandler */
313 bool StoneDispatchJsonToHandler( 344 inline bool StoneDispatchJsonToHandler(
314 const Json::Value& jsonValue, IHandler* handler) 345 const Json::Value& jsonValue, IHandler* handler)
315 { 346 {
316 StoneCheckSerializedValueTypeGeneric(jsonValue); 347 StoneCheckSerializedValueTypeGeneric(jsonValue);
317 std::string type = jsonValue["type"].asString(); 348 std::string type = jsonValue["type"].asString();
318 if (type == "") 349 if (type == "")
331 return false; 362 return false;
332 } 363 }
333 } 364 }
334 365
335 /** Takes a serialized type and passes this to the handler */ 366 /** Takes a serialized type and passes this to the handler */
336 bool StoneDispatchToHandler(std::string strValue, IHandler* handler) 367 inline bool StoneDispatchToHandler(std::string strValue, IHandler* handler)
337 { 368 {
338 Json::Value readValue; 369 Json::Value readValue;
339 370
340 Json::CharReaderBuilder builder; 371 Json::CharReaderBuilder builder;
341 Json::CharReader* reader = builder.newCharReader(); 372 Json::CharReader* reader = builder.newCharReader();