Assertion with own exception throwing
authordeba
Thu, 10 Nov 2005 12:40:58 +0000
changeset 1785606178a14011
parent 1784 d9eb186547d7
child 1786 a263c131e999
Assertion with own exception throwing
lemon/error.h
     1.1 --- a/lemon/error.h	Thu Nov 10 12:40:26 2005 +0000
     1.2 +++ b/lemon/error.h	Thu Nov 10 12:40:58 2005 +0000
     1.3 @@ -498,15 +498,33 @@
     1.4    /****************  Macros  ****************/
     1.5  
     1.6  
     1.7 -  inline
     1.8 -  void assert_fail(const char *file, int line, const char *func,
     1.9 -		   const char *message, const char *assertion = 0,
    1.10 +  template <typename Exception>
    1.11 +  inline void assert_fail(const char *file, int line, const char *func,
    1.12 +		   Exception exception, const char *assertion = 0,
    1.13  		   bool do_abort=true)
    1.14    {
    1.15      using namespace std;
    1.16      cerr << file << ":" << line << ": ";
    1.17      if( func )
    1.18        cerr << func << ": ";
    1.19 +    cerr << exception.what();
    1.20 +    if( assertion )
    1.21 +      cerr << " (assertion '" << assertion << "' failed)";
    1.22 +    cerr << endl;
    1.23 +    if(do_abort)
    1.24 +      abort();
    1.25 +  }
    1.26 +
    1.27 +  template <>
    1.28 +  inline void assert_fail<const char *>(const char *file, int line, const char *func,
    1.29 +				 const char *message, 
    1.30 +				 const char *assertion,
    1.31 +				 bool do_abort)
    1.32 +  {
    1.33 +    using namespace std;
    1.34 +    cerr << file << ":" << line << ": ";
    1.35 +    if( func )
    1.36 +      cerr << func << ": ";
    1.37      cerr << message;
    1.38      if( assertion )
    1.39        cerr << " (assertion '" << assertion << "' failed)";
    1.40 @@ -515,10 +533,39 @@
    1.41        abort();
    1.42    }
    1.43  
    1.44 -  inline
    1.45 -  void assert_fail_throw(const char *file, int line, const char *func,
    1.46 -		   const char *message, const char *assertion = 0,
    1.47 -		   bool = true)
    1.48 +  template <typename Exception>
    1.49 +  inline void assert_fail_failure(const char *file, int line, const char *func,
    1.50 +			   Exception exception, 
    1.51 +			   const char *assertion = 0,
    1.52 +			   bool = true)
    1.53 +  {
    1.54 +    throw AssertionFailedError(file, line, func, exception.what(), assertion);
    1.55 +  }
    1.56 +
    1.57 +  template <>
    1.58 +  inline void assert_fail_failure<const char *>(const char *file, int line, 
    1.59 +					 const char *func,
    1.60 +					 const char *message, 
    1.61 +					 const char *assertion,
    1.62 +					 bool)
    1.63 +  {
    1.64 +    throw AssertionFailedError(file, line, func, message, assertion);
    1.65 +  }
    1.66 +
    1.67 +  template <typename Exception> 
    1.68 +  inline void assert_fail_exception(const char *file, int line, const char *func,
    1.69 +			     Exception exception, 
    1.70 +			     const char *assertion = 0, bool = true)
    1.71 +  {
    1.72 +    throw exception;
    1.73 +  }
    1.74 +
    1.75 +  template <> 
    1.76 +  inline void assert_fail_exception<const char *>(const char *file, int line, 
    1.77 +					   const char *func,
    1.78 +					   const char *message, 
    1.79 +					   const char *assertion,
    1.80 +					   bool)
    1.81    {
    1.82      throw AssertionFailedError(file, line, func, message, assertion);
    1.83    }
    1.84 @@ -536,8 +583,10 @@
    1.85  #endif
    1.86  
    1.87  #ifndef LEMON_ASSERT_HANDLER
    1.88 -#  ifdef LEMON_ASSERT_EXCEPTION
    1.89 -#    define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw
    1.90 +#  if defined LEMON_ASSERT_EXCEPTION
    1.91 +#    define LEMON_ASSERT_HANDLER ::lemon::assert_fail_exception
    1.92 +#  elif defined LEMON_ASSERT_FAILURE
    1.93 +#    define LEMON_ASSERT_HANDLER ::lemon::assert_fail_failure
    1.94  #  else
    1.95  #    define LEMON_ASSERT_HANDLER ::lemon::assert_fail
    1.96  #  endif
    1.97 @@ -562,7 +611,9 @@
    1.98   * shouldn't we?
    1.99   *
   1.100   * \todo This whole 'assert' business should be placed in a separate
   1.101 - * include file.
   1.102 + * include file. The boost assert is not guarded by header sentries
   1.103 + * which may help to change the behaviour of the assertions in 
   1.104 + * the files.
   1.105   *
   1.106   * \todo __PRETTY_FUNCTION__ should be replaced by something
   1.107   * compiler-independent, like BOOST_CURRENT_FUNCTION