Mercurial > hg > orthanc-stone
comparison Resources/CodeGeneration/template.in.h @ 495:6405435480ae bgo-commands-codegen
Fixed template to add dump capabilities + started work on an integrated TS/WASM test
author | bgo-osimis |
---|---|
date | Sat, 23 Feb 2019 14:14:32 +0100 |
parents | fc17251477d6 |
children | baa9e1e932db |
comparison
equal
deleted
inserted
replaced
494:fc17251477d6 | 495:6405435480ae |
---|---|
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 */ |
5 | 6 |
6 #include <iostream> | 7 #include <iostream> |
7 #include <string> | 8 #include <string> |
8 #include <sstream> | 9 #include <sstream> |
70 { | 71 { |
71 // the following is better than | 72 // the following is better than |
72 Json::Value result(value.data(),value.data()+value.size()); | 73 Json::Value result(value.data(),value.data()+value.size()); |
73 return result; | 74 return result; |
74 } | 75 } |
75 | 76 |
77 std::string MakeIndent(int indent) | |
78 { | |
79 char* txt = reinterpret_cast<char*>(malloc(indent+1)); // NO EXCEPTION BELOW!!!!!!!!!!!! | |
80 for(size_t i = 0; i < indent; ++i) | |
81 txt[i] = ' '; | |
82 txt[indent] = 0; | |
83 std::string retVal(txt); | |
84 free(txt); // NO EXCEPTION ABOVE !!!!!!!!!! | |
85 return retVal; | |
86 } | |
87 | |
88 // generic dumper | |
89 template<typename T> | |
90 std::ostream& StoneDumpValue(std::ostream& out, const T& value, int indent) | |
91 { | |
92 out << MakeIndent(indent) << value; | |
93 return out; | |
94 } | |
95 | |
96 // string dumper | |
97 std::ostream& StoneDumpValue(std::ostream& out, const std::string& value, int indent) | |
98 { | |
99 out << MakeIndent(indent) << "\"" << value << "\""; | |
100 return out; | |
101 } | |
102 | |
76 /** Throws in case of problem */ | 103 /** Throws in case of problem */ |
77 template<typename T> | 104 template<typename T> |
78 void _StoneDeserializeValue( | 105 void _StoneDeserializeValue( |
79 std::map<std::string, T>& destValue, const Json::Value& jsonValue) | 106 std::map<std::string, T>& destValue, const Json::Value& jsonValue) |
80 { | 107 { |
106 result[it->first] = _StoneSerializeValue(it->second); | 133 result[it->first] = _StoneSerializeValue(it->second); |
107 } | 134 } |
108 return result; | 135 return result; |
109 } | 136 } |
110 | 137 |
138 template<typename T> | |
139 std::ostream& StoneDumpValue(std::ostream& out, const std::map<std::string,T>& value, int indent) | |
140 { | |
141 out << MakeIndent(indent) << "{\n"; | |
142 for (std::map<std::string, T>::const_iterator it = value.cbegin(); | |
143 it != value.cend(); ++it) | |
144 { | |
145 out << MakeIndent(indent+2) << "\"" << it->first << "\" : "; | |
146 StoneDumpValue(out, it->second, indent+2); | |
147 } | |
148 out << MakeIndent(indent) << "}\n"; | |
149 return out; | |
150 } | |
151 | |
111 /** Throws in case of problem */ | 152 /** Throws in case of problem */ |
112 template<typename T> | 153 template<typename T> |
113 void _StoneDeserializeValue( | 154 void _StoneDeserializeValue( |
114 std::vector<T>& destValue, const Json::Value& jsonValue) | 155 std::vector<T>& destValue, const Json::Value& jsonValue) |
115 { | 156 { |
130 for (size_t i = 0; i < value.size(); ++i) | 171 for (size_t i = 0; i < value.size(); ++i) |
131 { | 172 { |
132 result.append(_StoneSerializeValue(value[i])); | 173 result.append(_StoneSerializeValue(value[i])); |
133 } | 174 } |
134 return result; | 175 return result; |
176 } | |
177 | |
178 template<typename T> | |
179 std::ostream& StoneDumpValue(std::ostream& out, const std::vector<T>& value, int indent) | |
180 { | |
181 out << MakeIndent(indent) << "[\n"; | |
182 for (size_t i = 0; i < value.size(); ++i) | |
183 { | |
184 StoneDumpValue(out, value[i], indent+2); | |
185 } | |
186 out << MakeIndent(indent) << "]\n"; | |
187 return out; | |
135 } | 188 } |
136 | 189 |
137 void StoneCheckSerializedValueTypeGeneric(const Json::Value& value) | 190 void StoneCheckSerializedValueTypeGeneric(const Json::Value& value) |
138 { | 191 { |
139 if ((!value.isMember("type")) || (!value["type"].isString())) | 192 if ((!value.isMember("type")) || (!value["type"].isString())) |
175 | 228 |
176 Json::Value _StoneSerializeValue(const {{enum['name']}}& value) | 229 Json::Value _StoneSerializeValue(const {{enum['name']}}& value) |
177 { | 230 { |
178 return Json::Value(static_cast<int64_t>(value)); | 231 return Json::Value(static_cast<int64_t>(value)); |
179 } | 232 } |
233 | |
234 std::ostream& StoneDumpValue(std::ostream& out, const {{enum['name']}}& value, int indent = 0) | |
235 { | |
236 {% for key in enum['fields']%} if( value == {{key}}) | |
237 { | |
238 out << MakeIndent(indent) << "{{key}}" << std::endl; | |
239 } | |
240 {%endfor%} return out; | |
241 } | |
242 | |
180 {%endfor%} | 243 {%endfor%} |
181 {% for struct in structs%} | 244 {% for struct in structs%} |
182 #ifdef _MSC_VER | 245 #ifdef _MSC_VER |
183 #pragma region {{struct['name']}} | 246 #pragma region {{struct['name']}} |
184 #endif //_MSC_VER | 247 #endif //_MSC_VER |
206 {% for key in struct['fields']%} result["{{key}}"] = _StoneSerializeValue(value.{{key}}); | 269 {% for key in struct['fields']%} result["{{key}}"] = _StoneSerializeValue(value.{{key}}); |
207 {% endfor %} | 270 {% endfor %} |
208 return result; | 271 return result; |
209 } | 272 } |
210 | 273 |
274 std::ostream& StoneDumpValue(std::ostream& out, const {{struct['name']}}& value, int indent = 0) | |
275 { | |
276 out << MakeIndent(indent) << "{\n"; | |
277 {% for key in struct['fields']%} out << MakeIndent(indent) << "{{key}}:\n"; | |
278 StoneDumpValue(out, value.{{key}},indent+2); | |
279 out << "\n"; | |
280 {% endfor %} | |
281 out << MakeIndent(indent) << "}\n"; | |
282 return out; | |
283 } | |
284 | |
211 void StoneDeserialize({{struct['name']}}& destValue, const Json::Value& value) | 285 void StoneDeserialize({{struct['name']}}& destValue, const Json::Value& value) |
212 { | 286 { |
213 StoneCheckSerializedValueType(value, "{{rootName}}.{{struct['name']}}"); | 287 StoneCheckSerializedValueType(value, "{{rootName}}.{{struct['name']}}"); |
214 _StoneDeserializeValue(destValue, value["value"]); | 288 _StoneDeserializeValue(destValue, value["value"]); |
215 } | 289 } |
228 {% endfor %} | 302 {% endfor %} |
229 #ifdef _MSC_VER | 303 #ifdef _MSC_VER |
230 #pragma region Dispatching code | 304 #pragma region Dispatching code |
231 #endif //_MSC_VER | 305 #endif //_MSC_VER |
232 | 306 |
233 class IDispatcher | 307 class IHandler |
234 { | 308 { |
235 public: | 309 public: |
236 {% for struct in structs%} virtual bool Handle(const {{struct['name']}}& value) = 0; | 310 {% for struct in structs%} virtual bool Handle(const {{struct['name']}}& value) = 0; |
237 {% endfor %} }; | 311 {% endfor %} }; |
238 | 312 |
239 /** Service function for StoneDispatchToHandler */ | 313 /** Service function for StoneDispatchToHandler */ |
240 bool StoneDispatchJsonToHandler( | 314 bool StoneDispatchJsonToHandler( |
241 const Json::Value& jsonValue, IDispatcher* dispatcher) | 315 const Json::Value& jsonValue, IHandler* handler) |
242 { | 316 { |
243 StoneCheckSerializedValueTypeGeneric(jsonValue); | 317 StoneCheckSerializedValueTypeGeneric(jsonValue); |
244 std::string type = jsonValue["type"].asString(); | 318 std::string type = jsonValue["type"].asString(); |
245 if (type == "") | 319 if (type == "") |
246 { | 320 { |
249 } | 323 } |
250 {% for struct in structs%} else if (type == "{{rootName}}.{{struct['name']}}") | 324 {% for struct in structs%} else if (type == "{{rootName}}.{{struct['name']}}") |
251 { | 325 { |
252 {{struct['name']}} value; | 326 {{struct['name']}} value; |
253 _StoneDeserializeValue(value, jsonValue["value"]); | 327 _StoneDeserializeValue(value, jsonValue["value"]); |
254 return dispatcher->Handle(value); | 328 return handler->Handle(value); |
255 } | 329 } |
256 {% endfor %} else | 330 {% endfor %} else |
257 { | 331 { |
258 return false; | 332 return false; |
259 } | 333 } |
260 } | 334 } |
261 | 335 |
262 /** Takes a serialized type and passes this to the dispatcher */ | 336 /** Takes a serialized type and passes this to the handler */ |
263 bool StoneDispatchToHandler(std::string strValue, IDispatcher* dispatcher) | 337 bool StoneDispatchToHandler(std::string strValue, IHandler* handler) |
264 { | 338 { |
265 Json::Value readValue; | 339 Json::Value readValue; |
266 | 340 |
267 Json::CharReaderBuilder builder; | 341 Json::CharReaderBuilder builder; |
268 Json::CharReader* reader = builder.newCharReader(); | 342 Json::CharReader* reader = builder.newCharReader(); |
281 { | 355 { |
282 std::stringstream ss; | 356 std::stringstream ss; |
283 ss << "Jsoncpp parsing error: " << errors; | 357 ss << "Jsoncpp parsing error: " << errors; |
284 throw std::runtime_error(ss.str()); | 358 throw std::runtime_error(ss.str()); |
285 } | 359 } |
286 return StoneDispatchJsonToHandler(readValue, dispatcher); | 360 return StoneDispatchJsonToHandler(readValue, handler); |
287 } | 361 } |
288 | 362 |
289 #ifdef _MSC_VER | 363 #ifdef _MSC_VER |
290 #pragma endregion Dispatching code | 364 #pragma endregion Dispatching code |
291 #endif //_MSC_VER | 365 #endif //_MSC_VER |