Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tests/except-2.c

    re9145a3 rfcc88a4  
    22
    33
    4 #include <stdlib>
    5 #include "except-mac.h"
     4#include <string.h>
    65
     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) \
     13struct name; \
     14struct 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}; \
     21extern TABLE(name) INSTANCE(name); \
     22struct name { \
     23        struct TABLE(name) const * virtual_table; \
     24}; \
     25const char * name##_msg(name * this) { \
     26        return #name; \
     27} \
     28void name##_copy(name * this, name * other) { \
     29        this->virtual_table = other->virtual_table; \
     30} \
     31TABLE(name) INSTANCE(name) @= { \
     32        .parent : &INSTANCE(BASE_EXCEPT), \
     33        .size : sizeof(name), .copy : name##_copy, \
     34        .free : ^?{}, .msg : name##_msg \
     35}; \
     36void ?{}(name * this) { \
     37        this->virtual_table = &INSTANCE(name); \
     38}
    739TRIVIAL_EXCEPTION(yin)
    840TRIVIAL_EXCEPTION(yang)
     
    1850};
    1951extern num_error_vtable INSTANCE(num_error);
    20 
    2152struct num_error {
    2253        struct num_error_vtable const * virtual_table;
     
    2455        int num;
    2556};
    26 
    2757void num_error_msg(num_error * this) {
    2858        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 );
    3261        }
    3362        this->msg[21] = '0' + this->num;
     
    6190        try {
    6291                yin black;
    63                 THROW(&black);
     92                throw (BASE_EXCEPT *)&black;
    6493        } catch ( yin * error ) {
    6594                printf("throw yin caught.\n");
     
    6897        try {
    6998                yang white;
    70                 THROW_RESUME(&white);
     99                throwResume (BASE_EXCEPT *)&white;
    71100                printf("> throwResume returned.\n");
    72101        } catchResume ( yang * error ) {
     
    76105        try {
    77106                num_error x = { 2 };
    78                 THROW(&x);
     107                throw (BASE_EXCEPT *)&x;
    79108        }
    80109        catch (num_error * error ; 3 == error->virtual_table->code( error ) ) {
Note: See TracChangeset for help on using the changeset viewer.