More exact concept checking for map concepts.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
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) throw() {
70 if (ptr.get() == 0) return;
72 if (!copy.valid()) return;
77 void set(const Type& type) throw() {
78 if (ptr.get() == 0) return;
84 const Type& get() const {
88 bool valid() const throw() {
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...
107 mutable std::auto_ptr<std::ostringstream> buf;
110 bool init() throw() {
112 buf.reset(new std::ostringstream);
123 ErrorMessage() throw() { init(); }
125 ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }
128 ErrorMessage(const char *msg) throw() {
134 ErrorMessage(const std::string &msg) throw() {
140 template <typename T>
141 ErrorMessage& operator<<(const T &t) throw() {
142 if( ! buf.get() ) return *this;
154 const char* message() throw() {
155 if( ! buf.get() ) return 0;
159 mes = buf->str().c_str();
167 /// Generic exception class.
169 /// Base class for exceptions used in LEMON.
171 class Exception : public std::exception {
176 virtual ~Exception() throw() {}
178 virtual const char* what() const throw() {
179 return "lemon::Exception";
183 /// One of the two main subclasses of \ref Exception.
185 /// Logic errors represent problems in the internal logic of a program;
186 /// in theory, these are preventable, and even detectable before the
187 /// program runs (e.g. violations of class invariants).
189 /// A typical example for this is \ref UninitializedParameter.
190 class LogicError : public Exception {
192 virtual const char* what() const throw() {
193 return "lemon::LogicError";
197 /// \ref Exception for uninitialized parameters.
199 /// This error represents problems in the initialization
200 /// of the parameters of the algorithms.
201 class UninitializedParameter : public LogicError {
203 virtual const char* what() const throw() {
204 return "lemon::UninitializedParameter";
209 /// One of the two main subclasses of \ref Exception.
211 /// Runtime errors represent problems outside the scope of a program;
212 /// they cannot be easily predicted and can generally only be caught
213 /// as the program executes.
214 class RuntimeError : public Exception {
216 virtual const char* what() const throw() {
217 return "lemon::RuntimeError";
222 class RangeError : public RuntimeError {
224 virtual const char* what() const throw() {
225 return "lemon::RangeError";
230 class IoError : public RuntimeError {
232 virtual const char* what() const throw() {
233 return "lemon::IoError";
238 class DataFormatError : public IoError {
240 ExceptionMember<std::string> _message;
241 ExceptionMember<std::string> _file;
244 mutable ExceptionMember<std::string> _message_holder;
247 DataFormatError(const DataFormatError &dfe) :
248 IoError(dfe), _message(dfe._message), _file(dfe._file),
252 explicit DataFormatError(const char *the_message)
253 : _message(the_message), _line(0) {}
256 DataFormatError(const std::string &file_name, int line_num,
257 const char *the_message)
258 : _message(the_message), _line(line_num) { file(file_name); }
261 void line(int ln) { _line = ln; }
263 void message(const std::string& msg) { _message.set(msg); }
265 void file(const std::string &fl) { _file.set(fl); }
268 int line() const { return _line; }
270 const char* message() const {
271 if (_message.valid() && !_message.get().empty()) {
272 return _message.get().c_str();
278 /// \brief Returns the filename.
280 /// Returns \e null if the filename was not specified.
281 const char* file() const {
282 if (_file.valid() && !_file.get().empty()) {
283 return _file.get().c_str();
290 virtual const char* what() const throw() {
292 std::ostringstream ostr;
293 ostr << "lemon:DataFormatError" << ": ";
294 if (message()) ostr << message();
295 if( file() || line() != 0 ) {
297 if( file() ) ostr << "in file '" << file() << "'";
298 if( file() && line() != 0 ) ostr << " ";
299 if( line() != 0 ) ostr << "at line " << line();
302 _message_holder.set(ostr.str());
305 if( _message_holder.valid()) return _message_holder.get().c_str();
306 return "lemon:DataFormatError";
309 virtual ~DataFormatError() throw() {}
313 class FileOpenError : public IoError {
315 ExceptionMember<std::string> _file;
317 mutable ExceptionMember<std::string> _message_holder;
320 FileOpenError(const FileOpenError &foe) :
321 IoError(foe), _file(foe._file) {}
324 explicit FileOpenError(const std::string& fl)
329 void file(const std::string &fl) { _file.set(fl); }
331 /// \brief Returns the filename.
333 /// Returns \e null if the filename was not specified.
334 const char* file() const {
335 if (_file.valid() && !_file.get().empty()) {
336 return _file.get().c_str();
343 virtual const char* what() const throw() {
345 std::ostringstream ostr;
346 ostr << "lemon::FileOpenError" << ": ";
347 ostr << "Cannot open file - " << file();
348 _message_holder.set(ostr.str());
351 if( _message_holder.valid()) return _message_holder.get().c_str();
352 return "lemon::FileOpenError";
354 virtual ~FileOpenError() throw() {}
357 class IoParameterError : public IoError {
359 ExceptionMember<std::string> _message;
360 ExceptionMember<std::string> _file;
362 mutable ExceptionMember<std::string> _message_holder;
365 IoParameterError(const IoParameterError &ile) :
366 IoError(ile), _message(ile._message), _file(ile._file) {}
369 explicit IoParameterError(const char *the_message)
370 : _message(the_message) {}
373 IoParameterError(const char *file_name, const char *the_message)
374 : _message(the_message), _file(file_name) {}
377 void message(const std::string& msg) { _message.set(msg); }
379 void file(const std::string &fl) { _file.set(fl); }
382 const char* message() const {
383 if (_message.valid()) {
384 return _message.get().c_str();
390 /// \brief Returns the filename.
392 /// Returns \c 0 if the filename was not specified.
393 const char* file() const {
395 return _file.get().c_str();
402 virtual const char* what() const throw() {
404 std::ostringstream ostr;
405 if (message()) ostr << message();
406 if (file()) ostr << "(when reading file '" << file() << "')";
407 _message_holder.set(ostr.str());
410 if( _message_holder.valid() ) return _message_holder.get().c_str();
411 return "lemon:IoParameterError";
413 virtual ~IoParameterError() throw() {}
418 class AssertionFailedError : public LogicError {
420 const char *assertion;
423 const char *function;
426 mutable ExceptionMember<std::string> _message_holder;
429 AssertionFailedError(const char *_file, int _line, const char *func,
430 const char *msg, const char *_assertion = 0) :
431 assertion(_assertion), file(_file), line(_line), function(func),
435 const char* get_assertion() const { return assertion; }
437 const char* get_message() const { return message; }
439 const char* get_file() const { return file; }
441 const char* get_function() const { return function; }
443 int get_line() const { return line; }
446 virtual const char* what() const throw() {
448 std::ostringstream ostr;
449 ostr << file << ":" << line << ": ";
451 ostr << function << ": ";
454 ostr << " (assertion '" << assertion << "' failed)";
455 _message_holder.set(ostr.str());
456 return ostr.str().c_str();
459 if( _message_holder.valid() ) return _message_holder.get().c_str();
460 return "lemon::AssertionFailedError";
462 virtual ~AssertionFailedError() throw() {}
466 /**************** Macros ****************/
469 template <typename Exception>
470 inline void assert_fail(const char *file, int line,
473 const char *assertion = 0,
477 cerr << file << ":" << line << ": ";
479 cerr << func << ": ";
480 cerr << exception.what();
482 cerr << " (assertion '" << assertion << "' failed)";
489 inline void assert_fail<const char *>(const char *file, int line,
492 const char *assertion,
496 cerr << file << ":" << line << ": ";
498 cerr << func << ": ";
501 cerr << " (assertion '" << assertion << "' failed)";
508 inline void assert_fail<std::string>(const char *file, int line,
511 const char *assertion,
514 assert_fail(file, line, func, message.c_str(), assertion, do_abort);
517 template <typename Exception>
518 inline void assert_fail_failure(const char *file, int line, const char *func,
520 const char *assertion = 0,
523 throw AssertionFailedError(file, line, func, exception.what(), assertion);
527 inline void assert_fail_failure<const char *>(const char *file, int line,
530 const char *assertion,
533 throw AssertionFailedError(file, line, func, message, assertion);
537 inline void assert_fail_failure<std::string>(const char *file, int line,
540 const char *assertion,
543 assert_fail_failure(file, line, func, message.c_str(), assertion, true);
546 template <typename Exception>
547 inline void assert_fail_exception(const char *file, int line, const char *func,
549 const char *assertion = 0, bool = true)
555 inline void assert_fail_exception<const char *>(const char *file, int line,
558 const char *assertion,
561 throw AssertionFailedError(file, line, func, message, assertion);
565 inline void assert_fail_exception<std::string>(const char *file, int line,
568 const char *assertion,
571 assert_fail_exception(file, line, func, message.c_str(), assertion, true);
577 #endif // LEMON_ERROR_H
582 #ifdef LEMON_ENABLE_ASSERTS
583 # define LEMON_ASSERT_ABORT
586 #ifndef LEMON_ASSERT_DO_ABORT
587 # define LEMON_ASSERT_DO_ABORT 1
590 #ifndef LEMON_ASSERT_HANDLER
591 # if defined LEMON_ASSERT_EXCEPTION
592 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail_exception
593 # elif defined LEMON_ASSERT_FAILURE
594 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail_failure
595 # elif defined LEMON_ASSERT_ABORT
596 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail
598 # define LEMON_DISABLE_ASSERTS
604 /// \brief Macro for assertions with customizable message
606 /// Macro for assertions with customizable message.
608 /// The assertions are disabled in the default behaviour. You can
609 /// enable the assertions with the
611 /// #define LEMON_ENABLE_ASSERTS
614 /// provides a log on the standard error about the assertion and aborts
615 /// the program if LEMON_ASSERT_DO_ABORT is also defined (otherwise the
616 /// program keeps on running).
617 /// By defining LEMON_ASSERT_FAILURE or
618 /// LEMON_ASSERT_EXCEPTION, you can set other behaviour to the
619 /// assertions. In case LEMON_ASSERT_FAILURE is given, LEMON_ASSERT
620 /// will always throw an \c AssertionFailedError exception with
621 /// the \c msg error message. By using
622 /// LEMON_ASSERT_EXCEPTION, one can define an arbitrary exception to be thrown.
624 /// The LEMON_ASSERT macro should be called with the \c exp parameter
625 /// which should be an expression convertible to bool. If the given
626 /// parameter is false the assertion is raised and one of the assertion
627 /// behaviour will be activated. The \c msg should be either a const
628 /// char* message or an exception. When the \c msg is an exception the
629 /// \ref lemon::Exception::what() "what()" function is called to retrieve and
630 /// display the error message.
632 /// \todo We should provide some way to reset to the default behaviour,
635 /// \todo This whole 'assert' business should be placed in a separate
636 /// include file. The boost assert is not guarded by header sentries
637 /// which may help to change the behaviour of the assertions in
640 /// \todo __PRETTY_FUNCTION__ should be replaced by something
641 /// compiler-independent, like BOOST_CURRENT_FUNCTION
643 # define LEMON_ASSERT(exp, msg) \
644 (static_cast<void> (!!(exp) ? 0 : ( \
645 LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
646 __PRETTY_FUNCTION__, \
647 msg, #exp, LEMON_ASSERT_DO_ABORT), 0)))
650 # if defined LEMON_DISABLE_ASSERTS
652 # define LEMON_ASSERT(exp, msg) (static_cast<void> (0))
655 # define LEMON_ASSERT(exp, msg) \
656 (static_cast<void> (!!(exp) ? 0 : ( \
657 LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
658 __PRETTY_FUNCTION__, \
659 msg, #exp, LEMON_ASSERT_DO_ABORT), 0)))
664 * \brief Macro for mark not yet implemented features.
666 * \todo Is this the right place for this? It should be used only in
667 * modules under development.
669 * \todo __PRETTY_FUNCTION__ should be replaced by something
670 * compiler-independent, like BOOST_CURRENT_FUNCTION
673 #define LEMON_FIXME(msg) \
674 (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \