Location: LEMON/LEMON-official/lemon/assert.h - annotation
Load file history
Reworking assertions and moving to distinict file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 r108:889d0c289d19 | /* -*- C++ -*-
*
* 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
* purpose.
*
*/
#ifndef LEMON_ASSERT_H
#define LEMON_ASSERT_H
/// \ingroup exceptions
/// \file
/// \brief Extended assertion handling
#include <lemon/error.h>
namespace lemon {
/// @{
///\e
class AssertionFailedError : public LogicError {
protected:
const char *_assertion;
const char *_file;
int _line;
const char *_function;
const char *_message;
mutable ExceptionMember<std::string> _message_holder;
public:
///\e
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) {}
///\e
const char* assertion() const { return _assertion; }
///\e
const char* message() const { return _message; }
///\e
const char* file() const { return _file; }
///\e
const char* function() const { return _function; }
///\e
int line() const { return _line; }
virtual const char* what() const throw() {
try {
std::ostringstream ostr;
ostr << _file << ":" << _line << ": ";
if (_function)
ostr << _function << ": ";
ostr << _message;
if (_assertion)
ostr << " (assertion '" << _assertion << "' failed)";
_message_holder.set(ostr.str());
return ostr.str().c_str();
}
catch(...) {}
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 char *function,
const std::exception& exception,
const char *assertion)
{
std::cerr << file << ":" << line << ": ";
if (function)
std::cerr << function << ": ";
std::cerr << exception.what();
if (assertion)
std::cerr << " (assertion '" << assertion << "' failed)";
std::cerr << std::endl;
}
inline void assert_fail_log(const char *file, int line, const char *function,
const char *message, const char *assertion)
{
std::cerr << file << ":" << line << ": ";
if (function)
std::cerr << function << ": ";
std::cerr << message;
if (assertion)
std::cerr << " (assertion '" << assertion << "' failed)";
std::cerr << std::endl;
}
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 char *function,
const std::exception& exception,
const char *assertion)
{
std::cerr << file << ":" << line << ": ";
if (function)
std::cerr << function << ": ";
std::cerr << exception.what();
if (assertion)
std::cerr << " (assertion '" << assertion << "' failed)";
std::cerr << std::endl;
std::abort();
}
inline void assert_fail_abort(const char *file, int line,
const char *function, const char* message,
const char *assertion)
{
std::cerr << file << ":" << line << ": ";
if (function)
std::cerr << function << ": ";
std::cerr << message;
if (assertion)
std::cerr << " (assertion '" << assertion << "' failed)";
std::cerr << std::endl;
std::abort();
}
inline void assert_fail_abort(const char *file, int line,
const char *function,
const std::string& message,
const char *assertion)
{
assert_fail_abort(file, line, function, message.c_str(), assertion);
}
inline void assert_fail_error(const char *file, int line,
const char *function,
const std::exception& exception,
const char *assertion)
{
throw AssertionFailedError(file, line, function,
exception.what(), assertion);
}
inline void assert_fail_error(const char *file, int line,
const char *function, const char *message,
const char *assertion)
{
throw AssertionFailedError(file, line, function, message, assertion);
}
inline void assert_fail_error(const char *file, int line,
const char *function,
const std::string& message,
const char *assertion)
{
assert_fail_error(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))
{
throw exception;
}
inline void assert_fail_exception(const char *file, int line,
const char *function, const char *message,
const char *assertion)
{
throw AssertionFailedError(file, line, function, message, assertion);
}
inline void assert_fail_exception(const char *file, int line,
const char *function,
const std::string& message,
const char *assertion)
{
assert_fail_exception(file, line, function, message.c_str(), assertion);
}
/// @}
}
#endif // LEMON_ASSERT_H
#undef LEMON_ASSERT
#undef LEMON_FIXME
#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"
#endif
#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_ASSERT)) && \
defined(LEMON_DISABLE_ASSERTS)
#error "Lemon assertion system is not set properly"
#endif
#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"
# endif
# 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
#else
# undef LEMON_ASSERT_HANDLER
#endif
#ifndef LEMON_FUNCTION_NAME
# define LEMON_FUNCTION_NAME (__PRETTY_FUNCTION__)
#endif
#ifdef DOXYGEN
/// \ingroup exceptions
///
/// \brief Macro for assertions with customizable message
///
/// Macro for assertions with customizable message.
/// \param exp An expression convertible to bool. If the expression is
/// false, then an assertion is raised. The concrete behaviour depends
/// on the settings of the assertion system.
/// \param msg A \e const \e char*, a \e const std::string& or a \e
/// const \e std::exception& parameter. The variable can be used to
/// provide information about the circumstances of failed assertion.
///
/// The assertions are disabled in the default behaviour. You can
/// enable the assertions with the following code:
/// \code
/// #define LEMON_ENABLE_ASSERTS
/// \endcode
/// or with compilation parameters:
/// \code
/// g++ -DLEMON_ENABLE_ASSERTS
/// make CXXFLAGS='-DLEMON_ENABLE_ASSERTS'
/// \endcode
///
/// The %lemon assertion system has a wide range of customization
/// properties. As default behaviour the failed assertion prints a
/// short log message to the standard ouput and aborts the execution.
///
/// The following modes can be used in the assertion system:
///
/// - \e LEMON_ASSERT_LOG The failed assert print a short convenient
/// error message to the standard error and continues the
/// execution.
/// - \e LEMON_ASSERT_ABORT This mode is similar to the \e
/// LEMON_ASSERT_LOG, but it aborts the program. It is the default
/// operation mode when the asserts are enabled with \e
/// LEMON_ENABLE_ASSERTS.
/// - \e LEMON_ASSERT_ERROR The assert 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 as
/// error message.
/// - \e 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 parameter.
/// - \e LEMON_ASSERT_CUSTOM The user can define an own assertion
/// handler functions. Three overloaded functions should be defined
/// with the following parameter lists:
/// \code
/// void custom_assert_handler(const char* file, int line,
/// const char* function, const char* message, const char* expression);
/// void custom_assert_handler(const char* file, int line,
/// const char* function, const std::string& message, const char* expression);
/// void custom_assert_handler(const char* file, int line,
/// const char* function, const std::exception& message, const char* expression);
/// \endcode
/// The name of the functions should be defined as the \c
/// LEMON_CUSTOM_ASSERT_HANDLER macro name.
/// \code
/// #define LEMON_CUSTOM_ASSERT_HANDLER custom_assert_handler
/// \endcode
/// Whenever an assertion is occured, one of the custom assertion
/// handler is called with appropiate parameters.
///
/// The assertion mode can be changed within one compilation unit, if
/// the macros are redefined with other settings and the
/// lemon/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__, \
LEMON_FUNCTION_NAME, \
msg, #exp), 0)))
/// \ingroup exceptions
///
/// \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:
/// \code
/// LEMON_ASSERT(false, msg);
/// \endcode
# define LEMON_FIXME(msg) \
(LEMON_ASSERT_HANDLER(__FILE__, __LINE__, LEMON_FUNCTION_NAME, \
"FIXME: " msg, static_cast<const char*>(0)))
#else
# ifndef LEMON_ASSERT_HANDLER
# define LEMON_ASSERT(exp, msg) (static_cast<void> (0))
# define LEMON_FIXME(msg) (static_cast<void>(0))
# else
# define LEMON_ASSERT(exp, msg) \
(static_cast<void> (!!(exp) ? 0 : ( \
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
LEMON_FUNCTION_NAME, \
msg, #exp), 0)))
# define LEMON_FIXME(msg) \
(LEMON_ASSERT_HANDLER(__FILE__, __LINE__, LEMON_FUNCTION_NAME, \
"FIXME: " msg, static_cast<const char*>(0)))
# endif
#endif
|