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