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