Erasing from graph would be possible, if erasing from graph would be possible... (Afternoon has become timewasting...)
2 * lemon/error.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi
5 * Kutatocsoport (Egervary Research Group on Combinatorial Optimization,
8 * Permission to use, modify and distribute this software is granted
9 * provided that this copyright notice appears in all copies. For
10 * precise terms see the accompanying LICENSE file.
12 * This software is provided "AS IS" with no warranty of any kind,
13 * express or implied, and with no claim as to its suitability for any
21 //! \ingroup exceptions
23 //! \brief Basic exception classes and error handling.
34 /// \addtogroup exceptions
37 /// \brief Exception safe wrapper class.
39 /// Exception safe wrapper class to implement the members of exceptions.
40 template <typename _Type>
41 class ExceptionMember {
45 ExceptionMember() throw () {
47 ptr.reset(new Type());
51 ExceptionMember(const Type& type) throw () {
53 ptr.reset(new Type());
54 if (ptr.get() == 0) return;
59 ExceptionMember(const ExceptionMember& copy) throw() {
61 if (!copy.valid()) return;
62 ptr.reset(new Type());
63 if (ptr.get() == 0) return;
68 ExceptionMember& operator=(const ExceptionMember& copy) {
69 if (ptr.get() == 0) return;
71 if (!copy.valid()) return;
76 void set(const Type& type) {
77 if (ptr.get() == 0) return;
83 const Type& get() const {
88 return ptr.get() != 0;
92 std::auto_ptr<_Type> ptr;
95 /// Exception-safe convenient "error message" class.
97 /// Helper class which provides a convenient ostream-like (operator <<
98 /// based) interface to create a string message. Mostly useful in
99 /// exception classes (therefore the name).
103 ///\todo The good solution is boost:shared_ptr...
105 std::auto_ptr<std::ostringstream> buf;
108 bool init() throw() {
110 buf.reset(new std::ostringstream);
121 ErrorMessage() throw() { init(); }
123 ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }
126 ErrorMessage(const char *message) throw() {
132 ErrorMessage(const std::string &message) throw() {
138 template <typename T>
139 ErrorMessage& operator<<(const T &t) throw() {
140 if( ! buf.get() ) return *this;
152 const char* message() throw() {
153 if( ! buf.get() ) return 0;
157 mes = buf->str().c_str();
166 * \brief Generic exception class.
168 * Base class for exceptions used in LEMON.
170 class Exception : public std::exception {
175 virtual ~Exception() throw() {}
178 virtual const char* exceptionName() const {
179 return "lemon::Exception";
183 virtual const char* what() const throw() {
184 return exceptionName();
189 * \brief One of the two main subclasses of \ref Exception.
191 * Logic errors represent problems in the internal logic of a program;
192 * in theory, these are preventable, and even detectable before the
193 * program runs (e.g., violations of class invariants).
195 * A typical example for this is \ref UninitializedParameter.
197 class LogicError : public Exception {
199 virtual const char* exceptionName() const {
200 return "lemon::LogicError";
205 * \brief \ref Exception for uninitialized parameters.
207 * This error represents problems in the initialization
208 * of the parameters of the algorithms.
210 class UninitializedParameter : public LogicError {
212 virtual const char* exceptionName() const {
213 return "lemon::UninitializedParameter";
219 * \brief One of the two main subclasses of \ref Exception.
221 * Runtime errors represent problems outside the scope of a program;
222 * they cannot be easily predicted and can generally only be caught as
223 * the program executes.
225 class RuntimeError : public Exception {
227 virtual const char* exceptionName() const {
228 return "lemon::RuntimeError";
233 class RangeError : public RuntimeError {
235 virtual const char* exceptionName() const {
236 return "lemon::RangeError";
241 class IOError : public RuntimeError {
243 virtual const char* exceptionName() const {
244 return "lemon::IOError";
249 class DataFormatError : public IOError {
251 ExceptionMember<std::string> _message;
252 ExceptionMember<std::string> _file;
255 mutable ExceptionMember<std::string> _message_holder;
258 DataFormatError(const DataFormatError &dfe) :
259 IOError(dfe), _message(dfe._message), _file(dfe._file),
263 explicit DataFormatError(const char *the_message)
264 : _message(the_message), _line(0) {}
267 DataFormatError(const std::string &file_name, int line_num,
268 const char *the_message)
269 : _message(the_message), _line(line_num) { file(file_name); }
272 void line(int line) { _line = line; }
274 void message(const std::string& message) { _message.set(message); }
276 void file(const std::string &file) { _file.set(file); }
279 int line() const { return _line; }
281 const char* message() const {
282 if (_message.valid() && !_message.get().empty()) {
283 return _message.get().c_str();
289 /// \brief Returns the filename.
291 /// Returns \e null if the filename was not specified.
292 const char* file() const {
293 if (_file.valid() && !_file.get().empty()) {
294 return _file.get().c_str();
301 virtual const char* what() const throw() {
303 std::ostringstream ostr;
304 ostr << exceptionName() << ": ";
305 if (message()) ostr << message();
306 if( file() || line() != 0 ) {
308 if( file() ) ostr << "in file '" << file() << "'";
309 if( file() && line() != 0 ) ostr << " ";
310 if( line() != 0 ) ostr << "at line " << line();
313 _message_holder.set(ostr.str());
316 if( _message_holder.valid()) return _message_holder.get().c_str();
317 return exceptionName();
320 virtual const char* exceptionName() const {
321 return "lemon::DataFormatError";
324 virtual ~DataFormatError() throw() {}
327 class IOParameterError : public LogicError {
329 ExceptionMember<std::string> _message;
330 ExceptionMember<std::string> _file;
332 mutable ExceptionMember<std::string> _message_holder;
335 IOParameterError(const IOParameterError &ile) :
336 LogicError(ile), _message(ile._message), _file(ile._file) {}
339 explicit IOParameterError(const char *the_message)
340 : _message(the_message) {}
343 IOParameterError(const char *file_name, const char *the_message)
344 : _message(the_message), _file(file_name) {}
347 void message(const std::string& message) { _message.set(message); }
349 void file(const std::string &file) { _file.set(file); }
352 const char* message() const {
353 if (_message.valid()) {
354 return _message.get().c_str();
360 /// \brief Returns the filename.
362 /// Returns \e null if the filename was not specified.
363 const char* file() const {
365 return _file.get().c_str();
372 virtual const char* what() const throw() {
374 std::ostringstream ostr;
375 if (message()) ostr << message();
376 if (file()) ostr << "(when reading file '" << file() << "')";
377 _message_holder.set(ostr.str());
380 if( _message_holder.valid() ) return _message_holder.get().c_str();
381 return exceptionName();
384 virtual const char* exceptionName() const {
385 return "lemon::IOParameterError";
388 virtual ~IOParameterError() throw() {}
393 class AssertionFailedError : public LogicError {
395 const char *assertion;
398 const char *function;
401 mutable ExceptionMember<std::string> _message_holder;
404 AssertionFailedError(const char *_file, int _line, const char *func,
405 const char *msg, const char *_assertion = 0) :
406 assertion(_assertion), file(_file), line(_line), function(func),
410 const char* get_assertion() const { return assertion; }
412 const char* get_message() const { return message; }
414 const char* get_file() const { return file; }
416 const char* get_function() const { return function; }
418 int get_line() const { return line; }
421 virtual const char* what() const throw() {
423 std::ostringstream ostr;
424 ostr << file << ":" << line << ": ";
426 ostr << function << ": ";
429 ostr << " (assertion '" << assertion << "' failed)";
430 _message_holder.set(ostr.str());
431 return ostr.str().c_str();
434 if( _message_holder.valid() ) return _message_holder.get().c_str();
435 return exceptionName();
438 virtual const char* exceptionName() const {
439 return "lemon::AssertionFailedError";
442 virtual ~AssertionFailedError() throw() {}
446 /**************** Macros ****************/
450 void assert_fail(const char *file, int line, const char *func,
451 const char *message, const char *assertion = 0,
455 cerr << file << ":" << line << ": ";
457 cerr << func << ": ";
460 cerr << " (assertion '" << assertion << "' failed)";
467 void assert_fail_throw(const char *file, int line, const char *func,
468 const char *message, const char *assertion = 0,
471 throw AssertionFailedError(file, line, func, message, assertion);
477 #endif // LEMON_ERROR_H
482 #ifndef LEMON_ASSERT_ABORT
483 # define LEMON_ASSERT_ABORT 1
486 #ifndef LEMON_ASSERT_HANDLER
487 # ifdef LEMON_ASSERT_EXCEPTION
488 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw
490 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail
494 #if defined(NDEBUG) || defined(LEMON_DISABLE_ASSERTS)
496 # define LEMON_ASSERT(exp, msg) (static_cast<void> (0))
501 * \brief Macro for assertions with customizable message
503 * Macro for assertions with customizable message.
505 * The behaviour can be customized with LEMON_ASSERT_HANDLER,
506 * LEMON_ASSERT_EXCEPTION and LEMON_ASSERT_ABORT defines. Asserts can be
507 * disabled by defining either NDEBUG or LEMON_DISABLE_ASSERTS macros.
509 * \todo We should provide some way to reset to the default behaviour,
512 * \todo This whole 'assert' business should be placed in a separate
515 * \todo __PRETTY_FUNCTION__ should be replaced by something
516 * compiler-independent, like BOOST_CURRENT_FUNCTION
519 # define LEMON_ASSERT(exp, msg) \
520 (static_cast<void> (!!(exp) ? 0 : ( \
521 LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
522 __PRETTY_FUNCTION__, \
523 (msg), #exp, LEMON_ASSERT_ABORT), 0)))
525 #endif // NDEBUG || LEMON_DISABLE_ASSERTS
528 * \brief Macro for mark not yet implemented features.
530 * \todo Is this the right place for this? It should be used only in
531 * modules under development.
533 * \todo __PRETTY_FUNCTION__ should be replaced by something
534 * compiler-independent, like BOOST_CURRENT_FUNCTION
537 # define LEMON_FIXME(msg) \
538 (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \