# HG changeset patch # User Alain Mazy # Date 1558438074 -7200 # Node ID 529189f399ece073e6b3e310d70ac411e31eb201 # Parent 853e30d17caef73333b827e604454ccfb110e64e# Parent 8190213e2279eac51cb598250fd15875e9aa73c6 Merged am-dev into default diff -r 853e30d17cae -r 529189f399ec Framework/Radiography/RadiographyMaskLayer.cpp --- a/Framework/Radiography/RadiographyMaskLayer.cpp Tue May 21 11:45:06 2019 +0200 +++ b/Framework/Radiography/RadiographyMaskLayer.cpp Tue May 21 13:27:54 2019 +0200 @@ -135,8 +135,16 @@ // first fill the complete image Orthanc::ImageProcessing::Set(*mask_, OUT_MASK_VALUE); + // clip corners + std::vector clippedCorners; + for (size_t i = 0; i < corners_.size(); i++) + { + clippedCorners.push_back(corners_[i]); + clippedCorners[i].ClipTo(0, mask_->GetWidth() - 1, 0, mask_->GetHeight() - 1); + } + // fill mask - Orthanc::ImageProcessing::FillPolygon(*mask_, corners_, IN_MASK_VALUE); + Orthanc::ImageProcessing::FillPolygon(*mask_, clippedCorners, IN_MASK_VALUE); } diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/README.md --- a/Resources/CodeGeneration/README.md Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/README.md Tue May 21 13:27:54 2019 +0200 @@ -10,11 +10,11 @@ `testCppHandler` contains a C++ project that produces an executable slurping a set of text files representing messages defined against -the `test_data/test1.yaml' schema and dumping them to `cout`. +the `test_data/testTestStoneCodeGen.yaml' schema and dumping them to `cout`. 'testWasmIntegrated` contains a small Web app demonstrating the interaction between TypeScript and C++ in WASM. source ~/apps/emsdk/emsdk_env.sh -Install Python and the following packages `pip install pyyaml jinja2` +Install Python and the following packages `pip install pyyaml yamlloader jinja2` diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/stonegentool.py --- a/Resources/CodeGeneration/stonegentool.py Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/stonegentool.py Tue May 21 13:27:54 2019 +0200 @@ -7,6 +7,7 @@ from io import StringIO import time import datetime +import yamlloader """ 1 2 3 4 5 6 7 @@ -186,7 +187,6 @@ RegisterTemplateFunction(template,NeedsCppConstruction) RegisterTemplateFunction(template, DefaultValueToTs) RegisterTemplateFunction(template, DefaultValueToCpp) - RegisterTemplateFunction(template, sorted) return template def MakeTemplateFromFile(templateFileName): @@ -533,7 +533,7 @@ if not (nextCh == ' ' or nextCh == '\n'): lineNumber = schemaText.count("\n",0,i) + 1 raise RuntimeError("Error at line " + str(lineNumber) + " in the schema: colons must be followed by a space or a newline!") - schema = yaml.load(schemaText, Loader = yaml.SafeLoader) + schema = yaml.load(schemaText, Loader = yamlloader.ordereddict.SafeLoader) return schema def GetTemplatingDictFromSchemaFilename(fn): diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/stonegentool_test.py --- a/Resources/CodeGeneration/stonegentool_test.py Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/stonegentool_test.py Tue May 21 13:27:54 2019 +0200 @@ -87,13 +87,13 @@ self.assertEqual(b4,["int","vector"]) def test_ParseSchema(self): - fn = os.path.join(os.path.dirname(__file__), 'test_data', 'test1.yaml') + fn = os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') obj = LoadSchema(fn) # we're happy if it does not crash :) CheckSchemaSchema(obj) def test_ComputeRequiredDeclarationOrder(self): - fn = os.path.join(os.path.dirname(__file__), 'test_data', 'test1.yaml') + fn = os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') obj = LoadSchema(fn) genOrder: str = ComputeRequiredDeclarationOrder(obj) self.assertEqual(5,len(genOrder)) @@ -110,7 +110,7 @@ def test_genEnums(self): self.maxDiff = None - fn = os.path.join(os.path.dirname(__file__), 'test_data', 'test1.yaml') + fn = os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') obj = LoadSchema(fn) genOrder: str = ComputeRequiredDeclarationOrder(obj) processedSchema = ProcessSchema(obj, genOrder) @@ -127,13 +127,15 @@ self.assertTrue('someBs' in structs['C']['fields']) self.assertTrue('CrispType' in enums) self.assertTrue('Message1' in structs) - self.assertEqual('int32', structs['Message1']['fields']['a'].type) - self.assertEqual('string', structs['Message1']['fields']['b'].type) - self.assertEqual('EnumMonth0', structs['Message1']['fields']['c'].type) - self.assertEqual('bool', structs['Message1']['fields']['d'].type) + self.assertEqual('int32', structs['Message1']['fields']['memberInt32'].type) + self.assertEqual('string', structs['Message1']['fields']['memberString'].type) + self.assertEqual('EnumMonth0', structs['Message1']['fields']['memberEnumMonth'].type) + self.assertEqual('bool', structs['Message1']['fields']['memberBool'].type) + self.assertEqual('float32', structs['Message1']['fields']['memberFloat32'].type) + self.assertEqual('float64', structs['Message1']['fields']['memberFloat64'].type) def test_GenerateTypeScriptEnums(self): - fn = os.path.join(os.path.dirname(__file__), 'test_data', 'test1.yaml') + fn = os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') tdico = GetTemplatingDictFromSchemaFilename(fn) template = Template(""" // end of generic methods {% for enum in enums%} export enum {{enum['name']}} { @@ -167,7 +169,7 @@ self.assertEqual(renderedCodeRef,renderedCode) def test_GenerateCplusplusEnums(self): - fn = os.path.join(os.path.dirname(__file__), 'test_data', 'test1.yaml') + fn = os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') tdico = GetTemplatingDictFromSchemaFilename(fn) template = Template(""" // end of generic methods {% for enum in enums%} enum {{enum['name']}} { @@ -201,45 +203,9 @@ self.assertEqual(renderedCodeRef,renderedCode) def test_generateTsStructType(self): - fn = os.path.join(os.path.dirname(__file__), 'test_data', 'test1.yaml') + fn = os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') tdico = GetTemplatingDictFromSchemaFilename(fn) - ref = """ export class Message1 { - a: number; - b: string; - c: EnumMonth0; - d: boolean; - public StoneSerialize(): string { - let container: object = {}; - container['type'] = 'VsolStuff.Message1'; - container['value'] = this; - return JSON.stringify(container); - } - }; - export class Message2 { - toto: string; - tata: Message1[]; - tutu: string[]; - titi: Map; - lulu: Map; - - constructor() - { - this.tata = new Array(); - this.tutu = new Array(); - this.titi = new Map(); - this.lulu = new Map(); - } - - public StoneSerialize(): string { - let container: object = {}; - container['type'] = 'VsolStuff.Message2'; - container['value'] = this; - return JSON.stringify(container); - } - }; - -""" # template = MakeTemplate(""" // end of generic methods # {% for struct in struct%} export class {{struct['name']}} { # {% for key in struct['fields']%} {{key}}:{{struct['fields'][key]}}, @@ -290,7 +256,7 @@ public StoneSerialize(): string { let container: object = {}; - container['type'] = 'VsolMessages.A'; + container['type'] = 'TestStoneCodeGen.A'; container['value'] = this; return JSON.stringify(container); } @@ -307,7 +273,7 @@ public StoneSerialize(): string { let container: object = {}; - container['type'] = 'VsolMessages.B'; + container['type'] = 'TestStoneCodeGen.B'; container['value'] = this; return JSON.stringify(container); } @@ -324,53 +290,63 @@ public StoneSerialize(): string { let container: object = {}; - container['type'] = 'VsolMessages.C'; + container['type'] = 'TestStoneCodeGen.C'; container['value'] = this; return JSON.stringify(container); } }; export class Message1 { - a:number; - b:string; - c:EnumMonth0; - d:boolean; + memberInt32:number; + memberString:string; + memberEnumMonth:EnumMonth0; + memberBool:boolean; + memberFloat32:number; + memberFloat64:number; constructor() { - this.a = new number(); - this.b = new string(); - this.c = new EnumMonth0(); - this.d = new boolean(); + this.memberInt32 = new number(); + this.memberString = new string(); + this.memberEnumMonth = new EnumMonth0(); + this.memberBool = new boolean(); + this.memberFloat32 = new number(); + this.memberFloat64 = new number(); } public StoneSerialize(): string { let container: object = {}; - container['type'] = 'VsolMessages.Message1'; + container['type'] = 'TestStoneCodeGen.Message1'; container['value'] = this; return JSON.stringify(container); } }; export class Message2 { - toto:string; - tata:Array; - tutu:Array; - titi:Map; - lulu:Map; - movieType:MovieType; + memberString:string; + memberStringWithDefault:string; + memberVectorOfMessage1:Array; + memberVectorOfString:Array; + memberMapStringString:Map; + memberMapStringStruct:Map; + memberMapEnumFloat:Map; + memberEnumMovieType:MovieType; + memberJson:Object; constructor() { - this.toto = new string(); - this.tata = new Array(); - this.tutu = new Array(); - this.titi = new Map(); - this.lulu = new Map(); - this.movieType = new MovieType(); + this.memberString = new string(); + this.memberStringWithDefault = new string(); + this.memberVectorOfMessage1 = new Array(); + this.memberVectorOfString = new Array(); + this.memberMapStringString = new Map(); + this.memberMapStringStruct = new Map(); + this.memberMapEnumFloat = new Map(); + this.memberEnumMovieType = new MovieType(); + this.memberJson = new Object(); } public StoneSerialize(): string { let container: object = {}; - container['type'] = 'VsolMessages.Message2'; + container['type'] = 'TestStoneCodeGen.Message2'; container['value'] = this; return JSON.stringify(container); } @@ -383,7 +359,7 @@ def test_generateWholeTsFile(self): schemaFile = \ - os.path.join(os.path.dirname(__file__), 'test_data', 'test1.yaml') + os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') tdico = GetTemplatingDictFromSchemaFilename(schemaFile) tsTemplateFile = \ os.path.join(os.path.dirname(__file__), 'template.in.ts.j2') diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/template.in.h.j2 --- a/Resources/CodeGeneration/template.in.h.j2 Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/template.in.h.j2 Tue May 21 13:27:54 2019 +0200 @@ -109,6 +109,21 @@ } /** Throws in case of problem */ + inline void _StoneDeserializeValue(float& destValue, const Json::Value& jsonValue) + { + if (!jsonValue.isNull()) + { + destValue = jsonValue.asFloat(); + } + } + + inline Json::Value _StoneSerializeValue(float value) + { + Json::Value result(value); + return result; + } + + /** Throws in case of problem */ inline void _StoneDeserializeValue(bool& destValue, const Json::Value& jsonValue) { if (!jsonValue.isNull()) @@ -167,10 +182,21 @@ return out; } + inline std::string ToString(const std::string& str) + { + return str; + } + + inline void FromString(std::string& value, std::string strValue) + { + value = strValue; + } + + /** Throws in case of problem */ - template + template void _StoneDeserializeValue( - std::map& destValue, const Json::Value& jsonValue) + std::map& destValue, const Json::Value& jsonValue) { if (!jsonValue.isNull()) { @@ -180,10 +206,12 @@ itr != jsonValue.end(); itr++) { - std::string key; - _StoneDeserializeValue(key, itr.key()); + std::string strKey; + _StoneDeserializeValue(strKey, itr.key()); + TK key; + 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) - T innerDestValue; + TV innerDestValue; _StoneDeserializeValue(innerDestValue, *itr); destValue[key] = innerDestValue; @@ -191,29 +219,30 @@ } } - template - Json::Value _StoneSerializeValue(const std::map& value) + template + Json::Value _StoneSerializeValue(const std::map& value) { Json::Value result(Json::objectValue); - for (typename std::map::const_iterator it = value.cbegin(); + for (typename std::map::const_iterator it = value.cbegin(); it != value.cend(); ++it) { // it->first it->second - result[it->first] = _StoneSerializeValue(it->second); + result[ToString(it->first)] = _StoneSerializeValue(it->second); } return result; } - template - std::ostream& StoneDumpValue(std::ostream& out, const std::map& value, size_t indent) + template + std::ostream& StoneDumpValue(std::ostream& out, const std::map& value, size_t indent) { out << MakeIndent(indent) << "{\n"; - for (typename std::map::const_iterator it = value.cbegin(); + for (typename std::map::const_iterator it = value.cbegin(); it != value.cend(); ++it) { out << MakeIndent(indent+2) << "\"" << it->first << "\" : "; StoneDumpValue(out, it->second, indent+2); + out << ", \n"; } out << MakeIndent(indent) << "}\n"; return out; @@ -224,7 +253,7 @@ void _StoneDeserializeValue( std::vector& destValue, const Json::Value& jsonValue) { - if (!jsonValue.isNull()) + if (!jsonValue.isNull() && jsonValue.isArray()) { destValue.clear(); destValue.reserve(jsonValue.size()); @@ -255,6 +284,7 @@ for (size_t i = 0; i < value.size(); ++i) { StoneDumpValue(out, value[i], indent+2); + out << ", \n"; } out << MakeIndent(indent) << "]\n"; return out; @@ -265,7 +295,7 @@ void _StoneDeserializeValue( std::set& destValue, const Json::Value& jsonValue) { - if (!jsonValue.isNull()) + if (!jsonValue.isNull() && jsonValue.isArray()) { destValue.clear(); for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++) @@ -295,6 +325,7 @@ for (typename std::set::const_iterator it = value.begin(); it != value.end(); ++it) { StoneDumpValue(out, *it, indent+2); + out << ", \n"; } out << MakeIndent(indent) << "]\n"; return out; @@ -381,7 +412,7 @@ { {% for key in enum['fields']%} if( value == {{enum['name']}}_{{key}}) { - out << MakeIndent(indent) << "{{key}}" << std::endl; + out << MakeIndent(indent) << "{{key}}"; } {%endfor%} return out; } @@ -394,9 +425,9 @@ struct {{struct['name']}} { -{% if struct %}{% if struct['fields'] %}{% for key in sorted(struct['fields']) %} {{CanonToCpp(struct['fields'][key]['type'])}} {{key}}; +{% if struct %}{% if struct['fields'] %}{% for key in struct['fields'] %} {{CanonToCpp(struct['fields'][key]['type'])}} {{key}}; {% endfor %}{% endif %}{% endif %} - {{struct['name']}}({% if struct %}{% if struct['fields'] %}{% for key in sorted(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 %}) + {{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 %}) { {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} this->{{key}} = {{key}}; {% endfor %}{% endif %}{% endif %} } @@ -421,11 +452,11 @@ inline std::ostream& StoneDumpValue(std::ostream& out, const {{struct['name']}}& value, size_t indent = 0) { out << MakeIndent(indent) << "{\n"; -{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} out << MakeIndent(indent) << "{{key}}:\n"; +{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%} out << MakeIndent(indent+2) << "{{key}}: "; StoneDumpValue(out, value.{{key}},indent+2); - out << "\n"; + out << ", \n"; {% endfor %}{% endif %}{% endif %} - out << MakeIndent(indent) << "}\n"; + out << MakeIndent(indent) << "}"; return out; } diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/testCppHandler/CMakeLists.txt --- a/Resources/CodeGeneration/testCppHandler/CMakeLists.txt Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/testCppHandler/CMakeLists.txt Tue May 21 13:27:54 2019 +0200 @@ -3,13 +3,13 @@ project(testCppHandler) set(testCppHandler_Codegen_Deps - ${CMAKE_CURRENT_LIST_DIR}/../test_data/test1.yaml + ${CMAKE_CURRENT_LIST_DIR}/../test_data/testTestStoneCodeGen.yaml ${CMAKE_CURRENT_LIST_DIR}/../template.in.h.j2 ) add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/VsolMessages_generated.hpp - COMMAND python ${CMAKE_CURRENT_LIST_DIR}/../stonegentool.py -o ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/../test_data/test1.yaml + COMMAND python ${CMAKE_CURRENT_LIST_DIR}/../stonegentool.py -o ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/../test_data/testTestStoneCodeGen.yaml DEPENDS ${testCppHandler_Codegen_Deps} ) diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/testCppHandler/README.md --- a/Resources/CodeGeneration/testCppHandler/README.md Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/testCppHandler/README.md Tue May 21 13:27:54 2019 +0200 @@ -25,8 +25,10 @@ - `cmake -G "Visual Studio 15 2017 Win64" ..` (modify for your current Visual Studio version) - `cmake --build . --config Debug` or - `cmake --build . --config Release` +How to execute the test +======================= +- `cd test_data && testCppHandler --pattern=*.json` - diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/testCppHandler/main.cpp --- a/Resources/CodeGeneration/testCppHandler/main.cpp Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/testCppHandler/main.cpp Tue May 21 13:27:54 2019 +0200 @@ -8,7 +8,7 @@ #include using namespace boost::program_options; -#include "VsolMessages_generated.hpp" +#include "TestStoneCodeGen_generated.hpp" /** Transforms `str` by replacing occurrences of `oldStr` with `newStr`, using @@ -39,32 +39,32 @@ return string(bytes.data(), fileSize); } -class MyHandler : public VsolMessages::IHandler +class MyHandler : public TestStoneCodeGen::IHandler { public: - virtual bool Handle(const VsolMessages::A& value) override + virtual bool Handle(const TestStoneCodeGen::A& value) override { - VsolMessages::StoneDumpValue(cout, value); + TestStoneCodeGen::StoneDumpValue(cout, value); return true; } - virtual bool Handle(const VsolMessages::B& value) override + virtual bool Handle(const TestStoneCodeGen::B& value) override { - VsolMessages::StoneDumpValue(cout, value); + TestStoneCodeGen::StoneDumpValue(cout, value); return true; } - virtual bool Handle(const VsolMessages::C& value) override + virtual bool Handle(const TestStoneCodeGen::C& value) override { - VsolMessages::StoneDumpValue(cout, value); + TestStoneCodeGen::StoneDumpValue(cout, value); return true; } - virtual bool Handle(const VsolMessages::Message1& value) override + virtual bool Handle(const TestStoneCodeGen::Message1& value) override { - VsolMessages::StoneDumpValue(cout, value); + TestStoneCodeGen::StoneDumpValue(cout, value); return true; } - virtual bool Handle(const VsolMessages::Message2& value) override + virtual bool Handle(const TestStoneCodeGen::Message2& value) override { - VsolMessages::StoneDumpValue(cout, value); + TestStoneCodeGen::StoneDumpValue(cout, value); return true; } }; @@ -77,7 +77,7 @@ cout << "+--------------------------------------------+\n"; MyHandler handler; auto contents = SlurpFile(filePath.path().string()); - VsolMessages::StoneDispatchToHandler(contents, &handler); + TestStoneCodeGen::StoneDispatchToHandler(contents, &handler); } int main(int argc, char** argv) diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/testCppHandler/test_data/test_Message2.json --- a/Resources/CodeGeneration/testCppHandler/test_data/test_Message2.json Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/testCppHandler/test_data/test_Message2.json Tue May 21 13:27:54 2019 +0200 @@ -1,40 +1,47 @@ { - "type": "VsolMessages.Message2", + "type": "TestStoneCodeGen.Message2", "value": { - "tata": [ + "memberVectorOfMessage1": [ { - "a": 42, - "b": "Benjamin", - "c": 0, - "d": false + "memberInt32": 42, + "memberString": "Benjamin", + "memberEnumMonth": "January", + "memberBool": false, + "memberFloat32": 0.1, + "memberFloat64": -0.2 }, { - "a": 43, - "b": "Sandrine", - "c": 2 + "memberInt32": 43, + "memberString": "Sandrine", + "memberEnumMonth": "March" } ], - "tutu": [ + "memberVectorOfString": [ "Mercadet", "Poisson" ], - "titi": { + "memberMapStringString": { "44": "key 44", "45": "key 45" }, - "lulu": { + "memberMapStringStruct": { "54": { - "a": 43, - "b": "Sandrine", - "c": 2 + "memberInt32": 43, + "memberString": "Sandrine", + "memberEnumMonth": "March" }, "55": { - "a": 42, - "b": "Benjamin", - "c": 0, - "d": false + "memberInt32": 42, + "memberString": "Benjamin", + "memberEnumMonth": "January", + "memberBool": false } }, - "toto": "Prout zizi" + "memberString": "Prout zizi", + "memberMapEnumFloat" : { + "SaltAndPepper" : 0.1, + "CreamAndChives" : -0.2 + }, + "memberJson" : {"custom-key": "custom-value"} } } \ No newline at end of file diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/testWasmIntegrated/CMakeLists.txt --- a/Resources/CodeGeneration/testWasmIntegrated/CMakeLists.txt Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/CMakeLists.txt Tue May 21 13:27:54 2019 +0200 @@ -21,14 +21,14 @@ set(jsoncppRootDir ${CMAKE_CURRENT_LIST_DIR}/jsoncpp-1.8.4) add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/testWasmIntegratedCpp_generated.hpp ${CMAKE_CURRENT_BINARY_DIR}/testWasmIntegratedCpp_generated.ts - COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/../stonegentool.py -o ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/testWasmIntegratedCpp_api.yaml - DEPENDS ${testCppHandler_Codegen_Deps} ${CMAKE_CURRENT_LIST_DIR}/testWasmIntegratedCpp_api.yaml + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/TestStoneCodeGen_generated.hpp ${CMAKE_CURRENT_BINARY_DIR}/TestStoneCodeGen_generated.ts + COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/../stonegentool.py -o ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/../test_data/testTestStoneCodeGen.yaml + DEPENDS ${testCppHandler_Codegen_Deps} ${CMAKE_CURRENT_LIST_DIR}/../test_data/testTestStoneCodeGen.yaml ) add_executable(testWasmIntegratedCpp main.cpp - ${CMAKE_CURRENT_BINARY_DIR}/testWasmIntegratedCpp_generated.hpp + ${CMAKE_CURRENT_BINARY_DIR}/TestStoneCodeGen_generated.hpp ${jsoncppRootDir}/jsoncpp.cpp ${testCppHandler_Codegen_Deps}) diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/testWasmIntegrated/DefaultLibrary.js --- a/Resources/CodeGeneration/testWasmIntegrated/DefaultLibrary.js Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/DefaultLibrary.js Tue May 21 13:27:54 2019 +0200 @@ -9,7 +9,7 @@ }, SendMessageFromCppJS: function(statusUpdateMessage) { var statusUpdateMessage_ = UTF8ToString(statusUpdateMessage); - SendMessageFromCpp(statusUpdateMessage_); + window.SendMessageFromCpp(statusUpdateMessage_); } }); diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/testWasmIntegrated/build-web.sh --- a/Resources/CodeGeneration/testWasmIntegrated/build-web.sh Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/build-web.sh Tue May 21 13:27:54 2019 +0200 @@ -4,7 +4,7 @@ mkdir -p build-final # compile TS to JS -tsc --module commonjs --sourceMap -t ES2015 --outDir "build-tsc/" build-wasm/testWasmIntegratedCpp_generated.ts testWasmIntegrated.ts +tsc --module commonjs --sourceMap -t ES2015 --outDir "build-tsc/" build-wasm/TestStoneCodeGen_generated.ts testWasmIntegrated.ts # bundle JS files to final build dir browserify "build-tsc/build-wasm/testWasmIntegratedCpp_generated.js" "build-tsc/testWasmIntegrated.js" -o "build-final/testWasmIntegratedApp.js" @@ -22,8 +22,10 @@ # copy WASM binary to output dir cp build-wasm/testWasmIntegratedCpp.wasm build-final/ -echo "...Serving files at http://127.0.0.1:8080/" -echo "Please open build_final/testWasmIntegrated.html" +cp ../test_data/testTestStoneCodeGen.yaml build-final/ +cp ../testCppHandler/test_data/test_Message2.json build-final/cppHandler_test_Message2.json + +echo "...Serving files at http://127.0.0.1:8080/build-final/testWasmIntegrated.html" sudo python3 serve.py diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/testWasmIntegrated/main.cpp --- a/Resources/CodeGeneration/testWasmIntegrated/main.cpp Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/main.cpp Tue May 21 13:27:54 2019 +0200 @@ -1,7 +1,7 @@ #include #include #include -#include "testWasmIntegratedCpp_generated.hpp" +#include "TestStoneCodeGen_generated.hpp" using std::stringstream; @@ -55,35 +55,72 @@ #define HANDLE_MESSAGE(Type,value) \ stringstream ss; \ ss << "Received an instance of:\n" #Type "\n. Here's the dump:\n"; \ - testWasmIntegratedCpp::StoneDumpValue(ss, value, 0); \ + TestStoneCodeGen::StoneDumpValue(ss, value, 0); \ SendFreeTextFromCppJS(ss.str().c_str()); \ return true; -class MyHandler : public testWasmIntegratedCpp::IHandler +#define ECHO_MESSAGE(Type,value) \ + stringstream ss; \ + ss << "Received an instance of:\n" #Type "\n. Here's the dump:\n"; \ + TestStoneCodeGen::StoneDumpValue(ss, value, 0); \ + SendFreeTextFromCppJS(ss.str().c_str()); \ + std::string serializedInCpp = StoneSerialize(value); \ + SendMessageFromCppJS(serializedInCpp.c_str()); \ + return true; + +class MyHandler : public TestStoneCodeGen::IHandler { public: - virtual bool Handle(const testWasmIntegratedCpp::A& value) override + virtual bool Handle(const TestStoneCodeGen::A& value) override { - HANDLE_MESSAGE(testWasmIntegratedCpp::A,value) + HANDLE_MESSAGE(TestStoneCodeGen::A,value) + } + virtual bool Handle(const TestStoneCodeGen::B& value) override + { + HANDLE_MESSAGE(TestStoneCodeGen::B,value) } - virtual bool Handle(const testWasmIntegratedCpp::B& value) override + + virtual bool Handle(const TestStoneCodeGen::Message1& value) override { - HANDLE_MESSAGE(testWasmIntegratedCpp::B,value) + HANDLE_MESSAGE(TestStoneCodeGen::Message1,value) + } + + virtual bool Handle(const TestStoneCodeGen::Message2& value) override + { + HANDLE_MESSAGE(TestStoneCodeGen::Message2,value) } - virtual bool Handle(const testWasmIntegratedCpp::Message1& value) override + virtual bool Handle(const TestStoneCodeGen::C& value) override { - HANDLE_MESSAGE(testWasmIntegratedCpp::Message1,value) + HANDLE_MESSAGE(TestStoneCodeGen::C,value) + } +}; + +class MyEchoHandler : public TestStoneCodeGen::IHandler +{ + public: + virtual bool Handle(const TestStoneCodeGen::A& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::A,value) + } + virtual bool Handle(const TestStoneCodeGen::B& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::B,value) } - virtual bool Handle(const testWasmIntegratedCpp::Message2& value) override + virtual bool Handle(const TestStoneCodeGen::Message1& value) override { - HANDLE_MESSAGE(testWasmIntegratedCpp::Message2,value) + ECHO_MESSAGE(TestStoneCodeGen::Message1,value) } - virtual bool Handle(const testWasmIntegratedCpp::C& value) override + virtual bool Handle(const TestStoneCodeGen::Message2& value) override { - HANDLE_MESSAGE(testWasmIntegratedCpp::C,value) + ECHO_MESSAGE(TestStoneCodeGen::Message2,value) + } + + virtual bool Handle(const TestStoneCodeGen::C& value) override + { + ECHO_MESSAGE(TestStoneCodeGen::C,value) } }; @@ -92,10 +129,29 @@ MyHandler handler; try { - bool handled = testWasmIntegratedCpp::StoneDispatchToHandler(message,&handler); + bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&handler); if(!handled) { - SendFreeTextFromCppJS("This message is valid JSON, but was not recognized!"); + SendFreeTextFromCppJS("This message is valid JSON, but was not handled!"); + } + } + catch(std::exception& e) + { + stringstream ss; + ss << "Error while parsing message: " << e.what() << "\n"; + SendFreeTextFromCppJS(ss.str().c_str()); + } +} + +extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCppForEcho(const char* message) +{ + MyEchoHandler echoHandler; + try + { + bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&echoHandler); + if(!handled) + { + SendFreeTextFromCppJS("This message is valid JSON, but was not handled by the echo handler!"); } } catch(std::exception& e) diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/testWasmIntegrated/styles.css --- a/Resources/CodeGeneration/testWasmIntegrated/styles.css Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/styles.css Tue May 21 13:27:54 2019 +0200 @@ -9,7 +9,7 @@ "Test1 Test2 Test3 Test4 . . ." ". . . . . . ." "Test5 Test6 Test7 Test8 . . ." - ". . . . . . ." + "TestTsCppTs . . . . . ." ". . . . . . ." ; height: 480px; @@ -59,6 +59,8 @@ .TestWasm-Test8 { grid-area: Test8; } +.TestWasm-ts-cpp-ts { grid-area: TestTsCppTs; } + .TestWasm-button { width:80px; } diff -r 853e30d17cae -r 529189f399ec Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.html --- a/Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.html Tue May 21 11:45:06 2019 +0200 +++ b/Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.html Tue May 21 13:27:54 2019 +0200 @@ -31,7 +31,7 @@
- +
@@ -54,6 +54,9 @@
+
+ +