| 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 |     virtual ~PThrowable() { }
 | 
|---|
| 32 |     //! Returns the associated message string
 | 
|---|
| 33 |     virtual string const& Msg() const  {return msg;}
 | 
|---|
| 34 |     //! Returns the associated error-id
 | 
|---|
| 35 |     virtual int Id() const {return id; }
 | 
|---|
| 36 |   private:
 | 
|---|
| 37 |     string msg;
 | 
|---|
| 38 |     int    id;
 | 
|---|
| 39 |   };
 | 
|---|
| 40 | 
 | 
|---|
| 41 | //  PThrowable
 | 
|---|
| 42 | //      PError
 | 
|---|
| 43 | //      PException
 | 
|---|
| 44 |   
 | 
|---|
| 45 | /*! \ingroup BaseTools 
 | 
|---|
| 46 |     \brief A PError is a serious logic error. Usually not caught... */
 | 
|---|
| 47 |   class PError : public PThrowable {
 | 
|---|
| 48 |   public:
 | 
|---|
| 49 |     explicit PError(const string& m, int id=0) : PThrowable(m,id) {}
 | 
|---|
| 50 |   };
 | 
|---|
| 51 |   
 | 
|---|
| 52 | /*! \ingroup BaseTools 
 | 
|---|
| 53 |     \brief A PException is not as serious... Can be caught. */
 | 
|---|
| 54 |   class PException : public PThrowable {
 | 
|---|
| 55 |   public:
 | 
|---|
| 56 |     explicit PException(const string& m, int id=0) : PThrowable(m,id) {}
 | 
|---|
| 57 |   };
 | 
|---|
| 58 |   
 | 
|---|
| 59 | //  ----   Errors ----
 | 
|---|
| 60 | //  PError
 | 
|---|
| 61 | //     AllocationError
 | 
|---|
| 62 | //     NullPtrError
 | 
|---|
| 63 | //     ForbiddenError
 | 
|---|
| 64 | //     AssertionFailedError
 | 
|---|
| 65 | 
 | 
|---|
| 66 | /*! \ingroup BaseTools 
 | 
|---|
| 67 |     \brief Memory allocation failure */
 | 
|---|
| 68 |   class AllocationError : public PError {
 | 
|---|
| 69 |   public:
 | 
|---|
| 70 |     explicit AllocationError(const string& m, int id=0) : PError(m,id) {}
 | 
|---|
| 71 |   };
 | 
|---|
| 72 |   
 | 
|---|
| 73 | /*! \ingroup BaseTools 
 | 
|---|
| 74 |     \brief Null pointer error */
 | 
|---|
| 75 |   class NullPtrError : public PError {
 | 
|---|
| 76 |   public:
 | 
|---|
| 77 |     explicit NullPtrError(const string& m, int id=0) : PError(m,id) {}
 | 
|---|
| 78 |   };
 | 
|---|
| 79 |   
 | 
|---|
| 80 |   
 | 
|---|
| 81 | /*! \ingroup BaseTools 
 | 
|---|
| 82 |     \brief Calling a forbidden method, trying a forbidden operation */
 | 
|---|
| 83 |   class ForbiddenError : public PError {
 | 
|---|
| 84 |   public:
 | 
|---|
| 85 |     explicit ForbiddenError(const string& m, int id=0) : PError(m,id) {}
 | 
|---|
| 86 |   };
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   
 | 
|---|
| 89 | /*! \ingroup BaseTools 
 | 
|---|
| 90 |     \brief ASSERT macro failure. The message is the assertion... */
 | 
|---|
| 91 |   class AssertionFailedError : public PError {
 | 
|---|
| 92 |   public:
 | 
|---|
| 93 |     explicit AssertionFailedError(const string& m, int id=0) : PError(m,id) {}
 | 
|---|
| 94 |   };
 | 
|---|
| 95 |   
 | 
|---|
| 96 |   // Standard exceptions
 | 
|---|
| 97 |   //
 | 
|---|
| 98 |   // PException
 | 
|---|
| 99 |   //   IOExc
 | 
|---|
| 100 |   //     FileFormatExc
 | 
|---|
| 101 |   //   SzMismatchError
 | 
|---|
| 102 |   //   RangeCheckError
 | 
|---|
| 103 |   //   ParmError
 | 
|---|
| 104 |   //   TypeMismatchExc
 | 
|---|
| 105 |   //   MathExc
 | 
|---|
| 106 |   //     SingMatxExc
 | 
|---|
| 107 |   //   DuplicateIdExc
 | 
|---|
| 108 |   //   NotFoundExc
 | 
|---|
| 109 |   //   CaughtSignalExc
 | 
|---|
| 110 |   
 | 
|---|
| 111 | /*! \ingroup BaseTools 
 | 
|---|
| 112 |     \brief Generic IO Exception */
 | 
|---|
| 113 |   class IOExc : public PException {
 | 
|---|
| 114 |   public:
 | 
|---|
| 115 |     explicit IOExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 116 |   };
 | 
|---|
| 117 | 
 | 
|---|
| 118 | /*! \ingroup BaseTools 
 | 
|---|
| 119 |     \brief Bad file format */
 | 
|---|
| 120 |   class FileFormatExc : public IOExc {
 | 
|---|
| 121 |   public:
 | 
|---|
| 122 |     explicit FileFormatExc(const string& m, int id=0) : IOExc(m,id) {}
 | 
|---|
| 123 |   };
 | 
|---|
| 124 | 
 | 
|---|
| 125 | /*! \ingroup BaseTools 
 | 
|---|
| 126 |     \brief Size mismatch between objects */
 | 
