Mercurial > hg > orthanc-stone
comparison Resources/CodeGeneration/testWasmIntegrated/main.cpp @ 690:f185cfcb72a0 am-dev
CodeGen: tests improvements
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Thu, 16 May 2019 19:10:38 +0200 |
parents | 8c0b073efda8 |
children |
comparison
equal
deleted
inserted
replaced
688:8c0b073efda8 | 690:f185cfcb72a0 |
---|---|
57 ss << "Received an instance of:\n" #Type "\n. Here's the dump:\n"; \ | 57 ss << "Received an instance of:\n" #Type "\n. Here's the dump:\n"; \ |
58 TestStoneCodeGen::StoneDumpValue(ss, value, 0); \ | 58 TestStoneCodeGen::StoneDumpValue(ss, value, 0); \ |
59 SendFreeTextFromCppJS(ss.str().c_str()); \ | 59 SendFreeTextFromCppJS(ss.str().c_str()); \ |
60 return true; | 60 return true; |
61 | 61 |
62 #define ECHO_MESSAGE(Type,value) \ | |
63 stringstream ss; \ | |
64 ss << "Received an instance of:\n" #Type "\n. Here's the dump:\n"; \ | |
65 TestStoneCodeGen::StoneDumpValue(ss, value, 0); \ | |
66 SendFreeTextFromCppJS(ss.str().c_str()); \ | |
67 std::string serializedInCpp = StoneSerialize(value); \ | |
68 SendMessageFromCppJS(serializedInCpp.c_str()); \ | |
69 return true; | |
70 | |
62 class MyHandler : public TestStoneCodeGen::IHandler | 71 class MyHandler : public TestStoneCodeGen::IHandler |
63 { | 72 { |
64 public: | 73 public: |
65 virtual bool Handle(const TestStoneCodeGen::A& value) override | 74 virtual bool Handle(const TestStoneCodeGen::A& value) override |
66 { | 75 { |
85 { | 94 { |
86 HANDLE_MESSAGE(TestStoneCodeGen::C,value) | 95 HANDLE_MESSAGE(TestStoneCodeGen::C,value) |
87 } | 96 } |
88 }; | 97 }; |
89 | 98 |
99 class MyEchoHandler : public TestStoneCodeGen::IHandler | |
100 { | |
101 public: | |
102 virtual bool Handle(const TestStoneCodeGen::A& value) override | |
103 { | |
104 ECHO_MESSAGE(TestStoneCodeGen::A,value) | |
105 } | |
106 virtual bool Handle(const TestStoneCodeGen::B& value) override | |
107 { | |
108 ECHO_MESSAGE(TestStoneCodeGen::B,value) | |
109 } | |
110 | |
111 virtual bool Handle(const TestStoneCodeGen::Message1& value) override | |
112 { | |
113 ECHO_MESSAGE(TestStoneCodeGen::Message1,value) | |
114 } | |
115 | |
116 virtual bool Handle(const TestStoneCodeGen::Message2& value) override | |
117 { | |
118 ECHO_MESSAGE(TestStoneCodeGen::Message2,value) | |
119 } | |
120 | |
121 virtual bool Handle(const TestStoneCodeGen::C& value) override | |
122 { | |
123 ECHO_MESSAGE(TestStoneCodeGen::C,value) | |
124 } | |
125 }; | |
126 | |
90 extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCpp(const char* message) | 127 extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCpp(const char* message) |
91 { | 128 { |
92 MyHandler handler; | 129 MyHandler handler; |
93 try | 130 try |
94 { | 131 { |
95 bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&handler); | 132 bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&handler); |
96 if(!handled) | 133 if(!handled) |
97 { | 134 { |
98 SendFreeTextFromCppJS("This message is valid JSON, but was not handled!"); | 135 SendFreeTextFromCppJS("This message is valid JSON, but was not handled!"); |
136 } | |
137 } | |
138 catch(std::exception& e) | |
139 { | |
140 stringstream ss; | |
141 ss << "Error while parsing message: " << e.what() << "\n"; | |
142 SendFreeTextFromCppJS(ss.str().c_str()); | |
143 } | |
144 } | |
145 | |
146 extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCppForEcho(const char* message) | |
147 { | |
148 MyEchoHandler echoHandler; | |
149 try | |
150 { | |
151 bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&echoHandler); | |
152 if(!handled) | |
153 { | |
154 SendFreeTextFromCppJS("This message is valid JSON, but was not handled by the echo handler!"); | |
99 } | 155 } |
100 } | 156 } |
101 catch(std::exception& e) | 157 catch(std::exception& e) |
102 { | 158 { |
103 stringstream ss; | 159 stringstream ss; |