3 * src/lemon/error.h - Part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi
6 * Kutatocsoport (Egervary Research Group on Combinatorial Optimization,
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
22 //! \ingroup exceptions
24 //! \brief Basic exception classes and error handling.
35 /// \addtogroup exceptions
38 /// \brief Exception safe wrapper class.
40 /// Exception safe wrapper class to implement the members of exceptions.
41 template <typename _Type>
42 class ExceptionMember {
46 ExceptionMember() throw () {
48 ptr.reset(new Type());
52 ExceptionMember(const Type& type) throw () {
54 ptr.reset(new Type());
55 if (ptr.get() == 0) return;
60 ExceptionMember(const ExceptionMember& copy) throw() {
62 if (!copy.valid()) return;
63 ptr.reset(new Type());
64 if (ptr.get() == 0) return;
69 ExceptionMember& operator=(const ExceptionMember& copy) {
70 if (ptr.get() == 0) return;
72 if (!copy.valid()) return;
77 void set(const Type& type) {
78 if (ptr.get() == 0) return;
84 const Type& get() const {
89 return ptr.get() != 0;
93 std::auto_ptr<_Type> ptr;
96 /// Exception-safe convenient "error message" class.
98 /// Helper class which provides a convenient ostream-like (operator <<
99 /// based) interface to create a string message. Mostly useful in
100 /// exception classes (therefore the name).
104 ///\todo The good solution is boost:shared_ptr...
106 std::auto_ptr<std::ostringstream> buf;
109 bool init() throw() {
111 buf.reset(new std::ostringstream);
122 ErrorMessage() throw() { init(); }
124 ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }
127 ErrorMessage(const char *message) throw() {
133 ErrorMessage(const std::string &message) throw() {
139 template <typename T>
140 ErrorMessage& operator<<(const T &t) throw() {
141 if( ! buf.get() ) return *this;
153 const char* message() throw() {
154 if( ! buf.get() ) return 0;
158 mes = buf->str().c_str();
167 * \brief Generic exception class.
169 * Base class for exceptions used in LEMON.
171 class Exception : public std::exception {
176 virtual ~Exception() throw() {}
179 virtual const char* exceptionName() const {
180 return "lemon::Exception";
184 virtual const char* what() const throw() {
185 return exceptionName();
190 * \brief One of the two main subclasses of \ref Exception.
192 * Logic errors represent problems in the internal logic of a program;
193 * in theory, these are preventable, and even detectable before the
194 * program runs (e.g., violations of class invariants).
196 * A typical example for this is \ref UninitializedParameter.
198 class LogicError : public Exception {
200 virtual const char* exceptionName() const {
201 return "lemon::LogicError";
206 * \brief \ref Exception for uninitialized parameters.
208 * This error represents problems in the initialization
209 * of the parameters of the algorithms.
211 class UninitializedParameter : public LogicError {
213 virtual const char* exceptionName() const {
214 return "lemon::UninitializedParameter";
220 * \brief One of the two main subclasses of \ref Exception.
222 * Runtime errors represent problems outside the scope of a program;
223 * they cannot be easily predicted and can generally only be caught as
224 * the program executes.
226 class RuntimeError : public Exception {
228 virtual const char* exceptionName() const {
229 return "lemon::RuntimeError";
234 class RangeError : public RuntimeError {
236 virtual const char* exceptionName() const {
237 return "lemon::RangeError";
242 class IOError : public RuntimeError {
244 virtual const char* exceptionName() const {
245 return "lemon::IOError";
250 class DataFormatError : public IOError {
252 ExceptionMember<std::string> _message;
253 ExceptionMember<std::string> _file;
256 mutable ExceptionMember<std::string> _message_holder;
259 DataFormatError(const DataFormatError &dfe) :
260 IOError(dfe), _message(dfe._message), _file(dfe._file),
264 explicit DataFormatError(const char *the_message)
265 : _message(the_message), _line(0) {}
268 DataFormatError(const std::string &file_name, int line_num,
269 const char *the_message)
270 : _message(the_message), _line(line_num) { file(file_name); }
273 void line(int line) { _line = line; }
275 void message(const std::string& message) { _message.set(message); }
277 void file(const std::string &file) { _file.set(file); }
280 int line() const { return _line; }
282 const char* message() const {
283 if (_message.valid() && !_message.get().empty()) {
284 return _message.get().c_str();
290 /// \brief Returns the filename.
292 /// Returns \e null if the filename was not specified.
293 const char* file() const {
294 if (_file.valid() && !_file.get().empty()) {
295 return _file.get().c_str();
302 virtual const char* what() const throw() {
304 std::ostringstream ostr;
305 ostr << exceptionName() << ": ";
306 if (message()) ostr << message();
307 if( file() || line() != 0 ) {
309 if( file() ) ostr << "in file '" << file() << "'";
310 if( file() && line() != 0 ) ostr << " ";
311 if( line() != 0 ) ostr << "at line " << line();
314 _message_holder.set(ostr.str());
317 if( _message_holder.valid()) return _message_holder.get().c_str();
318 return exceptionName();
321 virtual const char* exceptionName() const {
322 return "lemon::DataFormatError";
325 virtual ~DataFormatError() throw() {}
328 class IOParameterError : public LogicError {
330 ExceptionMember<std::string> _message;
331 ExceptionMember<std::string> _file;
333 mutable ExceptionMember<std::string> _message_holder;
336 IOParameterError(const IOParameterError &ile) :
337 LogicError(ile), _message(ile._message), _file(ile._file) {}
340 explicit IOParameterError(const char *the_message)
341 : _message(the_message) {}
344 IOParameterError(const char *file_name, const char *the_message)
345 : _message(the_message), _file(file_name) {}
348 void message(const std::string& message) { _message.set(message); }
350 void file(const std::string &file) { _file.set(file); }
353 const char* message() const {
354 if (_message.valid()) {
355 return _message.get().c_str();
361 /// \brief Returns the filename.
363 /// Returns \e null if the filename was not specified.
364 const char* file() const {
366 return _file.get().c_str();
373 virtual const char* what() const throw() {
375 std::ostringstream ostr;
376 if (message()) ostr << message();
377 if (file()) ostr << "(when reading file '" << file() << "')";
378 _message_holder.set(ostr.str());
381 if( _message_holder.valid() ) return _message_holder.get().c_str();
382 return exceptionName();
385 virtual const char* exceptionName() const {
386 return "lemon::IOParameterError";
389 virtual ~IOParameterError() throw() {}
394 class AssertionFailedError : public LogicError {
396 const char *assertion;
399 const char *function;
402 mutable ExceptionMember<std::string> _message_holder;
405 AssertionFailedError(const char *_file, int _line, const char *func,
406 const char *msg, const char *_assertion = 0) :
407 assertion(_assertion), file(_file), line(_line), function(func),
411 const char* get_assertion() const { return assertion; }
413 const char* get_message() const { return message; }
415 const char* get_file() const { return file; }
417 const char* get_function() const { return function; }
419 int get_line() const { return line; }
422 virtual const char* what() const throw() {
424 std::ostringstream ostr;
425 ostr << file << ":" << line << ": ";
427 ostr << function << ": ";
430 ostr << " (assertion '" << assertion << "' failed)";
431 _message_holder.set(ostr.str());
432 return ostr.str().c_str();
435 if( _message_holder.valid() ) return _message_holder.get().c_str();
436 return exceptionName();
439 virtual const char* exceptionName() const {
440 return "lemon::AssertionFailedError";
443 virtual ~AssertionFailedError() throw() {}
447 /**************** Macros ****************/
451 void assert_fail(const char *file, int line, const char *func,
452 const char *message, const char *assertion = 0,
456 cerr << file << ":" << line << ": ";
458 cerr << func << ": ";
461 cerr << " (assertion '" << assertion << "' failed)";
468 void assert_fail_throw(const char *file, int line, const char *func,
469 const char *message, const char *assertion = 0,
472 throw AssertionFailedError(file, line, func, message, assertion);
478 #endif // LEMON_ERROR_H
483 #ifndef LEMON_ASSERT_ABORT
484 # define LEMON_ASSERT_ABORT 1
487 #ifndef LEMON_ASSERT_HANDLER
488 # ifdef LEMON_ASSERT_EXCEPTION
489 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw
491 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail
495 #if defined(NDEBUG) || defined(LEMON_DISABLE_ASSERTS)
497 # define LEMON_ASSERT(exp, msg) (static_cast<void> (0))
502 * \brief Macro for assertions with customizable message
504 * Macro for assertions with customizable message.
506 * The behaviour can be customized with LEMON_ASSERT_HANDLER,
507 * LEMON_ASSERT_EXCEPTION and LEMON_ASSERT_ABORT defines. Asserts can be
508 * disabled by defining either NDEBUG or LEMON_DISABLE_ASSERTS macros.
510 * \todo We should provide some way to reset to the default behaviour,
513 * \todo This whole 'assert' business should be placed in a separate
516 * \todo __PRETTY_FUNCTION__ should be replaced by something
517 * compiler-independent, like BOOST_CURRENT_FUNCTION
520 # define LEMON_ASSERT(exp, msg) \
521 (static_cast<void> (!!(exp) ? 0 : ( \
522 LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
523 __PRETTY_FUNCTION__, \
524 (msg), #exp, LEMON_ASSERT_ABORT), 0)))
526 #endif // NDEBUG || LEMON_DISABLE_ASSERTS
529 * \brief Macro for mark not yet implemented features.
531 * \todo Is this the right place for this? It should be used only in
532 * modules under development.
534 * \todo __PRETTY_FUNCTION__ should be replaced by something
535 * compiler-independent, like BOOST_CURRENT_FUNCTION
538 # define LEMON_FIXME(msg) \
539 (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \