lemon/error.h
changeset 1813 5c5d1574667d
parent 1746 874e4bc21435
child 1827 dc660ed95b31
equal deleted inserted replaced
2:2f076dff977e 3:95e8a94e41db
   496 
   496 
   497 
   497 
   498   /****************  Macros  ****************/
   498   /****************  Macros  ****************/
   499 
   499 
   500 
   500 
   501   inline
   501   template <typename Exception>
   502   void assert_fail(const char *file, int line, const char *func,
   502   inline void assert_fail(const char *file, int line, const char *func,
   503 		   const char *message, const char *assertion = 0,
   503 		   Exception exception, const char *assertion = 0,
   504 		   bool do_abort=true)
   504 		   bool do_abort=true)
       
   505   {
       
   506     using namespace std;
       
   507     cerr << file << ":" << line << ": ";
       
   508     if( func )
       
   509       cerr << func << ": ";
       
   510     cerr << exception.what();
       
   511     if( assertion )
       
   512       cerr << " (assertion '" << assertion << "' failed)";
       
   513     cerr << endl;
       
   514     if(do_abort)
       
   515       abort();
       
   516   }
       
   517 
       
   518   template <>
       
   519   inline void assert_fail<const char *>(const char *file, int line, const char *func,
       
   520 				 const char *message, 
       
   521 				 const char *assertion,
       
   522 				 bool do_abort)
   505   {
   523   {
   506     using namespace std;
   524     using namespace std;
   507     cerr << file << ":" << line << ": ";
   525     cerr << file << ":" << line << ": ";
   508     if( func )
   526     if( func )
   509       cerr << func << ": ";
   527       cerr << func << ": ";
   513     cerr << endl;
   531     cerr << endl;
   514     if(do_abort)
   532     if(do_abort)
   515       abort();
   533       abort();
   516   }
   534   }
   517 
   535 
   518   inline
   536   template <typename Exception>
   519   void assert_fail_throw(const char *file, int line, const char *func,
   537   inline void assert_fail_failure(const char *file, int line, const char *func,
   520 		   const char *message, const char *assertion = 0,
   538 			   Exception exception, 
   521 		   bool = true)
   539 			   const char *assertion = 0,
       
   540 			   bool = true)
       
   541   {
       
   542     throw AssertionFailedError(file, line, func, exception.what(), assertion);
       
   543   }
       
   544 
       
   545   template <>
       
   546   inline void assert_fail_failure<const char *>(const char *file, int line, 
       
   547 					 const char *func,
       
   548 					 const char *message, 
       
   549 					 const char *assertion,
       
   550 					 bool)
   522   {
   551   {
   523     throw AssertionFailedError(file, line, func, message, assertion);
   552     throw AssertionFailedError(file, line, func, message, assertion);
   524   }
   553   }
   525 
   554 
       
   555   template <typename Exception> 
       
   556   inline void assert_fail_exception(const char *file, int line, const char *func,
       
   557 			     Exception exception, 
       
   558 			     const char *assertion = 0, bool = true)
       
   559   {
       
   560     throw exception;
       
   561   }
       
   562 
       
   563   template <> 
       
   564   inline void assert_fail_exception<const char *>(const char *file, int line, 
       
   565 					   const char *func,
       
   566 					   const char *message, 
       
   567 					   const char *assertion,
       
   568 					   bool)
       
   569   {
       
   570     throw AssertionFailedError(file, line, func, message, assertion);
       
   571   }
       
   572 
   526 /// @}
   573 /// @}
   527 
   574 
   528 }
   575 }
   529 #endif // LEMON_ERROR_H
   576 #endif // LEMON_ERROR_H
   530 
   577 
   534 #ifndef LEMON_ASSERT_ABORT
   581 #ifndef LEMON_ASSERT_ABORT
   535 #  define LEMON_ASSERT_ABORT 1
   582 #  define LEMON_ASSERT_ABORT 1
   536 #endif
   583 #endif
   537 
   584 
   538 #ifndef LEMON_ASSERT_HANDLER
   585 #ifndef LEMON_ASSERT_HANDLER
   539 #  ifdef LEMON_ASSERT_EXCEPTION
   586 #  if defined LEMON_ASSERT_EXCEPTION
   540 #    define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw
   587 #    define LEMON_ASSERT_HANDLER ::lemon::assert_fail_exception
       
   588 #  elif defined LEMON_ASSERT_FAILURE
       
   589 #    define LEMON_ASSERT_HANDLER ::lemon::assert_fail_failure
   541 #  else
   590 #  else
   542 #    define LEMON_ASSERT_HANDLER ::lemon::assert_fail
   591 #    define LEMON_ASSERT_HANDLER ::lemon::assert_fail
   543 #  endif
   592 #  endif
   544 #endif
   593 #endif
   545 
   594 
   560  *
   609  *
   561  * \todo We should provide some way to reset to the default behaviour,
   610  * \todo We should provide some way to reset to the default behaviour,
   562  * shouldn't we?
   611  * shouldn't we?
   563  *
   612  *
   564  * \todo This whole 'assert' business should be placed in a separate
   613  * \todo This whole 'assert' business should be placed in a separate
   565  * include file.
   614  * include file. The boost assert is not guarded by header sentries
       
   615  * which may help to change the behaviour of the assertions in 
       
   616  * the files.
   566  *
   617  *
   567  * \todo __PRETTY_FUNCTION__ should be replaced by something
   618  * \todo __PRETTY_FUNCTION__ should be replaced by something
   568  * compiler-independent, like BOOST_CURRENT_FUNCTION
   619  * compiler-independent, like BOOST_CURRENT_FUNCTION
   569  */
   620  */
   570 
   621