CoinError.hpp 7.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
/* $Id$ */
// Copyright (C) 2000, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifndef CoinError_H
#define CoinError_H

#include <string>
#include <iostream>
#include <cassert>
#include <cstring>

#include "CoinUtilsConfig.h"
#include "CoinPragma.hpp"

/** A function to block the popup windows that windows creates when the code
    crashes */
void WindowsErrorPopupBlocker();

//-------------------------------------------------------------------
//
// Error class used to throw exceptions
//
// Errors contain:
//
//-------------------------------------------------------------------

/** Error Class thrown by an exception

This class is used when exceptions are thrown.
It contains:
  <ul>
  <li>message text
  <li>name of method throwing exception
  <li>name of class throwing exception or hint
  <li>name of file if assert
  <li>line number
  </ul>
  For asserts class=> optional hint
*/
class CoinError {
  friend void CoinErrorUnitTest();

private:
  CoinError()
    : message_()
    , method_()
    , class_()
    , file_()
    , lineNumber_()
  {
    // nothing to do here
  }

public:
  //-------------------------------------------------------------------
  // Get methods
  //-------------------------------------------------------------------
  /**@name Get error attributes */
  //@{
  /// get message text
  inline const std::string &message() const
  {
    return message_;
  }
  /// get name of method instantiating error
  inline const std::string &methodName() const
  {
    return method_;
  }
  /// get name of class instantiating error (or hint for assert)
  inline const std::string &className() const
  {
    return class_;
  }
  /// get name of file for assert
  inline const std::string &fileName() const
  {
    return file_;
  }
  /// get line number of assert (-1 if not assert)
  inline int lineNumber() const
  {
    return lineNumber_;
  }
  /// Just print (for asserts)
  inline void print(bool doPrint = true) const
  {
    if (!doPrint)
      return;
    if (lineNumber_ < 0) {
      std::cout << message_ << " in " << class_ << "::" << method_ << std::endl;
    } else {
      std::cout << file_ << ":" << lineNumber_ << " method " << method_
                << " : assertion \'" << message_ << "\' failed." << std::endl;
      if (class_ != "")
        std::cout << "Possible reason: " << class_ << std::endl;
    }
  }
  //@}

  /**@name Constructors and destructors */
  //@{
  /// Alternate Constructor
  CoinError(
    std::string message__,
    std::string methodName__,
    std::string className__,
    std::string fileName_ = std::string(),
    int line = -1)
    : message_(message__)
    , method_(methodName__)
    , class_(className__)
    , file_(fileName_)
    , lineNumber_(line)
  {
    print(printErrors_);
  }

  /// Copy constructor
  CoinError(const CoinError &source)
    : message_(source.message_)
    , method_(source.method_)
    , class_(source.class_)
    , file_(source.file_)
    , lineNumber_(source.lineNumber_)
  {
    // nothing to do here
  }

  /// Assignment operator
  CoinError &operator=(const CoinError &rhs)
  {
    if (this != &rhs) {
      message_ = rhs.message_;
      method_ = rhs.method_;
      class_ = rhs.class_;
      file_ = rhs.file_;
      lineNumber_ = rhs.lineNumber_;
    }
    return *this;
  }

  /// Destructor
  virtual ~CoinError()
  {
    // nothing to do here
  }
  //@}

private:
  /**@name Private member data */
  //@{
  /// message test
  std::string message_;
  /// method name
  std::string method_;
  /// class name or hint
  std::string class_;
  /// file name
  std::string file_;
  /// Line number
  int lineNumber_;
  //@}

public:
  /// Whether to print every error
  static bool printErrors_;
};

#ifndef __STRING
#define __STRING(x) #x
#endif

#ifndef __GNUC_PREREQ
#define __GNUC_PREREQ(maj, min) (0)
#endif

#ifndef COIN_ASSERT
#define CoinAssertDebug(expression) assert(expression)
#define CoinAssertDebugHint(expression, hint) assert(expression)
#define CoinAssert(expression) assert(expression)
#define CoinAssertHint(expression, hint) assert(expression)
#else
#ifdef NDEBUG
#define CoinAssertDebug(expression) \
  {                                 \
  }
#define CoinAssertDebugHint(expression, hint) \
  {                                           \
  }
#else
#if defined(__GNUC__) && __GNUC_PREREQ(2, 6)
#define CoinAssertDebug(expression)                              \
  {                                                              \
    if (!(expression)) {                                         \
      throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
        "", __FILE__, __LINE__);                                 \
    }                                                            \
  }
#define CoinAssertDebugHint(expression, hint)                    \
  {                                                              \
    if (!(expression)) {                                         \
      throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
        hint, __FILE__, __LINE__);                               \
    }                                                            \
  }
#else
#define CoinAssertDebug(expression)             \
  {                                             \
    if (!(expression)) {                        \
      throw CoinError(__STRING(expression), "", \
        "", __FILE__, __LINE__);                \
    }                                           \
  }
#define CoinAssertDebugHint(expression, hint)   \
  {                                             \
    if (!(expression)) {                        \
      throw CoinError(__STRING(expression), "", \
        hint, __FILE__, __LINE__);              \
    }                                           \
  }
#endif
#endif
#if defined(__GNUC__) && __GNUC_PREREQ(2, 6)
#define CoinAssert(expression)                                   \
  {                                                              \
    if (!(expression)) {                                         \
      throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
        "", __FILE__, __LINE__);                                 \
    }                                                            \
  }
#define CoinAssertHint(expression, hint)                         \
  {                                                              \
    if (!(expression)) {                                         \
      throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
        hint, __FILE__, __LINE__);                               \
    }                                                            \
  }
#else
#define CoinAssert(expression)                  \
  {                                             \
    if (!(expression)) {                        \
      throw CoinError(__STRING(expression), "", \
        "", __FILE__, __LINE__);                \
    }                                           \
  }
#define CoinAssertHint(expression, hint)        \
  {                                             \
    if (!(expression)) {                        \
      throw CoinError(__STRING(expression), "", \
        hint, __FILE__, __LINE__);              \
    }                                           \
  }
#endif
#endif

//#############################################################################
/** A function that tests the methods in the CoinError class. The
    only reason for it not to be a member method is that this way it doesn't
    have to be compiled into the library. And that's a gain, because the
    library should be compiled with optimization on, but this method should be
    compiled with debugging. */
void CoinErrorUnitTest();

#ifdef __LINE__
#define CoinErrorFL(x, y, z) CoinError((x), (y), (z), __FILE__, __LINE__)
#endif

#endif

/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
*/