- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# /home/coin/SVN-release/Osi-0.98.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 "CoinPragma.hpp"
    00012
    00013 //-------------------------------------------------------------------
    00014 //
    00015 // Error class used to throw exceptions
    00016 //
    00017 // Errors contain:
    00018 //
    00019 //-------------------------------------------------------------------
    00020
    00034 class CoinError  {
    00035     friend void CoinErrorUnitTest();
    00036
    00037 private:
    00038     CoinError()
    00039       :
    00040       message_(),
    00041       method_(),
    00042       class_(),
    00043       file_(),
    00044       lineNumber_()
    00045     {
    00046       // nothing to do here
    00047     }
    00048
    00049 public:
    00050
    00051   //-------------------------------------------------------------------
    00052   // Get methods
    00053   //-------------------------------------------------------------------
    00056
    00057     inline const std::string & message() const
    00058     { return message_; }
    00060     inline const std::string & methodName() const
    00061     { return method_;  }
    00063     inline const std::string & className() const
    00064     { return class_;   }
    00066     inline const std::string & fileName() const
    00067     { return file_;  }
    00069     inline int lineNumber() const
    00070     { return lineNumber_;   }
    00072     inline void print(bool doPrint = true) const
    00073     {
    00074       if (! doPrint)
    00075         return;
    00076       if (lineNumber_<0) {
    00077         std::cout<<message_<<" in "<<class_<<"::"<<method_<<std::endl;
    00078       } else {
    00079         std::cout<<file_<<":"<<lineNumber_<<" method "<<method_
    00080                  <<" : assertion \'"<<message_<<"\' failed."<<std::endl;
    00081         if(class_!="")
    00082           std::cout<<"Possible reason: "<<class_<<std::endl;
    00083       }
    00084     }
    00086
    00087
    00090
    00091     CoinError (
    00092       std::string message,
    00093       std::string methodName,
    00094       std::string className,
    00095       std::string fileName = std::string(),
    00096       int line = -1)
    00097       :
    00098       message_(message),
    00099       method_(methodName),
    00100       class_(className),
    00101       file_(fileName),
    00102       lineNumber_(line)
    00103     {
    00104       print(printErrors_);
    00105     }
    00106
    00108     CoinError (const CoinError & source)
    00109       :
    00110       message_(source.message_),
    00111       method_(source.method_),
    00112       class_(source.class_),
    00113       file_(source.file_),
    00114       lineNumber_(source.lineNumber_)
    00115     {
    00116       // nothing to do here
    00117     }
    00118
    00120     CoinError & operator=(const CoinError& rhs)
    00121     {
    00122       if (this != &rhs) {
    00123         message_=rhs.message_;
    00124         method_=rhs.method_;
    00125         class_=rhs.class_;
    00126         file_=rhs.file_;
    00127         lineNumber_ = rhs.lineNumber_;
    00128       }
    00129       return *this;
    00130     }
    00131
    00133     virtual ~CoinError ()
    00134     {
    00135       // nothing to do here
    00136     }
    00138
    00139 private:
    00140
    00143
    00144     std::string message_;
    00146     std::string method_;
    00148     std::string class_;
    00150     std::string file_;
    00152     int lineNumber_;
    00154
    00155 public:
    00157   static bool printErrors_;
    00158 };
    00159
    00160 #ifndef __STRING
    00161 #define __STRING(x)     #x
    00162 #endif
    00163
    00164 #ifndef __GNUC_PREREQ
    00165 # define __GNUC_PREREQ(maj, min) (0)
    00166 #endif
    00167
    00168 #ifndef COIN_ASSERT
    00169 #   define CoinAssertDebug(expression) assert(expression)
    00170 #   define CoinAssertDebugHint(expression,hint) assert(expression)
    00171 #   define CoinAssert(expression) assert(expression)
    00172 #   define CoinAssertHint(expression,hint) assert(expression)
    00173 #else
    00174 #   ifdef NDEBUG
    00175 #      define CoinAssertDebug(expression)               {}
    00176 #      define CoinAssertDebugHint(expression,hint)      {}
    00177 #   else
    00178 #      if defined(__GNUC__) && __GNUC_PREREQ(2, 6)
    00179 #         define CoinAssertDebug(expression) {                             \
    00180              if (!(expression)) {                                          \
    00181                 throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
    00182                                 "", __FILE__, __LINE__);                   \
    00183              }                                                             \
    00184           }
    00185 #         define CoinAssertDebugHint(expression,hint) {                    \
    00186              if (!(expression)) {                                          \
    00187                 throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
    00188                                 hint, __FILE__,__LINE__);                  \
    00189              }                                                             \
    00190           }
    00191 #      else
    00192 #         define CoinAssertDebug(expression) {                             \
    00193              if (!(expression)) {                                          \
    00194                 throw CoinError(__STRING(expression), "",                  \
    00195                                 "", __FILE__,__LINE__);                    \
    00196              }                                                             \
    00197           }
    00198 #         define CoinAssertDebugHint(expression,hint) {                    \
    00199              if (!(expression)) {                                          \
    00200                 throw CoinError(__STRING(expression), "",                  \
    00201                                 hint, __FILE__,__LINE__);                  \
    00202              }                                                             \
    00203           }
    00204 #      endif
    00205 #   endif
    00206 #   if defined(__GNUC__) && __GNUC_PREREQ(2, 6)
    00207 #      define CoinAssert(expression) {                                  \
    00208           if (!(expression)) {                                          \
    00209              throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
    00210                              "", __FILE__, __LINE__);                   \
    00211           }                                                             \
    00212        }
    00213 #      define CoinAssertHint(expression,hint) {                         \
    00214           if (!(expression)) {                                          \
    00215              throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
    00216                              hint, __FILE__,__LINE__);                  \
    00217           }                                                             \
    00218        }
    00219 #   else
    00220 #      define CoinAssert(expression) {                                  \
    00221           if (!(expression)) {                                          \
    00222              throw CoinError(__STRING(expression), "",                  \
    00223                              "", __FILE__,__LINE__);                    \
    00224           }                                                             \
    00225        }
    00226 #      define CoinAssertHint(expression,hint) {                         \
    00227           if (!(expression)) {                                          \
    00228              throw CoinError(__STRING(expression), "",                  \
    00229                              hint, __FILE__,__LINE__);                  \
    00230           }                                                             \
    00231        }
    00232 #   endif
    00233 #endif
    00234
    00235
    00236 //#############################################################################
    00242 void
    00243 CoinErrorUnitTest();
    00244
    00245 #ifdef __LINE__
    00246 #define CoinErrorFL(x, y, z) CoinError((x), (y), (z), __FILE__, __LINE__)
    00247 #endif
    00248
    00249 #endif