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