-
Notifications
You must be signed in to change notification settings - Fork 0
/
AMException.h
73 lines (55 loc) · 3.39 KB
/
AMException.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef AMEXCEPTION_H
#define AMEXCEPTION_H
#include <setjmp.h>
#if defined(TRY) || defined(CATCH) || defined(CATCH_ALL) || defined(FINALLY) || defined(END_TRY) || defined(THROW)
#error "AMException: One of the exception macros is already defined"
#endif
struct AMExceptionStack {
jmp_buf buf;
struct AMExceptionStack *prev;
};
struct AMException {
struct AMExceptionStack *stack;
// if nonzero, an exception is currently being processed
// catch statement will reset this to 0
// if !0 when END_TRY is reached, the exception is rethrown
int num;
void *arg;
int line;
const char *file;
};
_Noreturn void AMExceptionThrow(void);
extern _Thread_local struct AMException AMException;
#define THROW_ARG(NUM, ARG) \
do { \
AMException.num = NUM; \
AMException.arg = ARG; \
AMException.line = __LINE__; \
AMException.file = __FILE__; \
AMExceptionThrow(); \
} while (0)
#define THROW(NUM) THROW_ARG(NUM, NULL)
#define TRY \
do { \
struct AMExceptionStack AMExceptionStack; /* declare a new jump stack */ \
AMExceptionStack.prev = AMException.stack; /* remember the previous jump stack */ \
AMException.stack = &AMExceptionStack; /* set this as the current jump stack */ \
switch (setjmp(AMExceptionStack.buf)) { \
/* setjmp returns 0 on first call, so case 0 represents the try block */ \
case 0:
#define CATCH(E) _Static_assert(E != 0, "0 cannot be thrown"); \
break; \
case E: \
AMException.num = 0;
#define CATCH_ALL \
break; \
default: \
AMException.num = 0;
#define FINALLY \
} \
{
#define TRY_END \
} \
if (AMException.num != 0) AMExceptionThrow(); \
} while (0)
#endif