1 |
johnpye |
1144 |
/* ASCEND modelling environment |
2 |
|
|
Copyright (C) 1997 Benjamin Andrew Allan |
3 |
|
|
Copyright (C) 2006 Carnegie Mellon University |
4 |
|
|
|
5 |
|
|
This program is free software; you can redistribute it and/or modify |
6 |
|
|
it under the terms of the GNU General Public License as published by |
7 |
|
|
the Free Software Foundation; either version 2, or (at your option) |
8 |
|
|
any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License |
16 |
|
|
along with this program; if not, write to the Free Software |
17 |
|
|
Foundation, Inc., 59 Temple Place - Suite 330, |
18 |
|
|
Boston, MA 02111-1307, USA. |
19 |
|
|
*//* @file |
20 |
|
|
Some nice syntax for exceptions in C. |
21 |
|
|
See http://www.swig.org/Doc1.1/HTML/Exceptions.html#n3 |
22 |
|
|
|
23 |
|
|
Note that you will enter a world of pain if you attempt to nest these |
24 |
|
|
try...catch statements. |
25 |
|
|
|
26 |
|
|
Usage example from the above link |
27 |
|
|
@CODE |
28 |
|
|
try { |
29 |
|
|
$function |
30 |
|
|
} catch(RangeError) { |
31 |
|
|
croak("Range Error"); |
32 |
|
|
} catch(DivisionByZero) { |
33 |
|
|
croak("Division by zero"); |
34 |
|
|
} catch(OutOfMemory) { |
35 |
|
|
croak("Out of memory"); |
36 |
|
|
} finally { |
37 |
|
|
croak("Unknown exception"); |
38 |
|
|
} |
39 |
|
|
@ENDCODE |
40 |
|
|
|
41 |
|
|
*/ |
42 |
|
|
#ifndef ASC_EXCEPT_H |
43 |
|
|
#define ASC_EXCEPT_H |
44 |
|
|
|
45 |
|
|
/* File : except.h */ |
46 |
|
|
#include <setjmp.h> |
47 |
|
|
extern jmp_buf exception_buffer; |
48 |
|
|
extern int exception_status; |
49 |
|
|
|
50 |
|
|
#define TRY if((exception_status = setjmp(exception_buffer)) == 0) |
51 |
|
|
#define CATCH(val) else if(exception_status == val) |
52 |
|
|
#define THROW(val) longjmp(exception_buffer,val) |
53 |
|
|
#define FINALLY else |
54 |
|
|
|
55 |
|
|
/* Exception codes */ |
56 |
|
|
|
57 |
|
|
#define RangeError 1 |
58 |
|
|
#define DivisionByZero 2 |
59 |
|
|
#define OutOfMemory 3 |
60 |
|
|
|
61 |
|
|
/* #define ASC_JMP_INFO */ |
62 |
|
|
/**< Whether to store additional information before making a setjmp call */ |
63 |
|
|
|
64 |
|
|
#ifndef ASC_JMP_INFO |
65 |
|
|
# define SETJMP setjmp |
66 |
|
|
# define LONGJMP longjmp |
67 |
|
|
# define SIGNAL signal |
68 |
|
|
# define JMP_BUF jmp_buf |
69 |
|
|
#else |
70 |
|
|
# define SETJMP(ENV) (\ |
71 |
|
|
CONSOLE_DEBUG("SETJMP at %s:%d (%s=%p)",__FILE__,__LINE__,#ENV,ENV.jmp)\ |
72 |
|
|
,ENV.filename = __FILE__, ENV.line = __LINE__, ENV.func = __FUNCTION__\ |
73 |
|
|
,ENV.varname = #ENV\ |
74 |
|
|
, setjmp(ENV.jmp)\ |
75 |
|
|
) |
76 |
|
|
# define LONGJMP(ENV,VAL) (\ |
77 |
|
|
CONSOLE_DEBUG("LONGJMP to %s:%d (%s) (%s=%p)",ENV.filename,ENV.line,ENV.func,ENV.varname,ENV.jmp)\ |
78 |
|
|
, longjmp(ENV.jmp, VAL)\ |
79 |
|
|
) |
80 |
|
|
typedef struct{ |
81 |
|
|
jmp_buf jmp; |
82 |
|
|
const char *filename; |
83 |
|
|
int line; |
84 |
|
|
const char *func; |
85 |
|
|
const char *varname; |
86 |
|
|
} asc_jmp_buf; |
87 |
|
|
#define JMP_BUF asc_jmp_buf |
88 |
|
|
#define SIGNAL(SIG,HANDLER) (CONSOLE_DEBUG("SIGNAL(%d,%s)",SIG,#HANDLER),signal(SIG,HANDLER)) |
89 |
|
|
#endif |
90 |
|
|
|
91 |
|
|
#endif |