1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #include <Ice/Ice.h>
6 #include <TestAMDI.h>
7 #include <TestHelper.h>
8 
9 using namespace Test;
10 using namespace std;
11 
12 bool
endsWith(const string & s,const string & findme)13 endsWith(const string& s, const string& findme)
14 {
15     if(s.length() > findme.length())
16     {
17         return 0 == s.compare(s.length() - findme.length(), findme.length(), findme);
18     }
19     return false;
20 }
21 
ThrowerI()22 ThrowerI::ThrowerI()
23 {
24 }
25 
26 #ifdef ICE_CPP11_MAPPING
27 void
shutdownAsync(function<void ()> response,function<void (exception_ptr)>,const Ice::Current & current)28 ThrowerI::shutdownAsync(function<void()> response,
29                         function<void(exception_ptr)>,
30                         const Ice::Current& current)
31 {
32     current.adapter->getCommunicator()->shutdown();
33     response();
34 }
35 
36 void
supportsUndeclaredExceptionsAsync(function<void (bool)> response,function<void (exception_ptr)>,const Ice::Current &)37 ThrowerI::supportsUndeclaredExceptionsAsync(function<void(bool)> response,
38                                             function<void(exception_ptr)>,
39                                             const Ice::Current&)
40 {
41     response(true);
42 }
43 
44 void
supportsAssertExceptionAsync(function<void (bool)> response,function<void (exception_ptr)>,const Ice::Current &)45 ThrowerI::supportsAssertExceptionAsync(function<void(bool)> response,
46                                        function<void(exception_ptr)>,
47                                        const Ice::Current&)
48 {
49     response(false);
50 }
51 
52 void
throwAasAAsync(int a,function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)53 ThrowerI::throwAasAAsync(int a,
54                          function<void()>,
55                          function<void(exception_ptr)> exception,
56                          const Ice::Current&)
57 {
58     try
59     {
60         A ex;
61         ex.aMem = a;
62         throw ex;
63     }
64     catch(...)
65     {
66         exception(current_exception());
67     }
68 }
69 
70 void
throwAorDasAorDAsync(int a,function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)71 ThrowerI::throwAorDasAorDAsync(int a,
72                                function<void()>,
73                                function<void(exception_ptr)> exception,
74                                const Ice::Current&)
75 {
76     try
77     {
78         if(a > 0)
79         {
80             A ex;
81             ex.aMem = a;
82             throw ex;
83         }
84         else
85         {
86             D ex;
87             ex.dMem = a;
88             throw ex;
89         }
90     }
91     catch(...)
92     {
93         exception(current_exception());
94     }
95 }
96 
97 void
throwBasAAsync(int a,int b,function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)98 ThrowerI::throwBasAAsync(int a, int b,
99                          function<void()>,
100                          function<void(exception_ptr)> exception,
101                          const Ice::Current&)
102 {
103     try
104     {
105         B ex;
106         ex.aMem = a;
107         ex.bMem = b;
108         throw ex;
109     }
110     catch(...)
111     {
112         exception(current_exception());
113     }
114 }
115 
116 void
throwCasAAsync(int a,int b,int c,function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)117 ThrowerI::throwCasAAsync(int a, int b, int c,
118                          function<void()>,
119                          function<void(exception_ptr)> exception,
120                          const Ice::Current&)
121 {
122     try
123     {
124         C ex;
125         ex.aMem = a;
126         ex.bMem = b;
127         ex.cMem = c;
128         throw ex;
129     }
130     catch(...)
131     {
132         exception(current_exception());
133     }
134 }
135 
136 void
throwBasBAsync(int a,int b,function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)137 ThrowerI::throwBasBAsync(int a, int b,
138                          function<void()>,
139                          function<void(exception_ptr)> exception,
140                          const Ice::Current&)
141 {
142     try
143     {
144         B ex;
145         ex.aMem = a;
146         ex.bMem = b;
147         throw ex;
148     }
149     catch(...)
150     {
151         exception(current_exception());
152     }
153 }
154 
155 void
throwCasBAsync(int a,int b,int c,function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)156 ThrowerI::throwCasBAsync(int a, int b, int c,
157                          function<void()>,
158                          function<void(exception_ptr)> exception,
159                          const Ice::Current&)
160 {
161     try
162     {
163         C ex;
164         ex.aMem = a;
165         ex.bMem = b;
166         ex.cMem = c;
167         throw ex;
168     }
169     catch(...)
170     {
171         exception(current_exception());
172     }
173 }
174 
175 void
throwCasCAsync(int a,int b,int c,function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)176 ThrowerI::throwCasCAsync(int a, int b, int c,
177                          function<void()>,
178                          function<void(exception_ptr)> exception,
179                          const Ice::Current&)
180 {
181     try
182     {
183         C ex;
184         ex.aMem = a;
185         ex.bMem = b;
186         ex.cMem = c;
187         throw ex;
188     }
189     catch(...)
190     {
191         exception(current_exception());
192     }
193 }
194 
195 void
throwModAAsync(int a,int a2,function<void ()>,function<void (exception_ptr)>,const Ice::Current &)196 ThrowerI::throwModAAsync(int a, int a2,
197                          function<void()>,
198                          function<void(exception_ptr)>,
199                          const Ice::Current&)
200 {
201     Mod::A ex;
202     ex.aMem = a;
203     ex.a2Mem = a2;
204     throw ex;
205 }
206 
207 void
throwUndeclaredAAsync(int a,function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)208 ThrowerI::throwUndeclaredAAsync(int a,
209                                 function<void()>,
210                                 function<void(exception_ptr)> exception,
211                                 const Ice::Current&)
212 {
213     try
214     {
215         A ex;
216         ex.aMem = a;
217         throw ex;
218     }
219     catch(...)
220     {
221         exception(current_exception());
222     }
223 }
224 
225 void
throwUndeclaredBAsync(int a,int b,function<void ()>,function<void (exception_ptr)>,const Ice::Current &)226 ThrowerI::throwUndeclaredBAsync(int a, int b,
227                                 function<void()>,
228                                 function<void(exception_ptr)>,
229                                 const Ice::Current&)
230 {
231     B ex;
232     ex.aMem = a;
233     ex.bMem = b;
234     throw ex;
235 }
236 
237 void
throwUndeclaredCAsync(int a,int b,int c,function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)238 ThrowerI::throwUndeclaredCAsync(int a, int b, int c,
239                                 function<void()>,
240                                 function<void(exception_ptr)> exception,
241                                 const Ice::Current&)
242 {
243     try
244     {
245         C ex;
246         ex.aMem = a;
247         ex.bMem = b;
248         ex.cMem = c;
249         throw ex;
250     }
251     catch(...)
252     {
253         exception(current_exception());
254     }
255 }
256 
257 void
throwLocalExceptionAsync(function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)258 ThrowerI::throwLocalExceptionAsync(function<void()>,
259                                    function<void(exception_ptr)> exception,
260                                    const Ice::Current&)
261 {
262     try
263     {
264         throw Ice::TimeoutException(__FILE__, __LINE__);
265     }
266     catch(...)
267     {
268         exception(current_exception());
269     }
270 }
271 
272 void
throwNonIceExceptionAsync(function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)273 ThrowerI::throwNonIceExceptionAsync(function<void()>,
274                                     function<void(exception_ptr)> exception,
275                                     const Ice::Current&)
276 {
277     try
278     {
279         throw int(12345);
280     }
281     catch(...)
282     {
283         exception(current_exception());
284     }
285 }
286 
287 void
throwAssertExceptionAsync(function<void ()>,function<void (exception_ptr)>,const Ice::Current &)288 ThrowerI::throwAssertExceptionAsync(function<void()>,
289                                     function<void(exception_ptr)>,
290                                     const Ice::Current&)
291 {
292     assert(false); // No supported in C++
293 }
294 
295 void
throwMemoryLimitExceptionAsync(ICE_IN (Ice::ByteSeq),function<void (const Ice::ByteSeq &)> response,function<void (exception_ptr)>,const Ice::Current &)296 ThrowerI::throwMemoryLimitExceptionAsync(ICE_IN(Ice::ByteSeq),
297                                          function<void(const Ice::ByteSeq&)> response,
298                                          function<void(exception_ptr)>,
299                                          const Ice::Current&)
300 {
301     response(Ice::ByteSeq(1024 * 20)); // 20 KB.
302 }
303 
304 void
throwLocalExceptionIdempotentAsync(function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)305 ThrowerI::throwLocalExceptionIdempotentAsync(function<void()>,
306                                              function<void(exception_ptr)> exception,
307                                              const Ice::Current&)
308 {
309     try
310     {
311         throw Ice::TimeoutException(__FILE__, __LINE__);
312     }
313     catch(...)
314     {
315         exception(current_exception());
316     }
317 
318 }
319 
320 void
throwAfterResponseAsync(function<void ()> response,function<void (exception_ptr)>,const Ice::Current &)321 ThrowerI::throwAfterResponseAsync(function<void()> response,
322                                   function<void(exception_ptr)>,
323                                   const Ice::Current&)
324 {
325     response();
326 
327     throw std::string();
328 }
329 
330 void
throwAfterExceptionAsync(function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)331 ThrowerI::throwAfterExceptionAsync(function<void()>,
332                                    function<void(exception_ptr)> exception,
333                                    const Ice::Current&)
334 {
335     try
336     {
337         throw A(12345);
338     }
339     catch(...)
340     {
341         exception(current_exception());
342     }
343     throw std::string();
344 }
345 
346 void
throwEAsync(function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)347 ThrowerI::throwEAsync(function<void()>,
348                       function<void(exception_ptr)> exception,
349                       const Ice::Current&)
350 {
351     try
352     {
353         throw E("E");
354     }
355     catch(...)
356     {
357         exception(current_exception());
358     }
359 }
360 
361 void
throwFAsync(function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)362 ThrowerI::throwFAsync(function<void()>,
363                       function<void(exception_ptr)> exception,
364                       const Ice::Current&)
365 {
366     try
367     {
368         throw F("F");
369     }
370     catch(...)
371     {
372         exception(current_exception());
373     }
374 }
375 
376 void
throwGAsync(function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)377 ThrowerI::throwGAsync(function<void()>,
378                       function<void(exception_ptr)> exception,
379                       const Ice::Current&)
380 {
381     try
382     {
383         throw G(__FILE__, __LINE__, "G");
384     }
385     catch(const G& ex)
386     {
387         ostringstream os;
388         ex.ice_print(os);
389         test(endsWith(os.str(), "Test::G"));
390         test(ex.data == "G");
391         exception(current_exception());
392     }
393 }
394 
395 void
throwHAsync(function<void ()>,function<void (exception_ptr)> exception,const Ice::Current &)396 ThrowerI::throwHAsync(function<void()>,
397                       function<void(exception_ptr)> exception,
398                       const Ice::Current&)
399 {
400     try
401     {
402         throw H(__FILE__, __LINE__, "H");
403     }
404     catch(const H& ex)
405     {
406         ostringstream os;
407         ex.ice_print(os);
408         test(endsWith(os.str(), "Test::H data:'H'"));
409         test(ex.data == "H");
410         exception(current_exception());
411     }
412 }
413 #else
414 
415 void
shutdown_async(const AMD_Thrower_shutdownPtr & cb,const Ice::Current & current)416 ThrowerI::shutdown_async(const AMD_Thrower_shutdownPtr& cb,
417                          const Ice::Current& current)
418 {
419     current.adapter->getCommunicator()->shutdown();
420     cb->ice_response();
421 }
422 
423 void
supportsUndeclaredExceptions_async(const AMD_Thrower_supportsUndeclaredExceptionsPtr & cb,const Ice::Current &)424 ThrowerI::supportsUndeclaredExceptions_async(const AMD_Thrower_supportsUndeclaredExceptionsPtr& cb,
425                                              const Ice::Current&)
426 {
427     cb->ice_response(true);
428 }
429 
430 void
supportsAssertException_async(const AMD_Thrower_supportsAssertExceptionPtr & cb,const Ice::Current &)431 ThrowerI::supportsAssertException_async(const AMD_Thrower_supportsAssertExceptionPtr& cb,
432                                         const Ice::Current&)
433 {
434     cb->ice_response(false);
435 }
436 
437 void
throwAasA_async(const AMD_Thrower_throwAasAPtr & cb,Ice::Int a,const Ice::Current &)438 ThrowerI::throwAasA_async(const AMD_Thrower_throwAasAPtr& cb,
439                           Ice::Int a, const Ice::Current&)
440 {
441     A ex;
442     ex.aMem = a;
443     cb->ice_exception(ex);
444 }
445 
446 void
throwAorDasAorD_async(const AMD_Thrower_throwAorDasAorDPtr & cb,Ice::Int a,const Ice::Current &)447 ThrowerI::throwAorDasAorD_async(const AMD_Thrower_throwAorDasAorDPtr& cb,
448                                 Ice::Int a, const Ice::Current&)
449 {
450     if(a > 0)
451     {
452         A ex;
453         ex.aMem = a;
454         cb->ice_exception(ex);
455     }
456     else
457     {
458         D ex;
459         ex.dMem = a;
460         cb->ice_exception(ex);
461     }
462 }
463 
464 void
throwBasA_async(const AMD_Thrower_throwBasAPtr &,Ice::Int a,Ice::Int b,const Ice::Current &)465 ThrowerI::throwBasA_async(const AMD_Thrower_throwBasAPtr& /*cb*/,
466                           Ice::Int a, Ice::Int b, const Ice::Current&)
467 {
468     B ex;
469     ex.aMem = a;
470     ex.bMem = b;
471     throw ex;
472     //cb->ice_exception(ex);
473 }
474 
475 void
throwCasA_async(const AMD_Thrower_throwCasAPtr & cb,Ice::Int a,Ice::Int b,Ice::Int c,const Ice::Current &)476 ThrowerI::throwCasA_async(const AMD_Thrower_throwCasAPtr& cb,
477                           Ice::Int a, Ice::Int b, Ice::Int c, const Ice::Current&)
478 {
479     C ex;
480     ex.aMem = a;
481     ex.bMem = b;
482     ex.cMem = c;
483     cb->ice_exception(ex);
484 }
485 
486 void
throwBasB_async(const AMD_Thrower_throwBasBPtr &,Ice::Int a,Ice::Int b,const Ice::Current &)487 ThrowerI::throwBasB_async(const AMD_Thrower_throwBasBPtr& /*cb*/,
488                           Ice::Int a, Ice::Int b, const Ice::Current&)
489 {
490     B ex;
491     ex.aMem = a;
492     ex.bMem = b;
493     throw ex;
494     //cb->ice_exception(ex);
495 }
496 
497 void
throwCasB_async(const AMD_Thrower_throwCasBPtr & cb,Ice::Int a,Ice::Int b,Ice::Int c,const Ice::Current &)498 ThrowerI::throwCasB_async(const AMD_Thrower_throwCasBPtr& cb,
499                           Ice::Int a, Ice::Int b, Ice::Int c, const Ice::Current&)
500 {
501     C ex;
502     ex.aMem = a;
503     ex.bMem = b;
504     ex.cMem = c;
505     cb->ice_exception(ex);
506 }
507 
508 void
throwCasC_async(const AMD_Thrower_throwCasCPtr & cb,Ice::Int a,Ice::Int b,Ice::Int c,const Ice::Current &)509 ThrowerI::throwCasC_async(const AMD_Thrower_throwCasCPtr& cb,
510                           Ice::Int a, Ice::Int b, Ice::Int c, const Ice::Current&)
511 {
512     C ex;
513     ex.aMem = a;
514     ex.bMem = b;
515     ex.cMem = c;
516     cb->ice_exception(ex);
517 }
518 
519 void
throwModA_async(const AMD_Thrower_throwModAPtr &,Ice::Int a,Ice::Int a2,const Ice::Current &)520 ThrowerI::throwModA_async(const AMD_Thrower_throwModAPtr& /*cb*/,
521                           Ice::Int a, Ice::Int a2, const Ice::Current&)
522 {
523     Mod::A ex;
524     ex.aMem = a;
525     ex.a2Mem = a2;
526     throw ex;
527 }
528 
529 void
throwUndeclaredA_async(const AMD_Thrower_throwUndeclaredAPtr & cb,Ice::Int a,const Ice::Current &)530 ThrowerI::throwUndeclaredA_async(const AMD_Thrower_throwUndeclaredAPtr& cb,
531                                  Ice::Int a, const Ice::Current&)
532 {
533     A ex;
534     ex.aMem = a;
535     cb->ice_exception(ex);
536 }
537 
538 void
throwUndeclaredB_async(const AMD_Thrower_throwUndeclaredBPtr &,Ice::Int a,Ice::Int b,const Ice::Current &)539 ThrowerI::throwUndeclaredB_async(const AMD_Thrower_throwUndeclaredBPtr& /*cb*/,
540                                  Ice::Int a, Ice::Int b, const Ice::Current&)
541 {
542     B ex;
543     ex.aMem = a;
544     ex.bMem = b;
545     throw ex;
546     //cb->ice_exception(ex);
547 }
548 
549 void
throwUndeclaredC_async(const AMD_Thrower_throwUndeclaredCPtr & cb,Ice::Int a,Ice::Int b,Ice::Int c,const Ice::Current &)550 ThrowerI::throwUndeclaredC_async(const AMD_Thrower_throwUndeclaredCPtr& cb,
551                                  Ice::Int a, Ice::Int b, Ice::Int c, const Ice::Current&)
552 {
553     C ex;
554     ex.aMem = a;
555     ex.bMem = b;
556     ex.cMem = c;
557     cb->ice_exception(ex);
558 }
559 
560 void
throwLocalException_async(const AMD_Thrower_throwLocalExceptionPtr & cb,const Ice::Current &)561 ThrowerI::throwLocalException_async(const AMD_Thrower_throwLocalExceptionPtr& cb,
562                                     const Ice::Current&)
563 {
564     cb->ice_exception(Ice::TimeoutException(__FILE__, __LINE__));
565 }
566 
567 void
throwNonIceException_async(const AMD_Thrower_throwNonIceExceptionPtr &,const Ice::Current &)568 ThrowerI::throwNonIceException_async(const AMD_Thrower_throwNonIceExceptionPtr&,
569                                      const Ice::Current&)
570 {
571     throw int(12345);
572 }
573 
574 void
throwAssertException_async(const AMD_Thrower_throwAssertExceptionPtr &,const Ice::Current &)575 ThrowerI::throwAssertException_async(const AMD_Thrower_throwAssertExceptionPtr&,
576                                      const Ice::Current&)
577 {
578     assert(false); // Not supported in C++.
579 }
580 
581 void
throwMemoryLimitException_async(const AMD_Thrower_throwMemoryLimitExceptionPtr & cb,const Ice::ByteSeq &,const Ice::Current &)582 ThrowerI::throwMemoryLimitException_async(const AMD_Thrower_throwMemoryLimitExceptionPtr& cb,
583                                           const Ice::ByteSeq&, const Ice::Current&)
584 {
585     cb->ice_response(Ice::ByteSeq(1024 * 20)); // 20 KB.
586 }
587 
588 void
throwLocalExceptionIdempotent_async(const AMD_Thrower_throwLocalExceptionIdempotentPtr & cb,const Ice::Current &)589 ThrowerI::throwLocalExceptionIdempotent_async(const AMD_Thrower_throwLocalExceptionIdempotentPtr& cb,
590                                               const Ice::Current&)
591 {
592     cb->ice_exception(Ice::TimeoutException(__FILE__, __LINE__));
593 }
594 
595 void
throwAfterResponse_async(const AMD_Thrower_throwAfterResponsePtr & cb,const Ice::Current &)596 ThrowerI::throwAfterResponse_async(const AMD_Thrower_throwAfterResponsePtr& cb, const Ice::Current&)
597 {
598     cb->ice_response();
599 
600     throw std::string();
601 }
602 
603 void
throwAfterException_async(const AMD_Thrower_throwAfterExceptionPtr & cb,const Ice::Current &)604 ThrowerI::throwAfterException_async(const AMD_Thrower_throwAfterExceptionPtr& cb, const Ice::Current&)
605 {
606     cb->ice_exception(A(12345));
607 
608     throw std::string();
609 }
610 
611 void
throwE_async(const Test::AMD_Thrower_throwEPtr & cb,const Ice::Current &)612 ThrowerI::throwE_async(const Test::AMD_Thrower_throwEPtr& cb, const Ice::Current&)
613 {
614     cb->ice_exception(E("E"));
615 }
616 
617 void
throwF_async(const Test::AMD_Thrower_throwFPtr & cb,const Ice::Current &)618 ThrowerI::throwF_async(const Test::AMD_Thrower_throwFPtr& cb, const Ice::Current&)
619 {
620     cb->ice_exception(F("F"));
621 }
622 
623 void
throwG_async(const Test::AMD_Thrower_throwGPtr & cb,const Ice::Current &)624 ThrowerI::throwG_async(const Test::AMD_Thrower_throwGPtr& cb, const Ice::Current&)
625 {
626     try
627     {
628         throw G(__FILE__, __LINE__, "G");
629     }
630     catch(const G& ex)
631     {
632         ostringstream os;
633         ex.ice_print(os);
634         test(endsWith(os.str(), "Test::G"));
635         test(ex.data == "G");
636         cb->ice_exception(ex);
637     }
638 }
639 
640 void
throwH_async(const Test::AMD_Thrower_throwHPtr & cb,const Ice::Current &)641 ThrowerI::throwH_async(const Test::AMD_Thrower_throwHPtr& cb, const Ice::Current&)
642 {
643     try
644     {
645         throw H(__FILE__, __LINE__, "H");
646     }
647     catch(const H& ex)
648     {
649         ostringstream os;
650         ex.ice_print(os);
651         test(endsWith(os.str(), "Test::H data:'H'"));
652         test(ex.data == "H");
653         cb->ice_exception(ex);
654     }
655 }
656 #endif
657