/home/coin/SVN-release/Bcp-1.2.1/CoinUtils/src/CoinError.hpp

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

Generated on Thu Jan 15 03:01:00 2009 for coin-Bcp by  doxygen 1.4.7