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