-
Notifications
You must be signed in to change notification settings - Fork 0
/
e4c_lite.h
91 lines (76 loc) · 3.07 KB
/
e4c_lite.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* exceptions4c lightweight version 1.0
*
* Copyright (c) 2014 Guillermo Calvo
* Licensed under the GNU Lesser General Public License
*/
#ifndef EXCEPTIONS4C_LITE
#define EXCEPTIONS4C_LITE
#include <stddef.h>
#include <setjmp.h>
#if defined(__GNUC__)
#define ATTR_NORETURN __attribute__((noreturn))
#elif defined(MSC_VER)
#define ATTR_NORETURN __declspec((noreturn))
#else
#define ATTR_NORETURN
#endif
/* Maximum number of nested `try` blocks */
#ifndef E4C_MAX_FRAMES
# define E4C_MAX_FRAMES 16
#endif
/* Maximum length (in bytes) of an exception message */
#ifndef E4C_MESSAGE_SIZE
# define E4C_MESSAGE_SIZE 128
#endif
/* Exception handling keywords: try/catch/finally/throw */
#ifndef E4C_NOKEYWORDS
# define try E4C_TRY
# define catch(type) E4C_CATCH(type)
# define finally E4C_FINALLY
# define throw(type, err, message) E4C_THROW(type, err, message)
# define rethrow E4C_RETHROW
#endif
/* Represents an exception type */
struct e4c_exception_type{
const char * name;
const char * default_message;
const struct e4c_exception_type * supertype;
};
/* Declarations and definitions of exception types */
#define E4C_DECLARE_EXCEPTION(name) extern const struct e4c_exception_type name
#define E4C_DEFINE_EXCEPTION(name, default_message, supertype) const struct e4c_exception_type name = { #name, default_message, &supertype }
/* Predefined exception types */
E4C_DECLARE_EXCEPTION(RuntimeException);
E4C_DECLARE_EXCEPTION(NullPointerException);
/* Represents an instance of an exception type */
struct e4c_exception{
char message[E4C_MESSAGE_SIZE];
int err;
const char * file;
int line;
const struct e4c_exception_type * type;
};
/* Retrieve current thrown exception */
#define E4C_EXCEPTION e4c.err
/* Returns whether current exception is of a given type */
#define E4C_IS_INSTANCE_OF(t) ( e4c.err.type == &t || e4c_extends(e4c.err.type, &t) )
/* Implementation details */
#define E4C_TRY if(e4c_try(E4C_INFO) && setjmp(e4c.jump[e4c.frames - 1]) >= 0) while(e4c_hook(0)) if(e4c.frame[e4c.frames].stage == e4c_trying)
#define E4C_CATCH(type) else if(e4c.frame[e4c.frames].stage == e4c_catching && E4C_IS_INSTANCE_OF(type) && e4c_hook(1))
#define E4C_FINALLY else if(e4c.frame[e4c.frames].stage == e4c_finalizing)
#define E4C_THROW(type, err, message) e4c_throw(&type, E4C_INFO, err, message)
#define E4C_RETHROW e4c_throw(e4c.err.type, e4c.err.file, e4c.err.line, e4c.err.err, e4c.err.message)
#define FN_THROW(X)
#ifndef NDEBUG
# define E4C_INFO __FILE__, __LINE__
#else
# define E4C_INFO NULL, 0
#endif
enum e4c_stage{e4c_beginning, e4c_trying, e4c_catching, e4c_finalizing, e4c_done};
extern struct e4c_context{jmp_buf jump[E4C_MAX_FRAMES]; struct e4c_exception err; struct{unsigned char stage; unsigned char uncaught;} frame[E4C_MAX_FRAMES + 1]; int frames;} e4c;
extern int e4c_try(const char * file, int line);
extern int e4c_hook(int is_catch);
extern int e4c_extends(const struct e4c_exception_type * child, const struct e4c_exception_type * parent);
extern void e4c_throw(const struct e4c_exception_type * exception_type, const char * file, int line, int err, const char * message) ATTR_NORETURN;
# endif