Mercurial > hg > orthanc-stone
changeset 507:ce49eae4c887 bgo-commands-codegen
Codegen + Warning fixes
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Fri, 01 Mar 2019 16:18:38 +0100 |
parents | 801d2697a1b1 |
children | 7105a0bad250 |
files | Applications/Commands/ICommand.h Framework/Radiography/RadiographyScene.cpp Framework/Toolbox/ImageGeometry.cpp Resources/CodeGeneration/stonegentool.py Resources/CodeGeneration/template.in.h Resources/CodeGeneration/template.in.ts |
diffstat | 6 files changed, 108 insertions(+), 59 deletions(-) [+] |
line wrap: on
line diff
--- a/Applications/Commands/ICommand.h Tue Feb 26 21:33:16 2019 +0100 +++ b/Applications/Commands/ICommand.h Fri Mar 01 16:18:38 2019 +0100 @@ -38,7 +38,6 @@ virtual ~ICommand() {} virtual void Execute() = 0; - virtual ~ICommand() {} // virtual void Configure(const Json::Value& arguments) = 0; const std::string& GetName() const {
--- a/Framework/Radiography/RadiographyScene.cpp Tue Feb 26 21:33:16 2019 +0100 +++ b/Framework/Radiography/RadiographyScene.cpp Fri Mar 01 16:18:38 2019 +0100 @@ -562,8 +562,8 @@ Extent2D extent = GetSceneExtent(); - int w = std::ceil(extent.GetWidth() / pixelSpacingX); - int h = std::ceil(extent.GetHeight() / pixelSpacingY); + int w = static_cast<int>(std::ceil(extent.GetWidth() / pixelSpacingX)); + int h = static_cast<int>(std::ceil(extent.GetHeight() / pixelSpacingY)); if (w < 0 || h < 0) {
--- a/Framework/Toolbox/ImageGeometry.cpp Tue Feb 26 21:33:16 2019 +0100 +++ b/Framework/Toolbox/ImageGeometry.cpp Fri Mar 01 16:18:38 2019 +0100 @@ -76,9 +76,7 @@ } else { - int tmp; - - tmp = std::floor(extent.GetX1()); + int tmp = static_cast<int>(std::floor(extent.GetX1())); if (tmp < 0) { x1 = 0; @@ -88,7 +86,7 @@ x1 = static_cast<unsigned int>(tmp); } - tmp = std::floor(extent.GetY1()); + tmp = static_cast<int>(std::floor(extent.GetY1())); if (tmp < 0) { y1 = 0; @@ -98,7 +96,7 @@ y1 = static_cast<unsigned int>(tmp); } - tmp = std::ceil(extent.GetX2()); + tmp = static_cast<int>(std::ceil(extent.GetX2())); if (tmp < 0) { return false; @@ -112,7 +110,7 @@ x2 = static_cast<unsigned int>(tmp); } - tmp = std::ceil(extent.GetY2()); + tmp = static_cast<int>(std::ceil(extent.GetY2())); if (tmp < 0) { return false; @@ -395,8 +393,8 @@ Reader reader(source); unsigned int x1, y1, x2, y2; - const float floatWidth = source.GetWidth(); - const float floatHeight = source.GetHeight(); + const float floatWidth = static_cast<float>(source.GetWidth()); + const float floatHeight = static_cast<float>(source.GetHeight()); if (GetProjectiveTransformExtent(x1, y1, x2, y2, a, source.GetWidth(), source.GetHeight(),
--- a/Resources/CodeGeneration/stonegentool.py Tue Feb 26 21:33:16 2019 +0100 +++ b/Resources/CodeGeneration/stonegentool.py Fri Mar 01 16:18:38 2019 +0100 @@ -6,6 +6,7 @@ from jinja2 import Template from io import StringIO import time +import datetime """ 1 2 3 4 5 6 7 @@ -263,13 +264,15 @@ struct = schema[GetLongTypename(shortTypename, schema)] # The keys in the struct dict are the member names # The values in the struct dict are the member types - for field in struct.keys(): - # we fill the chain of dependent types (starting here) - ancestors.append(shortTypename) - ComputeOrderFromTypeTree( - ancestors, genOrder, struct[field], schema) - # don't forget to restore it! - ancestors.pop() + if struct: + # we reach this if struct is not None AND not empty + for field in struct.keys(): + # we fill the chain of dependent types (starting here) + ancestors.append(shortTypename) + ComputeOrderFromTypeTree( + ancestors, genOrder, struct[field], schema) + # don't forget to restore it! + ancestors.pop() # now we're pretty sure our dependencies have been processed, # we can start marking our code for generation (it might @@ -445,6 +448,8 @@ obj = LoadSchema(fn) genOrder = ComputeRequiredDeclarationOrder(obj) templatingDict = ProcessSchema(obj, genOrder) + currentDT = datetime.datetime.now() + templatingDict['currentDatetime'] = str(currentDT) return templatingDict # +-----------------------+
--- a/Resources/CodeGeneration/template.in.h Tue Feb 26 21:33:16 2019 +0100 +++ b/Resources/CodeGeneration/template.in.h Fri Mar 01 16:18:38 2019 +0100 @@ -2,8 +2,12 @@ 1 2 3 4 5 6 7 12345678901234567890123456789012345678901234567890123456789012345678901234567890 +Generated on {{currentDatetime}} by stonegentool + */ +#pragma once +#include <exception> #include <iostream> #include <string> #include <sstream> @@ -23,57 +27,57 @@ namespace {{rootName}} { /** Throws in case of problem */ - void _StoneDeserializeValue(int32_t& destValue, const Json::Value& jsonValue) + inline void _StoneDeserializeValue(int32_t& destValue, const Json::Value& jsonValue) { destValue = jsonValue.asInt(); } - Json::Value _StoneSerializeValue(int32_t value) + inline Json::Value _StoneSerializeValue(int32_t value) { Json::Value result(value); return result; } /** Throws in case of problem */ - void _StoneDeserializeValue(double& destValue, const Json::Value& jsonValue) + inline void _StoneDeserializeValue(double& destValue, const Json::Value& jsonValue) { destValue = jsonValue.asDouble(); } - Json::Value _StoneSerializeValue(double value) + inline Json::Value _StoneSerializeValue(double value) { Json::Value result(value); return result; } /** Throws in case of problem */ - void _StoneDeserializeValue(bool& destValue, const Json::Value& jsonValue) + inline void _StoneDeserializeValue(bool& destValue, const Json::Value& jsonValue) { destValue = jsonValue.asBool(); } - Json::Value _StoneSerializeValue(bool value) + inline Json::Value _StoneSerializeValue(bool value) { Json::Value result(value); return result; } /** Throws in case of problem */ - void _StoneDeserializeValue( + inline void _StoneDeserializeValue( std::string& destValue , const Json::Value& jsonValue) { destValue = jsonValue.asString(); } - Json::Value _StoneSerializeValue(const std::string& value) + inline Json::Value _StoneSerializeValue(const std::string& value) { // the following is better than Json::Value result(value.data(),value.data()+value.size()); return result; } - std::string MakeIndent(int indent) + inline std::string MakeIndent(int indent) { char* txt = reinterpret_cast<char*>(malloc(indent+1)); // NO EXCEPTION BELOW!!!!!!!!!!!! for(size_t i = 0; i < indent; ++i) @@ -93,7 +97,7 @@ } // string dumper - std::ostream& StoneDumpValue(std::ostream& out, const std::string& value, int indent) + inline std::ostream& StoneDumpValue(std::ostream& out, const std::string& value, int indent) { out << MakeIndent(indent) << "\"" << value << "\""; return out; @@ -186,7 +190,7 @@ return out; } - void StoneCheckSerializedValueTypeGeneric(const Json::Value& value) + inline void StoneCheckSerializedValueTypeGeneric(const Json::Value& value) { if ((!value.isMember("type")) || (!value["type"].isString())) { @@ -196,7 +200,7 @@ } } - void StoneCheckSerializedValueType( + inline void StoneCheckSerializedValueType( const Json::Value& value, std::string typeStr) { StoneCheckSerializedValueTypeGeneric(value); @@ -216,23 +220,45 @@ // end of generic methods {% for enum in enums%} enum {{enum['name']}} { -{% for key in enum['fields']%} {{key}}, +{% for key in enum['fields']%} {{enum['name']}}_{{key}}, {%endfor%} }; - void _StoneDeserializeValue( + inline void _StoneDeserializeValue( {{enum['name']}}& destValue, const Json::Value& jsonValue) { destValue = static_cast<{{enum['name']}}>(jsonValue.asInt64()); } - Json::Value _StoneSerializeValue(const {{enum['name']}}& value) + inline Json::Value _StoneSerializeValue(const {{enum['name']}}& value) { return Json::Value(static_cast<int64_t>(value)); } - std::ostream& StoneDumpValue(std::ostream& out, const {{enum['name']}}& value, int indent = 0) + inline std::string ToString(const {{enum['name']}}& value) + { +{% for key in enum['fields']%} if( value == {{enum['name']}}_{{key}}) + { + return std::string("{{key}}"); + } +{%endfor%}; + } + + inline void FromString({{enum['name']}}& value, std::string strValue) { -{% for key in enum['fields']%} if( value == {{key}}) +{% for key in enum['fields']%} if( strValue == std::string("{{key}}") ) + { + value = {{enum['name']}}_{{key}}; + } +{%endfor%} + std::stringstream ss; + ss << "String \"" << strValue << "\" cannot be converted to {{enum['name']}}. Possible values are: {% for key in enum['fields']%}{{key}}{% endfor %}"; + std::string msg = ss.str(); + throw std::runtime_error(msg); + } + + inline std::ostream& StoneDumpValue(std::ostream& out, const {{enum['name']}}& value, int indent = 0) + { +{% for key in enum['fields']%} if( value == {{enum['name']}}_{{key}}) { out << MakeIndent(indent) << "{{key}}" << std::endl; } @@ -247,47 +273,45 @@ struct {{struct['name']}} { -{% for key in struct['fields']%} {{CanonToCpp(struct['fields'][key])}} {{key}}; -{% endfor %} - {{struct['name']}}() +{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} {{CanonToCpp(struct['fields'][key])}} {{key}}; +{% endfor %}{% endif %}{% endif %} + {{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 %}) { -{% for key in struct['fields']%} {{key}} = {{CanonToCpp(struct['fields'][key])}}(); -{% endfor %} - } +{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} this->{{key}} = {{key}}; +{% endfor %}{% endif %}{% endif %} } }; - void _StoneDeserializeValue({{struct['name']}}& destValue, const Json::Value& value) + inline void _StoneDeserializeValue({{struct['name']}}& destValue, const Json::Value& value) { -{% for key in struct['fields']%} _StoneDeserializeValue(destValue.{{key}}, value["{{key}}"]); -{% endfor %} - } +{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} _StoneDeserializeValue(destValue.{{key}}, value["{{key}}"]); +{% endfor %}{% endif %}{% endif %} } - Json::Value _StoneSerializeValue(const {{struct['name']}}& value) + inline Json::Value _StoneSerializeValue(const {{struct['name']}}& value) { Json::Value result(Json::objectValue); -{% for key in struct['fields']%} result["{{key}}"] = _StoneSerializeValue(value.{{key}}); -{% endfor %} +{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} result["{{key}}"] = _StoneSerializeValue(value.{{key}}); +{% endfor %}{% endif %}{% endif %} return result; } - std::ostream& StoneDumpValue(std::ostream& out, const {{struct['name']}}& value, int indent = 0) + inline std::ostream& StoneDumpValue(std::ostream& out, const {{struct['name']}}& value, int indent = 0) { out << MakeIndent(indent) << "{\n"; -{% for key in struct['fields']%} out << MakeIndent(indent) << "{{key}}:\n"; +{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} out << MakeIndent(indent) << "{{key}}:\n"; StoneDumpValue(out, value.{{key}},indent+2); out << "\n"; -{% endfor %} +{% endfor %}{% endif %}{% endif %} out << MakeIndent(indent) << "}\n"; return out; } - void StoneDeserialize({{struct['name']}}& destValue, const Json::Value& value) + inline void StoneDeserialize({{struct['name']}}& destValue, const Json::Value& value) { StoneCheckSerializedValueType(value, "{{rootName}}.{{struct['name']}}"); _StoneDeserializeValue(destValue, value["value"]); } - Json::Value StoneSerialize(const {{struct['name']}}& value) + inline Json::Value StoneSerializeToJson(const {{struct['name']}}& value) { Json::Value result(Json::objectValue); result["type"] = "{{rootName}}.{{struct['name']}}"; @@ -295,6 +319,13 @@ return result; } + inline std::string StoneSerialize(const {{struct['name']}}& value) + { + Json::Value resultJson = StoneSerializeToJson(value); + std::string resultStr = resultJson.toStyledString(); + return resultStr; + } + #ifdef _MSC_VER #pragma endregion {{struct['name']}} #endif //_MSC_VER @@ -310,7 +341,7 @@ {% endfor %} }; /** Service function for StoneDispatchToHandler */ - bool StoneDispatchJsonToHandler( + inline bool StoneDispatchJsonToHandler( const Json::Value& jsonValue, IHandler* handler) { StoneCheckSerializedValueTypeGeneric(jsonValue); @@ -333,7 +364,7 @@ } /** Takes a serialized type and passes this to the handler */ - bool StoneDispatchToHandler(std::string strValue, IHandler* handler) + inline bool StoneDispatchToHandler(std::string strValue, IHandler* handler) { Json::Value readValue;
--- a/Resources/CodeGeneration/template.in.ts Tue Feb 26 21:33:16 2019 +0100 +++ b/Resources/CodeGeneration/template.in.ts Fri Mar 01 16:18:38 2019 +0100 @@ -1,6 +1,9 @@ /* 1 2 3 4 5 6 7 12345678901234567890123456789012345678901234567890123456789012345678901234567890 + +Generated on {{currentDatetime}} by stonegentool + */ function StoneCheckSerializedValueType(value: any, typeStr: string) @@ -42,11 +45,24 @@ {%endfor%} {% for struct in structs%} export class {{struct['name']}} { -{% for key in struct['fields']%} {{key}}:{{CanonToTs(struct['fields'][key])}}; -{% endfor %} + {% if struct %} + {% if struct['fields'] %} + {% for key in struct['fields']%} + {{key}}:{{CanonToTs(struct['fields'][key])}}; + {% endfor %} + {% endif %} + {% endif %} constructor() { -{% for key in struct['fields']%}{% if NeedsTsConstruction(enums,CanonToTs(struct['fields'][key])) %} this.{{key}} = new {{CanonToTs(struct['fields'][key])}}(); -{% endif %}{% endfor %} } + {% if struct %} + {% if struct['fields'] %} + {% for key in struct['fields']%} + {% if NeedsTsConstruction(enums,CanonToTs(struct['fields'][key])) %} + this.{{key}} = new {{CanonToTs(struct['fields'][key])}}(); + {% endif %} + {% endfor %} + {% endif %} + {% endif %} +} public StoneSerialize(): string { let container: object = {};