1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| /*
Destructors.h
*/
#pragma once
#ifndef EXTERN_C
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C extern
#endif
#endif
#include "MyStack.h"
//----------------------------------------------------------------------------
//Macros
//Frame structure name
#if 0
#define FRAME_STRUCT_NAME_BEGIN FrameStruct42 ## __LINE__ //Doesn't work: appears as "FrameStruct42__LINE__"
#define FRAME_STRUCT_PTR_END NULL
#else
#define FRAME_STRUCT_NAME_BEGIN FrameStruct42
#define FRAME_STRUCT_PTR_END &FrameStruct42
#endif
//Debug macro to be used with the frame structure name
#if 0
#define INITIALIZE_FRAME2(name) InitializeFrameDebug(&name, #name)
#else
#define INITIALIZE_FRAME2(name) InitializeFrame(&name)
#endif
#define INITIALIZE_FRAME(name) INITIALIZE_FRAME2(name)
//Base structure contents (with additional field(s) in Debug mode)
#ifdef _DEBUG
#define BASE_FRAME {NULL, NULL, FALSE, FALSE, FALSE, 0}
#define BASE_FUNCTION_FRAME {NULL, NULL, TRUE, FALSE, FALSE, 0}
#define BASE_SWITCH_FRAME {NULL, NULL, FALSE, TRUE, FALSE, 0}
#define BASE_LOOP_FRAME {NULL, NULL, FALSE, TRUE, TRUE, 0}
#else
#define BASE_FRAME {NULL, NULL, FALSE, FALSE, FALSE}
#define BASE_FUNCTION_FRAME {NULL, NULL, TRUE, FALSE, FALSE}
#define BASE_SWITCH_FRAME {NULL, NULL, FALSE, TRUE, FALSE}
#define BASE_LOOP_FRAME {NULL, NULL, FALSE, TRUE, TRUE }
#endif
//Macros to be used in the source code
#ifdef __cplusplus
#define BEGIN_FUNCTION { CppFrame FRAME_STRUCT_NAME_BEGIN(TRUE, FALSE, FALSE);
#define SWITCH(expr) { CppFrame FRAME_STRUCT_NAME_BEGIN(FALSE, TRUE, FALSE); switch(expr){
#define BEGIN_LOOP { CppFrame FRAME_STRUCT_NAME_BEGIN(FALSE, TRUE, TRUE );
#define BEGIN { CppFrame FRAME_STRUCT_NAME_BEGIN(FALSE, FALSE, FALSE);
#define END_FUNCTION }
#define END_SWITCH }}
#define END_LOOP }
#define END }
#define RETURN return;
#define BREAK break;
#else
#define BEGIN_FUNCTION { struct frame FRAME_STRUCT_NAME_BEGIN = BASE_FUNCTION_FRAME; INITIALIZE_FRAME(FRAME_STRUCT_NAME_BEGIN); {
#define SWITCH(expr) { struct frame FRAME_STRUCT_NAME_BEGIN = BASE_SWITCH_FRAME; INITIALIZE_FRAME(FRAME_STRUCT_NAME_BEGIN); switch(expr){
#define BEGIN_LOOP { struct frame FRAME_STRUCT_NAME_BEGIN = BASE_LOOP_FRAME; INITIALIZE_FRAME(FRAME_STRUCT_NAME_BEGIN); {
#define BEGIN { struct frame FRAME_STRUCT_NAME_BEGIN = BASE_FRAME; INITIALIZE_FRAME(FRAME_STRUCT_NAME_BEGIN); {
#define END_FUNCTION } assert(IsTopFrameReturnLimit(FRAME_STRUCT_PTR_END)); UninitializeFrameNormal(FRAME_STRUCT_PTR_END); }
#define END_SWITCH } assert(IsTopFrameBreakLimit( FRAME_STRUCT_PTR_END)); UninitializeFrameNormal(FRAME_STRUCT_PTR_END); }
#define END_LOOP } assert(IsTopFrameBreakLimit( FRAME_STRUCT_PTR_END)); UninitializeFrameNormal(FRAME_STRUCT_PTR_END); }
#define END } UninitializeFrameNormal(FRAME_STRUCT_PTR_END); }
#define RETURN { UninitializeFrameReturn(FRAME_STRUCT_PTR_END); return; }
#define BREAK { UninitializeFrameBreak(FRAME_STRUCT_PTR_END); break; }
#endif
//----------------------------------------------------------------------------
//Data structures
struct frame
{
struct frame * pParentFrame;
struct st_stackOfDestructors *pDestructors;
BOOL bReturnLimit;
BOOL bBreakLimit;
//TRUE for loops because object is inside the loop,
//FALSE for switches because object is out of the switch
BOOL bPopOnBreak;
#ifdef _DEBUG
int cnt;
#endif
#ifdef __cplusplus
protected:
frame(void)
: pParentFrame(NULL), pDestructors(NULL)
{ }
#endif
};
//----------------------------------------------------------------------------
//Functions
EXTERN_C void InitializeFrame(struct frame *pFrame);
EXTERN_C void InitializeFrameDebug(struct frame *pFrame, char const *name);
EXTERN_C void UninitializeFrameNormal(struct frame *pFrame);
EXTERN_C void UninitializeFrameBreak(struct frame *pFrame);
EXTERN_C void UninitializeFrameReturn(struct frame *pFrame);
EXTERN_C BOOL IsTopFrameBreakLimit(struct frame *pFrame);
EXTERN_C BOOL IsTopFrameReturnLimit(struct frame *pFrame);
EXTERN_C void AddDestructor(void *pObj, DESTROYPROC proc);
EXTERN_C void FrameDebugIndent(void);
#ifdef __cplusplus
class CppFrame : private frame
{
public:
explicit CppFrame(BOOL bReturnLimit, BOOL bBreakLimit, BOOL bPopOnBreak)
{
this->bReturnLimit = bReturnLimit;
this->bBreakLimit = bBreakLimit;
this->bPopOnBreak = bPopOnBreak;
InitializeFrame(this);
}
CppFrame(frame const &src)
: frame(src)
{
InitializeFrame(this);
}
~CppFrame(void)
{
UninitializeFrameNormal(this); //In C++, it's always 'normal'.
}
};
#endif |
Partager