| 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
| 2 | #ifndef PEXCEPTIONS_SEEN | 
|---|
| 3 | #define PEXCEPTIONS_SEEN | 
|---|
| 4 |  | 
|---|
| 5 | #include "config.h" | 
|---|
| 6 | #include <string> | 
|---|
| 7 |  | 
|---|
| 8 | using namespace std; | 
|---|
| 9 |  | 
|---|
| 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 |  | 
|---|
| 14 |  | 
|---|
| 15 | //! Utility function for appending a file name and line number to a message | 
|---|
| 16 | string BuildLongExceptionMessage(const char * s, const char *file, int line); | 
|---|
| 17 |  | 
|---|
| 18 | /*! \ingroup SysTools | 
|---|
| 19 | \brief Base exception class in Sophya | 
|---|
| 20 |  | 
|---|
| 21 | Ancestor for PError and PException | 
|---|
| 22 | It has a message, and an id to give more | 
|---|
| 23 | information on the exception. | 
|---|
| 24 | */ | 
|---|
| 25 | class PThrowable { | 
|---|
| 26 | public: | 
|---|
| 27 | //! Constructor with the message and error-id (optional) specification | 
|---|
| 28 | explicit PThrowable(const string& m, int ident=0) | 
|---|
| 29 | : msg(m), id(ident) {} | 
|---|
| 30 | virtual ~PThrowable() { } | 
|---|
| 31 | //! Returns the associated message string | 
|---|
| 32 | virtual string const& Msg() const  {return msg;} | 
|---|
| 33 | //! Returns the associated error-id | 
|---|
| 34 | virtual int Id() const {return id; } | 
|---|
| 35 | private: | 
|---|
| 36 | string msg; | 
|---|
| 37 | int    id; | 
|---|
| 38 | }; | 
|---|
| 39 |  | 
|---|
| 40 | //  PThrowable | 
|---|
| 41 | //      PError | 
|---|
| 42 | //      PException | 
|---|
| 43 |  | 
|---|
| 44 | /*! \ingroup SysTools | 
|---|
| 45 | \brief A PError is a serious logic error. Usually not caught... */ | 
|---|
| 46 | class PError : public PThrowable { | 
|---|
| 47 | public: | 
|---|
| 48 | explicit PError(const string& m, int id=0) : PThrowable(m,id) {} | 
|---|
| 49 | }; | 
|---|
| 50 |  | 
|---|
| 51 | /*! \ingroup SysTools | 
|---|
| 52 | \brief A PException is not as serious... Can be caught. */ | 
|---|
| 53 | class PException : public PThrowable { | 
|---|
| 54 | public: | 
|---|
| 55 | explicit PException(const string& m, int id=0) : PThrowable(m,id) {} | 
|---|
| 56 | }; | 
|---|
| 57 |  | 
|---|
| 58 | //  ----   Errors ---- | 
|---|
| 59 | //  PError | 
|---|
| 60 | //     AllocationError | 
|---|
| 61 | //     NullPtrError | 
|---|
| 62 | //     ForbiddenError | 
|---|
| 63 | //     AssertionFailedError | 
|---|
| 64 |  | 
|---|
| 65 | /*! \ingroup SysTools | 
|---|
| 66 | \brief Memory allocation failure */ | 
|---|
| 67 | class AllocationError : public PError { | 
|---|
| 68 | public: | 
|---|
| 69 | explicit AllocationError(const string& m, int id=0) : PError(m,id) {} | 
|---|
| 70 | }; | 
|---|
| 71 |  | 
|---|
| 72 | /*! \ingroup SysTools | 
|---|
| 73 | \brief Null pointer error */ | 
|---|
| 74 | class NullPtrError : public PError { | 
|---|
| 75 | public: | 
|---|
| 76 | explicit NullPtrError(const string& m, int id=0) : PError(m,id) {} | 
|---|
| 77 | }; | 
|---|
| 78 |  | 
|---|
| 79 |  | 
|---|
| 80 | /*! \ingroup SysTools | 
|---|
| 81 | \brief Calling a forbidden method, trying a forbidden operation */ | 
|---|
| 82 | class ForbiddenError : public PError { | 
|---|
| 83 | public: | 
|---|
| 84 | explicit ForbiddenError(const string& m, int id=0) : PError(m,id) {} | 
|---|
| 85 | }; | 
|---|
| 86 |  | 
|---|
| 87 |  | 
|---|
| 88 | /*! \ingroup SysTools | 
|---|
| 89 | \brief ASSERT macro failure. The message is the assertion... */ | 
|---|
| 90 | class AssertionFailedError : public PError { | 
|---|
| 91 | public: | 
|---|
| 92 | explicit AssertionFailedError(const string& m, int id=0) : PError(m,id) {} | 
|---|
| 93 | }; | 
|---|
| 94 |  | 
|---|
| 95 | // Standard exceptions | 
|---|
| 96 | // | 
|---|
| 97 | // PException | 
|---|
| 98 | //   IOExc | 
|---|
| 99 | //     FileFormatExc | 
|---|
| 100 | //   SzMismatchError | 
|---|
| 101 | //   RangeCheckError | 
|---|
| 102 | //   ParmError | 
|---|
| 103 | //   TypeMismatchExc | 
|---|
| 104 | //   MathExc | 
|---|
| 105 | //     SingMatxExc | 
|---|
| 106 | //   DuplicateIdExc | 
|---|
| 107 | //   NotFoundExc | 
|---|
| 108 | //   CaughtSignalExc | 
|---|
| 109 |  | 
|---|
| 110 | /*! \ingroup SysTools | 
|---|
| 111 | \brief Generic IO Exception */ | 
|---|
| 112 | class IOExc : public PException { | 
|---|
| 113 | public: | 
|---|
| 114 | explicit IOExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| 115 | }; | 
|---|
| 116 |  | 
|---|
| 117 | /*! \ingroup SysTools | 
|---|
| 118 | \brief Bad file format */ | 
|---|
| 119 | class FileFormatExc : public IOExc { | 
|---|
| 120 | public: | 
|---|
| 121 | explicit FileFormatExc(const string& m, int id=0) : IOExc(m,id) {} | 
|---|
| 122 | }; | 
|---|
| 123 |  | 
|---|
| 124 | /*! \ingroup SysTools | 
|---|
| 125 | \brief Size mismatch between objects */ | 
|---|
| 126 | class SzMismatchError : public PException { | 
|---|
| 127 | public: | 
|---|
| 128 | explicit SzMismatchError(const string& m, int id=0) : PException(m,id) {} | 
|---|
| 129 | }; | 
|---|
| 130 |  | 
|---|
| 131 | /*! \ingroup SysTools | 
|---|
| 132 | \brief Out of bounds for array, matrix, etc */ | 
|---|
| 133 | class RangeCheckError : public PException { | 
|---|
| 134 | public: | 
|---|
| 135 | explicit RangeCheckError(const string& m, int id=0) : PException(m,id) {} | 
|---|
| 136 | }; | 
|---|
| 137 |  | 
|---|
| 138 | /*! \ingroup SysTools | 
|---|
| 139 | \brief Invalid parameter to method/constructor... */ | 
|---|
| 140 | class ParmError : public PException { | 
|---|
| 141 | public: | 
|---|
| 142 | explicit ParmError(const string& m, int id=0) : PException(m,id) {} | 
|---|
| 143 | }; | 
|---|
| 144 |  | 
|---|
| 145 | /*! \ingroup SysTools | 
|---|
| 146 | \brief Calling a non available / not implemented method */ | 
|---|
| 147 | class NotAvailableOperation : public PException { | 
|---|
| 148 | public: | 
|---|
| 149 | explicit NotAvailableOperation(const string& m, int id=0) : PException(m,id) {} | 
|---|
| 150 | }; | 
|---|
| 151 |  | 
|---|
| 152 | /*! \ingroup SysTools | 
|---|
| 153 | \brief Bad data type -> keep ? */ | 
|---|
| 154 | class TypeMismatchExc : public PException { | 
|---|
| 155 | public: | 
|---|
| 156 | explicit TypeMismatchExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| 157 | }; | 
|---|
| 158 |  | 
|---|
| 159 | /*! \ingroup SysTools | 
|---|
| 160 | \brief Math operation exception */ | 
|---|
| 161 | class MathExc : public PException { | 
|---|
| 162 | public: | 
|---|
| 163 | explicit MathExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| 164 | }; | 
|---|
| 165 |  | 
|---|
| 166 | /*! \ingroup SysTools | 
|---|
| 167 | \brief Singular matrix  */ | 
|---|
| 168 | class SingMatrixExc : public MathExc { | 
|---|
| 169 | public: | 
|---|
| 170 | explicit SingMatrixExc(const string& m, int id=0) : MathExc(m,id) {} | 
|---|
| 171 | }; | 
|---|
| 172 |  | 
|---|
| 173 | /*! \ingroup SysTools | 
|---|
| 174 | \brief Duplicate identifier during registration */ | 
|---|
| 175 | class DuplicateIdExc : public PException { | 
|---|
| 176 | public: | 
|---|
| 177 | explicit DuplicateIdExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| 178 | }; | 
|---|
| 179 |  | 
|---|
| 180 | /*! \ingroup SysTools | 
|---|
| 181 | \brief Not found identifier  */ | 
|---|
| 182 | class NotFoundExc : public PException { | 
|---|
| 183 | public: | 
|---|
| 184 | explicit NotFoundExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| 185 | }; | 
|---|
| 186 |  | 
|---|
| 187 | /*! \ingroup SysTools | 
|---|
| 188 | \brief Generated exception when processing a signal */ | 
|---|
| 189 | class CaughtSignalExc : public PException { | 
|---|
| 190 | public: | 
|---|
| 191 | explicit CaughtSignalExc(const string& m, int id=0) : PException(m,id) {} | 
|---|
| 192 | }; | 
|---|
| 193 |  | 
|---|
| 194 |  | 
|---|
| 195 |  | 
|---|
| 196 | #define ASSERT(_a_) if (!(_a_)) { \ | 
|---|
| 197 | cerr << "Assertion failed " #_a_ " file " __FILE__ " line " << __LINE__ \ | 
|---|
| 198 | << endl; \ | 
|---|
| 199 | throw(AssertionFailedError(#_a_)); } | 
|---|
| 200 |  | 
|---|
| 201 | #define FAILNIL(_x_) \ | 
|---|
| 202 | {if (!(_x_)) throw(NullPtrError(#_x_))} | 
|---|
| 203 |  | 
|---|
| 204 |  | 
|---|
| 205 | #endif | 
|---|