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