Tolerance<unsigned int> and Tolerance<unsigned long long int> added.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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).
105 ///\todo The good solution is boost::shared_ptr...
108 std::auto_ptr<std::ostringstream> buf;
111 bool init() throw() {
113 buf.reset(new std::ostringstream);
124 ErrorMessage() throw() { init(); }
126 ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }
129 ErrorMessage(const char *message) throw() {
135 ErrorMessage(const std::string &message) throw() {
141 template <typename T>
142 ErrorMessage& operator<<(const T &t) throw() {
143 if( ! buf.get() ) return *this;
155 const char* message() throw() {
156 if( ! buf.get() ) return 0;
160 mes = buf->str().c_str();
169 * \brief Generic exception class.
171 * Base class for exceptions used in LEMON.
173 class Exception : public std::exception {
178 virtual ~Exception() throw() {}
180 virtual const char* what() const throw() {
181 return "lemon::Exception";
186 * \brief One of the two main subclasses of \ref Exception.
188 * Logic errors represent problems in the internal logic of a program;
189 * in theory, these are preventable, and even detectable before the
190 * program runs (e.g., violations of class invariants).
192 * A typical example for this is \ref UninitializedParameter.
194 class LogicError : public Exception {
196 virtual const char* what() const throw() {
197 return "lemon::LogicError";
202 * \brief \ref Exception for uninitialized parameters.
204 * This error represents problems in the initialization
205 * of the parameters of the algorithms.
207 class UninitializedParameter : public LogicError {
209 virtual const char* what() const throw() {
210 return "lemon::UninitializedParameter";
216 * \brief One of the two main subclasses of \ref Exception.
218 * Runtime errors represent problems outside the scope of a program;
219 * they cannot be easily predicted and can generally only be caught as
220 * the program executes.
222 class RuntimeError : public Exception {
224 virtual const char* what() const throw() {
225 return "lemon::RuntimeError";
230 class RangeError : public RuntimeError {
232 virtual const char* what() const throw() {
233 return "lemon::RangeError";
238 class IoError : public RuntimeError {
240 virtual const char* what() const throw() {
241 return "lemon::IoError";
246 class DataFormatError : public IoError {
248 ExceptionMember<std::string> _message;
249 ExceptionMember<std::string> _file;
252 mutable ExceptionMember<std::string> _message_holder;
255 DataFormatError(const DataFormatError &dfe) :
256 IoError(dfe), _message(dfe._message), _file(dfe._file),
260 explicit DataFormatError(const char *the_message)
261 : _message(the_message), _line(0) {}
264 DataFormatError(const std::string &file_name, int line_num,
265 const char *the_message)
266 : _message(the_message), _line(line_num) { file(file_name); }
269 void line(int line) { _line = line; }
271 void message(const std::string& message) { _message.set(message); }
273 void file(const std::string &file) { _file.set(file); }
276 int line() const { return _line; }
278 const char* message() const {
279 if (_message.valid() && !_message.get().empty()) {
280 return _message.get().c_str();
286 /// \brief Returns the filename.
288 /// Returns \e null if the filename was not specified.
289 const char* file() const {
290 if (_file.valid() && !_file.get().empty()) {
291 return _file.get().c_str();
298 virtual const char* what() const throw() {
300 std::ostringstream ostr;
301 ostr << "lemon:DataFormatError" << ": ";
302 if (message()) ostr << message();
303 if( file() || line() != 0 ) {
305 if( file() ) ostr << "in file '" << file() << "'";
306 if( file() && line() != 0 ) ostr << " ";
307 if( line() != 0 ) ostr << "at line " << line();
310 _message_holder.set(ostr.str());
313 if( _message_holder.valid()) return _message_holder.get().c_str();
314 return "lemon:DataFormatError";
317 virtual ~DataFormatError() throw() {}
321 class FileOpenError : public IoError {
323 ExceptionMember<std::string> _file;
325 mutable ExceptionMember<std::string> _message_holder;
328 FileOpenError(const FileOpenError &foe) :
329 IoError(foe), _file(foe._file) {}
332 explicit FileOpenError(const std::string& file)
337 void file(const std::string &file) { _file.set(file); }
339 /// \brief Returns the filename.
341 /// Returns \e null if the filename was not specified.
342 const char* file() const {
343 if (_file.valid() && !_file.get().empty()) {
344 return _file.get().c_str();
351 virtual const char* what() const throw() {
353 std::ostringstream ostr;
354 ostr << "lemon::FileOpenError" << ": ";
355 ostr << "Cannot open file - " << file();
356 _message_holder.set(ostr.str());
359 if( _message_holder.valid()) return _message_holder.get().c_str();
360 return "lemon::FileOpenError";
362 virtual ~FileOpenError() throw() {}
365 class IoParameterError : public IoError {
367 ExceptionMember<std::string> _message;
368 ExceptionMember<std::string> _file;
370 mutable ExceptionMember<std::string> _message_holder;
373 IoParameterError(const IoParameterError &ile) :
374 IoError(ile), _message(ile._message), _file(ile._file) {}
377 explicit IoParameterError(const char *the_message)
378 : _message(the_message) {}
381 IoParameterError(const char *file_name, const char *the_message)
382 : _message(the_message), _file(file_name) {}
385 void message(const std::string& message) { _message.set(message); }
387 void file(const std::string &file) { _file.set(file); }
390 const char* message() const {
391 if (_message.valid()) {
392 return _message.get().c_str();
398 /// \brief Returns the filename.
400 /// Returns \e null if the filename was not specified.
401 const char* file() const {
403 return _file.get().c_str();
410 virtual const char* what() const throw() {
412 std::ostringstream ostr;
413 if (message()) ostr << message();
414 if (file()) ostr << "(when reading file '" << file() << "')";
415 _message_holder.set(ostr.str());
418 if( _message_holder.valid() ) return _message_holder.get().c_str();
419 return "lemon:IoParameterError";
421 virtual ~IoParameterError() throw() {}
426 class AssertionFailedError : public LogicError {
428 const char *assertion;
431 const char *function;
434 mutable ExceptionMember<std::string> _message_holder;
437 AssertionFailedError(const char *_file, int _line, const char *func,
438 const char *msg, const char *_assertion = 0) :
439 assertion(_assertion), file(_file), line(_line), function(func),
443 const char* get_assertion() const { return assertion; }
445 const char* get_message() const { return message; }
447 const char* get_file() const { return file; }
449 const char* get_function() const { return function; }
451 int get_line() const { return line; }
454 virtual const char* what() const throw() {
456 std::ostringstream ostr;
457 ostr << file << ":" << line << ": ";
459 ostr << function << ": ";
462 ostr << " (assertion '" << assertion << "' failed)";
463 _message_holder.set(ostr.str());
464 return ostr.str().c_str();
467 if( _message_holder.valid() ) return _message_holder.get().c_str();
468 return "lemon::AssertionFailedError";
470 virtual ~AssertionFailedError() throw() {}
474 /**************** Macros ****************/
477 template <typename Exception>
478 inline void assert_fail(const char *file, int line, const char *func,
479 Exception exception, const char *assertion = 0,
483 cerr << file << ":" << line << ": ";
485 cerr << func << ": ";
486 cerr << exception.what();
488 cerr << " (assertion '" << assertion << "' failed)";
495 inline void assert_fail<const char *>(const char *file, int line,
498 const char *assertion,
502 cerr << file << ":" << line << ": ";
504 cerr << func << ": ";
507 cerr << " (assertion '" << assertion << "' failed)";
513 template <typename Exception>
514 inline void assert_fail_failure(const char *file, int line, const char *func,
516 const char *assertion = 0,
519 throw AssertionFailedError(file, line, func, exception.what(), assertion);
523 inline void assert_fail_failure<const char *>(const char *file, int line,
526 const char *assertion,
529 throw AssertionFailedError(file, line, func, message, assertion);
532 template <typename Exception>
533 inline void assert_fail_exception(const char *file, int line, const char *func,
535 const char *assertion = 0, bool = true)
541 inline void assert_fail_exception<const char *>(const char *file, int line,
544 const char *assertion,
547 throw AssertionFailedError(file, line, func, message, assertion);
553 #endif // LEMON_ERROR_H
558 #ifdef LEMON_ENABLE_ASSERTS
559 # define LEMON_ASSERT_ABORT
562 #ifndef LEMON_ASSERT_DO_ABORT
563 # define LEMON_ASSERT_DO_ABORT 1
566 #ifndef LEMON_ASSERT_HANDLER
567 # if defined LEMON_ASSERT_EXCEPTION
568 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail_exception
569 # elif defined LEMON_ASSERT_FAILURE
570 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail_failure
571 # elif defined LEMON_ASSERT_ABORT
572 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail
574 # define LEMON_DISABLE_ASSERTS
580 /// \brief Macro for assertions with customizable message
582 /// Macro for assertions with customizable message.
584 /// The assertions are disabled in the default behaviour. You can
585 /// enable the assertions with the LEMON_ENABLE_ASSERTS macro what
586 /// provides a log on the standard error about the assertion and aborts
587 /// the program. If the LEMON_ASSERT_DO_ABORT macro is setted to false
588 /// then the just the log message can be seen on the standard error but
589 /// the program is not stopped. With the LEMON_ASSERT_FAILURE and
590 /// LEMON_ASSERT_EXCEPTION macros you can set other behaviour to the
591 /// assertions. The LEMON_ASSERT_FAILURE will always throw an \c
592 /// AssertionFailedError exception with the \c msg error message. The
593 /// \c LEMON_ASSERT_EXCEPTION can throw a user defined exception.
595 /// The LEMON_ASSERT macro should be called with the \c exp parameter
596 /// which should be an expression convertible to bool. If the given
597 /// parameter is false the assertion is raised and one of the assertion
598 /// behaviour will be activated. The \c msg should be either a const
599 /// char* message or an exception. When the \c msg is an exception the
600 /// \ref "Exception::what" what() function is called to retrieve and
601 /// display the error message.
604 /// \todo We should provide some way to reset to the default behaviour,
607 /// \todo This whole 'assert' business should be placed in a separate
608 /// include file. The boost assert is not guarded by header sentries
609 /// which may help to change the behaviour of the assertions in
612 /// \todo __PRETTY_FUNCTION__ should be replaced by something
613 /// compiler-independent, like BOOST_CURRENT_FUNCTION
615 # define LEMON_ASSERT(exp, msg) \
616 (static_cast<void> (!!(exp) ? 0 : ( \
617 LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
618 __PRETTY_FUNCTION__, \
619 msg, #exp, LEMON_ASSERT_DO_ABORT), 0)))
622 # if defined LEMON_DISABLE_ASSERTS
624 # define LEMON_ASSERT(exp, msg) (static_cast<void> (0))
627 # define LEMON_ASSERT(exp, msg) \
628 (static_cast<void> (!!(exp) ? 0 : ( \
629 LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
630 __PRETTY_FUNCTION__, \
631 msg, #exp, LEMON_ASSERT_DO_ABORT), 0)))
636 * \brief Macro for mark not yet implemented features.
638 * \todo Is this the right place for this? It should be used only in
639 * modules under development.
641 * \todo __PRETTY_FUNCTION__ should be replaced by something
642 * compiler-independent, like BOOST_CURRENT_FUNCTION
645 # define LEMON_FIXME(msg) \
646 (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \