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