00001
00002
00003 #ifndef CoinError_H
00004 #define CoinError_H
00005
00006 #include <string>
00007 #include <iostream>
00008 #include <cassert>
00009
00010 #include "CoinPragma.hpp"
00011
00012
00013
00014
00015
00016
00017
00018
00019
00033 class CoinError {
00034 friend void CoinErrorUnitTest();
00035
00036 private:
00037 CoinError()
00038 :
00039 message_(),
00040 method_(),
00041 class_(),
00042 file_(),
00043 lineNumber_()
00044 {
00045
00046 }
00047
00048 public:
00049
00050
00051
00052
00055
00056 inline const std::string & message() const
00057 { return message_; }
00059 inline const std::string & methodName() const
00060 { return method_; }
00062 inline const std::string & className() const
00063 { return class_; }
00065 inline const std::string & fileName() const
00066 { return file_; }
00068 inline int lineNumber() const
00069 { return lineNumber_; }
00071 inline void print(bool doPrint = true) const
00072 {
00073 if (! doPrint)
00074 return;
00075 if (lineNumber_<0) {
00076 std::cout<<message_<<" in "<<method_<<" class "<<class_<<std::endl;
00077 } else {
00078 std::cout<<file_<<":"<<lineNumber_<<" method "<<method_
00079 <<" : assertion \'"<<message_<<"\' failed."<<std::endl;
00080 if(class_!="")
00081 std::cout<<"Possible reason: "<<class_<<std::endl;
00082 }
00083 }
00085
00086
00089
00090 CoinError (
00091 std::string message,
00092 std::string methodName,
00093 std::string className,
00094 std::string fileName = std::string(),
00095 int line = -1)
00096 :
00097 message_(message),
00098 method_(methodName),
00099 class_(className),
00100 file_(fileName),
00101 lineNumber_(line)
00102 {
00103 print(printErrors_);
00104 }
00105
00107 CoinError (const CoinError & source)
00108 :
00109 message_(source.message_),
00110 method_(source.method_),
00111 class_(source.class_),
00112 file_(source.file_),
00113 lineNumber_(source.lineNumber_)
00114 {
00115
00116 }
00117
00119 CoinError & operator=(const CoinError& rhs)
00120 {
00121 if (this != &rhs) {
00122 message_=rhs.message_;
00123 method_=rhs.method_;
00124 class_=rhs.class_;
00125 file_=rhs.file_;
00126 lineNumber_ = rhs.lineNumber_;
00127 }
00128 return *this;
00129 }
00130
00132 virtual ~CoinError ()
00133 {
00134
00135 }
00137
00138 private:
00139
00142
00143 std::string message_;
00145 std::string method_;
00147 std::string class_;
00149 std::string file_;
00151 int lineNumber_;
00153
00154 public:
00156 static bool printErrors_;
00157 };
00158
00159 #ifndef __STRING
00160 #define __STRING(x) #x
00161 #endif
00162
00163 #ifndef __GNUC_PREREQ
00164 # define __GNUC_PREREQ(maj, min) (0)
00165 #endif
00166
00167 #ifndef COIN_ASSERT
00168 # define CoinAssertDebug(expression) assert(expression)
00169 # define CoinAssertDebugHint(expression,hint) assert(expression)
00170 # define CoinAssert(expression) assert(expression)
00171 # define CoinAssertHint(expression,hint) assert(expression)
00172 #else
00173 # ifdef NDEBUG
00174 # define CoinAssertDebug(expression) {}
00175 # define CoinAssertDebugHint(expression,hint) {}
00176 # else
00177 # if defined(__GNUC__) && __GNUC_PREREQ(2, 6)
00178 # define CoinAssertDebug(expression) { \
00179 if (!(expression)) { \
00180 throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
00181 "", __FILE__, __LINE__); \
00182 } \
00183 }
00184 # define CoinAssertDebugHint(expression,hint) { \
00185 if (!(expression)) { \
00186 throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
00187 hint, __FILE__,__LINE__); \
00188 } \
00189 }
00190 # else
00191 # define CoinAssertDebug(expression) { \
00192 if (!(expression)) { \
00193 throw CoinError(__STRING(expression), "", \
00194 "", __FILE__,__LINE__); \
00195 } \
00196 }
00197 # define CoinAssertDebugHint(expression,hint) { \
00198 if (!(expression)) { \
00199 throw CoinError(__STRING(expression), "", \
00200 hint, __FILE__,__LINE__); \
00201 } \
00202 }
00203 # endif
00204 # endif
00205 # if defined(__GNUC__) && __GNUC_PREREQ(2, 6)
00206 # define CoinAssert(expression) { \
00207 if (!(expression)) { \
00208 throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
00209 "", __FILE__, __LINE__); \
00210 } \
00211 }
00212 # define CoinAssertHint(expression,hint) { \
00213 if (!(expression)) { \
00214 throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
00215 hint, __FILE__,__LINE__); \
00216 } \
00217 }
00218 # else
00219 # define CoinAssert(expression) { \
00220 if (!(expression)) { \
00221 throw CoinError(__STRING(expression), "", \
00222 "", __FILE__,__LINE__); \
00223 } \
00224 }
00225 # define CoinAssertHint(expression,hint) { \
00226 if (!(expression)) { \
00227 throw CoinError(__STRING(expression), "", \
00228 hint, __FILE__,__LINE__); \
00229 } \
00230 }
00231 # endif
00232 #endif
00233
00234
00235
00241 void
00242 CoinErrorUnitTest();
00243
00244 #ifdef __LINE__
00245 #define CoinErrorFL(x, y, z) CoinError((x), (y), (z), __FILE__, __LINE__)
00246 #endif
00247
00248 #endif