alpar@906
|
1 |
/* -*- C++ -*-
|
klao@1157
|
2 |
*
|
alpar@921
|
3 |
* src/lemon/error.h - Part of LEMON, a generic C++ optimization library
|
alpar@906
|
4 |
*
|
alpar@1164
|
5 |
* Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi
|
alpar@1359
|
6 |
* Kutatocsoport (Egervary Research Group on Combinatorial Optimization,
|
klao@1157
|
7 |
* EGRES).
|
alpar@906
|
8 |
*
|
alpar@906
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
12 |
*
|
alpar@906
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
15 |
* purpose.
|
alpar@906
|
16 |
*
|
alpar@906
|
17 |
*/
|
alpar@883
|
18 |
|
alpar@921
|
19 |
#ifndef LEMON_ERROR_H
|
alpar@921
|
20 |
#define LEMON_ERROR_H
|
alpar@883
|
21 |
|
alpar@1151
|
22 |
//! \ingroup exceptions
|
alpar@883
|
23 |
//! \file
|
klao@1067
|
24 |
//! \brief Basic exception classes and error handling.
|
alpar@883
|
25 |
|
alpar@883
|
26 |
#include <exception>
|
alpar@883
|
27 |
#include <string>
|
alpar@883
|
28 |
#include <sstream>
|
klao@1067
|
29 |
#include <iostream>
|
klao@1067
|
30 |
#include <cstdlib>
|
klao@1157
|
31 |
#include <memory>
|
alpar@883
|
32 |
|
alpar@921
|
33 |
namespace lemon {
|
alpar@883
|
34 |
|
deba@1207
|
35 |
/// \addtogroup exceptions
|
deba@1207
|
36 |
/// @{
|
deba@1207
|
37 |
|
deba@1207
|
38 |
/// \brief Exception safe wrapper class.
|
deba@1207
|
39 |
///
|
deba@1207
|
40 |
/// Exception safe wrapper class to implement the members of exceptions.
|
deba@1207
|
41 |
template <typename _Type>
|
deba@1207
|
42 |
class ExceptionMember {
|
deba@1207
|
43 |
public:
|
deba@1207
|
44 |
typedef _Type Type;
|
deba@1207
|
45 |
|
deba@1207
|
46 |
ExceptionMember() throw () {
|
deba@1207
|
47 |
try {
|
deba@1207
|
48 |
ptr.reset(new Type());
|
deba@1207
|
49 |
} catch (...) {}
|
deba@1207
|
50 |
}
|
deba@1207
|
51 |
|
deba@1207
|
52 |
ExceptionMember(const Type& type) throw () {
|
deba@1207
|
53 |
try {
|
deba@1207
|
54 |
ptr.reset(new Type());
|
deba@1207
|
55 |
if (ptr.get() == 0) return;
|
deba@1207
|
56 |
*ptr = type;
|
deba@1207
|
57 |
} catch (...) {}
|
deba@1207
|
58 |
}
|
deba@1207
|
59 |
|
deba@1207
|
60 |
ExceptionMember(const ExceptionMember& copy) throw() {
|
deba@1207
|
61 |
try {
|
deba@1207
|
62 |
if (!copy.valid()) return;
|
deba@1207
|
63 |
ptr.reset(new Type());
|
deba@1207
|
64 |
if (ptr.get() == 0) return;
|
deba@1207
|
65 |
*ptr = copy.get();
|
deba@1207
|
66 |
} catch (...) {}
|
deba@1207
|
67 |
}
|
deba@1207
|
68 |
|
deba@1207
|
69 |
ExceptionMember& operator=(const ExceptionMember& copy) {
|
deba@1207
|
70 |
if (ptr.get() == 0) return;
|
deba@1207
|
71 |
try {
|
deba@1207
|
72 |
if (!copy.valid()) return;
|
deba@1207
|
73 |
*ptr = copy.get();
|
deba@1207
|
74 |
} catch (...) {}
|
deba@1207
|
75 |
}
|
deba@1207
|
76 |
|
deba@1207
|
77 |
void set(const Type& type) {
|
deba@1207
|
78 |
if (ptr.get() == 0) return;
|
deba@1207
|
79 |
try {
|
deba@1207
|
80 |
*ptr = type;
|
deba@1207
|
81 |
} catch (...) {}
|
deba@1207
|
82 |
}
|
deba@1207
|
83 |
|
deba@1207
|
84 |
const Type& get() const {
|
deba@1207
|
85 |
return *ptr;
|
deba@1207
|
86 |
}
|
deba@1207
|
87 |
|
deba@1207
|
88 |
bool valid() const {
|
deba@1207
|
89 |
return ptr.get() != 0;
|
deba@1207
|
90 |
}
|
deba@1207
|
91 |
|
deba@1207
|
92 |
private:
|
deba@1207
|
93 |
std::auto_ptr<_Type> ptr;
|
deba@1207
|
94 |
};
|
alpar@1151
|
95 |
|
klao@1056
|
96 |
/// Exception-safe convenient "error message" class.
|
klao@1067
|
97 |
|
klao@1067
|
98 |
/// Helper class which provides a convenient ostream-like (operator <<
|
klao@1067
|
99 |
/// based) interface to create a string message. Mostly useful in
|
klao@1067
|
100 |
/// exception classes (therefore the name).
|
klao@1056
|
101 |
class ErrorMessage {
|
klao@1056
|
102 |
protected:
|
klao@1067
|
103 |
///\e
|
klao@1157
|
104 |
///\todo The good solution is boost:shared_ptr...
|
klao@1157
|
105 |
mutable
|
klao@1157
|
106 |
std::auto_ptr<std::ostringstream> buf;
|
klao@1056
|
107 |
|
klao@1067
|
108 |
///\e
|
klao@1056
|
109 |
bool init() throw() {
|
klao@1056
|
110 |
try {
|
klao@1056
|
111 |
buf.reset(new std::ostringstream);
|
klao@1056
|
112 |
}
|
klao@1056
|
113 |
catch(...) {
|
klao@1056
|
114 |
buf.reset();
|
klao@1056
|
115 |
}
|
klao@1157
|
116 |
return buf.get();
|
klao@1056
|
117 |
}
|
klao@1056
|
118 |
|
klao@1056
|
119 |
public:
|
klao@1056
|
120 |
|
klao@1067
|
121 |
///\e
|
klao@1056
|
122 |
ErrorMessage() throw() { init(); }
|
klao@1056
|
123 |
|
klao@1157
|
124 |
ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }
|
klao@1157
|
125 |
|
klao@1067
|
126 |
///\e
|
klao@1056
|
127 |
ErrorMessage(const char *message) throw() {
|
klao@1056
|
128 |
init();
|
klao@1056
|
129 |
*this << message;
|
klao@1056
|
130 |
}
|
klao@1056
|
131 |
|
klao@1067
|
132 |
///\e
|
klao@1056
|
133 |
ErrorMessage(const std::string &message) throw() {
|
klao@1056
|
134 |
init();
|
klao@1056
|
135 |
*this << message;
|
klao@1056
|
136 |
}
|
klao@1056
|
137 |
|
klao@1067
|
138 |
///\e
|
klao@1056
|
139 |
template <typename T>
|
klao@1056
|
140 |
ErrorMessage& operator<<(const T &t) throw() {
|
klao@1157
|
141 |
if( ! buf.get() ) return *this;
|
klao@1056
|
142 |
|
klao@1056
|
143 |
try {
|
klao@1056
|
144 |
*buf << t;
|
klao@1056
|
145 |
}
|
klao@1056
|
146 |
catch(...) {
|
klao@1056
|
147 |
buf.reset();
|
klao@1056
|
148 |
}
|
deba@1207
|
149 |
return *this;
|
klao@1056
|
150 |
}
|
klao@1056
|
151 |
|
klao@1067
|
152 |
///\e
|
klao@1056
|
153 |
const char* message() throw() {
|
klao@1157
|
154 |
if( ! buf.get() ) return 0;
|
klao@1056
|
155 |
|
klao@1056
|
156 |
const char* mes = 0;
|
klao@1056
|
157 |
try {
|
klao@1056
|
158 |
mes = buf->str().c_str();
|
klao@1056
|
159 |
}
|
klao@1056
|
160 |
catch(...) {}
|
klao@1056
|
161 |
return mes;
|
klao@1056
|
162 |
}
|
klao@1056
|
163 |
|
klao@1056
|
164 |
};
|
klao@1056
|
165 |
|
alpar@883
|
166 |
/**
|
alpar@883
|
167 |
* \brief Generic exception class.
|
alpar@883
|
168 |
*
|
klao@1056
|
169 |
* Base class for exceptions used in LEMON.
|
alpar@883
|
170 |
*/
|
klao@1067
|
171 |
class Exception : public std::exception {
|
alpar@883
|
172 |
public:
|
klao@1067
|
173 |
///\e
|
klao@1120
|
174 |
Exception() {}
|
klao@1067
|
175 |
///\e
|
alpar@883
|
176 |
virtual ~Exception() throw() {}
|
klao@1120
|
177 |
|
klao@1120
|
178 |
///\e
|
klao@1120
|
179 |
virtual const char* exceptionName() const {
|
klao@1120
|
180 |
return "lemon::Exception";
|
klao@1120
|
181 |
}
|
alpar@883
|
182 |
|
klao@1067
|
183 |
///\e
|
alpar@883
|
184 |
virtual const char* what() const throw() {
|
klao@1120
|
185 |
return exceptionName();
|
klao@1056
|
186 |
}
|
klao@1056
|
187 |
};
|
klao@1056
|
188 |
|
klao@1120
|
189 |
/**
|
klao@1120
|
190 |
* \brief One of the two main subclasses of \ref Exception.
|
klao@1120
|
191 |
*
|
klao@1120
|
192 |
* Logic errors represent problems in the internal logic of a program;
|
klao@1120
|
193 |
* in theory, these are preventable, and even detectable before the
|
klao@1120
|
194 |
* program runs (e.g., violations of class invariants).
|
klao@1120
|
195 |
*
|
alpar@1125
|
196 |
* A typical example for this is \ref UninitializedParameter.
|
klao@1120
|
197 |
*/
|
klao@1056
|
198 |
class LogicError : public Exception {
|
klao@1067
|
199 |
public:
|
klao@1120
|
200 |
virtual const char* exceptionName() const {
|
klao@1120
|
201 |
return "lemon::LogicError";
|
klao@1120
|
202 |
}
|
klao@1056
|
203 |
};
|
klao@1056
|
204 |
|
alpar@1125
|
205 |
/**
|
alpar@1125
|
206 |
* \brief \ref Exception for uninitialized parameters.
|
alpar@1125
|
207 |
*
|
alpar@1125
|
208 |
* This error represents problems in the initialization
|
alpar@1125
|
209 |
* of the parameters of the algorithms.
|
alpar@1125
|
210 |
*/
|
alpar@1125
|
211 |
class UninitializedParameter : public LogicError {
|
alpar@1125
|
212 |
public:
|
alpar@1125
|
213 |
virtual const char* exceptionName() const {
|
alpar@1125
|
214 |
return "lemon::UninitializedParameter";
|
alpar@1125
|
215 |
}
|
alpar@1125
|
216 |
};
|
alpar@1125
|
217 |
|
klao@1120
|
218 |
|
klao@1120
|
219 |
/**
|
klao@1120
|
220 |
* \brief One of the two main subclasses of \ref Exception.
|
klao@1120
|
221 |
*
|
klao@1120
|
222 |
* Runtime errors represent problems outside the scope of a program;
|
klao@1120
|
223 |
* they cannot be easily predicted and can generally only be caught as
|
klao@1120
|
224 |
* the program executes.
|
klao@1120
|
225 |
*/
|
klao@1056
|
226 |
class RuntimeError : public Exception {
|
klao@1067
|
227 |
public:
|
klao@1120
|
228 |
virtual const char* exceptionName() const {
|
klao@1120
|
229 |
return "lemon::RuntimeError";
|
klao@1120
|
230 |
}
|
klao@1056
|
231 |
};
|
klao@1056
|
232 |
|
klao@1067
|
233 |
///\e
|
klao@1056
|
234 |
class RangeError : public RuntimeError {
|
klao@1067
|
235 |
public:
|
klao@1120
|
236 |
virtual const char* exceptionName() const {
|
klao@1120
|
237 |
return "lemon::RangeError";
|
klao@1120
|
238 |
}
|
klao@1056
|
239 |
};
|
klao@1056
|
240 |
|
alpar@1061
|
241 |
///\e
|
klao@1056
|
242 |
class IOError : public RuntimeError {
|
klao@1067
|
243 |
public:
|
klao@1120
|
244 |
virtual const char* exceptionName() const {
|
klao@1120
|
245 |
return "lemon::IOError";
|
klao@1120
|
246 |
}
|
klao@1056
|
247 |
};
|
klao@1056
|
248 |
|
alpar@1061
|
249 |
///\e
|
klao@1056
|
250 |
class DataFormatError : public IOError {
|
klao@1067
|
251 |
protected:
|
deba@1207
|
252 |
ExceptionMember<std::string> _message;
|
deba@1207
|
253 |
ExceptionMember<std::string> _file;
|
klao@1120
|
254 |
int _line;
|
klao@1157
|
255 |
|
deba@1207
|
256 |
mutable ExceptionMember<std::string> _message_holder;
|
klao@1067
|
257 |
public:
|
klao@1157
|
258 |
|
klao@1157
|
259 |
DataFormatError(const DataFormatError &dfe) :
|
deba@1207
|
260 |
IOError(dfe), _message(dfe._message), _file(dfe._file),
|
deba@1207
|
261 |
_line(dfe._line) {}
|
klao@1157
|
262 |
|
klao@1067
|
263 |
///\e
|
klao@1120
|
264 |
explicit DataFormatError(const char *the_message)
|
klao@1120
|
265 |
: _message(the_message), _line(0) {}
|
deba@1207
|
266 |
|
klao@1067
|
267 |
///\e
|
klao@1056
|
268 |
DataFormatError(const std::string &file_name, int line_num,
|
klao@1120
|
269 |
const char *the_message)
|
klao@1120
|
270 |
: _message(the_message), _line(line_num) { file(file_name); }
|
klao@1056
|
271 |
|
klao@1067
|
272 |
///\e
|
deba@1207
|
273 |
void line(int line) { _line = line; }
|
klao@1067
|
274 |
///\e
|
deba@1207
|
275 |
void message(const std::string& message) { _message.set(message); }
|
klao@1120
|
276 |
///\e
|
deba@1207
|
277 |
void file(const std::string &file) { _file.set(file); }
|
deba@1207
|
278 |
|
deba@1207
|
279 |
///\e
|
deba@1207
|
280 |
int line() const { return _line; }
|
deba@1207
|
281 |
///\e
|
deba@1207
|
282 |
const char* message() const {
|
deba@1207
|
283 |
if (_message.valid() && !_message.get().empty()) {
|
deba@1207
|
284 |
return _message.get().c_str();
|
deba@1207
|
285 |
} else {
|
deba@1207
|
286 |
return 0;
|
klao@1056
|
287 |
}
|
alpar@883
|
288 |
}
|
alpar@883
|
289 |
|
klao@1067
|
290 |
/// \brief Returns the filename.
|
klao@1067
|
291 |
///
|
deba@1207
|
292 |
/// Returns \e null if the filename was not specified.
|
klao@1120
|
293 |
const char* file() const {
|
deba@1207
|
294 |
if (_file.valid() && !_file.get().empty()) {
|
deba@1207
|
295 |
return _file.get().c_str();
|
deba@1207
|
296 |
} else {
|
deba@1207
|
297 |
return 0;
|
deba@1207
|
298 |
}
|
klao@1067
|
299 |
}
|
klao@1067
|
300 |
|
klao@1067
|
301 |
///\e
|
klao@1067
|
302 |
virtual const char* what() const throw() {
|
klao@1056
|
303 |
try {
|
klao@1056
|
304 |
std::ostringstream ostr;
|
alpar@1399
|
305 |
ostr << exceptionName() << ": ";
|
deba@1207
|
306 |
if (message()) ostr << message();
|
deba@1207
|
307 |
if( file() || line() != 0 ) {
|
klao@1056
|
308 |
ostr << " (";
|
deba@1207
|
309 |
if( file() ) ostr << "in file '" << file() << "'";
|
deba@1207
|
310 |
if( file() && line() != 0 ) ostr << " ";
|
deba@1207
|
311 |
if( line() != 0 ) ostr << "at line " << line();
|
klao@1067
|
312 |
ostr << ")";
|
klao@1056
|
313 |
}
|
deba@1207
|
314 |
_message_holder.set(ostr.str());
|
klao@1056
|
315 |
}
|
deba@1207
|
316 |
catch (...) {}
|
deba@1207
|
317 |
if( _message_holder.valid()) return _message_holder.get().c_str();
|
klao@1120
|
318 |
return exceptionName();
|
klao@1120
|
319 |
}
|
klao@1120
|
320 |
|
klao@1120
|
321 |
virtual const char* exceptionName() const {
|
klao@1056
|
322 |
return "lemon::DataFormatError";
|
klao@1056
|
323 |
}
|
klao@1067
|
324 |
|
klao@1067
|
325 |
virtual ~DataFormatError() throw() {}
|
alpar@883
|
326 |
};
|
alpar@883
|
327 |
|
deba@1213
|
328 |
class IOParameterError : public LogicError {
|
deba@1207
|
329 |
protected:
|
deba@1207
|
330 |
ExceptionMember<std::string> _message;
|
deba@1207
|
331 |
ExceptionMember<std::string> _file;
|
deba@1207
|
332 |
|
deba@1207
|
333 |
mutable ExceptionMember<std::string> _message_holder;
|
deba@1207
|
334 |
public:
|
deba@1207
|
335 |
|
deba@1213
|
336 |
IOParameterError(const IOParameterError &ile) :
|
deba@1213
|
337 |
LogicError(ile), _message(ile._message), _file(ile._file) {}
|
deba@1207
|
338 |
|
deba@1207
|
339 |
///\e
|
deba@1213
|
340 |
explicit IOParameterError(const char *the_message)
|
deba@1213
|
341 |
: _message(the_message) {}
|
deba@1207
|
342 |
|
deba@1207
|
343 |
///\e
|
deba@1213
|
344 |
IOParameterError(const char *file_name, const char *the_message)
|
deba@1393
|
345 |
: _message(the_message), _file(file_name) {}
|
deba@1207
|
346 |
|
deba@1207
|
347 |
///\e
|
deba@1207
|
348 |
void message(const std::string& message) { _message.set(message); }
|
deba@1207
|
349 |
///\e
|
deba@1207
|
350 |
void file(const std::string &file) { _file.set(file); }
|
deba@1207
|
351 |
|
deba@1207
|
352 |
///\e
|
deba@1207
|
353 |
const char* message() const {
|
deba@1207
|
354 |
if (_message.valid()) {
|
deba@1207
|
355 |
return _message.get().c_str();
|
deba@1207
|
356 |
} else {
|
deba@1207
|
357 |
return 0;
|
deba@1207
|
358 |
}
|
deba@1207
|
359 |
}
|
deba@1207
|
360 |
|
deba@1207
|
361 |
/// \brief Returns the filename.
|
deba@1207
|
362 |
///
|
deba@1207
|
363 |
/// Returns \e null if the filename was not specified.
|
deba@1207
|
364 |
const char* file() const {
|
deba@1207
|
365 |
if (_file.valid()) {
|
deba@1207
|
366 |
return _file.get().c_str();
|
deba@1207
|
367 |
} else {
|
deba@1207
|
368 |
return 0;
|
deba@1207
|
369 |
}
|
deba@1207
|
370 |
}
|
deba@1207
|
371 |
|
deba@1207
|
372 |
///\e
|
deba@1207
|
373 |
virtual const char* what() const throw() {
|
deba@1207
|
374 |
try {
|
deba@1207
|
375 |
std::ostringstream ostr;
|
deba@1207
|
376 |
if (message()) ostr << message();
|
deba@1207
|
377 |
if (file()) ostr << "(when reading file '" << file() << "')";
|
deba@1207
|
378 |
_message_holder.set(ostr.str());
|
deba@1207
|
379 |
}
|
deba@1207
|
380 |
catch (...) {}
|
deba@1207
|
381 |
if( _message_holder.valid() ) return _message_holder.get().c_str();
|
deba@1207
|
382 |
return exceptionName();
|
deba@1207
|
383 |
}
|
deba@1207
|
384 |
|
deba@1207
|
385 |
virtual const char* exceptionName() const {
|
deba@1213
|
386 |
return "lemon::IOParameterError";
|
deba@1207
|
387 |
}
|
deba@1207
|
388 |
|
deba@1213
|
389 |
virtual ~IOParameterError() throw() {}
|
deba@1207
|
390 |
};
|
deba@1207
|
391 |
|
klao@1056
|
392 |
|
klao@1068
|
393 |
///\e
|
klao@1067
|
394 |
class AssertionFailedError : public LogicError {
|
klao@1067
|
395 |
protected:
|
klao@1067
|
396 |
const char *assertion;
|
klao@1067
|
397 |
const char *file;
|
klao@1067
|
398 |
int line;
|
klao@1067
|
399 |
const char *function;
|
klao@1067
|
400 |
const char *message;
|
deba@1213
|
401 |
|
deba@1213
|
402 |
mutable ExceptionMember<std::string> _message_holder;
|
klao@1067
|
403 |
public:
|
klao@1067
|
404 |
///\e
|
klao@1067
|
405 |
AssertionFailedError(const char *_file, int _line, const char *func,
|
klao@1067
|
406 |
const char *msg, const char *_assertion = 0) :
|
klao@1067
|
407 |
assertion(_assertion), file(_file), line(_line), function(func),
|
klao@1067
|
408 |
message(msg) {}
|
klao@1067
|
409 |
|
klao@1067
|
410 |
///\e
|
klao@1067
|
411 |
const char* get_assertion() const { return assertion; }
|
klao@1067
|
412 |
///\e
|
klao@1067
|
413 |
const char* get_message() const { return message; }
|
klao@1067
|
414 |
///\e
|
klao@1067
|
415 |
const char* get_file() const { return file; }
|
klao@1067
|
416 |
///\e
|
klao@1067
|
417 |
const char* get_function() const { return function; }
|
klao@1067
|
418 |
///\e
|
klao@1067
|
419 |
int get_line() const { return line; }
|
klao@1067
|
420 |
|
klao@1067
|
421 |
|
klao@1067
|
422 |
virtual const char* what() const throw() {
|
klao@1067
|
423 |
try {
|
klao@1067
|
424 |
std::ostringstream ostr;
|
klao@1067
|
425 |
ostr << file << ":" << line << ": ";
|
klao@1067
|
426 |
if( function )
|
klao@1067
|
427 |
ostr << function << ": ";
|
klao@1067
|
428 |
ostr << message;
|
klao@1067
|
429 |
if( assertion )
|
deba@1213
|
430 |
ostr << " (assertion '" << assertion << "' failed)";
|
deba@1213
|
431 |
_message_holder.set(ostr.str());
|
klao@1209
|
432 |
return ostr.str().c_str();
|
klao@1067
|
433 |
}
|
klao@1067
|
434 |
catch(...) {}
|
deba@1213
|
435 |
if( _message_holder.valid() ) return _message_holder.get().c_str();
|
klao@1120
|
436 |
return exceptionName();
|
klao@1120
|
437 |
}
|
klao@1120
|
438 |
|
klao@1120
|
439 |
virtual const char* exceptionName() const {
|
klao@1067
|
440 |
return "lemon::AssertionFailedError";
|
klao@1067
|
441 |
}
|
klao@1067
|
442 |
|
klao@1067
|
443 |
virtual ~AssertionFailedError() throw() {}
|
klao@1067
|
444 |
};
|
klao@1067
|
445 |
|
klao@1056
|
446 |
|
klao@1056
|
447 |
/**************** Macros ****************/
|
klao@1056
|
448 |
|
klao@1056
|
449 |
|
klao@1067
|
450 |
inline
|
klao@1067
|
451 |
void assert_fail(const char *file, int line, const char *func,
|
klao@1067
|
452 |
const char *message, const char *assertion = 0,
|
klao@1067
|
453 |
bool do_abort=true)
|
klao@1067
|
454 |
{
|
klao@1067
|
455 |
using namespace std;
|
klao@1067
|
456 |
cerr << file << ":" << line << ": ";
|
klao@1067
|
457 |
if( func )
|
klao@1067
|
458 |
cerr << func << ": ";
|
klao@1067
|
459 |
cerr << message;
|
klao@1067
|
460 |
if( assertion )
|
klao@1067
|
461 |
cerr << " (assertion '" << assertion << "' failed)";
|
klao@1067
|
462 |
cerr << endl;
|
klao@1067
|
463 |
if(do_abort)
|
klao@1067
|
464 |
abort();
|
klao@1067
|
465 |
}
|
klao@1056
|
466 |
|
klao@1067
|
467 |
inline
|
klao@1067
|
468 |
void assert_fail_throw(const char *file, int line, const char *func,
|
klao@1067
|
469 |
const char *message, const char *assertion = 0,
|
klao@1067
|
470 |
bool = true)
|
klao@1067
|
471 |
{
|
klao@1067
|
472 |
throw AssertionFailedError(file, line, func, message, assertion);
|
klao@1067
|
473 |
}
|
klao@1056
|
474 |
|
alpar@1151
|
475 |
/// @}
|
alpar@883
|
476 |
|
alpar@883
|
477 |
}
|
alpar@921
|
478 |
#endif // LEMON_ERROR_H
|
klao@1067
|
479 |
|
klao@1067
|
480 |
#undef LEMON_ASSERT
|
klao@1067
|
481 |
#undef LEMON_FIXME
|
klao@1067
|
482 |
|
klao@1067
|
483 |
#ifndef LEMON_ASSERT_ABORT
|
klao@1067
|
484 |
# define LEMON_ASSERT_ABORT 1
|
klao@1067
|
485 |
#endif
|
klao@1067
|
486 |
|
klao@1067
|
487 |
#ifndef LEMON_ASSERT_HANDLER
|
klao@1120
|
488 |
# ifdef LEMON_ASSERT_EXCEPTION
|
klao@1120
|
489 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw
|
klao@1120
|
490 |
# else
|
klao@1120
|
491 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail
|
klao@1120
|
492 |
# endif
|
klao@1067
|
493 |
#endif
|
klao@1067
|
494 |
|
klao@1067
|
495 |
#if defined(NDEBUG) || defined(LEMON_DISABLE_ASSERTS)
|
klao@1067
|
496 |
|
klao@1067
|
497 |
# define LEMON_ASSERT(exp, msg) (static_cast<void> (0))
|
klao@1067
|
498 |
|
klao@1067
|
499 |
#else
|
klao@1067
|
500 |
|
klao@1067
|
501 |
/**
|
klao@1067
|
502 |
* \brief Macro for assertions with customizable message
|
klao@1067
|
503 |
*
|
klao@1120
|
504 |
* Macro for assertions with customizable message.
|
klao@1120
|
505 |
*
|
klao@1120
|
506 |
* The behaviour can be customized with LEMON_ASSERT_HANDLER,
|
klao@1120
|
507 |
* LEMON_ASSERT_EXCEPTION and LEMON_ASSERT_ABORT defines. Asserts can be
|
klao@1120
|
508 |
* disabled by defining either NDEBUG or LEMON_DISABLE_ASSERTS macros.
|
klao@1120
|
509 |
*
|
klao@1120
|
510 |
* \todo We should provide some way to reset to the default behaviour,
|
klao@1120
|
511 |
* shouldn't we?
|
klao@1120
|
512 |
*
|
klao@1120
|
513 |
* \todo This whole 'assert' business should be placed in a separate
|
klao@1120
|
514 |
* include file.
|
klao@1120
|
515 |
*
|
klao@1067
|
516 |
* \todo __PRETTY_FUNCTION__ should be replaced by something
|
zsuzska@1274
|
517 |
* compiler-independent, like BOOST_CURRENT_FUNCTION
|
klao@1067
|
518 |
*/
|
klao@1067
|
519 |
|
klao@1067
|
520 |
# define LEMON_ASSERT(exp, msg) \
|
klao@1067
|
521 |
(static_cast<void> (!!(exp) ? 0 : ( \
|
klao@1067
|
522 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
|
klao@1067
|
523 |
__PRETTY_FUNCTION__, \
|
klao@1067
|
524 |
(msg), #exp, LEMON_ASSERT_ABORT), 0)))
|
klao@1067
|
525 |
|
klao@1120
|
526 |
#endif // NDEBUG || LEMON_DISABLE_ASSERTS
|
klao@1067
|
527 |
|
klao@1067
|
528 |
/**
|
klao@1067
|
529 |
* \brief Macro for mark not yet implemented features.
|
klao@1067
|
530 |
*
|
klao@1067
|
531 |
* \todo Is this the right place for this? It should be used only in
|
klao@1067
|
532 |
* modules under development.
|
klao@1067
|
533 |
*
|
klao@1067
|
534 |
* \todo __PRETTY_FUNCTION__ should be replaced by something
|
zsuzska@1274
|
535 |
* compiler-independent, like BOOST_CURRENT_FUNCTION
|
klao@1067
|
536 |
*/
|
klao@1067
|
537 |
|
klao@1067
|
538 |
# define LEMON_FIXME(msg) \
|
klao@1067
|
539 |
(LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \
|
klao@1067
|
540 |
"FIXME: " msg))
|