alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@921
|
2 |
* src/lemon/error.h - Part of LEMON, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@906
|
4 |
* Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@906
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
alpar@906
|
6 |
*
|
alpar@906
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
10 |
*
|
alpar@906
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
13 |
* purpose.
|
alpar@906
|
14 |
*
|
alpar@906
|
15 |
*/
|
alpar@883
|
16 |
|
alpar@921
|
17 |
#ifndef LEMON_ERROR_H
|
alpar@921
|
18 |
#define LEMON_ERROR_H
|
alpar@883
|
19 |
|
alpar@883
|
20 |
//! \ingroup misc
|
alpar@883
|
21 |
//! \file
|
klao@1067
|
22 |
//! \brief Basic exception classes and error handling.
|
alpar@883
|
23 |
|
alpar@883
|
24 |
#include <exception>
|
alpar@883
|
25 |
#include <string>
|
alpar@883
|
26 |
#include <sstream>
|
klao@1067
|
27 |
#include <iostream>
|
klao@1067
|
28 |
#include <cstdlib>
|
alpar@883
|
29 |
|
klao@1056
|
30 |
#include <boost/shared_ptr.hpp>
|
alpar@883
|
31 |
|
alpar@921
|
32 |
namespace lemon {
|
alpar@883
|
33 |
|
klao@1056
|
34 |
/// Exception-safe convenient "error message" class.
|
klao@1067
|
35 |
|
klao@1067
|
36 |
/// Helper class which provides a convenient ostream-like (operator <<
|
klao@1067
|
37 |
/// based) interface to create a string message. Mostly useful in
|
klao@1067
|
38 |
/// exception classes (therefore the name).
|
klao@1056
|
39 |
class ErrorMessage {
|
klao@1056
|
40 |
protected:
|
klao@1067
|
41 |
///\e
|
klao@1056
|
42 |
boost::shared_ptr<std::ostringstream> buf;
|
klao@1056
|
43 |
|
klao@1067
|
44 |
///\e
|
klao@1056
|
45 |
bool init() throw() {
|
klao@1056
|
46 |
try {
|
klao@1056
|
47 |
buf.reset(new std::ostringstream);
|
klao@1056
|
48 |
}
|
klao@1056
|
49 |
catch(...) {
|
klao@1056
|
50 |
buf.reset();
|
klao@1056
|
51 |
}
|
klao@1056
|
52 |
return buf;
|
klao@1056
|
53 |
}
|
klao@1056
|
54 |
|
klao@1056
|
55 |
public:
|
klao@1056
|
56 |
|
klao@1067
|
57 |
///\e
|
klao@1056
|
58 |
ErrorMessage() throw() { init(); }
|
klao@1056
|
59 |
|
klao@1067
|
60 |
///\e
|
klao@1056
|
61 |
ErrorMessage(const char *message) throw() {
|
klao@1056
|
62 |
init();
|
klao@1056
|
63 |
*this << message;
|
klao@1056
|
64 |
}
|
klao@1056
|
65 |
|
klao@1067
|
66 |
///\e
|
klao@1056
|
67 |
ErrorMessage(const std::string &message) throw() {
|
klao@1056
|
68 |
init();
|
klao@1056
|
69 |
*this << message;
|
klao@1056
|
70 |
}
|
klao@1056
|
71 |
|
klao@1067
|
72 |
///\e
|
klao@1056
|
73 |
template <typename T>
|
klao@1056
|
74 |
ErrorMessage& operator<<(const T &t) throw() {
|
klao@1056
|
75 |
if( !buf ) return *this;
|
klao@1056
|
76 |
|
klao@1056
|
77 |
try {
|
klao@1056
|
78 |
*buf << t;
|
klao@1056
|
79 |
}
|
klao@1056
|
80 |
catch(...) {
|
klao@1056
|
81 |
buf.reset();
|
klao@1056
|
82 |
}
|
klao@1056
|
83 |
}
|
klao@1056
|
84 |
|
klao@1067
|
85 |
///\e
|
klao@1056
|
86 |
const char* message() throw() {
|
klao@1056
|
87 |
if( !buf ) return 0;
|
klao@1056
|
88 |
|
klao@1056
|
89 |
const char* mes = 0;
|
klao@1056
|
90 |
try {
|
klao@1056
|
91 |
mes = buf->str().c_str();
|
klao@1056
|
92 |
}
|
klao@1056
|
93 |
catch(...) {}
|
klao@1056
|
94 |
return mes;
|
klao@1056
|
95 |
}
|
klao@1056
|
96 |
|
klao@1056
|
97 |
};
|
klao@1056
|
98 |
|
alpar@883
|
99 |
/**
|
alpar@883
|
100 |
* \brief Generic exception class.
|
alpar@883
|
101 |
*
|
klao@1056
|
102 |
* Base class for exceptions used in LEMON.
|
alpar@883
|
103 |
*/
|
klao@1067
|
104 |
class Exception : public std::exception {
|
alpar@883
|
105 |
public:
|
klao@1067
|
106 |
///\e
|
klao@1120
|
107 |
Exception() {}
|
klao@1067
|
108 |
///\e
|
alpar@883
|
109 |
virtual ~Exception() throw() {}
|
klao@1120
|
110 |
|
klao@1120
|
111 |
///\e
|
klao@1120
|
112 |
virtual const char* exceptionName() const {
|
klao@1120
|
113 |
return "lemon::Exception";
|
klao@1120
|
114 |
}
|
alpar@883
|
115 |
|
klao@1067
|
116 |
///\e
|
alpar@883
|
117 |
virtual const char* what() const throw() {
|
klao@1120
|
118 |
return exceptionName();
|
klao@1056
|
119 |
}
|
klao@1056
|
120 |
};
|
klao@1056
|
121 |
|
klao@1120
|
122 |
/**
|
klao@1120
|
123 |
* \brief One of the two main subclasses of \ref Exception.
|
klao@1120
|
124 |
*
|
klao@1120
|
125 |
* Logic errors represent problems in the internal logic of a program;
|
klao@1120
|
126 |
* in theory, these are preventable, and even detectable before the
|
klao@1120
|
127 |
* program runs (e.g., violations of class invariants).
|
klao@1120
|
128 |
*
|
klao@1120
|
129 |
* For a typical example \see UninitializedParameterError.
|
klao@1120
|
130 |
*/
|
klao@1056
|
131 |
class LogicError : public Exception {
|
klao@1067
|
132 |
public:
|
klao@1120
|
133 |
virtual const char* exceptionName() const {
|
klao@1120
|
134 |
return "lemon::LogicError";
|
klao@1120
|
135 |
}
|
klao@1056
|
136 |
};
|
klao@1056
|
137 |
|
klao@1120
|
138 |
|
klao@1120
|
139 |
/**
|
klao@1120
|
140 |
* \brief One of the two main subclasses of \ref Exception.
|
klao@1120
|
141 |
*
|
klao@1120
|
142 |
* Runtime errors represent problems outside the scope of a program;
|
klao@1120
|
143 |
* they cannot be easily predicted and can generally only be caught as
|
klao@1120
|
144 |
* the program executes.
|
klao@1120
|
145 |
*/
|
klao@1056
|
146 |
class RuntimeError : public Exception {
|
klao@1067
|
147 |
public:
|
klao@1120
|
148 |
virtual const char* exceptionName() const {
|
klao@1120
|
149 |
return "lemon::RuntimeError";
|
klao@1120
|
150 |
}
|
klao@1056
|
151 |
};
|
klao@1056
|
152 |
|
klao@1067
|
153 |
///\e
|
klao@1056
|
154 |
class RangeError : public RuntimeError {
|
klao@1067
|
155 |
public:
|
klao@1120
|
156 |
virtual const char* exceptionName() const {
|
klao@1120
|
157 |
return "lemon::RangeError";
|
klao@1120
|
158 |
}
|
klao@1056
|
159 |
};
|
klao@1056
|
160 |
|
alpar@1061
|
161 |
///\e
|
klao@1056
|
162 |
class IOError : public RuntimeError {
|
klao@1067
|
163 |
public:
|
klao@1120
|
164 |
virtual const char* exceptionName() const {
|
klao@1120
|
165 |
return "lemon::IOError";
|
klao@1120
|
166 |
}
|
klao@1056
|
167 |
};
|
klao@1056
|
168 |
|
alpar@1061
|
169 |
///\e
|
klao@1056
|
170 |
class DataFormatError : public IOError {
|
klao@1067
|
171 |
protected:
|
klao@1120
|
172 |
const char *_message;
|
klao@1120
|
173 |
int _line;
|
klao@1120
|
174 |
boost::shared_ptr<std::string> _file;
|
klao@1067
|
175 |
|
klao@1067
|
176 |
public:
|
klao@1067
|
177 |
///\e
|
klao@1120
|
178 |
explicit DataFormatError(const char *the_message)
|
klao@1120
|
179 |
: _message(the_message), _line(0) {}
|
klao@1067
|
180 |
///\e
|
klao@1056
|
181 |
DataFormatError(const std::string &file_name, int line_num,
|
klao@1120
|
182 |
const char *the_message)
|
klao@1120
|
183 |
: _message(the_message), _line(line_num) { file(file_name); }
|
klao@1056
|
184 |
|
klao@1067
|
185 |
///\e
|
klao@1120
|
186 |
void line(int line_num) { _line=line_num; }
|
klao@1067
|
187 |
///\e
|
klao@1120
|
188 |
void message(char *the_message) { _message=the_message; }
|
klao@1120
|
189 |
///\e
|
klao@1120
|
190 |
void file(const std::string &file_name) {
|
klao@1056
|
191 |
try {
|
klao@1120
|
192 |
_file.reset(new std::string);
|
klao@1120
|
193 |
*_file = file_name;
|
klao@1056
|
194 |
}
|
klao@1056
|
195 |
catch(...) {
|
klao@1120
|
196 |
_file.reset();
|
klao@1056
|
197 |
}
|
alpar@883
|
198 |
}
|
alpar@883
|
199 |
|
klao@1067
|
200 |
///\e
|
klao@1120
|
201 |
int line() const { return _line; }
|
klao@1120
|
202 |
///\e
|
klao@1120
|
203 |
const char* message() const { return _message; }
|
klao@1067
|
204 |
|
klao@1067
|
205 |
/// \brief Returns the filename.
|
klao@1067
|
206 |
///
|
klao@1068
|
207 |
/// Returns \e "(unknown)" if the filename was not specified.
|
klao@1120
|
208 |
const char* file() const {
|
klao@1120
|
209 |
if( _file )
|
klao@1120
|
210 |
return _file->c_str();
|
klao@1067
|
211 |
else
|
klao@1067
|
212 |
return "(unknown)";
|
klao@1067
|
213 |
}
|
klao@1067
|
214 |
|
klao@1067
|
215 |
///\e
|
klao@1067
|
216 |
virtual const char* what() const throw() {
|
klao@1056
|
217 |
const char *mes = 0;
|
klao@1056
|
218 |
try {
|
klao@1056
|
219 |
std::ostringstream ostr;
|
klao@1120
|
220 |
ostr << _message;
|
klao@1120
|
221 |
if( _file || _line ) {
|
klao@1056
|
222 |
ostr << " (";
|
klao@1120
|
223 |
if( _file ) ostr << "in file '" << *_file << "'";
|
klao@1120
|
224 |
if( _file && _line ) ostr << " ";
|
klao@1120
|
225 |
if( _line ) ostr << "at line " << _line;
|
klao@1067
|
226 |
ostr << ")";
|
klao@1056
|
227 |
}
|
klao@1056
|
228 |
mes = ostr.str().c_str();
|
klao@1056
|
229 |
}
|
klao@1056
|
230 |
catch(...) {}
|
klao@1056
|
231 |
if( mes ) return mes;
|
klao@1120
|
232 |
return exceptionName();
|
klao@1120
|
233 |
}
|
klao@1120
|
234 |
|
klao@1120
|
235 |
virtual const char* exceptionName() const {
|
klao@1056
|
236 |
return "lemon::DataFormatError";
|
klao@1056
|
237 |
}
|
klao@1067
|
238 |
|
klao@1067
|
239 |
virtual ~DataFormatError() throw() {}
|
alpar@883
|
240 |
};
|
alpar@883
|
241 |
|
klao@1056
|
242 |
|
klao@1068
|
243 |
///\e
|
klao@1067
|
244 |
class AssertionFailedError : public LogicError {
|
klao@1067
|
245 |
protected:
|
klao@1067
|
246 |
const char *assertion;
|
klao@1067
|
247 |
const char *file;
|
klao@1067
|
248 |
int line;
|
klao@1067
|
249 |
const char *function;
|
klao@1067
|
250 |
const char *message;
|
klao@1067
|
251 |
public:
|
klao@1067
|
252 |
///\e
|
klao@1067
|
253 |
AssertionFailedError(const char *_file, int _line, const char *func,
|
klao@1067
|
254 |
const char *msg, const char *_assertion = 0) :
|
klao@1067
|
255 |
assertion(_assertion), file(_file), line(_line), function(func),
|
klao@1067
|
256 |
message(msg) {}
|
klao@1067
|
257 |
|
klao@1067
|
258 |
///\e
|
klao@1067
|
259 |
const char* get_assertion() const { return assertion; }
|
klao@1067
|
260 |
///\e
|
klao@1067
|
261 |
const char* get_message() const { return message; }
|
klao@1067
|
262 |
///\e
|
klao@1067
|
263 |
const char* get_file() const { return file; }
|
klao@1067
|
264 |
///\e
|
klao@1067
|
265 |
const char* get_function() const { return function; }
|
klao@1067
|
266 |
///\e
|
klao@1067
|
267 |
int get_line() const { return line; }
|
klao@1067
|
268 |
|
klao@1067
|
269 |
|
klao@1067
|
270 |
virtual const char* what() const throw() {
|
klao@1067
|
271 |
const char *mes = 0;
|
klao@1067
|
272 |
try {
|
klao@1067
|
273 |
std::ostringstream ostr;
|
klao@1067
|
274 |
ostr << file << ":" << line << ": ";
|
klao@1067
|
275 |
if( function )
|
klao@1067
|
276 |
ostr << function << ": ";
|
klao@1067
|
277 |
ostr << message;
|
klao@1067
|
278 |
if( assertion )
|
klao@1067
|
279 |
ostr << " (assertion '" << assertion << "' failed)";
|
klao@1067
|
280 |
mes = ostr.str().c_str();
|
klao@1067
|
281 |
}
|
klao@1067
|
282 |
catch(...) {}
|
klao@1067
|
283 |
if( mes ) return mes;
|
klao@1120
|
284 |
return exceptionName();
|
klao@1120
|
285 |
}
|
klao@1120
|
286 |
|
klao@1120
|
287 |
virtual const char* exceptionName() const {
|
klao@1067
|
288 |
return "lemon::AssertionFailedError";
|
klao@1067
|
289 |
}
|
klao@1067
|
290 |
|
klao@1067
|
291 |
virtual ~AssertionFailedError() throw() {}
|
klao@1067
|
292 |
};
|
klao@1067
|
293 |
|
klao@1056
|
294 |
|
klao@1056
|
295 |
/**************** Macros ****************/
|
klao@1056
|
296 |
|
klao@1056
|
297 |
|
klao@1067
|
298 |
inline
|
klao@1067
|
299 |
void assert_fail(const char *file, int line, const char *func,
|
klao@1067
|
300 |
const char *message, const char *assertion = 0,
|
klao@1067
|
301 |
bool do_abort=true)
|
klao@1067
|
302 |
{
|
klao@1067
|
303 |
using namespace std;
|
klao@1067
|
304 |
cerr << file << ":" << line << ": ";
|
klao@1067
|
305 |
if( func )
|
klao@1067
|
306 |
cerr << func << ": ";
|
klao@1067
|
307 |
cerr << message;
|
klao@1067
|
308 |
if( assertion )
|
klao@1067
|
309 |
cerr << " (assertion '" << assertion << "' failed)";
|
klao@1067
|
310 |
cerr << endl;
|
klao@1067
|
311 |
if(do_abort)
|
klao@1067
|
312 |
abort();
|
klao@1067
|
313 |
}
|
klao@1056
|
314 |
|
klao@1067
|
315 |
inline
|
klao@1067
|
316 |
void assert_fail_throw(const char *file, int line, const char *func,
|
klao@1067
|
317 |
const char *message, const char *assertion = 0,
|
klao@1067
|
318 |
bool = true)
|
klao@1067
|
319 |
{
|
klao@1067
|
320 |
throw AssertionFailedError(file, line, func, message, assertion);
|
klao@1067
|
321 |
}
|
klao@1056
|
322 |
|
alpar@883
|
323 |
|
alpar@883
|
324 |
}
|
alpar@921
|
325 |
#endif // LEMON_ERROR_H
|
klao@1067
|
326 |
|
klao@1067
|
327 |
#undef LEMON_ASSERT
|
klao@1067
|
328 |
#undef LEMON_FIXME
|
klao@1067
|
329 |
|
klao@1067
|
330 |
#ifndef LEMON_ASSERT_ABORT
|
klao@1067
|
331 |
# define LEMON_ASSERT_ABORT 1
|
klao@1067
|
332 |
#endif
|
klao@1067
|
333 |
|
klao@1067
|
334 |
#ifndef LEMON_ASSERT_HANDLER
|
klao@1120
|
335 |
# ifdef LEMON_ASSERT_EXCEPTION
|
klao@1120
|
336 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw
|
klao@1120
|
337 |
# else
|
klao@1120
|
338 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail
|
klao@1120
|
339 |
# endif
|
klao@1067
|
340 |
#endif
|
klao@1067
|
341 |
|
klao@1067
|
342 |
#if defined(NDEBUG) || defined(LEMON_DISABLE_ASSERTS)
|
klao@1067
|
343 |
|
klao@1067
|
344 |
# define LEMON_ASSERT(exp, msg) (static_cast<void> (0))
|
klao@1067
|
345 |
|
klao@1067
|
346 |
#else
|
klao@1067
|
347 |
|
klao@1067
|
348 |
/**
|
klao@1067
|
349 |
* \brief Macro for assertions with customizable message
|
klao@1067
|
350 |
*
|
klao@1120
|
351 |
* Macro for assertions with customizable message.
|
klao@1120
|
352 |
*
|
klao@1120
|
353 |
* The behaviour can be customized with LEMON_ASSERT_HANDLER,
|
klao@1120
|
354 |
* LEMON_ASSERT_EXCEPTION and LEMON_ASSERT_ABORT defines. Asserts can be
|
klao@1120
|
355 |
* disabled by defining either NDEBUG or LEMON_DISABLE_ASSERTS macros.
|
klao@1120
|
356 |
*
|
klao@1120
|
357 |
* \todo We should provide some way to reset to the default behaviour,
|
klao@1120
|
358 |
* shouldn't we?
|
klao@1120
|
359 |
*
|
klao@1120
|
360 |
* \todo This whole 'assert' business should be placed in a separate
|
klao@1120
|
361 |
* include file.
|
klao@1120
|
362 |
*
|
klao@1067
|
363 |
* \todo __PRETTY_FUNCTION__ should be replaced by something
|
klao@1067
|
364 |
* compiler-independant, like BOOST_CURRENT_FUNCTION
|
klao@1067
|
365 |
*/
|
klao@1067
|
366 |
|
klao@1067
|
367 |
# define LEMON_ASSERT(exp, msg) \
|
klao@1067
|
368 |
(static_cast<void> (!!(exp) ? 0 : ( \
|
klao@1067
|
369 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
|
klao@1067
|
370 |
__PRETTY_FUNCTION__, \
|
klao@1067
|
371 |
(msg), #exp, LEMON_ASSERT_ABORT), 0)))
|
klao@1067
|
372 |
|
klao@1120
|
373 |
#endif // NDEBUG || LEMON_DISABLE_ASSERTS
|
klao@1067
|
374 |
|
klao@1067
|
375 |
/**
|
klao@1067
|
376 |
* \brief Macro for mark not yet implemented features.
|
klao@1067
|
377 |
*
|
klao@1067
|
378 |
* \todo Is this the right place for this? It should be used only in
|
klao@1067
|
379 |
* modules under development.
|
klao@1067
|
380 |
*
|
klao@1067
|
381 |
* \todo __PRETTY_FUNCTION__ should be replaced by something
|
klao@1067
|
382 |
* compiler-independant, like BOOST_CURRENT_FUNCTION
|
klao@1067
|
383 |
*/
|
klao@1067
|
384 |
|
klao@1067
|
385 |
# define LEMON_FIXME(msg) \
|
klao@1067
|
386 |
(LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \
|
klao@1067
|
387 |
"FIXME: " msg))
|