- updated CException to just be the core two files, and the custom config include is now optional

- made the docs a bit better

git-svn-id: http://cexception.svn.sourceforge.net/svnroot/cexception/trunk@8 50f63946-2846-0410-8d77-f904c773002e
This commit is contained in:
mvandervoord
2009-12-05 00:34:54 +00:00
parent c12e50c56d
commit b0d50cba17
9 changed files with 70 additions and 67 deletions
+14 -22
View File
@@ -1,23 +1,15 @@
#include "Exception.h"
#include "CException.h"
volatile EXCEPTION_FRAME_T ExceptionFrames[EXCEPTION_NUM_ID];
volatile CEXCEPTION_FRAME_T CExceptionFrames[CEXCEPTION_NUM_ID];
//------------------------------------------------------------------------------------------
// Throw
//------------------------------------------------------------------------------------------
//
// PARAMETERS: None
//
// DESCRIPTION: throws a software exception
//
// RETURNS: None
//
//------------------------------------------------------------------------------------------
void Throw(EXCEPTION_T ExceptionID)
void Throw(CEXCEPTION_T ExceptionID)
{
unsigned int MY_ID = EXCEPTION_GET_ID;
ExceptionFrames[MY_ID].Exception = ExceptionID;
longjmp(*ExceptionFrames[MY_ID].pFrame, 1);
unsigned int MY_ID = CEXCEPTION_GET_ID;
CExceptionFrames[MY_ID].Exception = ExceptionID;
longjmp(*CExceptionFrames[MY_ID].pFrame, 1);
}
//------------------------------------------------------------------------------------------
@@ -27,21 +19,21 @@ void Throw(EXCEPTION_T ExceptionID)
#define Try
{ <- give us some local scope. most compilers are happy with this
jmp_buf *PrevFrame, NewFrame; <- prev frame points to the last try block's frame. new frame gets created on stack for this Try block
unsigned int MY_ID = EXCEPTION_GET_ID; <- look up this task's id for use in frame array. always 0 if single-tasking
PrevFrame = ExceptionFrames[EXCEPTION_GET_ID].pFrame; <- set pointer to point at old frame (which array is currently pointing at)
ExceptionFrames[MY_ID].pFrame = &NewFrame; <- set array to point at my new frame instead, now
ExceptionFrames[MY_ID].Exception = EXCEPTION_NONE; <- initialize my exception id to be NONE
unsigned int MY_ID = CEXCEPTION_GET_ID; <- look up this task's id for use in frame array. always 0 if single-tasking
PrevFrame = CExceptionFrames[CEXCEPTION_GET_ID].pFrame; <- set pointer to point at old frame (which array is currently pointing at)
CExceptionFrames[MY_ID].pFrame = &NewFrame; <- set array to point at my new frame instead, now
CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; <- initialize my exception id to be NONE
if (setjmp(NewFrame) == 0) { <- do setjmp. it returns 1 if longjump called, otherwise 0
if (&PrevFrame) <- this is here to force proper scoping. it requires braces or a single line to be but after Try, otherwise won't compile. This is always true at this point.
#define Catch(e)
else { } <- this also forces proper scoping. Without this they could stick their own 'else' in and it would get ugly
ExceptionFrames[MY_ID].Exception = EXCEPTION_NONE; <- no errors happened, so just set the exception id to NONE (in case it was corrupted)
CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; <- no errors happened, so just set the exception id to NONE (in case it was corrupted)
}
else <- an exception occurred
{ e = ExceptionFrames[MY_ID].Exception; e=e;} <- assign the caught exception id to the variable passed in.
ExceptionFrames[MY_ID].pFrame = PrevFrame; <- make the pointer in the array point at the previous frame again, as if NewFrame never existed.
{ e = CExceptionFrames[MY_ID].Exception; e=e;} <- assign the caught exception id to the variable passed in.
CExceptionFrames[MY_ID].pFrame = PrevFrame; <- make the pointer in the array point at the previous frame again, as if NewFrame never existed.
} <- finish off that local scope we created to have our own variables
if (ExceptionFrames[EXCEPTION_GET_ID].Exception != EXCEPTION_NONE) <- start the actual 'catch' processing if we have an exception id saved away
if (CExceptionFrames[CEXCEPTION_GET_ID].Exception != CEXCEPTION_NONE) <- start the actual 'catch' processing if we have an exception id saved away
*/
+34 -20
View File
@@ -3,54 +3,68 @@
#include <setjmp.h>
#ifndef EXCEPTION_NONE
#define EXCEPTION_NONE (0x5A5A5A5A)
//To Use CException, you have a number of options:
//1. Just include it and run with the defaults
//2. Define any of the following symbols at the command line to override them
//3. Include a header file before CException.h everywhere which defines any of these
//4. Create an Exception.h in your path, and just define EXCEPTION_USE_CONFIG_FILE first
#ifdef CEXCEPTION_USE_CONFIG_FILE
#include "CExceptionConfig.h"
#endif
#ifndef EXCEPTION_NUM_ID
#define EXCEPTION_NUM_ID (1) //there is only the one stack by default
//This is the value to assign when there isn't an exception
#ifndef CEXCEPTION_NONE
#define CEXCEPTION_NONE (0x5A5A5A5A)
#endif
#ifndef EXCEPTION_GET_ID
#define EXCEPTION_GET_ID (0) //use the first index always because there is only one anyway
//This is number of exception stacks to keep track of (one per task)
#ifndef CEXCEPTION_NUM_ID
#define CEXCEPTION_NUM_ID (1) //there is only the one stack by default
#endif
#ifndef EXCEPTION_T
#define EXCEPTION_T unsigned int
//This is the method of getting the current exception stack index (0 if only one stack)
#ifndef CEXCEPTION_GET_ID
#define CEXCEPTION_GET_ID (0) //use the first index always because there is only one anyway
#endif
//The type to use to store the exception values.
#ifndef CEXCEPTION_T
#define CEXCEPTION_T unsigned int
#endif
//exception frame structures
typedef struct {
jmp_buf* pFrame;
volatile EXCEPTION_T Exception;
} EXCEPTION_FRAME_T;
volatile CEXCEPTION_T Exception;
} CEXCEPTION_FRAME_T;
//actual root frame storage (only one if single-tasking)
extern volatile EXCEPTION_FRAME_T ExceptionFrames[];
extern volatile CEXCEPTION_FRAME_T CExceptionFrames[];
//Try (see C file for explanation)
#define Try \
{ \
jmp_buf *PrevFrame, NewFrame; \
unsigned int MY_ID = EXCEPTION_GET_ID; \
PrevFrame = ExceptionFrames[EXCEPTION_GET_ID].pFrame; \
ExceptionFrames[MY_ID].pFrame = &NewFrame; \
ExceptionFrames[MY_ID].Exception = EXCEPTION_NONE; \
unsigned int MY_ID = CEXCEPTION_GET_ID; \
PrevFrame = CExceptionFrames[CEXCEPTION_GET_ID].pFrame; \
CExceptionFrames[MY_ID].pFrame = &NewFrame; \
CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; \
if (setjmp(NewFrame) == 0) { \
if (&PrevFrame)
//Catch (see C file for explanation)
#define Catch(e) \
else { } \
ExceptionFrames[MY_ID].Exception = EXCEPTION_NONE; \
CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; \
} \
else \
{ e = ExceptionFrames[MY_ID].Exception; e=e; } \
ExceptionFrames[MY_ID].pFrame = PrevFrame; \
{ e = CExceptionFrames[MY_ID].Exception; e=e; } \
CExceptionFrames[MY_ID].pFrame = PrevFrame; \
} \
if (ExceptionFrames[EXCEPTION_GET_ID].Exception != EXCEPTION_NONE)
if (CExceptionFrames[CEXCEPTION_GET_ID].Exception != CEXCEPTION_NONE)
//Throw an Error
void Throw(EXCEPTION_T ExceptionID);
void Throw(CEXCEPTION_T ExceptionID);
#endif // _CEXCEPTION_H