# HG changeset patch # User deba # Date 1131626458 0 # Node ID 606178a14011deece20e20f74ab00a9e422d5636 # Parent d9eb186547d7badeccffcb67eb46fc2772775caf Assertion with own exception throwing diff -r d9eb186547d7 -r 606178a14011 lemon/error.h --- a/lemon/error.h Thu Nov 10 12:40:26 2005 +0000 +++ b/lemon/error.h Thu Nov 10 12:40:58 2005 +0000 @@ -498,15 +498,33 @@ /**************** Macros ****************/ - inline - void assert_fail(const char *file, int line, const char *func, - const char *message, const char *assertion = 0, + template + inline void assert_fail(const char *file, int line, const char *func, + Exception exception, const char *assertion = 0, bool do_abort=true) { using namespace std; cerr << file << ":" << line << ": "; if( func ) cerr << func << ": "; + cerr << exception.what(); + if( assertion ) + cerr << " (assertion '" << assertion << "' failed)"; + cerr << endl; + if(do_abort) + abort(); + } + + template <> + inline void assert_fail(const char *file, int line, const char *func, + const char *message, + const char *assertion, + bool do_abort) + { + using namespace std; + cerr << file << ":" << line << ": "; + if( func ) + cerr << func << ": "; cerr << message; if( assertion ) cerr << " (assertion '" << assertion << "' failed)"; @@ -515,10 +533,39 @@ abort(); } - inline - void assert_fail_throw(const char *file, int line, const char *func, - const char *message, const char *assertion = 0, - bool = true) + template + inline void assert_fail_failure(const char *file, int line, const char *func, + Exception exception, + const char *assertion = 0, + bool = true) + { + throw AssertionFailedError(file, line, func, exception.what(), assertion); + } + + template <> + inline void assert_fail_failure(const char *file, int line, + const char *func, + const char *message, + const char *assertion, + bool) + { + throw AssertionFailedError(file, line, func, message, assertion); + } + + template + inline void assert_fail_exception(const char *file, int line, const char *func, + Exception exception, + const char *assertion = 0, bool = true) + { + throw exception; + } + + template <> + inline void assert_fail_exception(const char *file, int line, + const char *func, + const char *message, + const char *assertion, + bool) { throw AssertionFailedError(file, line, func, message, assertion); } @@ -536,8 +583,10 @@ #endif #ifndef LEMON_ASSERT_HANDLER -# ifdef LEMON_ASSERT_EXCEPTION -# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw +# if defined LEMON_ASSERT_EXCEPTION +# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_exception +# elif defined LEMON_ASSERT_FAILURE +# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_failure # else # define LEMON_ASSERT_HANDLER ::lemon::assert_fail # endif @@ -562,7 +611,9 @@ * shouldn't we? * * \todo This whole 'assert' business should be placed in a separate - * include file. + * include file. The boost assert is not guarded by header sentries + * which may help to change the behaviour of the assertions in + * the files. * * \todo __PRETTY_FUNCTION__ should be replaced by something * compiler-independent, like BOOST_CURRENT_FUNCTION