CoinError.hpp
Go to the documentation of this file.
1 /* $Id: CoinError.hpp 1217 2009-11-06 18:58:32Z stefan $ */
2 // Copyright (C) 2000, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 #ifndef CoinError_H
5 #define CoinError_H
6 
7 #include <string>
8 #include <iostream>
9 #include <cassert>
10 #include <cstring>
11 
12 #include "CoinUtilsConfig.h"
13 #include "CoinPragma.hpp"
14 
18 
19 //-------------------------------------------------------------------
20 //
21 // Error class used to throw exceptions
22 //
23 // Errors contain:
24 //
25 //-------------------------------------------------------------------
26 
40 class CoinError {
41  friend void CoinErrorUnitTest();
42 
43 private:
45  :
46  message_(),
47  method_(),
48  class_(),
49  file_(),
50  lineNumber_()
51  {
52  // nothing to do here
53  }
54 
55 public:
56 
57  //-------------------------------------------------------------------
58  // Get methods
59  //-------------------------------------------------------------------
62  inline const std::string & message() const
64  { return message_; }
66  inline const std::string & methodName() const
67  { return method_; }
69  inline const std::string & className() const
70  { return class_; }
72  inline const std::string & fileName() const
73  { return file_; }
75  inline int lineNumber() const
76  { return lineNumber_; }
78  inline void print(bool doPrint = true) const
79  {
80  if (! doPrint)
81  return;
82  if (lineNumber_<0) {
83  std::cout<<message_<<" in "<<class_<<"::"<<method_<<std::endl;
84  } else {
85  std::cout<<file_<<":"<<lineNumber_<<" method "<<method_
86  <<" : assertion \'"<<message_<<"\' failed."<<std::endl;
87  if(class_!="")
88  std::cout<<"Possible reason: "<<class_<<std::endl;
89  }
90  }
92 
93 
96  CoinError (
98  std::string message__,
99  std::string methodName__,
100  std::string className__,
101  std::string fileName_ = std::string(),
102  int line = -1)
103  :
104  message_(message__),
105  method_(methodName__),
106  class_(className__),
107  file_(fileName_),
108  lineNumber_(line)
109  {
111  }
112 
114  CoinError (const CoinError & source)
115  :
116  message_(source.message_),
117  method_(source.method_),
118  class_(source.class_),
119  file_(source.file_),
120  lineNumber_(source.lineNumber_)
121  {
122  // nothing to do here
123  }
124 
127  {
128  if (this != &rhs) {
129  message_=rhs.message_;
130  method_=rhs.method_;
131  class_=rhs.class_;
132  file_=rhs.file_;
133  lineNumber_ = rhs.lineNumber_;
134  }
135  return *this;
136  }
137 
139  virtual ~CoinError ()
140  {
141  // nothing to do here
142  }
144 
145 private:
146 
149  std::string message_;
152  std::string method_;
154  std::string class_;
156  std::string file_;
160 
161 public:
163  static bool printErrors_;
164 };
165 
166 #ifndef __STRING
167 #define __STRING(x) #x
168 #endif
169 
170 #ifndef __GNUC_PREREQ
171 # define __GNUC_PREREQ(maj, min) (0)
172 #endif
173 
174 #ifndef COIN_ASSERT
175 # define CoinAssertDebug(expression) assert(expression)
176 # define CoinAssertDebugHint(expression,hint) assert(expression)
177 # define CoinAssert(expression) assert(expression)
178 # define CoinAssertHint(expression,hint) assert(expression)
179 #else
180 # ifdef NDEBUG
181 # define CoinAssertDebug(expression) {}
182 # define CoinAssertDebugHint(expression,hint) {}
183 # else
184 # if defined(__GNUC__) && __GNUC_PREREQ(2, 6)
185 # define CoinAssertDebug(expression) { \
186  if (!(expression)) { \
187  throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
188  "", __FILE__, __LINE__); \
189  } \
190  }
191 # define CoinAssertDebugHint(expression,hint) { \
192  if (!(expression)) { \
193  throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
194  hint, __FILE__,__LINE__); \
195  } \
196  }
197 # else
198 # define CoinAssertDebug(expression) { \
199  if (!(expression)) { \
200  throw CoinError(__STRING(expression), "", \
201  "", __FILE__,__LINE__); \
202  } \
203  }
204 # define CoinAssertDebugHint(expression,hint) { \
205  if (!(expression)) { \
206  throw CoinError(__STRING(expression), "", \
207  hint, __FILE__,__LINE__); \
208  } \
209  }
210 # endif
211 # endif
212 # if defined(__GNUC__) && __GNUC_PREREQ(2, 6)
213 # define CoinAssert(expression) { \
214  if (!(expression)) { \
215  throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
216  "", __FILE__, __LINE__); \
217  } \
218  }
219 # define CoinAssertHint(expression,hint) { \
220  if (!(expression)) { \
221  throw CoinError(__STRING(expression), __PRETTY_FUNCTION__, \
222  hint, __FILE__,__LINE__); \
223  } \
224  }
225 # else
226 # define CoinAssert(expression) { \
227  if (!(expression)) { \
228  throw CoinError(__STRING(expression), "", \
229  "", __FILE__,__LINE__); \
230  } \
231  }
232 # define CoinAssertHint(expression,hint) { \
233  if (!(expression)) { \
234  throw CoinError(__STRING(expression), "", \
235  hint, __FILE__,__LINE__); \
236  } \
237  }
238 # endif
239 #endif
240 
241 
242 //#############################################################################
248 void
250 
251 #ifdef __LINE__
252 #define CoinErrorFL(x, y, z) CoinError((x), (y), (z), __FILE__, __LINE__)
253 #endif
254 
255 #endif
std::string file_
file name
Definition: CoinError.hpp:156
int lineNumber() const
get line number of assert (-1 if not assert)
Definition: CoinError.hpp:75
void WindowsErrorPopupBlocker()
A function to block the popup windows that windows creates when the code crashes. ...
const std::string & className() const
get name of class instantiating error (or hint for assert)
Definition: CoinError.hpp:69
CoinError & operator=(const CoinError &rhs)
Assignment operator.
Definition: CoinError.hpp:126
friend void CoinErrorUnitTest()
A function that tests the methods in the CoinError class.
std::string message_
message test
Definition: CoinError.hpp:150
int lineNumber_
Line number.
Definition: CoinError.hpp:158
const std::string & fileName() const
get name of file for assert
Definition: CoinError.hpp:72
virtual ~CoinError()
Destructor.
Definition: CoinError.hpp:139
void print(bool doPrint=true) const
Just print (for asserts)
Definition: CoinError.hpp:78
const std::string & methodName() const
get name of method instantiating error
Definition: CoinError.hpp:66
std::string method_
method name
Definition: CoinError.hpp:152
Error Class thrown by an exception.
Definition: CoinError.hpp:40
const std::string & message() const
get message text
Definition: CoinError.hpp:63
std::string class_
class name or hint
Definition: CoinError.hpp:154
static bool printErrors_
Whether to print every error.
Definition: CoinError.hpp:163
CoinError(const CoinError &source)
Copy constructor.
Definition: CoinError.hpp:114
void CoinErrorUnitTest()
A function that tests the methods in the CoinError class.