Rev

Rev 2341 | Details | Compare with Previous | Last modification | View Log | SVN | Bug Tracker

Rev Author Line No. Line
2341 filatov 1
/*
2
 * exceptions4c lightweight version 1.0
3
 *
4
 * Copyright (c) 2014 Guillermo Calvo
5
 * Licensed under the GNU Lesser General Public License
6
 */
7
 
8
#ifndef EXCEPTIONS4C_LITE
9
#define EXCEPTIONS4C_LITE
10
 
11
#include <stddef.h>
12
#include <setjmp.h>
13
 
14
/* Maximum number of nested `try` blocks */
15
#ifndef E4C_MAX_FRAMES
16
# define E4C_MAX_FRAMES 16
17
#endif
18
 
19
/* Maximum length (in bytes) of an exception message */
20
#ifndef E4C_MESSAGE_SIZE
21
# define E4C_MESSAGE_SIZE 128
22
#endif
23
 
24
/* Exception handling keywords: try/catch/finally/throw */
25
#ifndef E4C_NOKEYWORDS
26
# define try E4C_TRY
27
# define catch(type) E4C_CATCH(type)
28
# define finally E4C_FINALLY
29
# define throw(type, err, message) E4C_THROW(type, err, message)
30
# define rethrow E4C_RETHROW
31
#endif
32
 
33
/* Represents an exception type */
34
struct e4c_exception_type{
35
        const char * name;
36
        const char * default_message;
37
        const struct e4c_exception_type * supertype;
38
};
39
 
40
/* Declarations and definitions of exception types */
41
#define E4C_DECLARE_EXCEPTION(name) extern const struct e4c_exception_type name
42
#define E4C_DEFINE_EXCEPTION(name, default_message, supertype) const struct e4c_exception_type name = { #name, default_message, &supertype }
43
 
44
/* Predefined exception types */
45
E4C_DECLARE_EXCEPTION(RuntimeException);
46
E4C_DECLARE_EXCEPTION(NullPointerException);
47
 
48
/* Represents an instance of an exception type */
49
struct e4c_exception{
50
        char message[E4C_MESSAGE_SIZE];
51
        int err;
52
        const char * file;
53
        int line;
54
        const struct e4c_exception_type * type;
55
};
56
 
57
/* Retrieve current thrown exception */
58
#define E4C_EXCEPTION e4c.err
59
 
60
/* Returns whether current exception is of a given type */
61
#define E4C_IS_INSTANCE_OF(t) ( e4c.err.type == &t || e4c_extends(e4c.err.type, &t) )
62
 
63
/* Implementation details */
64
#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)
65
#define E4C_CATCH(type) else if(e4c.frame[e4c.frames].stage == e4c_catching && E4C_IS_INSTANCE_OF(type) && e4c_hook(1))
66
#define E4C_FINALLY else if(e4c.frame[e4c.frames].stage == e4c_finalizing)
67
#define E4C_THROW(type, err, message) e4c_throw(&type, E4C_INFO, err, message)
68
#define E4C_RETHROW e4c_throw(e4c.err.type, e4c.err.file, e4c.err.line, e4c.err.err, e4c.err.message)
69
#define FN_THROW(X)
70
#ifndef NDEBUG
71
# define E4C_INFO __FILE__, __LINE__
72
#else
73
# define E4C_INFO NULL, 0
74
#endif
75
 
76
enum e4c_stage{e4c_beginning, e4c_trying, e4c_catching, e4c_finalizing, e4c_done};
77
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;
78
extern int e4c_try(const char * file, int line);
79
extern int e4c_hook(int is_catch);
80
extern int e4c_extends(const struct e4c_exception_type * child, const struct e4c_exception_type * parent);
81
extern void e4c_throw(const struct e4c_exception_type * exception_type, const char * file, int line, int err, const char * message);
82
 
83
# endif