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