1 | #ifndef _CEXCEPTION_H␊ |
2 | #define _CEXCEPTION_H␊ |
3 | ␊ |
4 | #include <setjmp.h>␊ |
5 | ␊ |
6 | #ifdef __cplusplus␊ |
7 | extern "C"␊ |
8 | {␊ |
9 | #endif␊ |
10 | ␉␊ |
11 | #define CEXCEPTION_USE_CONFIG_FILE␊ |
12 | ␉␊ |
13 | ␉//To Use CException, you have a number of options:␊ |
14 | ␉//1. Just include it and run with the defaults␊ |
15 | ␉//2. Define any of the following symbols at the command line to override them␊ |
16 | ␉//3. Include a header file before CException.h everywhere which defines any of these␊ |
17 | ␉//4. Create an Exception.h in your path, and just define EXCEPTION_USE_CONFIG_FILE first␊ |
18 | ␉␊ |
19 | #ifdef CEXCEPTION_USE_CONFIG_FILE␊ |
20 | #include "CExceptionConfig.h"␊ |
21 | #endif␊ |
22 | ␉␊ |
23 | ␉//This is the value to assign when there isn't an exception␊ |
24 | #ifndef CEXCEPTION_NONE␊ |
25 | #define CEXCEPTION_NONE (0x5A5A5A5A)␊ |
26 | #endif␊ |
27 | ␊ |
28 | #ifndef EXIT_SUCCESS␊ |
29 | #define EXIT_SUCCESS (1)␊ |
30 | #endif␊ |
31 | ␉␊ |
32 | #ifndef EXIT_FAILURE␊ |
33 | #define EXIT_FAILURE (-1)␊ |
34 | #endif␊ |
35 | ␉␊ |
36 | #ifndef SIGABRT␊ |
37 | #define SIGABRT (-2)␊ |
38 | #endif␊ |
39 | ␊ |
40 | ␉//This is number of exception stacks to keep track of (one per task)␊ |
41 | #ifndef CEXCEPTION_NUM_ID␊ |
42 | #define CEXCEPTION_NUM_ID (1) //there is only the one stack by default␊ |
43 | #endif␊ |
44 | ␉␊ |
45 | ␉//This is the method of getting the current exception stack index (0 if only one stack)␊ |
46 | #ifndef CEXCEPTION_GET_ID␊ |
47 | #define CEXCEPTION_GET_ID (0) //use the first index always because there is only one anyway␊ |
48 | #endif␊ |
49 | ␉␊ |
50 | ␉//The type to use to store the exception values.␊ |
51 | #ifndef CEXCEPTION_T␊ |
52 | #define CEXCEPTION_T unsigned int␊ |
53 | #endif␊ |
54 | ␉␊ |
55 | ␉//This is an optional special handler for when there is no global Catch␊ |
56 | #ifndef CEXCEPTION_NO_CATCH_HANDLER␊ |
57 | #define CEXCEPTION_NO_CATCH_HANDLER(id)␊ |
58 | #endif␊ |
59 | ␉␊ |
60 | ␉//exception frame structures␊ |
61 | ␉typedef struct {␊ |
62 | ␉␉jmp_buf* pFrame;␊ |
63 | ␉␉CEXCEPTION_T volatile Exception;␊ |
64 | ␉} CEXCEPTION_FRAME_T;␊ |
65 | ␉␊ |
66 | ␉//actual root frame storage (only one if single-tasking)␊ |
67 | ␉extern volatile CEXCEPTION_FRAME_T CExceptionFrames[];␊ |
68 | ␉␊ |
69 | ␉//Try (see C file for explanation)␊ |
70 | #define Try␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉\␊ |
71 | {␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉\␊ |
72 | jmp_buf *PrevFrame, NewFrame; \␊ |
73 | unsigned int MY_ID = CEXCEPTION_GET_ID; \␊ |
74 | PrevFrame = CExceptionFrames[CEXCEPTION_GET_ID].pFrame; \␊ |
75 | CExceptionFrames[MY_ID].pFrame = (jmp_buf*)(&NewFrame); \␊ |
76 | CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; \␊ |
77 | if (setjmp(NewFrame) == 0) { \␊ |
78 | if (1)␊ |
79 | ␉␊ |
80 | ␉//Catch (see C file for explanation)␊ |
81 | #define Catch(e)␉␉␉␉␉␉␉␉␉␉␉␉␉\␊ |
82 | else { } \␊ |
83 | CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; \␊ |
84 | } \␊ |
85 | else \␊ |
86 | { e = CExceptionFrames[MY_ID].Exception; e=e; } \␊ |
87 | CExceptionFrames[MY_ID].pFrame = PrevFrame; \␊ |
88 | }␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉\␊ |
89 | if (Install_Default_Handler()) \␊ |
90 | if (CExceptionFrames[CEXCEPTION_GET_ID].Exception != CEXCEPTION_NONE)␊ |
91 | ␉␊ |
92 | ␉␊ |
93 | ␉//Throw an Error␊ |
94 | ␉void Throw(CEXCEPTION_T ExceptionID);␊ |
95 | ␉int Install_Default_Handler(void);␊ |
96 | ␉␊ |
97 | ␉␊ |
98 | #ifdef DEBUG_ALL_EXIT␊ |
99 | #define exit(e) \␊ |
100 | ((void) (__exit (#e,e, __FILE__, __LINE__)))␊ |
101 | #define __exit(estr, e, file, line) \␊ |
102 | ((void)printf ("%s:%u: exit status `%s'\n", file, line, estr), Throw(e))␊ |
103 | #else␊ |
104 | #define exit(e) \␊ |
105 | ((void) ((e == 0) ? Throw(EXIT_SUCCESS) : __exit (#e, e, __FILE__, __LINE__)))␊ |
106 | #define __exit(estr, e, file, line) \␊ |
107 | ((void)printf ("%s:%u: exit status `%s'\n", file, line, estr), Throw(e))␊ |
108 | #endif␊ |
109 | ␉␊ |
110 | ␉␊ |
111 | #define abort() \␊ |
112 | ((void) (__abort ( __FILE__, __LINE__)))␊ |
113 | #define __abort(file, line) \␊ |
114 | ((void)printf ("%s:%u: aborted \n", file, line), Throw(SIGABRT))␊ |
115 | ␉␊ |
116 | #ifdef __cplusplus␊ |
117 | } // extern "C"␊ |
118 | #endif␊ |
119 | ␊ |
120 | ␊ |
121 | #endif // _CEXCEPTION_H␊ |
122 | |