| 1 | // This may look like C code, but it is really -*- C++ -*-
 | 
|---|
| 2 | #ifndef PEXCEPTIONS_SEEN
 | 
|---|
| 3 | #define PEXCEPTIONS_SEEN
 | 
|---|
| 4 | 
 | 
|---|
| 5 | #include "machdefs.h"
 | 
|---|
| 6 | #include <string>
 | 
|---|
| 7 | 
 | 
|---|
| 8 | using namespace std;
 | 
|---|
| 9 | 
 | 
|---|
| 10 | namespace SOPHYA {
 | 
|---|
| 11 | 
 | 
|---|
| 12 |   // Ancestor for PError and PException
 | 
|---|
| 13 |   // It has a message, and an id to give more
 | 
|---|
| 14 |   // information on the exception.
 | 
|---|
| 15 |   class PThrowable {
 | 
|---|
| 16 |   public:
 | 
|---|
| 17 |     explicit PThrowable(const string& m, int ident=0) 
 | 
|---|
| 18 |       : msg(m), id(ident) {}
 | 
|---|
| 19 |     virtual ~PThrowable() { }
 | 
|---|
| 20 |     virtual string const& Msg() const  {return msg;}
 | 
|---|
| 21 |   private:
 | 
|---|
| 22 |     string msg;
 | 
|---|
| 23 |     int    id;
 | 
|---|
| 24 |   };
 | 
|---|
| 25 |   
 | 
|---|
| 26 |   // A PError is a serious logic error. Usually not caught...
 | 
|---|
| 27 |   class PError : public PThrowable {
 | 
|---|
| 28 |   public:
 | 
|---|
| 29 |     explicit PError(const string& m, int id=0) : PThrowable(m,id) {}
 | 
|---|
| 30 |   };
 | 
|---|
| 31 |   
 | 
|---|
| 32 |   // A PException is not as serious... Can be caught.
 | 
|---|
| 33 |   class PException : public PThrowable {
 | 
|---|
| 34 |   public:
 | 
|---|
| 35 |     explicit PException(const string& m, int id=0) : PThrowable(m,id) {}
 | 
|---|
| 36 |   };
 | 
|---|
| 37 |   
 | 
|---|
| 38 |   // Errors
 | 
|---|
| 39 |   // Memory allocation failure
 | 
|---|
| 40 |   class AllocationError : public PError {
 | 
|---|
| 41 |   public:
 | 
|---|
| 42 |     explicit AllocationError(const string& m, int id=0) : PError(m,id) {}
 | 
|---|
| 43 |   };
 | 
|---|
| 44 |   
 | 
|---|
| 45 |   // Null pointer error
 | 
|---|
| 46 |   class NullPtrError : public PError {
 | 
|---|
| 47 |   public:
 | 
|---|
| 48 |     explicit NullPtrError(const string& m, int id=0) : PError(m,id) {}
 | 
|---|
| 49 |   };
 | 
|---|
| 50 |   
 | 
|---|
| 51 |   // Size mismatch between objects
 | 
|---|
| 52 |   class SzMismatchError : public PError {
 | 
|---|
| 53 |   public:
 | 
|---|
| 54 |     explicit SzMismatchError(const string& m, int id=0) : PError(m,id) {}
 | 
|---|
| 55 |   };
 | 
|---|
| 56 |   
 | 
|---|
| 57 |   // Out of bounds for array, matrix, etc.
 | 
|---|
| 58 |   class RangeCheckError : public PError {
 | 
|---|
| 59 |   public:
 | 
|---|
| 60 |     explicit RangeCheckError(const string& m, int id=0) : PError(m,id) {}
 | 
|---|
| 61 |   };
 | 
|---|
| 62 |   
 | 
|---|
| 63 |   // Invalid parameter to method/constructor...
 | 
|---|
| 64 |   class ParmError : public PError {
 | 
|---|
| 65 |   public:
 | 
|---|
| 66 |     explicit ParmError(const string& m, int id=0) : PError(m,id) {}
 | 
|---|
| 67 |   };
 | 
|---|
| 68 |   
 | 
|---|
| 69 |   // Calling a forbidden method, trying a forbidden operation
 | 
|---|
| 70 |   class ForbiddenError : public PError {
 | 
|---|
| 71 |   public:
 | 
|---|
| 72 |     explicit ForbiddenError(const string& m, int id=0) : PError(m,id) {}
 | 
|---|
| 73 |   };
 | 
|---|
| 74 |   
 | 
|---|
| 75 |   // ASSERT macro failure. The message is the assertion...
 | 
|---|
| 76 |   class AssertionFailedError : public PError {
 | 
|---|
| 77 |   public:
 | 
|---|
| 78 |     explicit AssertionFailedError(const string& m, int id=0) : PError(m,id) {}
 | 
|---|
| 79 |   };
 | 
|---|
| 80 |   
 | 
|---|
| 81 |   // Standard exceptions
 | 
|---|
| 82 |   //
 | 
|---|
| 83 |   // PException
 | 
|---|
| 84 |   //   IOExc
 | 
|---|
| 85 |   //     FileFormatExc
 | 
|---|
| 86 |   //   TypeMismatchExc
 | 
|---|
| 87 |   //   DuplicateIdExc
 | 
|---|
| 88 |   //   NotFoundExc
 | 
|---|
| 89 |   //   MathExc
 | 
|---|
| 90 |   //     SingMatxExc
 | 
|---|
| 91 |   
 | 
|---|
| 92 |   // generic IO Exception
 | 
|---|
| 93 |   class IOExc : public PException {
 | 
|---|
| 94 |   public:
 | 
|---|
| 95 |     explicit IOExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 96 |   };
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   // Bad type -> keep ?
 | 
|---|
| 99 |   class TypeMismatchExc : public PException {
 | 
|---|
| 100 |   public:
 | 
|---|
| 101 |     explicit TypeMismatchExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 102 |   };
 | 
|---|
| 103 |   
 | 
|---|
| 104 |   class MathExc : public PException {
 | 
|---|
| 105 |   public:
 | 
|---|
| 106 |     explicit MathExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 107 |   };
 | 
|---|
| 108 |   
 | 
|---|
| 109 |   class DuplicateIdExc : public PException {
 | 
|---|
| 110 |   public:
 | 
|---|
| 111 |     explicit DuplicateIdExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 112 |   };
 | 
|---|
| 113 |   
 | 
|---|
| 114 |   class NotFoundExc : public PException {
 | 
|---|
| 115 |   public:
 | 
|---|
| 116 |     explicit NotFoundExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 117 |   };
 | 
|---|
| 118 |   
 | 
|---|
| 119 |   class CaughtSignalExc : public PException {
 | 
|---|
| 120 |   public:
 | 
|---|
| 121 |     explicit CaughtSignalExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 122 |   };
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   // Bad file format
 | 
|---|
| 125 |   class FileFormatExc : public IOExc {
 | 
|---|
| 126 |   public:
 | 
|---|
| 127 |     explicit FileFormatExc(const string& m, int id=0) : IOExc(m,id) {}
 | 
|---|
| 128 |   };
 | 
|---|
| 129 |   
 | 
|---|
| 130 |   class SingMatrixExc : public MathExc {
 | 
|---|
| 131 |   public:
 | 
|---|
| 132 |     explicit SingMatrixExc(const string& m, int id=0) : MathExc(m,id) {}
 | 
|---|
| 133 |   };
 | 
|---|
| 134 |   
 | 
|---|
| 135 |   
 | 
|---|
| 136 | }
 | 
