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