source: ntrip/trunk/BNC/newmat/myexcept.h@ 10878

Last change on this file since 10878 was 10791, checked in by mervart, 5 months ago

BNC Multifrequency and PPPAR Client (initial version)

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