mirror of
https://github.com/throwtheswitch/cexception.git
synced 2026-06-05 21:24:36 +00:00
- 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:
Binary file not shown.
Binary file not shown.
+14
-22
@@ -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
@@ -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
|
||||
|
||||
@@ -15,7 +15,7 @@ endif
|
||||
SRC_FILES=lib/CException.c test/TestException.c test/TestException_Runner.c $(UNITY_DIR)/unity.c
|
||||
INC_DIRS=-Ilib -Itest -I$(UNITY_DIR)
|
||||
LIB_DIRS=-L$(C_LIBS)
|
||||
SYMBOLS=-DTEST
|
||||
SYMBOLS=-DTEST -DEXCEPTION_USE_CONFIG_FILE
|
||||
|
||||
#Default Task: Compile And Run Tests
|
||||
default:
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ OUT_EXTENSION = '.out'
|
||||
SRC_FILES = "lib/CException.c test/TestException.c test/TestException_Runner.c #{UNITY_DIR}/unity.c"
|
||||
INC_DIRS = "-Ilib -Itest -I#{UNITY_DIR}"
|
||||
LIB_DIRS = C_LIBS.empty? ? '' : "-L#{C_LIBS}"
|
||||
SYMBOLS = '-DTEST'
|
||||
SYMBOLS = '-DTEST -DEXCEPTION_USE_CONFIG_FILE'
|
||||
|
||||
CLEAN.include("#{HERE}*.out")
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
#define _EXCEPTION_H
|
||||
|
||||
//Optionally define the exception type (something like an int which can be directly assigned)
|
||||
#define EXCEPTION_T int
|
||||
#define CEXCEPTION_T int
|
||||
|
||||
// Optionally define the reserved value representing NO EXCEPTION
|
||||
#define EXCEPTION_NONE (1234)
|
||||
#define CEXCEPTION_NONE (1234)
|
||||
|
||||
// Multi-Tasking environments will need a couple of macros defined to make this library
|
||||
// properly handle multiple exception stacks. You will need to include and required
|
||||
@@ -16,13 +16,10 @@
|
||||
// For example, Quadros might include the following implementation:
|
||||
#ifndef TEST
|
||||
#include "OSAPI.h"
|
||||
#define EXCEPTION_GET_ID (KS_GetTaskID())
|
||||
#define EXCEPTION_NUM_ID (NTASKS + 1)
|
||||
#define CEXCEPTION_GET_ID (KS_GetTaskID())
|
||||
#define CEXCEPTION_NUM_ID (NTASKS + 1)
|
||||
#endif
|
||||
|
||||
// INCLUDE THE ACTUAL CEXCEPTION LIBRARY
|
||||
#include "CException.h"
|
||||
|
||||
//This could be a good place to define/include some error ID's:
|
||||
#define ERROR_ID_EVERYTHING_IS_BROKEN (0x88)
|
||||
#define ERROR_ID_ONLY_THIS_IS_BROKEN (0x77)
|
||||
+14
-14
@@ -1,5 +1,5 @@
|
||||
#include "unity.h"
|
||||
#include "Exception.h"
|
||||
#include "CException.h"
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
@@ -12,7 +12,7 @@ void tearDown(void)
|
||||
void test_BasicTryDoesNothingIfNoThrow(void)
|
||||
{
|
||||
int i;
|
||||
EXCEPTION_T e = 0x5a5a;
|
||||
CEXCEPTION_T e = 0x5a5a;
|
||||
|
||||
Try
|
||||
{
|
||||
@@ -29,7 +29,7 @@ void test_BasicTryDoesNothingIfNoThrow(void)
|
||||
|
||||
void test_BasicThrowAndCatch(void)
|
||||
{
|
||||
EXCEPTION_T e;
|
||||
CEXCEPTION_T e;
|
||||
|
||||
Try
|
||||
{
|
||||
@@ -48,7 +48,7 @@ void test_BasicThrowAndCatch(void)
|
||||
|
||||
void test_BasicThrowAndCatch_WithMiniSyntax(void)
|
||||
{
|
||||
EXCEPTION_T e;
|
||||
CEXCEPTION_T e;
|
||||
|
||||
//Mini Throw and Catch
|
||||
Try
|
||||
@@ -69,7 +69,7 @@ void test_BasicThrowAndCatch_WithMiniSyntax(void)
|
||||
void test_VerifyVolatilesSurviveThrowAndCatch(void)
|
||||
{
|
||||
volatile unsigned int VolVal = 0;
|
||||
EXCEPTION_T e;
|
||||
CEXCEPTION_T e;
|
||||
|
||||
Try
|
||||
{
|
||||
@@ -98,7 +98,7 @@ void HappyExceptionThrower(unsigned int ID)
|
||||
void test_ThrowFromASubFunctionAndCatchInRootFunc(void)
|
||||
{
|
||||
volatile unsigned int ID = 0;
|
||||
EXCEPTION_T e;
|
||||
CEXCEPTION_T e;
|
||||
|
||||
Try
|
||||
{
|
||||
@@ -117,7 +117,7 @@ void test_ThrowFromASubFunctionAndCatchInRootFunc(void)
|
||||
|
||||
void HappyExceptionRethrower(unsigned int ID)
|
||||
{
|
||||
EXCEPTION_T e;
|
||||
CEXCEPTION_T e;
|
||||
|
||||
Try
|
||||
{
|
||||
@@ -139,7 +139,7 @@ void HappyExceptionRethrower(unsigned int ID)
|
||||
void test_ThrowAndCatchFromASubFunctionAndRethrowToCatchInRootFunc(void)
|
||||
{
|
||||
volatile unsigned int ID = 0;
|
||||
EXCEPTION_T e;
|
||||
CEXCEPTION_T e;
|
||||
|
||||
Try
|
||||
{
|
||||
@@ -157,7 +157,7 @@ void test_ThrowAndCatchFromASubFunctionAndRethrowToCatchInRootFunc(void)
|
||||
|
||||
void test_ThrowAndCatchFromASubFunctionAndNoRethrowToCatchInRootFunc(void)
|
||||
{
|
||||
EXCEPTION_T e = 3;
|
||||
CEXCEPTION_T e = 3;
|
||||
|
||||
Try
|
||||
{
|
||||
@@ -174,7 +174,7 @@ void test_ThrowAndCatchFromASubFunctionAndNoRethrowToCatchInRootFunc(void)
|
||||
|
||||
void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThisDoesntCorruptExceptionId(void)
|
||||
{
|
||||
EXCEPTION_T e;
|
||||
CEXCEPTION_T e;
|
||||
|
||||
Try
|
||||
{
|
||||
@@ -192,7 +192,7 @@ void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThisDoesntCorruptE
|
||||
|
||||
void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThatEachExceptionIdIndependent(void)
|
||||
{
|
||||
EXCEPTION_T e1, e2;
|
||||
CEXCEPTION_T e1, e2;
|
||||
|
||||
Try
|
||||
{
|
||||
@@ -219,7 +219,7 @@ void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThatEachExceptionI
|
||||
|
||||
void test_CanHaveMultipleTryBlocksInASingleFunction(void)
|
||||
{
|
||||
EXCEPTION_T e;
|
||||
CEXCEPTION_T e;
|
||||
|
||||
Try
|
||||
{
|
||||
@@ -245,7 +245,7 @@ void test_CanHaveMultipleTryBlocksInASingleFunction(void)
|
||||
void test_CanHaveNestedTryBlocksInASingleFunction_ThrowInside(void)
|
||||
{
|
||||
int i = 0;
|
||||
EXCEPTION_T e;
|
||||
CEXCEPTION_T e;
|
||||
|
||||
Try
|
||||
{
|
||||
@@ -269,7 +269,7 @@ void test_CanHaveNestedTryBlocksInASingleFunction_ThrowInside(void)
|
||||
void test_CanHaveNestedTryBlocksInASingleFunction_ThrowOutside(void)
|
||||
{
|
||||
int i = 0;
|
||||
EXCEPTION_T e;
|
||||
CEXCEPTION_T e;
|
||||
|
||||
Try
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* AUTOGENERATED FILE. DO NOT EDIT. */
|
||||
#include "unity.h"
|
||||
#include "Exception.h"
|
||||
#include "CException.h"
|
||||
|
||||
extern void setUp(void);
|
||||
extern void tearDown(void);
|
||||
@@ -20,7 +20,7 @@ extern void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThatEachExc
|
||||
|
||||
static void runTest(UnityTestFunction test)
|
||||
{
|
||||
EXCEPTION_T e;
|
||||
CEXCEPTION_T e;
|
||||
if (TEST_PROTECT())
|
||||
{
|
||||
setUp();
|
||||
|
||||
Reference in New Issue
Block a user