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
|
klao@1157
|
6 |
* Kutatocsoport (Egervary Combinatorial Optimization Research Group,
|
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;
|
deba@1207
|
305 |
if (message()) ostr << message();
|
deba@1207
|
306 |
if( file() || line() != 0 ) {
|
klao@1056
|
307 |
ostr << " (";
|
deba@1207
|
308 |
if( file() ) ostr << "in file '" << file() << "'";
|
deba@1207
|
309 |
if( file() && line() != 0 ) ostr << " ";
|
deba@1207
|
310 |
if( line() != 0 ) ostr << "at line " << line();
|
klao@1067
|
311 |
ostr << ")";
|
klao@1056
|
312 |
}
|
deba@1207
|
313 |
_message_holder.set(ostr.str());
|
klao@1056
|
314 |
}
|
deba@1207
|
315 |
catch (...) {}
|
deba@1207
|
316 |
if( _message_holder.valid()) return _message_holder.get().c_str();
|
klao@1120
|
317 |
return exceptionName();
|
klao@1120
|
318 |
}
|
klao@1120
|
319 |
|
klao@1120
|
320 |
virtual const char* exceptionName() const {
|
klao@1056
|
321 |
return "lemon::DataFormatError";
|
klao@1056
|
322 |
}
|
klao@1067
|
323 |
|
klao@1067
|
324 |
virtual ~DataFormatError() throw() {}
|
alpar@883
|
325 |
};
|
alpar@883
|
326 |
|
deba@1213
|
327 |
class IOParameterError : public LogicError {
|
deba@1207
|
328 |
protected:
|
deba@1207
|
329 |
ExceptionMember<std::string> _message;
|
deba@1207
|
330 |
ExceptionMember<std::string> _file;
|
deba@1207
|
331 |
|
deba@1207
|
332 |
mutable ExceptionMember<std::string> _message_holder;
|
deba@1207
|
333 |
public:
|
deba@1207
|
334 |
|
deba@1213
|
335 |
IOParameterError(const IOParameterError &ile) :
|
deba@1213
|
336 |
LogicError(ile), _message(ile._message), _file(ile._file) {}
|
deba@1207
|
337 |
|
deba@1207
|
338 |
///\e
|
deba@1213
|
339 |
explicit IOParameterError(const char *the_message)
|
deba@1213
|
340 |
: _message(the_message) {}
|
deba@1207
|
341 |
|
deba@1207
|
342 |
///\e
|
deba@1213
|
343 |
IOParameterError(const char *file_name, const char *the_message)
|
deba@1207
|
344 |
: _message(file_name), _file(file_name) {}
|
deba@1207
|
345 |
|
deba@1207
|
346 |
///\e
|
deba@1207
|
347 |
void message(const std::string& message) { _message.set(message); }
|
deba@1207
|
348 |
///\e
|
deba@1207
|
349 |
void file(const std::string &file) { _file.set(file); }
|
deba@1207
|
350 |
|
deba@1207
|
351 |
///\e
|
deba@1207
|
352 |
const char* message() const {
|
deba@1207
|
353 |
if (_message.valid()) {
|
deba@1207
|
354 |
return _message.get().c_str();
|
deba@1207
|
355 |
} else {
|
deba@1207
|
356 |
return 0;
|
deba@1207
|
357 |
}
|
deba@1207
|
358 |
}
|
deba@1207
|
359 |
|
deba@1207
|
360 |
/// \brief Returns the filename.
|
deba@1207
|
361 |
///
|
deba@1207
|
362 |
/// Returns \e null if the filename was not specified.
|
deba@1207
|
363 |
const char* file() const {
|
deba@1207
|
364 |
if (_file.valid()) {
|
deba@1207
|
365 |
return _file.get().c_str();
|
deba@1207
|
366 |
} else {
|
deba@1207
|
367 |
return 0;
|
deba@1207
|
368 |
}
|
deba@1207
|
369 |
}
|
deba@1207
|
370 |
|
deba@1207
|
371 |
///\e
|
deba@1207
|
372 |
virtual const char* what() const throw() {
|
deba@1207
|
373 |
try {
|
deba@1207
|
374 |
std::ostringstream ostr;
|
deba@1207
|
375 |
if (message()) ostr << message();
|
deba@1207
|
376 |
if (file()) ostr << "(when reading file '" << file() << "')";
|
deba@1207
|
377 |
_message_holder.set(ostr.str());
|
deba@1207
|
378 |
}
|
deba@1207
|
379 |
catch (...) {}
|
deba@1207
|
380 |
if( _message_holder.valid() ) return _message_holder.get().c_str();
|
deba@1207
|
381 |
return exceptionName();
|
deba@1207
|
382 |
}
|
deba@1207
|
383 |
|
deba@1207
|
384 |
virtual const char* exceptionName() const {
|
deba@1213
|
385 |
return "lemon::IOParameterError";
|
deba@1207
|
386 |
}
|
deba@1207
|
387 |
|
deba@1213
|
388 |
virtual ~IOParameterError() throw() {}
|
deba@1207
|
389 |
};
|
deba@1207
|
390 |
|
klao@1056
|
391 |
|
klao@1068
|
392 |
///\e
|
klao@1067
|
393 |
class AssertionFailedError : public LogicError {
|
klao@1067
|
394 |
protected:
|
klao@1067
|
395 |
const char *assertion;
|
klao@1067
|
396 |
const char *file;
|
klao@1067
|
397 |
int line;
|
klao@1067
|
398 |
const char *function;
|
klao@1067
|
399 |
const char *message;
|
deba@1213
|
400 |
|
deba@1213
|
401 |
mutable ExceptionMember<std::string> _message_holder;
|
klao@1067
|
402 |
public:
|
klao@1067
|
403 |
///\e
|
klao@1067
|
404 |
AssertionFailedError(const char *_file, int _line, const char *func,
|
klao@1067
|
405 |
const char *msg, const char *_assertion = 0) :
|
klao@1067
|
406 |
assertion(_assertion), file(_file), line(_line), function(func),
|
klao@1067
|
407 |
message(msg) {}
|
klao@1067
|
408 |
|
klao@1067
|
409 |
///\e
|
klao@1067
|
410 |
const char* get_assertion() const { return assertion; }
|
klao@1067
|
411 |
///\e
|
klao@1067
|
412 |
const char* get_message() const { return message; }
|
klao@1067
|
413 |
///\e
|
klao@1067
|
414 |
const char* get_file() const { return file; }
|
klao@1067
|
415 |
///\e
|
klao@1067
|
416 |
const char* get_function() const { return function; }
|
klao@1067
|
417 |
///\e
|
klao@1067
|
418 |
int get_line() const { return line; }
|
klao@1067
|
419 |
|
klao@1067
|
420 |
|
klao@1067
|
421 |
virtual const char* what() const throw() {
|
klao@1067
|
422 |
try {
|
klao@1067
|
423 |
std::ostringstream ostr;
|
klao@1067
|
424 |
ostr << file << ":" << line << ": ";
|
klao@1067
|
425 |
if( function )
|
klao@1067
|
426 |
ostr << function << ": ";
|
klao@1067
|
427 |
ostr << message;
|
klao@1067
|
428 |
if( assertion )
|
deba@1213
|
429 |
ostr << " (assertion '" << assertion << "' failed)";
|
deba@1213
|
430 |
_message_holder.set(ostr.str());
|
klao@1209
|
431 |
return ostr.str().c_str();
|
klao@1067
|
432 |
}
|
klao@1067
|
433 |
catch(...) {}
|
deba@1213
|
434 |
if( _message_holder.valid() ) return _message_holder.get().c_str();
|
klao@1120
|
435 |
return exceptionName();
|
klao@1120
|
436 |
}
|
klao@1120
|
437 |
|
klao@1120
|
438 |
virtual const char* exceptionName() const {
|
klao@1067
|
439 |
return "lemon::AssertionFailedError";
|
klao@1067
|
440 |
}
|
klao@1067
|
441 |
|
klao@1067
|
442 |
virtual ~AssertionFailedError() throw() {}
|
klao@1067
|
443 |
};
|
klao@1067
|
444 |
|
klao@1056
|
445 |
|
klao@1056
|
446 |
/**************** Macros ****************/
|
klao@1056
|
447 |
|
klao@1056
|
448 |
|
klao@1067
|
449 |
inline
|
klao@1067
|
450 |
void assert_fail(const char *file, int line, const char *func,
|
klao@1067
|
451 |
const char *message, const char *assertion = 0,
|
klao@1067
|
452 |
bool do_abort=true)
|
klao@1067
|
453 |
{
|
klao@1067
|
454 |
using namespace std;
|
klao@1067
|
455 |
cerr << file << ":" << line << ": ";
|
klao@1067
|
456 |
if( func )
|
klao@1067
|
457 |
cerr << func << ": ";
|
klao@1067
|
458 |
cerr << message;
|
klao@1067
|
459 |
if( assertion )
|
klao@1067
|
460 |
cerr << " (assertion '" << assertion << "' failed)";
|
klao@1067
|
461 |
cerr << endl;
|
klao@1067
|
462 |
if(do_abort)
|
klao@1067
|
463 |
abort();
|
klao@1067
|
464 |
}
|
klao@1056
|
465 |
|
klao@1067
|
466 |
inline
|
klao@1067
|
467 |
void assert_fail_throw(const char *file, int line, const char *func,
|
klao@1067
|
468 |
const char *message, const char *assertion = 0,
|
klao@1067
|
469 |
bool = true)
|
klao@1067
|
470 |
{
|
klao@1067
|
471 |
throw AssertionFailedError(file, line, func, message, assertion);
|
klao@1067
|
472 |
}
|
klao@1056
|
473 |
|
alpar@1151
|
474 |
/// @}
|
alpar@883
|
475 |
|
alpar@883
|
476 |
}
|
alpar@921
|
477 |
#endif // LEMON_ERROR_H
|
klao@1067
|
478 |
|
klao@1067
|
479 |
#undef LEMON_ASSERT
|
klao@1067
|
480 |
#undef LEMON_FIXME
|
klao@1067
|
481 |
|
klao@1067
|
482 |
#ifndef LEMON_ASSERT_ABORT
|
klao@1067
|
483 |
# define LEMON_ASSERT_ABORT 1
|
klao@1067
|
484 |
#endif
|
klao@1067
|
485 |
|
klao@1067
|
486 |
#ifndef LEMON_ASSERT_HANDLER
|
klao@1120
|
487 |
# ifdef LEMON_ASSERT_EXCEPTION
|
klao@1120
|
488 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw
|
klao@1120
|
489 |
# else
|
klao@1120
|
490 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail
|
klao@1120
|
491 |
# endif
|
klao@1067
|
492 |
#endif
|
klao@1067
|
493 |
|
klao@1067
|
494 |
#if defined(NDEBUG) || defined(LEMON_DISABLE_ASSERTS)
|
klao@1067
|
495 |
|
klao@1067
|
496 |
# define LEMON_ASSERT(exp, msg) (static_cast<void> (0))
|
klao@1067
|
497 |
|
klao@1067
|
498 |
#else
|
klao@1067
|
499 |
|
klao@1067
|
500 |
/**
|
klao@1067
|
501 |
* \brief Macro for assertions with customizable message
|
klao@1067
|
502 |
*
|
klao@1120
|
503 |
* Macro for assertions with customizable message.
|
klao@1120
|
504 |
*
|
klao@1120
|
505 |
* The behaviour can be customized with LEMON_ASSERT_HANDLER,
|
klao@1120
|
506 |
* LEMON_ASSERT_EXCEPTION and LEMON_ASSERT_ABORT defines. Asserts can be
|
klao@1120
|
507 |
* disabled by defining either NDEBUG or LEMON_DISABLE_ASSERTS macros.
|
klao@1120
|
508 |
*
|
klao@1120
|
509 |
* \todo We should provide some way to reset to the default behaviour,
|
klao@1120
|
510 |
* shouldn't we?
|
klao@1120
|
511 |
*
|
klao@1120
|
512 |
* \todo This whole 'assert' business should be placed in a separate
|
klao@1120
|
513 |
* include file.
|
klao@1120
|
514 |
*
|
klao@1067
|
515 |
* \todo __PRETTY_FUNCTION__ should be replaced by something
|
klao@1067
|
516 |
* compiler-independant, like BOOST_CURRENT_FUNCTION
|
klao@1067
|
517 |
*/
|
klao@1067
|
518 |
|
klao@1067
|
519 |
# define LEMON_ASSERT(exp, msg) \
|
klao@1067
|
520 |
(static_cast<void> (!!(exp) ? 0 : ( \
|
klao@1067
|
521 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
|
klao@1067
|
522 |
__PRETTY_FUNCTION__, \
|
klao@1067
|
523 |
(msg), #exp, LEMON_ASSERT_ABORT), 0)))
|
klao@1067
|
524 |
|
klao@1120
|
525 |
#endif // NDEBUG || LEMON_DISABLE_ASSERTS
|
klao@1067
|
526 |
|
klao@1067
|
527 |
/**
|
klao@1067
|
528 |
* \brief Macro for mark not yet implemented features.
|
klao@1067
|
529 |
*
|
klao@1067
|
530 |
* \todo Is this the right place for this? It should be used only in
|
klao@1067
|
531 |
* modules under development.
|
klao@1067
|
532 |
*
|
klao@1067
|
533 |
* \todo __PRETTY_FUNCTION__ should be replaced by something
|
klao@1067
|
534 |
* compiler-independant, like BOOST_CURRENT_FUNCTION
|
klao@1067
|
535 |
*/
|
klao@1067
|
536 |
|
klao@1067
|
537 |
# define LEMON_FIXME(msg) \
|
klao@1067
|
538 |
(LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \
|
klao@1067
|
539 |
"FIXME: " msg))
|