CoinUtils
trunk
|
00001 /* $Id$ */ 00002 // Copyright (C) 2000, International Business Machines 00003 // Corporation and others. All Rights Reserved. 00004 // This code is licensed under the terms of the Eclipse Public License (EPL). 00005 00006 #ifndef CoinError_H 00007 #define CoinError_H 00008 00009 #include <string> 00010 #include <iostream> 00011 #include <cassert> 00012 #include <cstring> 00013 00014 #include "CoinUtilsConfig.h" 00015 #include "CoinPragma.hpp" 00016 00019 void WindowsErrorPopupBlocker(); 00020 00021 //------------------------------------------------------------------- 00022 // 00023 // Error class used to throw exceptions 00024 // 00025 // Errors contain: 00026 // 00027 //------------------------------------------------------------------- 00028 00042 class CoinError { 00043 friend void CoinErrorUnitTest(); 00044 00045 private: 00046 CoinError() 00047 : 00048 message_(), 00049 method_(), 00050 class_(), 00051 file_(), 00052 lineNumber_() 00053 { 00054 // nothing to do here 00055 } 00056 00057 public: 00058 00059 //------------------------------------------------------------------- 00060 // Get methods 00061 //------------------------------------------------------------------- 00064 00065 inline const std::string & message() const 00066 { return message_; } 00068 inline const std::string & methodName() const 00069 { return method_; } 00071 inline const std::string & className() const 00072 { return class_; } 00074 inline const std::string & fileName() const 00075 { return file_; } 00077 inline int lineNumber() const 00078 { return lineNumber_; } 00080 inline void print(bool doPrint = true) const 00081 { 00082 if (! doPrint) 00083 return; 00084 if (lineNumber_<0) { 00085 std::cout<<message_<<" in "<<class_<<"::"<<method_<<std::endl; 00086 } else { 00087 std::cout<<file_<<":"<<lineNumber_<<" method "<<method_ 00088 <<" : assertion \'"<<message_<<"\' failed."<<std::endl; 00089 if(class_!="") 00090 std::cout<<"Possible reason: "<<class_<<std::endl; 00091 } 00092 } 00094 00095 00098 00099 CoinError ( 00100 std::string message__, 00101 std::string methodName__, 00102 std::string className__, 00103 std::string fileName_ = std::string(), 00104 int line = -1) 00105 : 00106 message_(message__), 00107 method_(methodName__), 00108 class_(className__), 00109 file_(fileName_), 00110 lineNumber_(line) 00111 { 00112 print(printErrors_); 00113 } 00114 00116 CoinError (const CoinError & source) 00117 : 00118 message_(source.message_), 00119 method_(source.method_), 00120 class_(source.class_), 00121 file_(source.file_), 00122 lineNumber_(source.lineNumber_) 00123 { 00124 // nothing to do here 00125 } 00126 00128 CoinError & operator=(const CoinError& rhs) 00129 { 00130 if (this != &rhs) { 00131 message_=rhs.message_; 00132 method_=rhs.method_; 00133 class_=rhs.class_; 00134 file_=rhs.file_; 00135 lineNumber_ = rhs.lineNumber_; 00136 } 00137 return *this; 00138 } 00139 00141 virtual ~CoinError () 00142 { 00143 // nothing to do here 00144 } 00146 00147 private: 00148 00151 00152 std::string message_; 00154 std::string method_; 00156 std::string class_; 00158 std::string file_; 00160 int lineNumber_; 00162 00163 public: 00165 static bool printErrors_; 00166 }; 00167 00168 #ifndef __STRING 00169 #define __STRING(x) #x 00170 #endif 00171 00172 #ifndef __GNUC_PREREQ 00173 # define __GNUC_PREREQ(maj, min) (0) 00174 #endif 00175 00176 #ifndef COIN_ASSERT 00177 # define CoinAssertDebug(expression) assert(expression) 00178 # define CoinAssertDebugHint(expression,hint) assert(expression) 00179 # define CoinAssert(expression) assert(expression) 00180 # define CoinAssertHint(expression,hint) assert(expression) 00181 #else 00182 # ifdef NDEBUG 00183 # define CoinAssertDebug(expression) {} 00184 # define CoinAssertDebugHint(expression,hint) {} 00185 # else 00186 # if defined(__GNUC__) && __GNUC_PREREQ(2, 6) 00187 # define CoinAssertDebug(expression) { \ 00188 if (!(expression)) { \ 00189 throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \ 00190 "", __FILE__, __LINE__); \ 00191 } \ 00192 } 00193 # define CoinAssertDebugHint(expression,hint) { \ 00194 if (!(expression)) { \ 00195 throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \ 00196 hint, __FILE__,__LINE__); \ 00197 } \ 00198 } 00199 # else 00200 # define CoinAssertDebug(expression) { \ 00201 if (!(expression)) { \ 00202 throw CoinError(__STRING(expression), "", \ 00203 "", __FILE__,__LINE__); \ 00204 } \ 00205 } 00206 # define CoinAssertDebugHint(expression,hint) { \ 00207 if (!(expression)) { \ 00208 throw CoinError(__STRING(expression), "", \ 00209 hint, __FILE__,__LINE__); \ 00210 } \ 00211 } 00212 # endif 00213 # endif 00214 # if defined(__GNUC__) && __GNUC_PREREQ(2, 6) 00215 # define CoinAssert(expression) { \ 00216 if (!(expression)) { \ 00217 throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \ 00218 "", __FILE__, __LINE__); \ 00219 } \ 00220 } 00221 # define CoinAssertHint(expression,hint) { \ 00222 if (!(expression)) { \ 00223 throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \ 00224 hint, __FILE__,__LINE__); \ 00225 } \ 00226 } 00227 # else 00228 # define CoinAssert(expression) { \ 00229 if (!(expression)) { \ 00230 throw CoinError(__STRING(expression), "", \ 00231 "", __FILE__,__LINE__); \ 00232 } \ 00233 } 00234 # define CoinAssertHint(expression,hint) { \ 00235 if (!(expression)) { \ 00236 throw CoinError(__STRING(expression), "", \ 00237 hint, __FILE__,__LINE__); \ 00238 } \ 00239 } 00240 # endif 00241 #endif 00242 00243 00244 //############################################################################# 00250 void 00251 CoinErrorUnitTest(); 00252 00253 #ifdef __LINE__ 00254 #define CoinErrorFL(x, y, z) CoinError((x), (y), (z), __FILE__, __LINE__) 00255 #endif 00256 00257 #endif