Virtual U.org
Get Personal Training on VU Today
    
Top shadow
 
 register/help
User Name:

Password:

MYEXCEPT.H Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

MYEXCEPT.H

Go to the documentation of this file.
00001 //Owner: Fred
00002 //$$ myexcept.h                                  Exception handling classes
00003 
00004 // A set of classes to simulate exceptions in C++
00005 //
00006 //   Partially copied from Carlos Vidal's article in the C users' journal
00007 //   September 1992, pp 19-28
00008 //
00009 //   Operations defined
00010 //      Try {     }
00011 //      Throw ( exception object )
00012 //      ReThrow
00013 //      Catch ( exception class ) {      }
00014 //      CatchAll {      }
00015 //      CatchAndThrow
00016 //
00017 //   All catch lists must end with a CatchAll or CatchAndThrow statement
00018 //   but not both.
00019 //
00020 //   When exceptions are finally implemented replace Try, Throw(E), Rethrow,
00021 //   Catch, CatchAll, CatchAndThrow by try, throw E, throw, catch,
00022 //   catch(...), and {}.
00023 //
00024 //   All exception classes must be derived from Exception, have no non-static
00025 //   variables and must include the statement
00026 //
00027 //      static unsigned long Select;
00028 //
00029 //   Any constructor in one of these exception classes must include
00030 //
00031 //      Select = Exception::Select;
00032 //
00033 //   For each exceptions class, EX_1, some .cpp file must include
00034 //
00035 //      unsigned long EX_1::Select;
00036 //
00037 
00038 #ifndef EXCEPTION_LIB
00039 #define EXCEPTION_LIB
00040 
00041 #ifdef use_namespace
00042 namespace RBD_COMMON {
00043 #endif
00044 
00045     void Terminate();
00046 
00047     //********** classes for setting up exceptions and reporting ************//
00048 
00049     class Exception;
00050 
00053     class Tracer {                                  // linked list showing how
00054         // we got here
00055         char* entry;
00056         Tracer* previous;
00057     public:
00058         Tracer(char*);
00059         ~Tracer();
00060         void ReName(char*);
00061         static void PrintTrace();                   // for printing trace
00062         static void AddTrace();                     // insert trace in exception record
00063         static Tracer* last;                        // points to Tracer list
00064         friend class Exception;
00065     };
00066 
00069     class Exception {                               // The base exception class
00070     protected:
00071         static char* what_error;                    // error message
00072         static int SoFar;                           // no. characters already entered
00073         static int LastOne;                         // last location in error buffer
00074     public:
00075         static void AddMessage(const char* a_what);
00076         // messages about exception
00077         static void AddInt(int value);              // integer to error message
00078         static unsigned long Select;                // for identifying exception
00079         Exception(const char* a_what = 0);
00080         static const char* what() { return what_error; }
00081         // for getting error message
00082     };
00083 
00084     inline Tracer::Tracer(char* e)
00085         : entry(e), previous(last) { last = this; }
00086 
00087     inline Tracer::~Tracer() { last = previous; }
00088 
00089     inline void Tracer::ReName(char* e) { entry=e; }
00090 
00091 #ifdef SimulateExceptions                       // SimulateExceptions
00092 
00093 #include <setjmp.h>
00094 
00095     //************* the definitions of Try, Throw and Catch *****************//
00096 
00097     class JumpItem;
00098     class Janitor;
00099 
00102     class JumpBase {                                // pointer to a linked list of jmp_buf s
00103     public:
00104         static JumpItem *jl;
00105         static jmp_buf env;
00106     };
00107 
00110     class JumpItem {                                // an item in a linked list of jmp_buf s
00111     public:
00112         JumpItem *ji;
00113         jmp_buf env;
00114         Tracer* trace;                              // to keep check on Tracer items
00115         Janitor* janitor;                           // list of items for cleanup
00116         JumpItem() : trace(0), janitor(0), ji(JumpBase::jl)
00117             { JumpBase::jl = this; }
00118         ~JumpItem() { JumpBase::jl = ji; }
00119     };
00120 
00121     void Throw();
00122 
00123     inline void Throw(const Exception&) { Throw(); }
00124 
00125 #define Try \
00126     if (!setjmp( JumpBase::jl->env )) \ { \
00127         JumpBase::jl->trace = Tracer::last; \
00128         JumpItem JI387256156;
00129 
00130 #define ReThrow Throw()
00131 
00132 #define Catch(EXCEPTION) \
00133   } \
00134   else if (Exception::Select == EXCEPTION::Select) \ { \
00135 
00136 #define CatchAll } else
00137 
00138 #define CatchAndThrow  } else Throw();
00139 
00140 //****************** cleanup heap following Throw ***********************//
00141 
00144 class Janitor {
00145 protected:
00146     static bool do_not_link;                    // set when new is called
00147     bool OnStack;                               // false if created by new
00148 public:
00149     Janitor* NextJanitor;
00150     virtual void CleanUp() {}
00151     Janitor();
00152     virtual ~Janitor();
00153 };
00154 
00155 // The tiresome old trick for initializing the Janitor class
00156 // this is needed for classes derived from Janitor which have objects
00157 // declared globally
00158 
00161 class JanitorInitializer {
00162 public:
00163     JanitorInitializer();
00164 private:
00165     static int ref_count;
00166 };
00167 
00168 static JanitorInitializer JanInit;
00169 #endif                                          // end of SimulateExceptions
00170 
00171 #ifdef UseExceptions
00172 
00173 #define Try try
00174 #define Throw(E) throw E
00175 #define ReThrow throw
00176 #define Catch catch
00177 #define CatchAll catch(...)
00178 #define CatchAndThrow {}
00179 #endif                                          // end of UseExceptions
00180 
00181 #ifdef DisableExceptions                        // Disable exceptions
00182 
00183 #define Try {
00184 #define ReThrow Throw()
00185 #define Catch(EXCEPTION) } if (false) {
00186 #define CatchAll } if (false)
00187 #define CatchAndThrow }
00188 
00189 inline void Throw() { Terminate(); }
00190 inline void Throw(const Exception&) { Terminate(); }
00191 #endif                                          // end of DisableExceptions
00192 
00193 #ifndef SimulateExceptions                      // ! SimulateExceptions
00194 
00197 class Janitor {                                 // a dummy version
00198 public:
00199 virtual void CleanUp() {}
00200 Janitor() {}
00201 virtual ~Janitor() {}
00202 };
00203 #endif                                          // end of ! SimulateExceptions
00204 
00205 //******************** FREE_CHECK and NEW_DELETE ***********************//
00206 
00207 #ifdef DO_FREE_CHECK                            // DO_FREE_CHECK
00208 // Routines for tracing whether new and delete calls are balanced
00209 
00210 class FreeCheck;
00211 
00214 class FreeCheckLink {
00215 protected:
00216 FreeCheckLink* next;
00217 void* ClassStore;
00218 FreeCheckLink();
00219 virtual void Report()=0;                    // print details of link
00220 friend class FreeCheck;
00221 };
00222 
00226 class FCLClass : public FreeCheckLink {         // for registering objects
00227 char* ClassName;
00228 FCLClass(void* t, char* name);
00229 void Report();
00230 friend class FreeCheck;
00231 };
00232 
00236 class FCLRealArray : public FreeCheckLink {     // for registering real arrays
00237 char* Operation;
00238 int size;
00239 FCLRealArray(void* t, char* o, int s);
00240 void Report();
00241 friend class FreeCheck;
00242 };
00243 
00247 class FCLIntArray : public FreeCheckLink {      // for registering int arrays
00248 char* Operation;
00249 int size;
00250 FCLIntArray(void* t, char* o, int s);
00251 void Report();
00252 friend class FreeCheck;
00253 };
00254 
00257 class FreeCheck {
00258 static FreeCheckLink* next;
00259 static int BadDelete;
00260 public:
00261 static void Register(void*, char*);
00262 static void DeRegister(void*, char*);
00263 static void RegisterR(void*, char*, int);
00264 static void DeRegisterR(void*, char*, int);
00265 static void RegisterI(void*, char*, int);
00266 static void DeRegisterI(void*, char*, int);
00267 static void Status();
00268 friend class FreeCheckLink;
00269 friend class FCLClass;
00270 friend class FCLRealArray;
00271 friend class FCLIntArray;
00272 };
00273 
00274 #define FREE_CHECK(Class) \
00275     public: \
00276       void* operator new(size_t size) \ { \
00277         void* t = ::operator new(size); FreeCheck::Register(t,#Class); \
00278         return t; \
00279       } \
00280       void operator delete(void* t) \
00281         { FreeCheck::DeRegister(t,#Class); ::operator delete(t); }
00282 
00283 #ifdef SimulateExceptions                       // SimulateExceptions
00284 
00285 #define NEW_DELETE(Class) \
00286     public: \
00287       void* operator new(size_t size) \ { \
00288         do_not_link=true; \
00289         void* t = ::operator new(size); FreeCheck::Register(t,#Class); \
00290         return t; \
00291       } \
00292       void operator delete(void* t) \
00293         { FreeCheck::DeRegister(t,#Class); ::operator delete(t); }
00294 #endif                                          // end of SimulateExceptions
00295 
00296 #define MONITOR_REAL_NEW(Operation, Size, Pointer) \
00297       FreeCheck::RegisterR(Pointer, Operation, Size);
00298 #define MONITOR_INT_NEW(Operation, Size, Pointer) \
00299       FreeCheck::RegisterI(Pointer, Operation, Size);
00300 #define MONITOR_REAL_DELETE(Operation, Size, Pointer) \
00301       FreeCheck::DeRegisterR(Pointer, Operation, Size);
00302 #define MONITOR_INT_DELETE(Operation, Size, Pointer) \
00303       FreeCheck::DeRegisterI(Pointer, Operation, Size);
00304 
00305 #else                                           // DO_FREE_CHECK not defined
00306 
00307 #define FREE_CHECK(Class) public:
00308 #define MONITOR_REAL_NEW(Operation, Size, Pointer) {}
00309 #define MONITOR_INT_NEW(Operation, Size, Pointer) {}
00310 #define MONITOR_REAL_DELETE(Operation, Size, Pointer) {}
00311 #define MONITOR_INT_DELETE(Operation, Size, Pointer) {}
00312 
00313 #ifdef SimulateExceptions                       // SimulateExceptions
00314 
00315 #define NEW_DELETE(Class) \
00316     public: \
00317       void* operator new(size_t size) \
00318         { do_not_link=true; void* t = ::operator new(size); return t; } \
00319       void operator delete(void* t) { ::operator delete(t); }
00320 #endif                                          // end of SimulateExceptions
00321 #endif                                          // end of ! DO_FREE_CHECK
00322 
00323 #ifndef SimulateExceptions                      // ! SimulateExceptions
00324 
00325 #define NEW_DELETE(Class) FREE_CHECK(Class)
00326 #endif                                          // end of ! SimulateExceptions
00327 
00328 //********************* derived exceptions ******************************//
00329 
00331 class Logic_error : public Exception {
00332 public:
00333 static unsigned long Select;
00334 Logic_error(const char* a_what = 0);
00335 };
00336 
00339 class Runtime_error : public Exception {
00340 public:
00341 static unsigned long Select;
00342 Runtime_error(const char* a_what = 0);
00343 };
00344 
00346 class Domain_error : public Logic_error {
00347 public:
00348 static unsigned long Select;
00349 Domain_error(const char* a_what = 0);
00350 };
00351 
00353 class Invalid_argument : public Logic_error {
00354 public:
00355 static unsigned long Select;
00356 Invalid_argument(const char* a_what = 0);
00357 };
00358 
00360 class Length_error : public Logic_error {
00361 public:
00362 static unsigned long Select;
00363 Length_error(const char* a_what = 0);
00364 };
00365 
00367 class Out_of_range : public Logic_error {
00368 public:
00369 static unsigned long Select;
00370 Out_of_range(const char* a_what = 0);
00371 };
00372 
00374 class Bad_cast : public Logic_error {
00375 public:
00376 static unsigned long Select;
00377 Bad_cast(const char* a_what = 0);
00378 };
00379 
00381 class Bad_typeid : public Logic_error {
00382 public:
00383 static unsigned long Select;
00384 Bad_typeid(const char* a_what = 0);
00385 };
00386 
00388 class Range_error : public Runtime_error {
00389 public:
00390 static unsigned long Select;
00391 Range_error(const char* a_what = 0);
00392 };
00393 
00395 class Overflow_error : public Runtime_error {
00396 public:
00397 static unsigned long Select;
00398 Overflow_error(const char* a_what = 0);
00399 };
00400 
00402 class Bad_alloc : public Exception {
00403 public:
00404 static unsigned long Select;
00405 Bad_alloc(const char* a_what = 0);
00406 };
00407 
00408 #ifdef use_namespace
00409 }
00410 #endif
00411 #endif                                            // end of EXCEPTION_LIB

Generated on Fri Aug 23 01:37:03 2002 for VirtualU by doxygen1.2.17