- 论坛徽章:
- 0
|
下面是实现:- #include "file.h"
- #ifdef _CUA_MT
- # include "mt.h"
- #endif
- #include "except.h"
- #include <assert.h>
- #include <stdlib.h>
- #include <stdio.h>
- #ifdef _CUA_MT
- static u_long __s_cua_exception_tvi;
- #else
- static cua_exception_info *__s_cua_exception = NULL;
- #endif
- #ifdef _CUA_MT
- static cua_exception_info *__get_current()
- {
- cua_exception_info *p;
- __cua_thread_get_variable(__s_cua_exception_tvi, &p);
- return p;
- }
- static bool __set_current( cua_exception_info *p )
- {
- return __cua_thread_set_variable(__s_cua_exception_tvi, p);
- }
- #else
- static cua_exception_info *__get_current()
- {
- return &__s_cua_exception;
- }
- static bool __set_current( cua_exception_info *p )
- {
- __s_cua_exception = p;
- return true;
- }
- #endif
- int __cua_except_init()
- {
- #ifdef _CUA_MT
- __s_cua_exception_tvi = __cua_thread_variable_alloc();
- #endif
- return 0;
- }
- int __cua_except_uninit()
- {
- return 0;
- }
- bool __cua_except_begin( cua_exception_info *p )
- {
- p->state = 0;
- p->clss = 0;
- p->code = 0;
- p->info = 0;
- p->prev = __get_current();
- return __set_current(p);
- }
- int __cua_except_end( cua_exception_info *p )
- {
- __set_current(p->prev);
- if(p->state < 4)
- p->state += 4;
- if(p->state == 1 + 4 || p->state == 3 + 4)
- __cua_except_throw(p->clss, p->code, p->info);
-
- return 0;
- }
- int __cua_except_throw( u_long clss, long cd, unichar const *txt )
- {
- cua_exception_info *p;
- if(!clss) {
- CUA_PANIC(_ctext("__cua_except_throw class 0"));
- return -1;
- }
-
- p = __get_current();
- if(!p) {
- unichar buff[512];
- __cua_sprintf(buff, 512, _ctext("unhandled cua exception (%u, %ld) %s\n"), clss, cd, txt);
- CUA_PANIC(buff);
- return -1;
- }
- CASSERT(p->state != 1);
- if(p->state == 0)
- p->state = 1;
- else if(p->state >= 4)
- p->state = 4 + 3;
- else
- p->state = 3;
- p->clss = clss;
- p->code = cd;
- p->info = txt;
- longjmp(p->jmp, 1);
- return 0;
- }
- bool __cua_except_catch( cua_exception_info *p, u_long clss, cua_exception_match_t match )
- {
- if(p->state != 1)
- return false;
-
- if(match ? match(clss, p->clss) : (clss == 0 || clss == p->clss)) {
- p->state = 2;
- return true;
- }
- return false;
- }
- bool __cua_except_finally( cua_exception_info *p )
- {
- if(p->state >= 4)
- return false;
- p->state += 4;
- return true;
- }
复制代码 |
|