OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
myexcept.h
Go to the documentation of this file.
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 BaseException, have no
25 // non-static 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 = BaseException::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 BaseException;
53 
54 class Tracer // linked list showing how
55 { // we got here
56  const char* entry;
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 BaseException;
66 };
67 
68 
69 class BaseException // 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  BaseException(const char* a_what = 0);
81  static const char* what() { return what_error; }
82  // for getting error message
83 };
84 
85 #ifdef TypeDefException
86 typedef BaseException Exception; // for compatibility with my older libraries
87 #endif
88 
89 inline Tracer::Tracer(const char* e)
90  : entry(e), previous(last) { last = this; }
91 
92 inline Tracer::~Tracer() { last = previous; }
93 
94 inline void Tracer::ReName(const char* e) { entry=e; }
95 
96 #ifdef SimulateExceptions // SimulateExceptions
97 
98 #include <setjmp.h>
99 
100 
101 //************* the definitions of Try, Throw and Catch *****************//
102 
103 
104 class JumpItem;
105 class Janitor;
106 
107 class JumpBase // pointer to a linked list of jmp_buf s
108 {
109 public:
110  static JumpItem *jl;
111  static jmp_buf env;
112 };
113 
114 class JumpItem // an item in a linked list of jmp_buf s
115 {
116 public:
117  JumpItem *ji;
118  jmp_buf env;
119  Tracer* trace; // to keep check on Tracer items
120  Janitor* janitor; // list of items for cleanup
121  JumpItem() : ji(JumpBase::jl), trace(0), janitor(0)
122  { JumpBase::jl = this; }
123  ~JumpItem() { JumpBase::jl = ji; }
124 };
125 
126 void Throw();
127 
128 inline void Throw(const BaseException&) { Throw(); }
129 
130 #define Try \
131  if (!setjmp( JumpBase::jl->env )) { \
132  JumpBase::jl->trace = Tracer::last; \
133  JumpItem JI387256156;
134 
135 #define ReThrow Throw()
136 
137 #define Catch(EXCEPTION) \
138  } else if (BaseException::Select == EXCEPTION::Select) {
139 
140 #define CatchAll } else
141 
142 #define CatchAndThrow } else Throw();
143 
144 
145 //****************** cleanup heap following Throw ***********************//
146 
147 class Janitor
148 {
149 protected:
150  static bool do_not_link; // set when new is called
151  bool OnStack; // false if created by new
152 public:
153  Janitor* NextJanitor;
154  virtual void CleanUp() {}
155  Janitor();
156  virtual ~Janitor();
157 };
158 
159 
160 // The tiresome old trick for initializing the Janitor class
161 // this is needed for classes derived from Janitor which have objects
162 // declared globally
163 
164 class JanitorInitializer
165 {
166 public:
167  JanitorInitializer();
168 private:
169  static int ref_count;
170 };
171 
172 static JanitorInitializer JanInit;
173 
174 #endif // end of SimulateExceptions
175 
176 #ifdef UseExceptions
177 
178 #define Try try
179 #define Throw(E) throw E
180 #define ReThrow throw
181 #define Catch catch
182 #define CatchAll catch(...)
183 #define CatchAndThrow {}
184 
185 #endif // end of UseExceptions
186 
187 
188 #ifdef DisableExceptions // Disable exceptions
189 
190 #define Try {
191 #define ReThrow Throw()
192 #define Catch(EXCEPTION) } if (false) {
193 #define CatchAll } if (false)
194 #define CatchAndThrow }
195 
196 inline void Throw() { Terminate(); }
197 inline void Throw(const BaseException&) { Terminate(); }
198 
199 
200 #endif // end of DisableExceptions
201 
202 #ifndef SimulateExceptions // ! SimulateExceptions
203 
204 class Janitor // a dummy version
205 {
206 public:
207  virtual void CleanUp() {}
208  Janitor() {}
209  virtual ~Janitor() {}
210 };
211 
212 #endif // end of ! SimulateExceptions
213 
214 
215 //******************** FREE_CHECK and NEW_DELETE ***********************//
216 
217 #ifdef DO_FREE_CHECK // DO_FREE_CHECK
218 // Routines for tracing whether new and delete calls are balanced
219 
220 class FreeCheck;
221 
222 class FreeCheckLink
223 {
224 protected:
225  FreeCheckLink* next;
226  void* ClassStore;
227  FreeCheckLink();
228  virtual void Report()=0; // print details of link
229  friend class FreeCheck;
230 };
231 
232 class FCLClass : public FreeCheckLink // for registering objects
233 {
234  char* ClassName;
235  FCLClass(void* t, char* name);
236  void Report();
237  friend class FreeCheck;
238 };
239 
240 class FCLRealArray : public FreeCheckLink // for registering real arrays
241 {
242  char* Operation;
243  int size;
244  FCLRealArray(void* t, char* o, int s);
245  void Report();
246  friend class FreeCheck;
247 };
248 
249 class FCLIntArray : public FreeCheckLink // for registering int arrays
250 {
251  char* Operation;
252  int size;
253  FCLIntArray(void* t, char* o, int s);
254  void Report();
255  friend class FreeCheck;
256 };
257 
258 
259 class FreeCheck
260 {
261  static FreeCheckLink* next;
262  static int BadDelete;
263 public:
264  static void Register(void*, char*);
265  static void DeRegister(void*, char*);
266  static void RegisterR(void*, char*, int);
267  static void DeRegisterR(void*, char*, int);
268  static void RegisterI(void*, char*, int);
269  static void DeRegisterI(void*, char*, int);
270  static void Status();
271  friend class FreeCheckLink;
272  friend class FCLClass;
273  friend class FCLRealArray;
274  friend class FCLIntArray;
275 };
276 
277 #define FREE_CHECK(Class) \
278 public: \
279  void* operator new(size_t size) \
280  { \
281  void* t = ::operator new(size); FreeCheck::Register(t,#Class); \
282  return t; \
283  } \
284  void operator delete(void* t) \
285  { FreeCheck::DeRegister(t,#Class); ::operator delete(t); }
286 
287 
288 #ifdef SimulateExceptions // SimulateExceptions
289 
290 #define NEW_DELETE(Class) \
291 public: \
292  void* operator new(size_t size) \
293  { \
294  do_not_link=true; \
295  void* t = ::operator new(size); FreeCheck::Register(t,#Class); \
296  return t; \
297  } \
298  void operator delete(void* t) \
299  { FreeCheck::DeRegister(t,#Class); ::operator delete(t); }
300 
301 
302 #endif // end of SimulateExceptions
303 
304 
305 #define MONITOR_REAL_NEW(Operation, Size, Pointer) \
306  FreeCheck::RegisterR(Pointer, Operation, Size);
307 #define MONITOR_INT_NEW(Operation, Size, Pointer) \
308  FreeCheck::RegisterI(Pointer, Operation, Size);
309 #define MONITOR_REAL_DELETE(Operation, Size, Pointer) \
310  FreeCheck::DeRegisterR(Pointer, Operation, Size);
311 #define MONITOR_INT_DELETE(Operation, Size, Pointer) \
312  FreeCheck::DeRegisterI(Pointer, Operation, Size);
313 
314 #else // DO_FREE_CHECK not defined
315 
316 #define FREE_CHECK(Class) public:
317 #define MONITOR_REAL_NEW(Operation, Size, Pointer) {}
318 #define MONITOR_INT_NEW(Operation, Size, Pointer) {}
319 #define MONITOR_REAL_DELETE(Operation, Size, Pointer) {}
320 #define MONITOR_INT_DELETE(Operation, Size, Pointer) {}
321 
322 
323 #ifdef SimulateExceptions // SimulateExceptions
324 
325 
326 #define NEW_DELETE(Class) \
327 public: \
328  void* operator new(size_t size) \
329  { do_not_link=true; void* t = ::operator new(size); return t; } \
330  void operator delete(void* t) { ::operator delete(t); }
331 
332 #endif // end of SimulateExceptions
333 
334 #endif // end of ! DO_FREE_CHECK
335 
336 #ifndef SimulateExceptions // ! SimulateExceptions
337 
338 #define NEW_DELETE(Class) FREE_CHECK(Class)
339 
340 #endif // end of ! SimulateExceptions
341 
342 
343 //********************* derived exceptions ******************************//
344 
346 {
347 public:
348  static unsigned long Select;
349  Logic_error(const char* a_what = 0);
350 };
351 
353 {
354 public:
355  static unsigned long Select;
356  Runtime_error(const char* a_what = 0);
357 };
358 
359 class Domain_error : public Logic_error
360 {
361 public:
362  static unsigned long Select;
363  Domain_error(const char* a_what = 0);
364 };
365 
367 {
368 public:
369  static unsigned long Select;
370  Invalid_argument(const char* a_what = 0);
371 };
372 
373 class Length_error : public Logic_error
374 {
375 public:
376  static unsigned long Select;
377  Length_error(const char* a_what = 0);
378 };
379 
380 class Out_of_range : public Logic_error
381 {
382 public:
383  static unsigned long Select;
384  Out_of_range(const char* a_what = 0);
385 };
386 
387 //class Bad_cast : public Logic_error
388 //{
389 //public:
390 // static unsigned long Select;
391 // Bad_cast(const char* a_what = 0);
392 //};
393 
394 //class Bad_typeid : public Logic_error
395 //{
396 //public:
397 // static unsigned long Select;
398 // Bad_typeid(const char* a_what = 0);
399 //};
400 
402 {
403 public:
404  static unsigned long Select;
405  Range_error(const char* a_what = 0);
406 };
407 
409 {
410 public:
411  static unsigned long Select;
412  Overflow_error(const char* a_what = 0);
413 };
414 
415 class Bad_alloc : public BaseException
416 {
417 public:
418  static unsigned long Select;
419  Bad_alloc(const char* a_what = 0);
420 };
421 
422 #ifdef use_namespace
423 }
424 #endif
425 
426 
427 #endif // end of EXCEPTION_LIB
428 
429 
430 // body file: myexcept.cpp
431 
432 
433 
Janitor()
Definition: myexcept.h:208
Tracer * previous
Definition: myexcept.h:57
static unsigned long Select
Definition: myexcept.h:369
static unsigned long Select
Definition: myexcept.h:348
Tracer(const char *)
Definition: myexcept.h:89
virtual ~Janitor()
Definition: myexcept.h:209
void Terminate()
Definition: myexcept.cpp:220
static unsigned long Select
Definition: myexcept.h:418
Invalid_argument(const char *a_what=0)
Definition: myexcept.cpp:413
Out_of_range(const char *a_what=0)
Definition: myexcept.cpp:427
Domain_error(const char *a_what=0)
Definition: myexcept.cpp:406
static unsigned long Select
Definition: myexcept.h:362
Bad_alloc(const char *a_what=0)
Definition: myexcept.cpp:462
~Tracer()
Definition: myexcept.h:92
static unsigned long Select
Definition: myexcept.h:383
static unsigned long Select
Definition: myexcept.h:355
yy_size_t size
static unsigned long Select
Definition: myexcept.h:404
static unsigned long Select
Definition: myexcept.h:411
static Tracer * last
Definition: myexcept.h:64
Runtime_error(const char *a_what=0)
Definition: myexcept.cpp:398
Overflow_error(const char *a_what=0)
Definition: myexcept.cpp:455
Range_error(const char *a_what=0)
Definition: myexcept.cpp:448
static int SoFar
Definition: myexcept.h:73
static const char * what()
Definition: myexcept.h:81
virtual void CleanUp()
Definition: myexcept.h:207
static int LastOne
Definition: myexcept.h:74
static unsigned long Select
Definition: myexcept.h:79
Logic_error(const char *a_what=0)
Definition: myexcept.cpp:391
void ReName(const char *)
Definition: myexcept.h:94
static char * what_error
Definition: myexcept.h:72
const char * entry
Definition: myexcept.h:56
static unsigned long Select
Definition: myexcept.h:376
Length_error(const char *a_what=0)
Definition: myexcept.cpp:420