msa
git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/msa@102253 bc3139a8-67e5-0310-9ffc-ced21a209358
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,80 @@ |
1 |
+/* |
|
2 |
+ * exceptions4c lightweight version 1.0 |
|
3 |
+ * |
|
4 |
+ * Copyright (c) 2013 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, message) E4C_THROW(type, message) |
|
30 |
+#endif |
|
31 |
+ |
|
32 |
+/* Represents an exception type */ |
|
33 |
+struct e4c_exception_type{ |
|
34 |
+ const char * name; |
|
35 |
+ const char * default_message; |
|
36 |
+ const struct e4c_exception_type * supertype; |
|
37 |
+}; |
|
38 |
+ |
|
39 |
+/* Declarations and definitions of exception types */ |
|
40 |
+#define E4C_DECLARE_EXCEPTION(name) extern const struct e4c_exception_type name |
|
41 |
+#define E4C_DEFINE_EXCEPTION(name, default_message, supertype) const struct e4c_exception_type name = { #name, default_message, &supertype } |
|
42 |
+ |
|
43 |
+/* Predefined exception types */ |
|
44 |
+E4C_DECLARE_EXCEPTION(RuntimeException); |
|
45 |
+E4C_DECLARE_EXCEPTION(NullPointerException); |
|
46 |
+E4C_DECLARE_EXCEPTION(ClustalOmegaException); |
|
47 |
+ |
|
48 |
+/* Represents an instance of an exception type */ |
|
49 |
+struct e4c_exception{ |
|
50 |
+ char message[E4C_MESSAGE_SIZE]; |
|
51 |
+ const char * file; |
|
52 |
+ int line; |
|
53 |
+ const struct e4c_exception_type * type; |
|
54 |
+}; |
|
55 |
+ |
|
56 |
+/* Retrieve current thrown exception */ |
|
57 |
+#define E4C_EXCEPTION e4c.err |
|
58 |
+ |
|
59 |
+/* Returns whether current exception is of a given type */ |
|
60 |
+#define E4C_IS_INSTANCE_OF(t) ( e4c.err.type == &t || e4c_extends(e4c.err.type, &t) ) |
|
61 |
+ |
|
62 |
+/* Implementation details */ |
|
63 |
+#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) |
|
64 |
+#define E4C_CATCH(type) else if(e4c.frame[e4c.frames].stage == e4c_catching && E4C_IS_INSTANCE_OF(type) && e4c_hook(1)) |
|
65 |
+#define E4C_FINALLY else if(e4c.frame[e4c.frames].stage == e4c_finalizing) |
|
66 |
+#define E4C_THROW(type, message) e4c_throw(&type, E4C_INFO, message) |
|
67 |
+#ifndef NDEBUG |
|
68 |
+# define E4C_INFO __FILE__, __LINE__ |
|
69 |
+#else |
|
70 |
+# define E4C_INFO NULL, 0 |
|
71 |
+#endif |
|
72 |
+ |
|
73 |
+enum e4c_stage{e4c_beginning, e4c_trying, e4c_catching, e4c_finalizing, e4c_done}; |
|
74 |
+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; |
|
75 |
+extern int e4c_try(const char * file, int line); |
|
76 |
+extern int e4c_hook(int is_catch); |
|
77 |
+extern int e4c_extends(const struct e4c_exception_type * child, const struct e4c_exception_type * parent); |
|
78 |
+extern void e4c_throw(const struct e4c_exception_type * exception_type, const char * file, int line, const char * message); |
|
79 |
+ |
|
80 |
+# endif |