| 1 | // Tests for providing new default operations.
 | 
|---|
| 2 | 
 | 
|---|
| 3 | #include <string.h>
 | 
|---|
| 4 | #include <exception.hfa>
 | 
|---|
| 5 | 
 | 
|---|
| 6 | exception log_message {
 | 
|---|
| 7 |         char * msg;
 | 
|---|
| 8 | };
 | 
|---|
| 9 | 
 | 
|---|
| 10 | _EHM_DEFINE_COPY(log_message, )
 | 
|---|
| 11 | const char * msg(log_message * this) {
 | 
|---|
| 12 |         return this->msg;
 | 
|---|
| 13 | }
 | 
|---|
| 14 | _EHM_VIRTUAL_TABLE(log_message, , log_vt);
 | 
|---|
| 15 | 
 | 
|---|
| 16 | // Logging messages don't have to be handled.
 | 
|---|
| 17 | void defaultResumptionHandler(log_message &) {}
 | 
|---|
| 18 | 
 | 
|---|
| 19 | // And should never be used to terminate computation.
 | 
|---|
| 20 | void defaultTerminationHandler(log_message &) = void;
 | 
|---|
| 21 | 
 | 
|---|
| 22 | void log_test(void) {
 | 
|---|
| 23 |         // We can catch log:
 | 
|---|
| 24 |         try {
 | 
|---|
| 25 |                 throwResume (log_message){&log_vt, "Should be printed.\n"};
 | 
|---|
| 26 |         } catchResume (log_message * this) {
 | 
|---|
| 27 |                 printf("%s", this->virtual_table->msg(this));
 | 
|---|
| 28 |         }
 | 
|---|
| 29 |         // But we don't have to:
 | 
|---|
| 30 |         throwResume (log_message){&log_vt, "Should not be printed.\n"};
 | 
|---|
| 31 | }
 | 
|---|
| 32 | 
 | 
|---|
| 33 | // I don't have a good use case for doing the same with termination.
 | 
|---|
| 34 | exception jump {};
 | 
|---|
| 35 | void defaultTerminationHandler(jump &) {
 | 
|---|
| 36 |         printf("jump default handler.\n");
 | 
|---|
| 37 | }
 | 
|---|
| 38 | 
 | 
|---|
| 39 | vtable(jump) jump_vt;
 | 
|---|
| 40 | 
 | 
|---|
| 41 | void jump_test(void) {
 | 
|---|
| 42 |         try {
 | 
|---|
| 43 |                 throw (jump){&jump_vt};
 | 
|---|
| 44 |         } catch (jump * this) {
 | 
|---|
| 45 |                 printf("jump catch handler.\n");
 | 
|---|
| 46 |         }
 | 
|---|
| 47 |         throw (jump){&jump_vt};
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | exception first {};
 | 
|---|
| 51 | vtable(first) first_vt;
 | 
|---|
| 52 | 
 | 
|---|
| 53 | exception unhandled_exception {};
 | 
|---|
| 54 | vtable(unhandled_exception) unhandled_vt;
 | 
|---|
| 55 | 
 | 
|---|
| 56 | void unhandled_test(void) {
 | 
|---|
| 57 |         forall(T &, V & | is_exception(T, V))
 | 
|---|
| 58 |         void defaultTerminationHandler(T &) {
 | 
|---|
| 59 |                 throw (unhandled_exception){&unhandled_vt};
 | 
|---|
| 60 |         }
 | 
|---|
| 61 |         void defaultTerminationHandler(unhandled_exception &) {
 | 
|---|
| 62 |                 abort();
 | 
|---|
| 63 |         }
 | 
|---|
| 64 |         try {
 | 
|---|
| 65 |                 throw (first){&first_vt};
 | 
|---|
| 66 |         } catch (unhandled_exception * t) {
 | 
|---|
| 67 |                 printf("Catch unhandled_exception.\n");
 | 
|---|
| 68 |         }
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | exception second {};
 | 
|---|
| 72 | vtable(second) second_vt;
 | 
|---|
| 73 | 
 | 
|---|
| 74 | void cross_test(void) {
 | 
|---|
| 75 |         void defaultTerminationHandler(first &) {
 | 
|---|
| 76 |                 printf("cross terminate default\n");
 | 
|---|
| 77 |                 throw (second){&second_vt};
 | 
|---|
| 78 |         }
 | 
|---|
| 79 |         void defaultResumptionHandler(first &) {
 | 
|---|
| 80 |                 printf("cross resume default\n");
 | 
|---|
| 81 |                 throwResume (second){&second_vt};
 | 
|---|
| 82 |         }
 | 
|---|
| 83 |         try {
 | 
|---|
| 84 |                 printf("cross terminate throw\n");
 | 
|---|
| 85 |                 throw (first){&first_vt};
 | 
|---|
| 86 |         } catch (second *) {
 | 
|---|
| 87 |                 printf("cross terminate catch\n");
 | 
|---|
| 88 |         }
 | 
|---|
| 89 |         try {
 | 
|---|
| 90 |                 printf("cross resume throw\n");
 | 
|---|
| 91 |                 throwResume (first){&first_vt};
 | 
|---|
| 92 |         } catchResume (second *) {
 | 
|---|
| 93 |                 printf("cross resume catch\n");
 | 
|---|
| 94 |         }
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | int main(int argc, char * argv[]) {
 | 
|---|
| 98 |         log_test();
 | 
|---|
| 99 |         jump_test();
 | 
|---|
| 100 |         unhandled_test();
 | 
|---|
| 101 |         cross_test();
 | 
|---|
| 102 | }
 | 
|---|