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

- File List
- File Members

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

Go to the documentation of this file.

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