1 | /*␊ |
2 | * contains some ideas from the Mark VanderVoord's CException project␊ |
3 | *␊ |
4 | * Copyright (c) 2012-2013 Cadet-Petit Armel␊ |
5 | * All rights reserved.␊ |
6 | *␊ |
7 | * Redistribution and use in source and binary forms, with or without␊ |
8 | * modification, are permitted provided that the following conditions are␊ |
9 | * met:␊ |
10 | *␊ |
11 | * * Redistributions of source code must retain the above copyright␊ |
12 | * notice, this list of conditions and the following disclaimer.␊ |
13 | *␊ |
14 | * * Redistributions in binary form must reproduce the above copyright␊ |
15 | * notice, this list of conditions and the following disclaimer in the␊ |
16 | * documentation and/or other materials provided with the␊ |
17 | * distribution.␊ |
18 | *␊ |
19 | * * Neither the name of Cadet-Petit Armel nor the names of the␊ |
20 | * contributors may be used to endorse or promote products derived␊ |
21 | * from this software without specific prior written permission.␊ |
22 | *␊ |
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS␊ |
24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT␊ |
25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR␊ |
26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT␊ |
27 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,␊ |
28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT␊ |
29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,␊ |
30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY␊ |
31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT␊ |
32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE␊ |
33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.␊ |
34 | */␊ |
35 | ␊ |
36 | #ifndef _CEXCEPTION_H␊ |
37 | #define _CEXCEPTION_H␊ |
38 | ␊ |
39 | #include <setjmp.h>␊ |
40 | ␊ |
41 | #ifdef __cplusplus␊ |
42 | extern "C"␊ |
43 | {␊ |
44 | #endif␊ |
45 | ␉␊ |
46 | #define CEXCEPTION_USE_CONFIG_FILE␊ |
47 | //#define DEBUG_EXCEPTION␊ |
48 | ␊ |
49 | ␉//To Use CException, you have a number of options:␊ |
50 | ␉//1. Just include it and run with the defaults␊ |
51 | ␉//2. Define any of the following symbols at the command line to override them␊ |
52 | ␉//3. Include a header file before CException.h everywhere which defines any of these␊ |
53 | ␉//4. Create an Exception.h in your path, and just define EXCEPTION_USE_CONFIG_FILE first␊ |
54 | ␉␊ |
55 | #ifdef CEXCEPTION_USE_CONFIG_FILE␊ |
56 | #include "CExceptionConfig.h"␊ |
57 | #endif␊ |
58 | ␉␊ |
59 | ␉//This is the value to assign when there isn't an exception␊ |
60 | #ifndef CEXCEPTION_NONE␊ |
61 | #define CEXCEPTION_NONE (0x5A5A5A5A)␊ |
62 | #endif␊ |
63 | ␊ |
64 | #ifndef SIGABRT␊ |
65 | #define SIGABRT (-2)␊ |
66 | #endif␊ |
67 | ␊ |
68 | ␉//This is number of exception stacks to keep track of (one per task)␊ |
69 | #ifndef CEXCEPTION_NUM_ID␊ |
70 | #define CEXCEPTION_NUM_ID (1) //there is only the one stack by default␊ |
71 | #endif␊ |
72 | ␉␊ |
73 | ␉//This is the method of getting the current exception stack index (0 if only one stack)␊ |
74 | #ifndef CEXCEPTION_GET_ID␊ |
75 | #define CEXCEPTION_GET_ID (0) //use the first index always because there is only one anyway␊ |
76 | #endif␊ |
77 | ␉␊ |
78 | ␉//The type to use to store the exception values.␊ |
79 | #ifndef CEXCEPTION_T␊ |
80 | #define CEXCEPTION_T unsigned int␊ |
81 | #endif␊ |
82 | ␉␊ |
83 | ␉//This is an optional special handler for when there is no global Catch␊ |
84 | #ifndef CEXCEPTION_NO_CATCH_HANDLER␊ |
85 | #define CEXCEPTION_NO_CATCH_HANDLER(id)␊ |
86 | #endif␊ |
87 | ␉␊ |
88 | ␉//exception frame structures␊ |
89 | ␉typedef struct CEXCEPTION_FRAME {␊ |
90 | ␉␉jmp_buf cx;␊ |
91 | ␉␉struct CEXCEPTION_FRAME *pFrame; ␊ |
92 | ␉␉void *zone; ␊ |
93 | ␉␉void *atexit;␊ |
94 | ␉␉int new_registration;␊ |
95 | ␉␉int err_no;␊ |
96 | ␉} CEXCEPTION_FRAME_T;␊ |
97 | ␉␉␊ |
98 | void pushFrame(void);␊ |
99 | void popFrame(void);␊ |
100 | unsigned long getFramesCount(void);␊ |
101 | ␉CEXCEPTION_FRAME_T * getcurrentFrame(void);␊ |
102 | ␉CEXCEPTION_FRAME_T * getMainFrames(void);␊ |
103 | ␊ |
104 | ␉//Try (see C file for explanation)␊ |
105 | #define Try␉␉␉␉␉␉␉␉␉␉␉␉␉␉\␊ |
106 | {␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉\␊ |
107 | pushFrame(); \␊ |
108 | if ((e = setjmp(getcurrentFrame()->cx)) == 0) { \␊ |
109 | e = CEXCEPTION_NONE; \␊ |
110 | if (1)␊ |
111 | ␉␊ |
112 | ␉//Catch (see C file for explanation)␊ |
113 | #define Catch(e)␉␉␉␉␉␉␉␉␉␉␉␉␉\␊ |
114 | else { } \␊ |
115 | } \␊ |
116 | popFrame();\␊ |
117 | }␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉\␊ |
118 | if (e != CEXCEPTION_NONE)␊ |
119 | ␉␊ |
120 | ␊ |
121 | ␉//Throw an Error␊ |
122 | ␉void Throw(CEXCEPTION_T ExceptionID);␊ |
123 | ␉int Install_Default_Handler(void);␊ |
124 | ␉␊ |
125 | #ifdef __cplusplus␊ |
126 | } // extern "C"␊ |
127 | #endif␊ |
128 | ␊ |
129 | ␊ |
130 | #endif // _CEXCEPTION_H␊ |
131 | |