* This file is a part of LEMON, a generic C++ optimization library
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
/// \brief Extended assertion handling
class AssertionFailedError : public LogicError {
mutable ExceptionMember<std::string> _message_holder;
AssertionFailedError(const char *file, int line, const char *function,
const char *msg, const char *assertion = 0) :
_assertion(assertion), _file(file), _line(line),
_function(function), _message(msg) {}
const char* assertion() const { return _assertion; }
const char* message() const { return _message; }
const char* file() const { return _file; }
const char* function() const { return _function; }
int line() const { return _line; }
virtual const char* what() const throw() {
ostr << _file << ":" << _line << ": ";
ostr << _function << ": ";
ostr << " (assertion '" << _assertion << "' failed)";
_message_holder.set(ostr.str());
return ostr.str().c_str();
if( _message_holder.valid() ) return _message_holder.get().c_str();
return "lemon::AssertionFailedError";
virtual ~AssertionFailedError() throw() {}
inline void assert_fail_log(const char *file, int line,
const std::exception& exception,
std::cerr << file << ":" << line << ": ";
std::cerr << function << ": ";
std::cerr << exception.what();
std::cerr << " (assertion '" << assertion << "' failed)";
inline void assert_fail_log(const char *file, int line, const char *function,
const char *message, const char *assertion)
std::cerr << file << ":" << line << ": ";
std::cerr << function << ": ";
std::cerr << " (assertion '" << assertion << "' failed)";
inline void assert_fail_log(const char *file, int line, const char *function,
const std::string& message, const char *assertion)
assert_fail_log(file, line, function, message.c_str(), assertion);
inline void assert_fail_abort(const char *file, int line,
const std::exception& exception,
assert_fail_log(file, line, function, exception, assertion);
inline void assert_fail_abort(const char *file, int line,
const char *function, const char* message,
assert_fail_log(file, line, function, message, assertion);
inline void assert_fail_abort(const char *file, int line,
const std::string& message,
assert_fail_log(file, line, function, message.c_str(), assertion);
inline void assert_fail_error(const char *file, int line,
const std::exception& exception,
throw AssertionFailedError(file, line, function,
exception.what(), assertion);
inline void assert_fail_error(const char *file, int line,
const char *function, const char *message,
throw AssertionFailedError(file, line, function, message, assertion);
inline void assert_fail_error(const char *file, int line,
const std::string& message,
throw AssertionFailedError(file, line, function, message.c_str(), assertion);
template <typename Exception>
inline void assert_fail_exception(const char *, int, const char *,
const Exception& exception,
const char *, const std::exception* =
static_cast<const Exception*>(0))
inline void assert_fail_exception(const char *file, int line,
const char *function, const char *message,
throw AssertionFailedError(file, line, function, message, assertion);
inline void assert_fail_exception(const char *file, int line,
const std::string& message,
throw AssertionFailedError(file, line, function, message.c_str(), assertion);
#if (defined(LEMON_ASSERT_LOG) ? 1 : 0) + \
(defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \
(defined(LEMON_ASSERT_ERROR) ? 1 : 0) + \
(defined(LEMON_ASSERT_EXCEPTION) ? 1 : 0) + \
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) > 1
#error "LEMON assertion system is not set properly"
#if ((defined(LEMON_ASSERT_LOG) ? 1 : 0) + \
(defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \
(defined(LEMON_ASSERT_ERROR) ? 1 : 0) + \
(defined(LEMON_ASSERT_EXCEPTION) ? 1 : 0) + \
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) == 1 || \
defined(LEMON_ENABLE_ASSERTS)) && \
defined(LEMON_DISABLE_ASSERTS)
#error "LEMON assertion system is not set properly"
#if defined LEMON_ASSERT_LOG
# undef LEMON_ASSERT_HANDLER
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_log
#elif defined LEMON_ASSERT_ABORT
# undef LEMON_ASSERT_HANDLER
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort
#elif defined LEMON_ASSERT_ERROR
# undef LEMON_ASSERT_HANDLER
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_error
#elif defined LEMON_ASSERT_EXCEPTION
# undef LEMON_ASSERT_HANDLER
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_exception
#elif defined LEMON_ASSERT_CUSTOM
# undef LEMON_ASSERT_HANDLER
# ifndef LEMON_CUSTOM_ASSERT_HANDLER
# error "LEMON_CUSTOM_ASSERT_HANDLER is not set"
# define LEMON_ASSERT_HANDLER LEMON_CUSTOM_ASSERT_HANDLER
#elif defined LEMON_ENABLE_ASSERTS
# undef LEMON_ASSERT_HANDLER
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort
# undef LEMON_ASSERT_HANDLER
#ifndef LEMON_FUNCTION_NAME
# define LEMON_FUNCTION_NAME (__PRETTY_FUNCTION__)
/// \brief Macro for assertion with customizable message
/// Macro for assertion with customizable message.
/// \param exp An expression that must be convertible to \c bool.
/// If it is \c false, then an assertion is raised. The concrete
/// behaviour depends on the settings of the assertion system.
/// \param msg A <tt>const char*</tt>, a <tt>const std::string&</tt> or
/// a <tt>const std::exception&</tt> parameter, which can be used to
/// provide information about the circumstances of the failed assertion.
/// The assertions are disabled in the default behaviour.
/// You can enable them with the following code:
/// #define LEMON_ENABLE_ASSERTS
/// or with compilation parameters:
/// g++ -DLEMON_ENABLE_ASSERTS
/// make CXXFLAGS='-DLEMON_ENABLE_ASSERTS'
/// The LEMON assertion system has a wide range of customization
/// properties. As a default behaviour the failed assertion prints a
/// short log message to the standard error and aborts the execution.
/// The following modes can be used in the assertion system:
/// - \c LEMON_ASSERT_LOG The failed assertion prints a short log
/// message to the standard error and continues the execution.
/// - \c LEMON_ASSERT_ABORT This mode is similar to the
/// \c LEMON_ASSERT_LOG, but it aborts the program. It is the default
/// behaviour mode when the assertions are enabled with
/// \c LEMON_ENABLE_ASSERTS.
/// - \c LEMON_ASSERT_ERROR The assertion throws an
/// \ref lemon::AssertionFailedError "AssertionFailedError".
/// If the \c msg parameter is an exception, then the result of the
/// \ref lemon::Exception::what() "what()" member function is passed
/// - \c LEMON_ASSERT_EXCEPTION If the specified \c msg is an
/// exception, then it raised directly (solving that the exception
/// can not be thrown polymorphically), otherwise an \ref
/// lemon::AssertionFailedError "AssertionFailedError" is thrown with
/// the given parameters.
/// - \c LEMON_ASSERT_CUSTOM The user can define own assertion handler
/// functions. Three overloaded functions should be defined with the
/// following parameter lists:
/// void custom_assert_handler(const char* file, int line, const char* function,
/// const char* message, const char* assertion);
/// void custom_assert_handler(const char* file, int line, const char* function,
/// const std::string& message, const char* assertion);
/// void custom_assert_handler(const char* file, int line, const char* function,
/// const std::exception& message, const char* assertion);
/// The name of the functions should be defined as the \c
/// LEMON_CUSTOM_ASSERT_HANDLER macro name.
/// #define LEMON_CUSTOM_ASSERT_HANDLER custom_assert_handler
/// Whenever an assertion is occured, one of the custom assertion
/// handlers is called with appropiate parameters.
/// The assertion mode can also be changed within one compilation unit.
/// If the macros are redefined with other settings and the
/// \ref lemon/assert.h "assert.h" file is reincluded, then the
/// behaviour is changed appropiately to the new settings.
# define LEMON_ASSERT(exp, msg) \
(static_cast<void> (!!(exp) ? 0 : ( \
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
/// \brief Macro for mark not yet implemented features.
/// Macro for mark not yet implemented features and outstanding bugs.
/// It is close to be the shortcut of the following code:
/// LEMON_ASSERT(false, msg);
# define LEMON_FIXME(msg) \
(LEMON_ASSERT_HANDLER(__FILE__, __LINE__, LEMON_FUNCTION_NAME, \
"FIXME: " msg, static_cast<const char*>(0)))
# ifndef LEMON_ASSERT_HANDLER
# define LEMON_ASSERT(exp, msg) (static_cast<void>(0))
# define LEMON_FIXME(msg) (static_cast<void>(0))
# define LEMON_ASSERT(exp, msg) \
(static_cast<void> (!!(exp) ? 0 : ( \
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
# define LEMON_FIXME(msg) \
(LEMON_ASSERT_HANDLER(__FILE__, __LINE__, LEMON_FUNCTION_NAME, \
"FIXME: " msg, static_cast<const char*>(0)))