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