Actual source code: ALE_exception.hh
1: #ifndef included_ALE_exception_hh
2: #define included_ALE_exception_hh
4: #include <string>
5: #include <sstream>
7: typedef std::basic_ostringstream<char> ostringstream;
8: typedef std::basic_ostringstream<char> ostrstr;
9: typedef std::string string;
12: namespace ALE {
13: class Exception {
14: string _msg;
15: public:
16: // explicit Exception(const char * msg) : _msg(msg){};
17: explicit Exception(const string& msg) : _msg(msg){};
18: explicit Exception(const ostringstream& txt) : _msg(txt.str()){};
19: Exception(const Exception& e) : _msg(e._msg) {};
20: const string& msg() const {return this->_msg;};
21: const char *message() const {return this->_msg.c_str();};
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