| 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 <exception>
|
|---|
| 7 | #include <string>
|
|---|
| 8 |
|
|---|
| 9 | using namespace std;
|
|---|
| 10 |
|
|---|
| 11 | // Petit utilitaire pour accoler numero de ligne et nom de fichier aux messages
|
|---|
| 12 | // d'exception
|
|---|
| 13 | //! Utiliy macro to append file name and line number to a const char* string
|
|---|
| 14 | #define PExcLongMessage(a) BuildLongExceptionMessage(a,__FILE__,__LINE__)
|
|---|
| 15 |
|
|---|
| 16 | #define SEXC_MAXMSGLEN 160
|
|---|
| 17 | namespace SOPHYA {
|
|---|
| 18 |
|
|---|
| 19 | //! Utility function for appending a file name and line number to a message
|
|---|
| 20 | string BuildLongExceptionMessage(const char * s, const char *file, int line);
|
|---|
| 21 |
|
|---|
| 22 | class PThrowable : public std::exception {
|
|---|
| 23 | public:
|
|---|
| 24 | //! Constructor with the message and error-id (optional) specification
|
|---|
| 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();
|
|---|
| 30 | //! Returns the associated message string
|
|---|
| 31 | virtual string const Msg() const;
|
|---|
| 32 | //! Returns the associated error-id
|
|---|
| 33 | virtual int Id() const;
|
|---|
| 34 | private:
|
|---|
| 35 | char msg_[SEXC_MAXMSGLEN];
|
|---|
| 36 | int id_;
|
|---|
| 37 | };
|
|---|
| 38 |
|
|---|
| 39 | // PThrowable
|
|---|
| 40 | // PError
|
|---|
| 41 | // PException
|
|---|
| 42 |
|
|---|
| 43 | /*! \ingroup BaseTools
|
|---|
| 44 | \brief A PError is a serious logic error. Usually not caught... */
|
|---|
| 45 | class PError : public PThrowable {
|
|---|
| 46 | public:
|
|---|
| 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) {}
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | /*! \ingroup BaseTools
|
|---|
| 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) throw() : PThrowable(m,id) {}
|
|---|
| 56 | explicit PException(const char* m, int id=0) throw() : 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) throw() : PError(m,id) {}
|
|---|
| 71 | explicit AllocationError(const char* m, int id=0) throw() : PError(m,id) {}
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | /*! \ingroup BaseTools
|
|---|
| 75 | \brief Null pointer error */
|
|---|
| 76 | class NullPtrError : public PError {
|
|---|
| 77 | public:
|
|---|
| 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) {}
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | /*! \ingroup BaseTools
|
|---|
| 84 | \brief Calling a forbidden method, trying a forbidden operation */
|
|---|
| 85 | class ForbiddenError : public PError {
|
|---|
| 86 | public:
|
|---|
| 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) {}
|
|---|
| 89 | };
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 | /*! \ingroup BaseTools
|
|---|
| 93 | \brief ASSERT macro failure. The message is the assertion... */
|
|---|
| 94 | class AssertionFailedError : public PError {
|
|---|
| 95 | public:
|
|---|
| 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) {}
|
|---|
| 98 | };
|
|---|
| 99 |
|
|---|
| 100 | // Standard exceptions
|
|---|
| 101 | //
|
|---|
| 102 | // PException
|
|---|
| 103 | // IOExc
|
|---|
| 104 | // FileFormatExc
|
|---|
| 105 | // SzMismatchError
|
|---|
| 106 | // RangeCheckError
|
|---|
| 107 | // ParmError
|
|---|
| 108 | // TypeMismatchExc
|
|---|
| 109 | // MathExc
|
|---|
| 110 | // SingMatxExc
|
|---|
| 111 | // DuplicateIdExc
|
|---|
| 112 | // NotFoundExc
|
|---|
| 113 | // CaughtSignalExc
|
|---|
| 114 |
|
|---|
| 115 | /*! \ingroup BaseTools
|
|---|
| 116 | \brief Generic IO Exception */
|
|---|
| 117 | class IOExc : public PException {
|
|---|
| 118 | public:
|
|---|
| 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) {}
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 | /*! \ingroup BaseTools
|
|---|
| 124 | \brief Bad file format */
|
|---|
| 125 | class FileFormatExc : public IOExc {
|
|---|
| 126 | public:
|
|---|
| 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) {}
|
|---|
| 129 | };
|
|---|
| 130 |
|
|---|
| 131 | /*! \ingroup BaseTools
|
|---|
| 132 | \brief Size mismatch between objects */
|
|---|
| 133 | class SzMismatchError : public PException {
|
|---|
| 134 | public:
|
|---|
| 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) {}
|
|---|
| 137 | };
|
|---|
| 138 |
|
|---|
| 139 | /*! \ingroup BaseTools
|
|---|
| 140 | \brief Out of bounds for array, matrix, etc */
|
|---|
| 141 | class RangeCheckError : public PException {
|
|---|
| 142 | public:
|
|---|
| 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) {}
|
|---|
| 145 | };
|
|---|
| 146 |
|
|---|
| 147 | /*! \ingroup BaseTools
|
|---|
| 148 | \brief Invalid parameter to method/constructor... */
|
|---|
| 149 | class ParmError : public PException {
|
|---|
| 150 | public:
|
|---|
| 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) {}
|
|---|
| 153 | };
|
|---|
| 154 |
|
|---|
| 155 | /*! \ingroup BaseTools
|
|---|
| 156 | \brief Calling a non available / not implemented method */
|
|---|
| 157 | class NotAvailableOperation : public PException {
|
|---|
| 158 | public:
|
|---|
| 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) {}
|
|---|
| 161 | };
|
|---|
| 162 |
|
|---|
| 163 | /*! \ingroup BaseTools
|
|---|
| 164 | \brief Bad data type -> keep ? */
|
|---|
| 165 | class TypeMismatchExc : public PException {
|
|---|
| 166 | public:
|
|---|
| 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) {}
|
|---|
| 169 | };
|
|---|
| 170 |
|
|---|
| 171 | /*! \ingroup BaseTools
|
|---|
| 172 | \brief Math operation exception */
|
|---|
| 173 | class MathExc : public PException {
|
|---|
| 174 | public:
|
|---|
| 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) {}
|
|---|
| 177 | };
|
|---|
| 178 |
|
|---|
| 179 | /*! \ingroup BaseTools
|
|---|
| 180 | \brief Singular matrix */
|
|---|
| 181 | class SingMatrixExc : public MathExc {
|
|---|
| 182 | public:
|
|---|
| 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) {}
|
|---|
| 185 | };
|
|---|
| 186 |
|
|---|
| 187 | /*! \ingroup BaseTools
|
|---|
| 188 | \brief Duplicate identifier during registration */
|
|---|
| 189 | class DuplicateIdExc : public PException {
|
|---|
| 190 | public:
|
|---|
| 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) {}
|
|---|
| 193 | };
|
|---|
| 194 |
|
|---|
| 195 | /*! \ingroup BaseTools
|
|---|
| 196 | \brief Not found identifier */
|
|---|
| 197 | class NotFoundExc : public PException {
|
|---|
| 198 | public:
|
|---|
| 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) {}
|
|---|
| 201 | };
|
|---|
| 202 |
|
|---|
| 203 | /*! \ingroup BaseTools
|
|---|
| 204 | \brief Generated exception when processing a signal */
|
|---|
| 205 | class CaughtSignalExc : public PException {
|
|---|
| 206 | public:
|
|---|
| 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) {}
|
|---|
| 209 | };
|
|---|
| 210 |
|
|---|
| 211 | } // namespace SOPHYA
|
|---|
| 212 |
|
|---|
| 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 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 | #endif
|
|---|