| 1 | //$$ myexcept.h                                  Exception handling classes | 
|---|
| 2 |  | 
|---|
| 3 |  | 
|---|
| 4 | // A set of classes to simulate exceptions in C++ | 
|---|
| 5 | // | 
|---|
| 6 | //   Partially copied from Carlos Vidal s article in the C users  journal | 
|---|
| 7 | //   September 1992, pp 19-28 | 
|---|
| 8 | // | 
|---|
| 9 | //   Operations defined | 
|---|
| 10 | //      Try {     } | 
|---|
| 11 | //      Throw ( exception object ) | 
|---|
| 12 | //      ReThrow | 
|---|
| 13 | //      Catch ( exception class ) {      } | 
|---|
| 14 | //      CatchAll {      } | 
|---|
| 15 | //      CatchAndThrow | 
|---|
| 16 | // | 
|---|
| 17 | //   All catch lists must end with a CatchAll or CatchAndThrow statement | 
|---|
| 18 | //   but not both. | 
|---|
| 19 | // | 
|---|
| 20 | //   When exceptions are finally implemented replace Try, Throw(E), Rethrow, | 
|---|
| 21 | //   Catch, CatchAll, CatchAndThrow by try, throw E, throw, catch, | 
|---|
| 22 | //   catch(...), and {}. | 
|---|
| 23 | // | 
|---|
| 24 | //   All exception classes must be derived from Exception, have no non-static | 
|---|
| 25 | //   variables and must include the statement | 
|---|
| 26 | // | 
|---|
| 27 | //      static unsigned long Select; | 
|---|
| 28 | // | 
|---|
| 29 | //   Any constructor in one of these exception classes must include | 
|---|
| 30 | // | 
|---|
| 31 | //      Select = Exception::Select; | 
|---|
| 32 | // | 
|---|
| 33 | //   For each exceptions class, EX_1, some .cpp file must include | 
|---|
| 34 | // | 
|---|
| 35 | //      unsigned long EX_1::Select; | 
|---|
| 36 | // | 
|---|
| 37 |  | 
|---|
| 38 |  | 
|---|
| 39 | #ifndef EXCEPTION_LIB | 
|---|
| 40 | #define EXCEPTION_LIB | 
|---|
| 41 |  | 
|---|
| 42 | #ifdef use_namespace | 
|---|
| 43 | namespace RBD_COMMON { | 
|---|
| 44 | #endif | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 | void Terminate(); | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 | //********** classes for setting up exceptions and reporting ************// | 
|---|
| 51 |  | 
|---|
| 52 | class Exception; | 
|---|
| 53 |  | 
|---|
| 54 | class Tracer                             // linked list showing how | 
|---|
| 55 | {                                        // we got here | 
|---|
| 56 |    const char* entry; | 
|---|
| 57 |    Tracer* previous; | 
|---|
| 58 | public: | 
|---|
| 59 |    Tracer(const char*); | 
|---|
| 60 |    ~Tracer(); | 
|---|
| 61 |    void ReName(const char*); | 
|---|
| 62 |    static void PrintTrace();             // for printing trace | 
|---|
| 63 |    static void AddTrace();               // insert trace in exception record | 
|---|
| 64 |    static Tracer* last;                  // points to Tracer list | 
|---|
| 65 |    friend class Exception; | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | class Exception                          // The base exception class | 
|---|
| 70 | { | 
|---|
| 71 | protected: | 
|---|
| 72 |    static char* what_error;              // error message | 
|---|
| 73 |    static int SoFar;                     // no. characters already entered | 
|---|
| 74 |    static int LastOne;                   // last location in error buffer | 
|---|
| 75 | public: | 
|---|
| 76 |    static void AddMessage(const char* a_what); | 
|---|
| 77 |                                          // messages about exception | 
|---|
| 78 |    static void AddInt(int value);        // integer to error message | 
|---|
| 79 |    static unsigned long Select;          // for identifying exception | 
|---|
| 80 |    Exception(const char* a_what = 0); | 
|---|
| 81 |    static const char* what() { return what_error; } | 
|---|
| 82 |                                          // for getting error message | 
|---|
| 83 | }; | 
|---|
| 84 |  | 
|---|
| 85 |  | 
|---|
| 86 | inline Tracer::Tracer(const char* e) | 
|---|
| 87 |    : entry(e), previous(last) { last = this; } | 
|---|
| 88 |  | 
|---|
| 89 | inline Tracer::~Tracer() { last = previous; } | 
|---|
| 90 |  | 
|---|
| 91 | inline void Tracer::ReName(const char* e) { entry=e; } | 
|---|
| 92 |  | 
|---|
| 93 | #ifdef SimulateExceptions                // SimulateExceptions | 
|---|
| 94 |  | 
|---|
| 95 | #include <setjmp.h> | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 | //************* the definitions of Try, Throw and Catch *****************// | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 | class JumpItem; | 
|---|
| 102 | class Janitor; | 
|---|
| 103 |  | 
|---|
| 104 | class JumpBase         // pointer to a linked list of jmp_buf s | 
|---|
| 105 | { | 
|---|
| 106 | public: | 
|---|
| 107 |    static JumpItem *jl; | 
|---|
| 108 |    static jmp_buf env; | 
|---|
| 109 | }; | 
|---|
| 110 |  | 
|---|
| 111 | class JumpItem         // an item in a linked list of jmp_buf s | 
|---|
| 112 | { | 
|---|
| 113 | public: | 
|---|
| 114 |    JumpItem *ji; | 
|---|
| 115 |    jmp_buf env; | 
|---|
| 116 |    Tracer* trace;                     // to keep check on Tracer items | 
|---|
| 117 |    Janitor* janitor;                  // list of items for cleanup | 
|---|
| 118 |    JumpItem() : ji(JumpBase::jl), trace(0), janitor(0) | 
|---|
| 119 |       { JumpBase::jl = this; } | 
|---|
| 120 |    ~JumpItem() { JumpBase::jl = ji; } | 
|---|
| 121 | }; | 
|---|
| 122 |  | 
|---|
| 123 | void Throw(); | 
|---|
| 124 |  | 
|---|
| 125 | inline void Throw(const Exception&) { Throw(); } | 
|---|
| 126 |  | 
|---|
| 127 | #define Try                                             \ | 
|---|
| 128 |    if (!setjmp( JumpBase::jl->env )) {                  \ | 
|---|
| 129 |    JumpBase::jl->trace = Tracer::last;               \ | 
|---|
| 130 |    JumpItem JI387256156; | 
|---|
| 131 |  | 
|---|
| 132 | #define ReThrow Throw() | 
|---|
| 133 |  | 
|---|
| 134 | #define Catch(EXCEPTION)                                \ | 
|---|
| 135 |    } else if (Exception::Select == EXCEPTION::Select) { | 
|---|
| 136 |  | 
|---|
| 137 | #define CatchAll } else | 
|---|
| 138 |  | 
|---|
| 139 | #define CatchAndThrow  } else Throw(); | 
|---|
| 140 |  | 
|---|
| 141 |  | 
|---|
| 142 | //****************** cleanup heap following Throw ***********************// | 
|---|
| 143 |  | 
|---|
| 144 | class Janitor | 
|---|
| 145 | { | 
|---|
| 146 | protected: | 
|---|
| 147 |    static bool do_not_link;                  // set when new is called | 
|---|
| 148 |    bool OnStack;                             // false if created by new | 
|---|
| 149 | public: | 
|---|
| 150 |    Janitor* NextJanitor; | 
|---|
| 151 |    virtual void CleanUp() {} | 
|---|
| 152 |    Janitor(); | 
|---|
| 153 |    virtual ~Janitor(); | 
|---|
| 154 | }; | 
|---|
| 155 |  | 
|---|
| 156 |  | 
|---|
| 157 | // The tiresome old trick for initializing the Janitor class | 
|---|
| 158 | // this is needed for classes derived from Janitor which have objects | 
|---|
| 159 | // declared globally | 
|---|
| 160 |  | 
|---|
| 161 | class JanitorInitializer | 
|---|
| 162 | { | 
|---|
| 163 | public: | 
|---|
| 164 |    JanitorInitializer(); | 
|---|
| 165 | private: | 
|---|
| 166 |    static int ref_count; | 
|---|
| 167 | }; | 
|---|
| 168 |  | 
|---|
| 169 | static JanitorInitializer JanInit; | 
|---|
| 170 |  | 
|---|
| 171 | #endif                                // end of SimulateExceptions | 
|---|
| 172 |  | 
|---|
| 173 | #ifdef UseExceptions | 
|---|
| 174 |  | 
|---|
| 175 | #define Try try | 
|---|
| 176 | #define Throw(E) throw E | 
|---|
| 177 | #define ReThrow throw | 
|---|
| 178 | #define Catch catch | 
|---|
| 179 | #define CatchAll catch(...) | 
|---|
| 180 | #define CatchAndThrow {} | 
|---|
| 181 |  | 
|---|
| 182 | #endif                                // end of UseExceptions | 
|---|
| 183 |  | 
|---|
| 184 |  | 
|---|
| 185 | #ifdef DisableExceptions              // Disable exceptions | 
|---|
| 186 |  | 
|---|
| 187 | #define Try { | 
|---|
| 188 | #define ReThrow Throw() | 
|---|
| 189 | #define Catch(EXCEPTION) } if (false) { | 
|---|
| 190 | #define CatchAll } if (false) | 
|---|
| 191 | #define CatchAndThrow } | 
|---|
| 192 |  | 
|---|
| 193 | inline void Throw() { Terminate(); } | 
|---|
| 194 | inline void Throw(const Exception&) { Terminate(); } | 
|---|
| 195 |  | 
|---|
| 196 |  | 
|---|
| 197 | #endif                                // end of DisableExceptions | 
|---|
| 198 |  | 
|---|
| 199 | #ifndef SimulateExceptions            // ! SimulateExceptions | 
|---|
| 200 |  | 
|---|
| 201 | class Janitor                         // a dummy version | 
|---|
| 202 | { | 
|---|
| 203 | public: | 
|---|
| 204 |    virtual void CleanUp() {} | 
|---|
| 205 |    Janitor() {} | 
|---|
| 206 |    virtual ~Janitor() {} | 
|---|
| 207 | }; | 
|---|
| 208 |  | 
|---|
| 209 | #endif                                // end of ! SimulateExceptions | 
|---|
| 210 |  | 
|---|
| 211 |  | 
|---|
| 212 | //******************** FREE_CHECK and NEW_DELETE ***********************// | 
|---|
| 213 |  | 
|---|
| 214 | #ifdef DO_FREE_CHECK                          // DO_FREE_CHECK | 
|---|
| 215 | // Routines for tracing whether new and delete calls are balanced | 
|---|
| 216 |  | 
|---|
| 217 | class FreeCheck; | 
|---|
| 218 |  | 
|---|
| 219 | class FreeCheckLink | 
|---|
| 220 | { | 
|---|
| 221 | protected: | 
|---|
| 222 |    FreeCheckLink* next; | 
|---|
| 223 |    void* ClassStore; | 
|---|
| 224 |    FreeCheckLink(); | 
|---|
| 225 |    virtual void Report()=0;                   // print details of link | 
|---|
| 226 |    friend class FreeCheck; | 
|---|
| 227 | }; | 
|---|
| 228 |  | 
|---|
| 229 | class FCLClass : public FreeCheckLink         // for registering objects | 
|---|
| 230 | { | 
|---|
| 231 |    char* ClassName; | 
|---|
| 232 |    FCLClass(void* t, char* name); | 
|---|
| 233 |    void Report(); | 
|---|
| 234 |    friend class FreeCheck; | 
|---|
| 235 | }; | 
|---|
| 236 |  | 
|---|
| 237 | class FCLRealArray : public FreeCheckLink     // for registering real arrays | 
|---|
| 238 | { | 
|---|
| 239 |    char* Operation; | 
|---|
| 240 |    int size; | 
|---|
| 241 |    FCLRealArray(void* t, char* o, int s); | 
|---|
| 242 |    void Report(); | 
|---|
| 243 |    friend class FreeCheck; | 
|---|
| 244 | }; | 
|---|
| 245 |  | 
|---|
| 246 | class FCLIntArray : public FreeCheckLink     // for registering int arrays | 
|---|
| 247 | { | 
|---|
| 248 |    char* Operation; | 
|---|
| 249 |    int size; | 
|---|
| 250 |    FCLIntArray(void* t, char* o, int s); | 
|---|
| 251 |    void Report(); | 
|---|
| 252 |    friend class FreeCheck; | 
|---|
| 253 | }; | 
|---|
| 254 |  | 
|---|
| 255 |  | 
|---|
| 256 | class FreeCheck | 
|---|
| 257 | { | 
|---|
| 258 |    static FreeCheckLink* next; | 
|---|
| 259 |    static int BadDelete; | 
|---|
| 260 | public: | 
|---|
| 261 |    static void Register(void*, char*); | 
|---|
| 262 |    static void DeRegister(void*, char*); | 
|---|
| 263 |    static void RegisterR(void*, char*, int); | 
|---|
| 264 |    static void DeRegisterR(void*, char*, int); | 
|---|
| 265 |    static void RegisterI(void*, char*, int); | 
|---|
| 266 |    static void DeRegisterI(void*, char*, int); | 
|---|
| 267 |    static void Status(); | 
|---|
| 268 |    friend class FreeCheckLink; | 
|---|
| 269 |    friend class FCLClass; | 
|---|
| 270 |    friend class FCLRealArray; | 
|---|
| 271 |    friend class FCLIntArray; | 
|---|
| 272 | }; | 
|---|
| 273 |  | 
|---|
| 274 | #define FREE_CHECK(Class)                                                  \ | 
|---|
| 275 | public:                                                                    \ | 
|---|
| 276 |    void* operator new(size_t size)                                         \ | 
|---|
| 277 |    {                                                                       \ | 
|---|
| 278 |       void* t = ::operator new(size); FreeCheck::Register(t,#Class);       \ | 
|---|
| 279 |       return t;                                                            \ | 
|---|
| 280 |    }                                                                       \ | 
|---|
| 281 |    void operator delete(void* t)                                           \ | 
|---|
| 282 |    { FreeCheck::DeRegister(t,#Class); ::operator delete(t); } | 
|---|
| 283 |  | 
|---|
| 284 |  | 
|---|
| 285 | #ifdef SimulateExceptions         // SimulateExceptions | 
|---|
| 286 |  | 
|---|
| 287 | #define NEW_DELETE(Class)                                                  \ | 
|---|
| 288 | public:                                                                    \ | 
|---|
| 289 |    void* operator new(size_t size)                                         \ | 
|---|
| 290 |    {                                                                       \ | 
|---|
| 291 |       do_not_link=true;                                                    \ | 
|---|
| 292 |       void* t = ::operator new(size); FreeCheck::Register(t,#Class);       \ | 
|---|
| 293 |       return t;                                                            \ | 
|---|
| 294 |    }                                                                       \ | 
|---|
| 295 |    void operator delete(void* t)                                           \ | 
|---|
| 296 |    { FreeCheck::DeRegister(t,#Class); ::operator delete(t); } | 
|---|
| 297 |  | 
|---|
| 298 |  | 
|---|
| 299 | #endif                           // end of SimulateExceptions | 
|---|
| 300 |  | 
|---|
| 301 |  | 
|---|
| 302 | #define MONITOR_REAL_NEW(Operation, Size, Pointer)                         \ | 
|---|
| 303 |         FreeCheck::RegisterR(Pointer, Operation, Size); | 
|---|
| 304 | #define MONITOR_INT_NEW(Operation, Size, Pointer)                          \ | 
|---|
| 305 |         FreeCheck::RegisterI(Pointer, Operation, Size); | 
|---|
| 306 | #define MONITOR_REAL_DELETE(Operation, Size, Pointer)                      \ | 
|---|
| 307 |         FreeCheck::DeRegisterR(Pointer, Operation, Size); | 
|---|
| 308 | #define MONITOR_INT_DELETE(Operation, Size, Pointer)                       \ | 
|---|
| 309 |         FreeCheck::DeRegisterI(Pointer, Operation, Size); | 
|---|
| 310 |  | 
|---|
| 311 | #else                            // DO_FREE_CHECK not defined | 
|---|
| 312 |  | 
|---|
| 313 | #define FREE_CHECK(Class) public: | 
|---|
| 314 | #define MONITOR_REAL_NEW(Operation, Size, Pointer) {} | 
|---|
| 315 | #define MONITOR_INT_NEW(Operation, Size, Pointer) {} | 
|---|
| 316 | #define MONITOR_REAL_DELETE(Operation, Size, Pointer) {} | 
|---|
| 317 | #define MONITOR_INT_DELETE(Operation, Size, Pointer) {} | 
|---|
| 318 |  | 
|---|
| 319 |  | 
|---|
| 320 | #ifdef SimulateExceptions         // SimulateExceptions | 
|---|
| 321 |  | 
|---|
| 322 |  | 
|---|
| 323 | #define NEW_DELETE(Class)                                                  \ | 
|---|
| 324 | public:                                                                    \ | 
|---|
| 325 |         void* operator new(size_t size)                                    \ | 
|---|
| 326 |         { do_not_link=true; void* t = ::operator new(size); return t; }    \ | 
|---|
| 327 |         void operator delete(void* t) { ::operator delete(t); } | 
|---|
| 328 |  | 
|---|
| 329 | #endif                            // end of SimulateExceptions | 
|---|
| 330 |  | 
|---|
| 331 | #endif                            // end of ! DO_FREE_CHECK | 
|---|
| 332 |  | 
|---|
| 333 | #ifndef SimulateExceptions        // ! SimulateExceptions | 
|---|
| 334 |  | 
|---|
| 335 | #define NEW_DELETE(Class) FREE_CHECK(Class) | 
|---|
| 336 |  | 
|---|
| 337 | #endif                            // end of ! SimulateExceptions | 
|---|
| 338 |  | 
|---|
| 339 |  | 
|---|
| 340 | //********************* derived exceptions ******************************// | 
|---|
| 341 |  | 
|---|
| 342 | class Logic_error : public Exception | 
|---|
| 343 | { | 
|---|
| 344 | public: | 
|---|
| 345 |    static unsigned long Select; | 
|---|
| 346 |    Logic_error(const char* a_what = 0); | 
|---|
| 347 | }; | 
|---|
| 348 |  | 
|---|
| 349 | class Runtime_error : public Exception | 
|---|
| 350 | { | 
|---|
| 351 | public: | 
|---|
| 352 |    static unsigned long Select; | 
|---|
| 353 |    Runtime_error(const char* a_what = 0); | 
|---|
| 354 | }; | 
|---|
| 355 |  | 
|---|
| 356 | class Domain_error : public Logic_error | 
|---|
| 357 | { | 
|---|
| 358 | public: | 
|---|
| 359 |    static unsigned long Select; | 
|---|
| 360 |    Domain_error(const char* a_what = 0); | 
|---|
| 361 | }; | 
|---|
| 362 |  | 
|---|
| 363 | class Invalid_argument : public Logic_error | 
|---|
| 364 | { | 
|---|
| 365 | public: | 
|---|
| 366 |    static unsigned long Select; | 
|---|
| 367 |    Invalid_argument(const char* a_what = 0); | 
|---|
| 368 | }; | 
|---|
| 369 |  | 
|---|
| 370 | class Length_error : public Logic_error | 
|---|
| 371 | { | 
|---|
| 372 | public: | 
|---|
| 373 |    static unsigned long Select; | 
|---|
| 374 |    Length_error(const char* a_what = 0); | 
|---|
| 375 | }; | 
|---|
| 376 |  | 
|---|
| 377 | class Out_of_range : public Logic_error | 
|---|
| 378 | { | 
|---|
| 379 | public: | 
|---|
| 380 |    static unsigned long Select; | 
|---|
| 381 |    Out_of_range(const char* a_what = 0); | 
|---|
| 382 | }; | 
|---|
| 383 |  | 
|---|
| 384 | //class Bad_cast : public Logic_error | 
|---|
| 385 | //{ | 
|---|
| 386 | //public: | 
|---|
| 387 | //   static unsigned long Select; | 
|---|
| 388 | //   Bad_cast(const char* a_what = 0); | 
|---|
| 389 | //}; | 
|---|
| 390 |  | 
|---|
| 391 | //class Bad_typeid : public Logic_error | 
|---|
| 392 | //{ | 
|---|
| 393 | //public: | 
|---|
| 394 | //   static unsigned long Select; | 
|---|
| 395 | //   Bad_typeid(const char* a_what = 0); | 
|---|
| 396 | //}; | 
|---|
| 397 |  | 
|---|
| 398 | class Range_error : public Runtime_error | 
|---|
| 399 | { | 
|---|
| 400 | public: | 
|---|
| 401 |    static unsigned long Select; | 
|---|
| 402 |    Range_error(const char* a_what = 0); | 
|---|
| 403 | }; | 
|---|
| 404 |  | 
|---|
| 405 | class Overflow_error : public Runtime_error | 
|---|
| 406 | { | 
|---|
| 407 | public: | 
|---|
| 408 |    static unsigned long Select; | 
|---|
| 409 |    Overflow_error(const char* a_what = 0); | 
|---|
| 410 | }; | 
|---|
| 411 |  | 
|---|
| 412 | class Bad_alloc : public Exception | 
|---|
| 413 | { | 
|---|
| 414 | public: | 
|---|
| 415 |    static unsigned long Select; | 
|---|
| 416 |    Bad_alloc(const char* a_what = 0); | 
|---|
| 417 | }; | 
|---|
| 418 |  | 
|---|
| 419 | #ifdef use_namespace | 
|---|
| 420 | } | 
|---|
| 421 | #endif | 
|---|
| 422 |  | 
|---|
| 423 |  | 
|---|
| 424 | #endif                            // end of EXCEPTION_LIB | 
|---|
| 425 |  | 
|---|
| 426 |  | 
|---|
| 427 | // body file: myexcept.cpp | 
|---|
| 428 |  | 
|---|
| 429 |  | 
|---|
| 430 |  | 
|---|