MeshKit
1.0
|
00001 #ifndef MESHKIT_ERROR_HPP 00002 #define MESHKIT_ERROR_HPP 00003 00006 #include <string> 00007 #include <typeinfo> 00008 00009 #include "meshkit/iGeom.hpp" 00010 #include "meshkit/iMesh.hpp" 00011 #include "meshkit/iRel.hpp" 00012 00013 namespace MeshKit { 00014 00015 #define ECERRCHK(err, descr) \ 00016 do { \ 00017 if (MK_SUCCESS != err) { \ 00018 Error tmp_err(0, "%s, line %d: %s", __FILE__, __LINE__, descr); \ 00019 throw tmp_err; \ 00020 } \ 00021 } while(false) 00022 00023 00024 #define MKERRCHK(err, descr) \ 00025 do { \ 00026 if (MK_SUCCESS != err.error_code()) { \ 00027 Error tmp_err(0, "%s, line %d: %s: %s", __FILE__, __LINE__, err.what(), descr); \ 00028 err.set_string(tmp_err.what()); throw err; \ 00029 } \ 00030 } while(false) 00031 00032 00033 #define MBERRCHK(err, mbimpl) \ 00034 do { \ 00035 if (moab::MB_SUCCESS != err) { \ 00036 std::string mb_err; \ 00037 mbimpl->get_last_error(mb_err); \ 00038 throw Error(err, "%s, line %d: %s", __FILE__, __LINE__, mb_err.c_str()); \ 00039 } \ 00040 } while(false) 00041 00042 #define IBERRCHK(err, x) IBERRCHK_((err), (x), __FILE__, __LINE__) 00043 00044 inline void IBERRCHK_(int err, const char *descr); 00045 inline void IBERRCHK_(int err, iMesh &mesh); 00046 inline void IBERRCHK_(int err, iGeom &geom); 00047 inline void IBERRCHK_(int err, iRel &rel); 00048 00049 enum ErrorCode { 00050 MK_SUCCESS = 0, 00051 MK_FAILURE, 00052 MK_NOT_FOUND, 00053 MK_MULTIPLE_FOUND, 00054 MK_MESHOP_NOT_FOUND, 00055 MK_NOT_IMPLEMENTED, 00056 MK_WRONG_DIMENSION, 00057 MK_ALREADY_DEFINED, 00058 MK_BAD_INPUT, 00059 MK_BAD_GEOMETRIC_EVALUATION, 00060 MK_INCOMPLETE_MESH_SPECIFICATION 00061 }; 00062 00068 class Error : public std::exception 00069 { 00070 public: 00071 00075 Error(int err); 00076 00077 Error(int err, const char* format, ...) 00078 #ifdef __GNUC__ 00079 __attribute__((format(printf,3,4))) 00080 #endif 00081 ; 00082 00083 Error() {}; 00084 00089 virtual ~Error() throw(); 00090 00092 virtual ErrorCode error_code() const; 00093 00099 virtual const char *what() const throw(); 00100 00104 virtual void set_string(const char *str); 00105 00110 static const char* error_str(ErrorCode err); 00111 00112 private: 00114 ErrorCode errorCode; 00115 00117 std::string errDescription; 00118 }; 00119 00120 inline Error::Error(int err) : errorCode((ErrorCode)err) {} 00121 00122 inline Error::~Error() throw () {} 00123 00124 inline ErrorCode Error::error_code() const 00125 { 00126 return errorCode; 00127 } 00128 00129 inline const char *Error::what() const throw () 00130 { 00131 return errDescription.c_str(); 00132 } 00133 00134 inline const char* Error::error_str(ErrorCode err) 00135 { 00136 switch (err) { 00137 case MK_SUCCESS: return "Success"; 00138 case MK_FAILURE: return "Failure"; 00139 case MK_NOT_FOUND: return "Not found"; 00140 case MK_MULTIPLE_FOUND: return "Multiple entities found"; 00141 case MK_MESHOP_NOT_FOUND: return "MeshOp not found"; 00142 case MK_NOT_IMPLEMENTED: return "Not implemented"; 00143 case MK_WRONG_DIMENSION: return "Wrong dimension"; 00144 case MK_ALREADY_DEFINED: return "Already defined"; 00145 case MK_BAD_INPUT: return "Bad input"; 00146 case MK_BAD_GEOMETRIC_EVALUATION: return "Bad geometric evaluation"; 00147 case MK_INCOMPLETE_MESH_SPECIFICATION: return "Incomplete mesh specification"; 00148 default : return "Unknown Error"; 00149 }; 00150 } 00151 00152 inline void Error::set_string(const char *str) 00153 { 00154 errDescription = str; 00155 } 00156 00157 inline void IBERRCHK_(int err, const char *descr, const char *file, int line) 00158 { 00159 if (iBase_SUCCESS != err) 00160 throw Error(err, "%s, line %d: %s", file, line, descr); 00161 } 00162 00163 inline void IBERRCHK_(int err, iMesh &mesh, const char *file, int line) 00164 { 00165 if (iBase_SUCCESS != err) 00166 throw Error(err, "%s, line %d: %s", file, line, 00167 mesh.getDescription().c_str()); 00168 } 00169 00170 inline void IBERRCHK_(int err, iGeom &geom, const char *file, int line) 00171 { 00172 if (iBase_SUCCESS != err) 00173 throw Error(err, "%s, line %d: %s", file, line, 00174 geom.getDescription().c_str()); 00175 } 00176 00177 inline void IBERRCHK_(int err, iRel &rel, const char *file, int line) 00178 { 00179 if (iBase_SUCCESS != err) 00180 throw Error(err, "%s, line %d: %s", file, line, 00181 rel.getDescription().c_str()); 00182 } 00183 00184 } // namespace MeshKit 00185 00186 #endif