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


lo
Author(s): U. Klank
autogenerated on Mon Oct 6 2014 10:44:13