| 1 | // Conditional Catch Test
 | 
|---|
| 2 | 
 | 
|---|
| 3 | // I may fold this back into terminate.cfa and resume.cfa once setting
 | 
|---|
| 4 | // up the non-trivial exception is reasonable to do.
 | 
|---|
| 5 | 
 | 
|---|
| 6 | #include "except-mac.hfa"
 | 
|---|
| 7 | #include <stdio.h>
 | 
|---|
| 8 | 
 | 
|---|
| 9 | DECLARE_EXCEPT(num_error, BASE_EXCEPT,
 | 
|---|
| 10 |         int (*code)(num_error *this);
 | 
|---|
| 11 | );
 | 
|---|
| 12 | 
 | 
|---|
| 13 | struct num_error {
 | 
|---|
| 14 |         VTABLE_FIELD(num_error);
 | 
|---|
| 15 |     char * msg;
 | 
|---|
| 16 |     int num;
 | 
|---|
| 17 | };
 | 
|---|
| 18 | 
 | 
|---|
| 19 | void num_error_msg(num_error * this) {
 | 
|---|
| 20 |     if ( ! this->msg ) {
 | 
|---|
| 21 |         static const char * base = "Num Error with code: X";
 | 
|---|
| 22 |         this->msg = (char *)malloc(22);
 | 
|---|
| 23 |         for (int i = 0 ; (this->msg[i] = base[i]) ; ++i);
 | 
|---|
| 24 |     }
 | 
|---|
| 25 |     this->msg[21] = '0' + this->num;
 | 
|---|
| 26 |     return this->msg;
 | 
|---|
| 27 | }
 | 
|---|
| 28 | void ?{}(num_error & this, int num) {
 | 
|---|
| 29 |         VTABLE_INIT(this, num_error);
 | 
|---|
| 30 |     this.msg = 0;
 | 
|---|
| 31 |     this.num = num;
 | 
|---|
| 32 | }
 | 
|---|
| 33 | void ?{}(num_error & this, num_error & other) {
 | 
|---|
| 34 |     this.virtual_table = other.virtual_table;
 | 
|---|
| 35 |     this.msg = 0;
 | 
|---|
| 36 |     this.num = other.num;
 | 
|---|
| 37 | }
 | 
|---|
| 38 | void copy(num_error * this, num_error * other) {
 | 
|---|
| 39 |         *this = *other;
 | 
|---|
| 40 | }
 | 
|---|
| 41 | void ^?{}(num_error & this) {
 | 
|---|
| 42 |     if( this.msg ) free( this.msg );
 | 
|---|
| 43 | }
 | 
|---|
| 44 | int num_error_code( num_error * this ) {
 | 
|---|
| 45 |     return this->num;
 | 
|---|
| 46 | }
 | 
|---|
| 47 | 
 | 
|---|
| 48 | VTABLE_INSTANCE(num_error, BASE_EXCEPT, copy, ^?{},
 | 
|---|
| 49 |         num_error_msg, num_error_code
 | 
|---|
| 50 | );
 | 
|---|
| 51 | 
 | 
|---|
| 52 | void caught_num_error(int expect, num_error * actual) {
 | 
|---|
| 53 |         printf("Caught num_error: expected=%d actual=%d.\n", expect, actual->num);
 | 
|---|
| 54 | }
 | 
|---|
| 55 | 
 | 
|---|
| 56 | int main(int argc, char * argv[]) {
 | 
|---|
| 57 |         num_error exc = 2;
 | 
|---|
| 58 | 
 | 
|---|
| 59 |         try {
 | 
|---|
| 60 |                 THROW(&exc);
 | 
|---|
| 61 |         } catch (num_error * error ; 3 == error->virtual_table->code( error )) {
 | 
|---|
| 62 |                 caught_num_error(3, error);
 | 
|---|
| 63 |         } catch (num_error * error ; 2 == error->virtual_table->code( error )) {
 | 
|---|
| 64 |                 caught_num_error(2, error);
 | 
|---|
| 65 |         }
 | 
|---|
| 66 | 
 | 
|---|
| 67 |         try {
 | 
|---|
| 68 |                 THROW_RESUME(&exc);
 | 
|---|
| 69 |         } catchResume (num_error * error ; 3 == error->virtual_table->code( error )) {
 | 
|---|
| 70 |                 caught_num_error(3, error);
 | 
|---|
| 71 |         } catchResume (num_error * error ; 2 == error->virtual_table->code( error )) {
 | 
|---|
| 72 |                 caught_num_error(2, error);
 | 
|---|
| 73 |         }
 | 
|---|
| 74 | }
 | 
|---|