|---|
| 137 | 
 | 
|---|
| 138 | 
 | 
|---|
| 139 | #define ASSERT(_a_) if (!(_a_)) { \
 | 
|---|
| 140 |      cerr << "Assertion failed " #_a_ " file " __FILE__ " line " << __LINE__ \
 | 
|---|
| 141 |           << endl; \
 | 
|---|
| 142 |                      throw(AssertionFailedError(#_a_)); }
 | 
|---|
| 143 |    
 | 
|---|
| 144 | #define FAILNIL(_x_) \
 | 
|---|
| 145 |        {if (!(_x_)) throw(NullPtrError(#_x_))}
 | 
|---|
| 146 | 
 | 
|---|
| 147 | void InitFailNewHandler();
 | 
|---|
| 148 |   
 | 
|---|
| 149 |      
 | 
|---|
| 150 | // Compatibility with EROS-PEIDA exceptions
 | 
|---|
| 151 | 
 | 
|---|
| 152 | #define EXC_ABORT_NEG(_x)
 | 
|---|
| 153 | #define EXC_ABORT_ALL(_x)
 | 
|---|
| 154 | 
 | 
|---|
| 155 | #define EXC_AWARE
 | 
|---|
| 156 | 
 | 
|---|
| 157 | #define END_CONSTRUCTOR
 | 
|---|
| 158 | 
 | 
|---|
| 159 | #define TRY try
 | 
|---|
| 160 | #define CATCH(_var) catch(long _var)
 | 
|---|
| 161 | #define CATCHALL catch(...)
 | 
|---|
| 162 | #define ENDTRY
 | 
|---|
| 163 | 
 | 
|---|
| 164 | #define THROW(_i) throw( _i);
 | 
|---|
| 165 | 
 | 
|---|
| 166 | #define THROW_ THROW(0)
 | 
|---|
| 167 | 
 | 
|---|
| 168 | #define THROW_SAME throw;
 | 
|---|
| 169 | 
 | 
|---|
| 170 | #define RETURN(x) return(x)
 | 
|---|
| 171 | #define RETURN_ return
 | 
|---|
| 172 | 
 | 
|---|
| 173 | #define rangeCheckErr    RangeCheckError("")
 | 
|---|
| 174 | #define typeMismatchErr  TypeMismatchExc("")
 | 
|---|
| 175 | #define allocationErr    AllocationError("")
 | 
|---|
| 176 | #define parmErr          ParmError("")
 | 
|---|
| 177 | #define inconsistentErr  ParmError("")
 | 
|---|
| 178 | #define sizeMismatchErr  SzMismatchError("")
 | 
|---|
| 179 | #define fileErr          IOExc("")
 | 
|---|
| 180 | #define nullPtrErr       NullPtrError("")
 | 
|---|
| 181 | #define singMatxErr      SingMatrixExc("")
 | 
|---|
| 182 | 
 | 
|---|
| 183 | #define DBASSERT(_x_) ASSERT(_x_)
 | 
|---|
| 184 | 
 | 
|---|
| 185 | #endif
 | 
|---|