| [242] | 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 |  | 
|---|
| [773] | 10 | // Petit utilitaire pour accoler numero de ligne et nom de fichier aux messages | 
|---|
|  | 11 | // d'exception | 
|---|
|  | 12 | #define PExcLongMessage(a) BuildLongExceptionMessage(a,__FILE__,__LINE__) | 
|---|
|  | 13 |  | 
|---|
| [552] | 14 | namespace SOPHYA { | 
|---|
| [242] | 15 |  | 
|---|
| [895] | 16 | //! Utility function for appending a file name and line number to a message | 
|---|
| [773] | 17 | string BuildLongExceptionMessage(const char * s, const char *file, int line); | 
|---|
|  | 18 |  | 
|---|
| [913] | 19 | /*! \ingroup SysTools | 
|---|
|  | 20 | \brief Base exception class in Sophya | 
|---|
|  | 21 |  | 
|---|
|  | 22 | Ancestor for PError and PException | 
|---|
| [895] | 23 | It has a message, and an id to give more | 
|---|
|  | 24 | information on the exception. | 
|---|
|  | 25 | */ | 
|---|
| [256] | 26 | class PThrowable { | 
|---|
|  | 27 | public: | 
|---|
| [895] | 28 | //! Constructor with the message and error-id (optional) specification | 
|---|
| [256] | 29 | explicit PThrowable(const string& m, int ident=0) | 
|---|
|  | 30 | : msg(m), id(ident) {} | 
|---|
|  | 31 | virtual ~PThrowable() { } | 
|---|
| [895] | 32 | //! Returns the associated message string | 
|---|
| [256] | 33 | virtual string const& Msg() const  {return msg;} | 
|---|
| [895] | 34 | //! Returns the associated error-id | 
|---|
|  | 35 | virtual int Id() const {return id; } | 
|---|
| [256] | 36 | private: | 
|---|
|  | 37 | string msg; | 
|---|
|  | 38 | int    id; | 
|---|
| [242] | 39 | }; | 
|---|
| [895] | 40 |  | 
|---|
|  | 41 | //  PThrowable | 
|---|
|  | 42 | //      PError | 
|---|
|  | 43 | //      PException | 
|---|
| [256] | 44 |  | 
|---|
| [913] | 45 | /*! \ingroup SysTools | 
|---|
|  | 46 | \brief A PError is a serious logic error. Usually not caught... */ | 
|---|
| [242] | 47 | class PError : public PThrowable { | 
|---|
| [256] | 48 | public: | 
|---|
|  | 49 | explicit PError(const string& m, int id=0) : PThrowable(m,id) {} | 
|---|
| [242] | 50 | }; | 
|---|
|  | 51 |  | 
|---|
| [913] | 52 | /*! \ingroup SysTools | 
|---|
|  | 53 | \brief A PException is not as serious... Can be caught. */ | 
|---|
| [242] | 54 | class PException : public PThrowable { | 
|---|
| [256] | 55 | public: | 
|---|
|  | 56 | explicit PException(const string& m, int id=0) : PThrowable(m,id) {} | 
|---|
| [242] | 57 | }; | 
|---|
| [256] | 58 |  | 
|---|
| [895] | 59 | //  ----   Errors ---- | 
|---|
|  | 60 | //  PError | 
|---|
|  | 61 | //     AllocationError | 
|---|
|  | 62 | //     NullPtrError | 
|---|
|  | 63 | //     ForbiddenError | 
|---|
|  | 64 | //     AssertionFailedError | 
|---|
|  | 65 |  | 
|---|
| [913] | 66 | /*! \ingroup SysTools | 
|---|
|  | 67 | \brief Memory allocation failure */ | 
|---|
| [242] | 68 | class AllocationError : public PError { | 
|---|
| [256] | 69 | public: | 
|---|
|  | 70 | explicit AllocationError(const string& m, int id=0) : PError(m,id) {} | 
|---|
| [242] | 71 | }; | 
|---|
|  | 72 |  | 
|---|
| [913] | 73 | /*! \ingroup SysTools | 
|---|
|  | 74 | \brief Null pointer error */ | 
|---|
| [242] | 75 | class NullPtrError : public PError { | 
|---|
| [256] | 76 | public: | 
|---|
|  | 77 | explicit NullPtrError(const string& m, int id=0) : PError(m,id) {} | 
|---|
| [242] | 78 | }; | 
|---|
| [256] | 79 |  | 
|---|
|  | 80 |  | 
|---|
| [913] | 81 | /*! \ingroup SysTools | 
|---|
|  | 82 | \brief Calling a forbidden method, trying a forbidden operation */ | 
|---|
| [242] | 83 | class ForbiddenError : public PError { | 
|---|
| [256] | 84 | public: | 
|---|
|  | 85 | explicit ForbiddenError(const string& m, int id=0) : PError(m,id) {} | 
|---|
| [242] | 86 | }; | 
|---|
| [749] | 87 |  | 
|---|
| [256] | 88 |  | 
|---|
| [913] | 89 | /*! \ingroup SysTools | 
|---|
|  | 90 | \brief ASSERT macro failure. The message is the assertion... */ | 
|---|
| [242] | 91 | class AssertionFailedError : public PError { | 
|---|
| [256] | 92 | public: | 
|---|
|  | 93 | explicit AssertionFailedError(const string& m, int id=0) : PError(m,id) {} | 
|---|
| [242] | 94 | }; | 
|---|
| [256] | 95 |  | 
|---|
| [242] | 96 | // Standard exceptions | 
|---|
| [256] | 97 | // | 
|---|
|  | 98 | // PException | 
|---|
|  | 99 | //   IOExc | 
|---|
|  | 100 | //     FileFormatExc | 
|---|
| [895] | 101 | //   SzMismatchError | 
|---|
|  | 102 | //   RangeCheckError | 
|---|
|  | 103 | //   ParmError | 
|---|
| [256] | 104 | //   TypeMismatchExc | 
|---|
| [895] | 105 | //   MathExc | 
|---|
|  | 106 | //     SingMatxExc | 
|---|
| [256] | 107 | //   DuplicateIdExc | 
|---|
|  | 108 | //   NotFoundExc | 
|---|
| [895] | 109 | //   CaughtSignalExc | 
|---|
| [256] | 110 |  | 
|---|
| [913] | 111 | /*! \ingroup SysTools | 
|---|
|  | 112 | \brief Generic IO Exception */ | 
|---|
| [256] | 113 | class IOExc : public PException { | 
|---|
|  | 114 | public: | 
|---|
|  | 115 | explicit IOExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| [242] | 116 | }; | 
|---|
|  | 117 |  | 
|---|
| [913] | 118 | /*! \ingroup SysTools | 
|---|
|  | 119 | \brief Bad file format */ | 
|---|
| [895] | 120 | class FileFormatExc : public IOExc { | 
|---|
|  | 121 | public: | 
|---|
|  | 122 | explicit FileFormatExc(const string& m, int id=0) : IOExc(m,id) {} | 
|---|
|  | 123 | }; | 
|---|
|  | 124 |  | 
|---|
| [913] | 125 | /*! \ingroup SysTools | 
|---|
|  | 126 | \brief Size mismatch between objects */ | 
|---|
| [895] | 127 | class SzMismatchError : public PException { | 
|---|
|  | 128 | public: | 
|---|
|  | 129 | explicit SzMismatchError(const string& m, int id=0) : PException(m,id) {} | 
|---|
|  | 130 | }; | 
|---|
|  | 131 |  | 
|---|
| [913] | 132 | /*! \ingroup SysTools | 
|---|
|  | 133 | \brief Out of bounds for array, matrix, etc */ | 
|---|
| [895] | 134 | class RangeCheckError : public PException { | 
|---|
|  | 135 | public: | 
|---|
|  | 136 | explicit RangeCheckError(const string& m, int id=0) : PException(m,id) {} | 
|---|
|  | 137 | }; | 
|---|
|  | 138 |  | 
|---|
| [913] | 139 | /*! \ingroup SysTools | 
|---|
|  | 140 | \brief Invalid parameter to method/constructor... */ | 
|---|
| [895] | 141 | class ParmError : public PException { | 
|---|
|  | 142 | public: | 
|---|
|  | 143 | explicit ParmError(const string& m, int id=0) : PException(m,id) {} | 
|---|
|  | 144 | }; | 
|---|
|  | 145 |  | 
|---|
| [913] | 146 | /*! \ingroup SysTools | 
|---|
|  | 147 | \brief Calling a non available / not implemented method */ | 
|---|
| [895] | 148 | class NotAvailableOperation : public PException { | 
|---|
|  | 149 | public: | 
|---|
|  | 150 | explicit NotAvailableOperation(const string& m, int id=0) : PException(m,id) {} | 
|---|
|  | 151 | }; | 
|---|
|  | 152 |  | 
|---|
| [913] | 153 | /*! \ingroup SysTools | 
|---|
|  | 154 | \brief Bad data type -> keep ? */ | 
|---|
| [256] | 155 | class TypeMismatchExc : public PException { | 
|---|
|  | 156 | public: | 
|---|
|  | 157 | explicit TypeMismatchExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| [242] | 158 | }; | 
|---|
| [895] | 159 |  | 
|---|
| [913] | 160 | /*! \ingroup SysTools | 
|---|
|  | 161 | \brief Math operation exception */ | 
|---|
| [256] | 162 | class MathExc : public PException { | 
|---|
|  | 163 | public: | 
|---|
|  | 164 | explicit MathExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| [242] | 165 | }; | 
|---|
| [256] | 166 |  | 
|---|
| [913] | 167 | /*! \ingroup SysTools | 
|---|
|  | 168 | \brief Singular matrix  */ | 
|---|
| [895] | 169 | class SingMatrixExc : public MathExc { | 
|---|
|  | 170 | public: | 
|---|
|  | 171 | explicit SingMatrixExc(const string& m, int id=0) : MathExc(m,id) {} | 
|---|
|  | 172 | }; | 
|---|
|  | 173 |  | 
|---|
| [913] | 174 | /*! \ingroup SysTools | 
|---|
|  | 175 | \brief Duplicate identifier during registration */ | 
|---|
| [242] | 176 | class DuplicateIdExc : public PException { | 
|---|
| [256] | 177 | public: | 
|---|
|  | 178 | explicit DuplicateIdExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| [242] | 179 | }; | 
|---|
| [256] | 180 |  | 
|---|
| [913] | 181 | /*! \ingroup SysTools | 
|---|
|  | 182 | \brief Not found identifier  */ | 
|---|
| [242] | 183 | class NotFoundExc : public PException { | 
|---|
| [256] | 184 | public: | 
|---|
|  | 185 | explicit NotFoundExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| [242] | 186 | }; | 
|---|
| [256] | 187 |  | 
|---|
| [913] | 188 | /*! \ingroup SysTools | 
|---|
|  | 189 | \brief Generated exception when processing a signal */ | 
|---|
| [242] | 190 | class CaughtSignalExc : public PException { | 
|---|
| [256] | 191 | public: | 
|---|
|  | 192 | explicit CaughtSignalExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| [242] | 193 | }; | 
|---|
| [895] | 194 |  | 
|---|
|  | 195 | } // namespace SOPHYA | 
|---|
| [256] | 196 |  | 
|---|
| [242] | 197 |  | 
|---|
|  | 198 | #define ASSERT(_a_) if (!(_a_)) { \ | 
|---|
|  | 199 | cerr << "Assertion failed " #_a_ " file " __FILE__ " line " << __LINE__ \ | 
|---|
|  | 200 | << endl; \ | 
|---|
|  | 201 | throw(AssertionFailedError(#_a_)); } | 
|---|
|  | 202 |  | 
|---|
|  | 203 | #define FAILNIL(_x_) \ | 
|---|
|  | 204 | {if (!(_x_)) throw(NullPtrError(#_x_))} | 
|---|
|  | 205 |  | 
|---|
|  | 206 | void InitFailNewHandler(); | 
|---|
|  | 207 |  | 
|---|
|  | 208 |  | 
|---|
|  | 209 | // Compatibility with EROS-PEIDA exceptions | 
|---|
|  | 210 |  | 
|---|
|  | 211 | #define EXC_ABORT_NEG(_x) | 
|---|
|  | 212 | #define EXC_ABORT_ALL(_x) | 
|---|
|  | 213 |  | 
|---|
|  | 214 | #define EXC_AWARE | 
|---|
|  | 215 |  | 
|---|
|  | 216 | #define END_CONSTRUCTOR | 
|---|
|  | 217 |  | 
|---|
|  | 218 | #define TRY try | 
|---|
|  | 219 | #define CATCH(_var) catch(long _var) | 
|---|
|  | 220 | #define CATCHALL catch(...) | 
|---|
|  | 221 | #define ENDTRY | 
|---|
|  | 222 |  | 
|---|
|  | 223 | #define THROW(_i) throw( _i); | 
|---|
|  | 224 |  | 
|---|
|  | 225 | #define THROW_ THROW(0) | 
|---|
|  | 226 |  | 
|---|
|  | 227 | #define THROW_SAME throw; | 
|---|
|  | 228 |  | 
|---|
|  | 229 | #define RETURN(x) return(x) | 
|---|
|  | 230 | #define RETURN_ return | 
|---|
|  | 231 |  | 
|---|
|  | 232 | #define rangeCheckErr    RangeCheckError("") | 
|---|
|  | 233 | #define typeMismatchErr  TypeMismatchExc("") | 
|---|
|  | 234 | #define allocationErr    AllocationError("") | 
|---|
|  | 235 | #define parmErr          ParmError("") | 
|---|
|  | 236 | #define inconsistentErr  ParmError("") | 
|---|
|  | 237 | #define sizeMismatchErr  SzMismatchError("") | 
|---|
| [256] | 238 | #define fileErr          IOExc("") | 
|---|
| [242] | 239 | #define nullPtrErr       NullPtrError("") | 
|---|
|  | 240 | #define singMatxErr      SingMatrixExc("") | 
|---|
|  | 241 |  | 
|---|
|  | 242 | #define DBASSERT(_x_) ASSERT(_x_) | 
|---|
|  | 243 |  | 
|---|
|  | 244 | #endif | 
|---|