Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/newmat/myexcept.h @ 4614

Last change on this file since 4614 was 4565, checked in by patrick, 19 years ago

orxonox/trunk: added the newmat library to the project. needs some translation in directory, temp under util/newmat. is needed by the collision detection engine to perform lin alg operations such as eigenvector decomposition. perhaps we will make our own library to do that later.

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