|---|
| 127 |   class SzMismatchError : public PException {
 | 
|---|
| 128 |   public:
 | 
|---|
| 129 |     explicit SzMismatchError(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 130 |   };
 | 
|---|
| 131 |   
 | 
|---|
| 132 | /*! \ingroup BaseTools 
 | 
|---|
| 133 |     \brief Out of bounds for array, matrix, etc */
 | 
|---|
| 134 |   class RangeCheckError : public PException {
 | 
|---|
| 135 |   public:
 | 
|---|
| 136 |     explicit RangeCheckError(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 137 |   };
 | 
|---|
| 138 |   
 | 
|---|
| 139 | /*! \ingroup BaseTools 
 | 
|---|
| 140 |     \brief Invalid parameter to method/constructor... */
 | 
|---|
| 141 |   class ParmError : public PException {
 | 
|---|
| 142 |   public:
 | 
|---|
| 143 |     explicit ParmError(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 144 |   };
 | 
|---|
| 145 | 
 | 
|---|
| 146 | /*! \ingroup BaseTools 
 | 
|---|
| 147 |     \brief Calling a non available / not implemented method */
 | 
|---|
| 148 |   class NotAvailableOperation : public PException {
 | 
|---|
| 149 |   public:
 | 
|---|
| 150 |     explicit NotAvailableOperation(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 151 |   };
 | 
|---|
| 152 | 
 | 
|---|
| 153 | /*! \ingroup BaseTools 
 | 
|---|
| 154 |     \brief Bad data type -> keep ? */
 | 
|---|
| 155 |   class TypeMismatchExc : public PException {
 | 
|---|
| 156 |   public:
 | 
|---|
| 157 |     explicit TypeMismatchExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 158 |   };
 | 
|---|
| 159 | 
 | 
|---|
| 160 | /*! \ingroup BaseTools 
 | 
|---|
| 161 |     \brief Math operation exception */
 | 
|---|
| 162 |   class MathExc : public PException {
 | 
|---|
| 163 |   public:
 | 
|---|
| 164 |     explicit MathExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 165 |   };
 | 
|---|
| 166 |   
 | 
|---|
| 167 | /*! \ingroup BaseTools 
 | 
|---|
| 168 |     \brief Singular matrix  */
 | 
|---|
| 169 |   class SingMatrixExc : public MathExc {
 | 
|---|
| 170 |   public:
 | 
|---|
| 171 |     explicit SingMatrixExc(const string& m, int id=0) : MathExc(m,id) {}
 | 
|---|
| 172 |   };
 | 
|---|
| 173 | 
 | 
|---|
| 174 | /*! \ingroup BaseTools 
 | 
|---|
| 175 |     \brief Duplicate identifier during registration */
 | 
|---|
| 176 |   class DuplicateIdExc : public PException {
 | 
|---|
| 177 |   public:
 | 
|---|
| 178 |     explicit DuplicateIdExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 179 |   };
 | 
|---|
| 180 |   
 | 
|---|
| 181 | /*! \ingroup BaseTools 
 | 
|---|
| 182 |     \brief Not found identifier  */
 | 
|---|
| 183 |   class NotFoundExc : public PException {
 | 
|---|
| 184 |   public:
 | 
|---|
| 185 |     explicit NotFoundExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 186 |   };
 | 
|---|
| 187 |   
 | 
|---|
| 188 | /*! \ingroup BaseTools 
 | 
|---|
| 189 |     \brief Generated exception when processing a signal */
 | 
|---|
| 190 |   class CaughtSignalExc : public PException {
 | 
|---|
| 191 |   public:
 | 
|---|
| 192 |     explicit CaughtSignalExc(const string& m, int id=0) : PException(m,id) {}
 | 
|---|
| 193 |   };
 | 
|---|
| 194 |     
 | 
|---|
| 195 | } // namespace SOPHYA
 | 
|---|
| 196 | 
 | 
|---|
| 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 | 
 | 
|---|
| 215 | #define END_CONSTRUCTOR
 | 
|---|
| 216 | 
 | 
|---|
| 217 | #define TRY try
 | 
|---|
| 218 | #define CATCH(_var) catch(long _var)
 | 
|---|
| 219 | #define CATCHALL catch(...)
 | 
|---|
| 220 | #define ENDTRY
 | 
|---|
| 221 | 
 | 
|---|
| 222 | #define THROW(_i) throw( _i);
 | 
|---|
| 223 | 
 | 
|---|
| 224 | #define THROW_ THROW(0)
 | 
|---|
| 225 | 
 | 
|---|
| 226 | #define THROW_SAME throw;
 | 
|---|
| 227 | 
 | 
|---|
| 228 | #define RETURN(x) return(x)
 | 
|---|
| 229 | #define RETURN_ return
 | 
|---|
| 230 | 
 | 
|---|
| 231 | #define rangeCheckErr    RangeCheckError("")
 | 
|---|
| 232 | #define typeMismatchErr  TypeMismatchExc("")
 | 
|---|
| 233 | #define allocationErr    AllocationError("")
 | 
|---|
| 234 | #define parmErr          ParmError("")
 | 
|---|
| 235 | #define inconsistentErr  ParmError("")
 | 
|---|
| 236 | #define sizeMismatchErr  SzMismatchError("")
 | 
|---|
| 237 | #define fileErr          IOExc("")
 | 
|---|
| 238 | #define nullPtrErr       NullPtrError("")
 | 
|---|
| 239 | #define singMatxErr      SingMatrixExc("")
 | 
|---|
| 240 | 
 | 
|---|
| 241 | #define DBASSERT(_x_) ASSERT(_x_)
 | 
|---|
| 242 | 
 | 
|---|
| 243 | #endif
 | 
|---|