Actual source code: ALE_exception.hh

petsc-3.4.2 2013-07-02
  1: #ifndef included_ALE_exception_hh
  2: #define included_ALE_exception_hh

  4: #include <stdexcept>
  5: #include <string>
  6: #include <sstream>

  8: typedef std::basic_ostringstream<char> ostringstream;
  9: typedef std::basic_ostringstream<char> ostrstr;
 10: typedef std::string                    string;


 13: namespace ALE {
 14:   class Exception : public std::runtime_error {
 15:   public:
 16:     explicit Exception(const char         * msg) : std::runtime_error(msg){};
 17:     explicit Exception(const string&        msg) : std::runtime_error(msg){};
 18:     explicit Exception(const ostringstream& txt) : std::runtime_error(txt.str()){};
 19:     Exception(const Exception& e)      : std::runtime_error(e.what()) {};
 20:     string msg()     const  {return std::string(this->what());};
 21:     const char   *message() const  {return this->what();};
 22:     // Printing
 23:     template <typename Stream_>
 24:     friend Stream_& operator<<(Stream_& os, const Exception& e) {
 25:       os << "ERROR: " << e.message() << std::endl;
 26:       return os;
 27:     }
 28:   };

 30:   class XException {
 31:     ostrstr _txt;
 32:   public:
 33:     XException(){};
 34:     explicit
 35:     XException(const string& msg)   {this->_txt << msg;};
 36:     explicit
 37:     XException(const ostrstr& txt)  {this->_txt << txt.str();};
 38:     XException(const XException& e) {this->_txt << e._txt.str();};
 39:     //
 40:     const string msg()     const {return this->_txt.str();};
 41:     const char   *message() const {return this->_txt.str().c_str();};
 42:     // Message input
 43:     template<typename Input_>
 44:     XException& operator<<(const Input_& in) {
 45:       this->_txt << in;
 46:       return *this;
 47:     }
 48:     // Printing
 49:     template <typename Stream_>
 50:     friend Stream_& operator<<(Stream_& os, const XException& e) {
 51:       os << "ERROR: " << e.message() << std::endl;
 52:       return os;
 53:     }
 54:   };// class XException


 57:   // A helper function that throws an ALE::Exception with a message identifying the function that returned the given error code,
 58:   // including the function and the line where the error occured.
 59:   void ERROR(PetscErrorCode ierr, const char *func, int line, const char *msg);
 60:   // A helper function that allocates and assembles an error message from a format string
 61:   const char *ERRORMSG(const char *fmt, ...);
 62:   // A helper function for converting MPI errors to exception
 63:   void MPIERROR(PetscErrorCode ierr, const char *func, int line, const char *msg);
 64: }// namespace ALE

 66: // A helper macro that passes __FUNCT__ and __LINE__ with the error msg to the ERROR routine
 67: #define CHKERROR(ierr, msg) \
 68:   ::ALE::ERROR(ierr, __FUNCT__,  __LINE__, msg);

 70: // A helper macro that passes __FUNCT__ and __LINE__ with the error msg to the MPIERROR routine
 71: #define CHKMPIERROR(ierr, msg) \
 72:   ::ALE::MPIERROR(ierr, __FUNCT__,  __LINE__, msg);

 74: #endif