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