Changes in src/tests/except-2.c [e9145a3:fcc88a4]
- File:
-
- 1 edited
-
src/tests/except-2.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/tests/except-2.c
re9145a3 rfcc88a4 2 2 3 3 4 #include <stdlib> 5 #include "except-mac.h" 4 #include <string.h> 6 5 6 // Local Exception Types and manual vtable types. 7 #define GLUE2(left, right) left##right 8 #define GLUE3(left, middle, right) left##middle##right 9 #define BASE_EXCEPT __cfaehm__base_exception_t 10 #define TABLE(name) GLUE2(name,_vtable) 11 #define INSTANCE(name) GLUE3(_,name,_vtable_instance) 12 #define TRIVIAL_EXCEPTION(name) \ 13 struct name; \ 14 struct TABLE(name) { \ 15 struct TABLE(BASE_EXCEPT) const * parent; \ 16 size_t size; \ 17 void (*copy)(name *this, name * other); \ 18 void (*free)(name *this); \ 19 const char * (*msg)(name *this); \ 20 }; \ 21 extern TABLE(name) INSTANCE(name); \ 22 struct name { \ 23 struct TABLE(name) const * virtual_table; \ 24 }; \ 25 const char * name##_msg(name * this) { \ 26 return #name; \ 27 } \ 28 void name##_copy(name * this, name * other) { \ 29 this->virtual_table = other->virtual_table; \ 30 } \ 31 TABLE(name) INSTANCE(name) @= { \ 32 .parent : &INSTANCE(BASE_EXCEPT), \ 33 .size : sizeof(name), .copy : name##_copy, \ 34 .free : ^?{}, .msg : name##_msg \ 35 }; \ 36 void ?{}(name * this) { \ 37 this->virtual_table = &INSTANCE(name); \ 38 } 7 39 TRIVIAL_EXCEPTION(yin) 8 40 TRIVIAL_EXCEPTION(yang) … … 18 50 }; 19 51 extern num_error_vtable INSTANCE(num_error); 20 21 52 struct num_error { 22 53 struct num_error_vtable const * virtual_table; … … 24 55 int num; 25 56 }; 26 27 57 void num_error_msg(num_error * this) { 28 58 if ( ! this->msg ) { 29 static const char * base = "Num Error with code: X"; 30 this->msg = malloc(22); 31 for (int i = 0 ; (this->msg[i] = base[i]) ; ++i); 59 const char * base = "Num Error with code: X"; 60 this->msg = strdup( base ); 32 61 } 33 62 this->msg[21] = '0' + this->num; … … 61 90 try { 62 91 yin black; 63 THROW(&black);92 throw (BASE_EXCEPT *)&black; 64 93 } catch ( yin * error ) { 65 94 printf("throw yin caught.\n"); … … 68 97 try { 69 98 yang white; 70 THROW_RESUME(&white);99 throwResume (BASE_EXCEPT *)&white; 71 100 printf("> throwResume returned.\n"); 72 101 } catchResume ( yang * error ) { … … 76 105 try { 77 106 num_error x = { 2 }; 78 THROW(&x);107 throw (BASE_EXCEPT *)&x; 79 108 } 80 109 catch (num_error * error ; 3 == error->virtual_table->code( error ) ) {
Note:
See TracChangeset
for help on using the changeset viewer.