| 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 |
#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 |
|
| 63 |
|
| 64 |
class BaseException; |
| 65 |
|
| 66 |
class Tracer |
| 67 |
{ |
| 68 |
const char* entry; |
| 69 |
Tracer* previous; |
| 70 |
public: |
| 71 |
Tracer(const char*); |
| 72 |
~Tracer(); |
| 73 |
void ReName(const char*); |
| 74 |
static void PrintTrace(); |
| 75 |
static void AddTrace(); |
| 76 |
static Tracer* last; |
| 77 |
friend class BaseException; |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
class BaseException |
| 82 |
{ |
| 83 |
protected: |
| 84 |
static char* what_error; |
| 85 |
static int SoFar; |
| 86 |
static int LastOne; |
| 87 |
public: |
| 88 |
static void AddMessage(const char* a_what); |
| 89 |
|
| 90 |
static void AddInt(int value); |
| 91 |
static unsigned long Select; |
| 92 |
BaseException(const char* a_what = 0); |
| 93 |
static const char* what() { return what_error; } |
| 94 |
|
| 95 |
}; |
| 96 |
|
| 97 |
#ifdef TypeDefException |
| 98 |
typedef BaseException Exception; |
| 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 |
| 109 |
|
| 110 |
#include <setjmp.h> |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
class JumpItem; |
| 117 |
class Janitor; |
| 118 |
|
| 119 |
class JumpBase |
| 120 |
{ |
| 121 |
public: |
| 122 |
static JumpItem *jl; |
| 123 |
static jmp_buf env; |
| 124 |
}; |
| 125 |
|
| 126 |
class JumpItem |
| 127 |
{ |
| 128 |
public: |
| 129 |
JumpItem *ji; |
| 130 |
jmp_buf env; |
| 131 |
Tracer* trace; |
| 132 |
Janitor* janitor; |
| 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 |
|
| 158 |
|
| 159 |
class Janitor |
| 160 |
{ |
| 161 |
protected: |
| 162 |
static bool do_not_link; |
| 163 |
bool OnStack; |
| 164 |
public: |
| 165 |
Janitor* NextJanitor; |
| 166 |
virtual void CleanUp() {} |
| 167 |
Janitor(); |
| 168 |
virtual ~Janitor(); |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 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 |
| 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 |
| 198 |
|
| 199 |
|
| 200 |
#ifdef DisableExceptions |
| 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 |
| 213 |
|
| 214 |
#ifndef SimulateExceptions |
| 215 |
|
| 216 |
class Janitor |
| 217 |
{ |
| 218 |
public: |
| 219 |
virtual void CleanUp() {} |
| 220 |
Janitor() {} |
| 221 |
virtual ~Janitor() {} |
| 222 |
}; |
| 223 |
|
| 224 |
#endif |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
#ifdef DO_FREE_CHECK |
| 230 |
|
| 231 |
|
| 232 |
class FreeCheck; |
| 233 |
|
| 234 |
class FreeCheckLink |
| 235 |
{ |
| 236 |
protected: |
| 237 |
FreeCheckLink* next; |
| 238 |
void* ClassStore; |
| 239 |
FreeCheckLink(); |
| 240 |
virtual void Report()=0; |
| 241 |
friend class FreeCheck; |
| 242 |
}; |
| 243 |
|
| 244 |
class FCLClass : public FreeCheckLink |
| 245 |
{ |
| 246 |
char* ClassName; |
| 247 |
FCLClass(void* t, char* name); |
| 248 |
void Report(); |
| 249 |
friend class FreeCheck; |
| 250 |
}; |
| 251 |
|
| 252 |
class FCLRealArray : public FreeCheckLink |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 345 |
|
| 346 |
#endif |
| 347 |
|
| 348 |
#ifndef SimulateExceptions |
| 349 |
|
| 350 |
#define NEW_DELETE(Class) FREE_CHECK(Class) |
| 351 |
|
| 352 |
#endif |
| 353 |
|
| 354 |
|
| 355 |
|
| 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 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 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 |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|