1 | // New draft of exception tests.
|
---|
2 |
|
---|
3 |
|
---|
4 | #include <string.h>
|
---|
5 |
|
---|
6 | // Local Exception Types and manual vtable types.
|
---|
7 | #define BASE_EXCEPT __cfaehm__base_exception_t
|
---|
8 | #define TABLE(name) name##_vtable
|
---|
9 | #define INSTANCE(name) _##name##_vtable_instance
|
---|
10 | #define TRIVIAL_EXCEPTION(name) \
|
---|
11 | struct name; \
|
---|
12 | struct TABLE(name) { \
|
---|
13 | struct __cfaehm__base_exception_t_vtable const * parent; \
|
---|
14 | size_t size; \
|
---|
15 | void (*copy)(name *this, name * other); \
|
---|
16 | void (*free)(name *this); \
|
---|
17 | const char * (*msg)(name *this); \
|
---|
18 | }; \
|
---|
19 | extern TABLE(name) INSTANCE(name); \
|
---|
20 | struct name { \
|
---|
21 | struct TABLE(name) const * virtual_table; \
|
---|
22 | }; \
|
---|
23 | const char * name##_msg(name * this) { \
|
---|
24 | return #name; \
|
---|
25 | } \
|
---|
26 | void name##_copy(name * this, name * other) { \
|
---|
27 | this->virtual_table = other->virtual_table; \
|
---|
28 | } \
|
---|
29 | TABLE(name) INSTANCE(name) @= { \
|
---|
30 | .parent : &INSTANCE(__cfaehm__base_exception_t), \
|
---|
31 | .size : sizeof(name), .copy : name##_copy, \
|
---|
32 | .free : ^?{}, .msg : name##_msg \
|
---|
33 | }; \
|
---|
34 | void ?{}(name * this) { \
|
---|
35 | this->virtual_table = &INSTANCE(name); \
|
---|
36 | }
|
---|
37 | TRIVIAL_EXCEPTION(yin)
|
---|
38 | TRIVIAL_EXCEPTION(yang)
|
---|
39 |
|
---|
40 | struct num_error;
|
---|
41 | struct num_error_vtable {
|
---|
42 | struct exception_t_vtable const * parent;
|
---|
43 | size_t size;
|
---|
44 | void (*copy)(num_error *this, num_error * other);
|
---|
45 | void (*free)(num_error *this);
|
---|
46 | const char * (*msg)(num_error *this);
|
---|
47 | int (*code)(num_error *this);
|
---|
48 | };
|
---|
49 | extern num_error_vtable INSTANCE(num_error);
|
---|
50 | struct num_error {
|
---|
51 | struct num_error_vtable const * virtual_table;
|
---|
52 | char * msg;
|
---|
53 | int num;
|
---|
54 | };
|
---|
55 | void num_error_msg(num_error * this) {
|
---|
56 | if ( ! this->msg ) {
|
---|
57 | const char * base = "Num Error with code: X";
|
---|
58 | this->msg = strdup( base );
|
---|
59 | }
|
---|
60 | this->msg[21] = '0' + this->num;
|
---|
61 | return this->msg;
|
---|
62 | }
|
---|
63 | void ?{}(num_error * this, int num) {
|
---|
64 | this->virtual_table = &_num_error_vtable_instance;
|
---|
65 | this->msg = 0;
|
---|
66 | this->num = num;
|
---|
67 | }
|
---|
68 | void ?{}(num_error * this, num_error * other) {
|
---|
69 | this->virtual_table = other->virtual_table;
|
---|
70 | this->msg = 0;
|
---|
71 | this->num = other->num;
|
---|
72 | }
|
---|
73 | void ^?{}(num_error * this) {
|
---|
74 | if( this->msg ) free( this->msg );
|
---|
75 | }
|
---|
76 | int num_error_code( num_error * this ) {
|
---|
77 | return this->num;
|
---|
78 | }
|
---|
79 | num_error_vtable _num_error_vtable_instance @= {
|
---|
80 | &___cfaehm__base_exception_t_vtable_instance,
|
---|
81 | sizeof(num_error), ?{}, ^?{},
|
---|
82 | num_error_msg, num_error_code
|
---|
83 | };
|
---|
84 |
|
---|
85 |
|
---|
86 | // Test simple throwing, matching and catching.
|
---|
87 | void throw_catch() {
|
---|
88 | try {
|
---|
89 | yin black;
|
---|
90 | throw (BASE_EXCEPT *)&black;
|
---|
91 | } catch( yin * error ) {
|
---|
92 | printf("throw yin caught.\n");
|
---|
93 | }
|
---|
94 |
|
---|
95 | try {
|
---|
96 | yang white;
|
---|
97 | throwResume (BASE_EXCEPT *)&white;
|
---|
98 | printf("> throwResume returned.\n");
|
---|
99 | } catchResume( yang * error ) {
|
---|
100 | printf("throwResume yang caught <");
|
---|
101 | }
|
---|
102 |
|
---|
103 | /* Conditional catches are still a work in progress.
|
---|
104 | try {
|
---|
105 | num_error x = { 2 };
|
---|
106 | throw (struct exception_t *)&x;
|
---|
107 | }
|
---|
108 | catch (num_error * error0 ; 3 == error0->virtual_table->code( error0 ) ) {
|
---|
109 | printf("exception at %p\n", error0 );
|
---|
110 | printf("Should not be printed.\n");
|
---|
111 | }
|
---|
112 | catch (num_error * error1 ; 2 == error1->virtual_table->code( error1 ) ) {
|
---|
113 | printf("Should be printed.\n");
|
---|
114 | }*/
|
---|
115 | }
|
---|
116 |
|
---|
117 | int main (int argc, char * argv[]) {
|
---|
118 | throw_catch();
|
---|
119 | return 0;
|
---|
120 | }
|
---|