lemon/assert.h
author Akos Ladanyi <ladanyi@tmit.bme.hu>
Sun, 13 Apr 2008 13:22:52 +0200
changeset 141 96f81c791f0c
parent 112 d2ee5e7f00ef
child 142 8b703d177341
permissions -rw-r--r--
CMake based build system
kpeter@66
     1
/* -*- C++ -*-
kpeter@66
     2
 *
kpeter@66
     3
 * This file is a part of LEMON, a generic C++ optimization library
kpeter@66
     4
 *
kpeter@66
     5
 * Copyright (C) 2003-2008
kpeter@66
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@66
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@66
     8
 *
kpeter@66
     9
 * Permission to use, modify and distribute this software is granted
kpeter@66
    10
 * provided that this copyright notice appears in all copies. For
kpeter@66
    11
 * precise terms see the accompanying LICENSE file.
kpeter@66
    12
 *
kpeter@66
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@66
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@66
    15
 * purpose.
kpeter@66
    16
 *
kpeter@66
    17
 */
kpeter@66
    18
deba@108
    19
#ifndef LEMON_ASSERT_H
deba@108
    20
#define LEMON_ASSERT_H
kpeter@66
    21
kpeter@66
    22
/// \ingroup exceptions
kpeter@66
    23
/// \file
deba@108
    24
/// \brief Extended assertion handling
kpeter@66
    25
deba@108
    26
#include <lemon/error.h>
kpeter@66
    27
kpeter@66
    28
namespace lemon {
kpeter@66
    29
deba@108
    30
  inline void assert_fail_log(const char *file, int line, const char *function,
deba@108
    31
			      const char *message, const char *assertion)
kpeter@66
    32
  {
deba@108
    33
    std::cerr << file << ":" << line << ": ";
deba@108
    34
    if (function)
deba@108
    35
      std::cerr << function << ": ";
deba@108
    36
    std::cerr << message;
kpeter@66
    37
    if (assertion)
deba@108
    38
      std::cerr << " (assertion '" << assertion << "' failed)";
deba@108
    39
    std::cerr << std::endl;
kpeter@66
    40
  }
kpeter@66
    41
deba@108
    42
  inline void assert_fail_abort(const char *file, int line,
deba@108
    43
				const char *function, const char* message,
deba@108
    44
				const char *assertion)
deba@108
    45
  {
kpeter@112
    46
    assert_fail_log(file, line, function, message, assertion);
deba@108
    47
    std::abort();
deba@108
    48
  }
deba@108
    49
deba@118
    50
  namespace _assert_bits {
deba@118
    51
    
deba@118
    52
    
deba@118
    53
    inline const char* cstringify(const std::string& str) {
deba@118
    54
      return str.c_str();
deba@118
    55
    }
deba@118
    56
deba@118
    57
    inline const char* cstringify(const char* str) {
deba@118
    58
      return str;
deba@118
    59
    }    
deba@108
    60
  }
deba@118
    61
}
deba@108
    62
deba@108
    63
#endif // LEMON_ASSERT_H
kpeter@66
    64
kpeter@66
    65
#undef LEMON_ASSERT
kpeter@66
    66
#undef LEMON_FIXME
deba@118
    67
#undef LEMON_DEBUG
kpeter@66
    68
deba@108
    69
#if (defined(LEMON_ASSERT_LOG) ? 1 : 0) +		\
deba@108
    70
  (defined(LEMON_ASSERT_ABORT) ? 1 : 0) +		\
deba@108
    71
  (defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) > 1
kpeter@112
    72
#error "LEMON assertion system is not set properly"
kpeter@66
    73
#endif
kpeter@66
    74
deba@108
    75
#if ((defined(LEMON_ASSERT_LOG) ? 1 : 0) +		\
deba@108
    76
     (defined(LEMON_ASSERT_ABORT) ? 1 : 0) +		\
deba@108
    77
     (defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) == 1 ||	\
kpeter@112
    78
     defined(LEMON_ENABLE_ASSERTS)) &&			\
deba@118
    79
  (defined(LEMON_DISABLE_ASSERTS) ||			\
deba@118
    80
   defined(NDEBUG))
kpeter@112
    81
#error "LEMON assertion system is not set properly"
kpeter@66
    82
#endif
kpeter@66
    83
deba@108
    84
deba@108
    85
#if defined LEMON_ASSERT_LOG
deba@108
    86
#  undef LEMON_ASSERT_HANDLER
deba@108
    87
#  define LEMON_ASSERT_HANDLER ::lemon::assert_fail_log
deba@108
    88
#elif defined LEMON_ASSERT_ABORT
deba@108
    89
#  undef LEMON_ASSERT_HANDLER
deba@108
    90
#  define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort
deba@108
    91
#elif defined LEMON_ASSERT_CUSTOM
deba@108
    92
#  undef LEMON_ASSERT_HANDLER
deba@108
    93
#  ifndef LEMON_CUSTOM_ASSERT_HANDLER
deba@108
    94
#    error "LEMON_CUSTOM_ASSERT_HANDLER is not set"
kpeter@66
    95
#  endif
deba@108
    96
#  define LEMON_ASSERT_HANDLER LEMON_CUSTOM_ASSERT_HANDLER
deba@118
    97
#elif defined LEMON_DISABLE_ASSERTS
deba@108
    98
#  undef LEMON_ASSERT_HANDLER
deba@118
    99
#elif defined NDEBUG
deba@118
   100
#  undef LEMON_ASSERT_HANDLER
deba@118
   101
#else
deba@108
   102
#  define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort
deba@108
   103
#endif
deba@108
   104
deba@108
   105
#ifndef LEMON_FUNCTION_NAME
deba@108
   106
#  define LEMON_FUNCTION_NAME (__PRETTY_FUNCTION__)
kpeter@66
   107
#endif
kpeter@66
   108
kpeter@66
   109
#ifdef DOXYGEN
kpeter@66
   110
deba@108
   111
/// \ingroup exceptions
deba@108
   112
///
kpeter@112
   113
/// \brief Macro for assertion with customizable message
kpeter@66
   114
///
deba@118
   115
/// Macro for assertion with customizable message.  \param exp An
deba@118
   116
/// expression that must be convertible to \c bool.  If it is \c
deba@118
   117
/// false, then an assertion is raised. The concrete behaviour depends
deba@118
   118
/// on the settings of the assertion system.  \param msg A <tt>const
deba@118
   119
/// char*</tt> parameter, which can be used to provide information
deba@118
   120
/// about the circumstances of the failed assertion.
kpeter@66
   121
///
deba@118
   122
/// The assertions are enabled in the default behaviour.
deba@118
   123
/// You can disable them with the following code:
kpeter@66
   124
/// \code
deba@118
   125
/// #define LEMON_DISABLE_ASSERTS
kpeter@66
   126
/// \endcode
deba@108
   127
/// or with compilation parameters:
deba@108
   128
/// \code
deba@118
   129
/// g++ -DLEMON_DISABLE_ASSERTS
deba@118
   130
/// make CXXFLAGS='-DLEMON_DISABLE_ASSERTS'
deba@108
   131
/// \endcode
deba@118
   132
/// The checking is also disabled when the standard macro \c NDEBUG is defined.
deba@108
   133
/// 
kpeter@112
   134
/// The LEMON assertion system has a wide range of customization
kpeter@112
   135
/// properties. As a default behaviour the failed assertion prints a
kpeter@112
   136
/// short log message to the standard error and aborts the execution.
kpeter@66
   137
///
deba@108
   138
/// The following modes can be used in the assertion system: 
kpeter@66
   139
///
kpeter@112
   140
/// - \c LEMON_ASSERT_LOG The failed assertion prints a short log
kpeter@112
   141
///   message to the standard error and continues the execution.
deba@118
   142
/// - \c LEMON_ASSERT_ABORT This mode is similar to the \c
deba@118
   143
///   LEMON_ASSERT_LOG, but it aborts the program. It is the default
deba@118
   144
///   behaviour.
kpeter@112
   145
/// - \c LEMON_ASSERT_CUSTOM The user can define own assertion handler
deba@118
   146
///   function.
deba@108
   147
///   \code
kpeter@112
   148
///     void custom_assert_handler(const char* file, int line, const char* function,
kpeter@112
   149
///                                const char* message, const char* assertion);
deba@108
   150
///   \endcode
deba@118
   151
///   The name of the function should be defined as the \c
deba@108
   152
///   LEMON_CUSTOM_ASSERT_HANDLER macro name. 
deba@108
   153
///   \code
deba@108
   154
///     #define LEMON_CUSTOM_ASSERT_HANDLER custom_assert_handler
deba@108
   155
///   \endcode
deba@118
   156
///   Whenever an assertion is occured, the custom assertion
deba@118
   157
///   handler is called with appropiate parameters.
kpeter@66
   158
///
kpeter@112
   159
/// The assertion mode can also be changed within one compilation unit.
kpeter@112
   160
/// If the macros are redefined with other settings and the
kpeter@112
   161
/// \ref lemon/assert.h "assert.h" file is reincluded, then the
kpeter@112
   162
/// behaviour is changed appropiately to the new settings.
deba@108
   163
#  define LEMON_ASSERT(exp, msg)					\
deba@108
   164
  (static_cast<void> (!!(exp) ? 0 : (					\
deba@108
   165
    LEMON_ASSERT_HANDLER(__FILE__, __LINE__,				\
deba@108
   166
			 LEMON_FUNCTION_NAME,				\
deba@118
   167
			 ::lemon::_assert_bits::cstringify(msg), #exp), 0)))
deba@108
   168
deba@108
   169
/// \ingroup exceptions
kpeter@66
   170
///
deba@108
   171
/// \brief Macro for mark not yet implemented features.
deba@108
   172
///
deba@108
   173
/// Macro for mark not yet implemented features and outstanding bugs.
deba@108
   174
/// It is close to be the shortcut of the following code:
deba@108
   175
/// \code
deba@108
   176
///   LEMON_ASSERT(false, msg);
deba@108
   177
/// \endcode
deba@118
   178
///
deba@118
   179
/// \see LEMON_ASSERT 
deba@108
   180
#  define LEMON_FIXME(msg)						\
deba@118
   181
  (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, LEMON_FUNCTION_NAME,	\
deba@118
   182
			::lemon::_assert_bits::cstringify(msg),		\
deba@118
   183
			static_cast<const char*>(0)))
deba@118
   184
deba@118
   185
/// \ingroup exceptions
deba@118
   186
///
deba@118
   187
/// \brief Macro for internal assertions
deba@118
   188
///
deba@118
   189
/// Macro for internal assertions, it is used in the library to check
deba@118
   190
/// the consistency of results of algorithms, several pre- and
deba@118
   191
/// postconditions and invariants. The checking is disabled by
deba@118
   192
/// default, but it can be turned on with the macro \c
deba@118
   193
/// LEMON_ENABLE_DEBUG.
deba@118
   194
/// \code
deba@118
   195
/// #define LEMON_ENABLE_DEBUG
deba@118
   196
/// \endcode
deba@118
   197
/// or with compilation parameters:
deba@118
   198
/// \code
deba@118
   199
/// g++ -DLEMON_ENABLE_DEBUG
deba@118
   200
/// make CXXFLAGS='-DLEMON_ENABLE_DEBUG'
deba@118
   201
/// \endcode
deba@118
   202
///
deba@118
   203
/// This macro works like the \c LEMON_ASSERT macro, therefore the
deba@118
   204
/// current behaviour depends on the settings of \c LEMON_ASSERT
deba@118
   205
/// macro.
deba@118
   206
///
deba@118
   207
/// \see LEMON_ASSERT 
deba@118
   208
#  define LEMON_DEBUG(exp, msg)						\
deba@118
   209
  (static_cast<void> (!!(exp) ? 0 : (					\
deba@118
   210
    LEMON_ASSERT_HANDLER(__FILE__, __LINE__,                            \
deba@118
   211
			 LEMON_FUNCTION_NAME,				\
deba@118
   212
			 ::lemon::_assert_bits::cstringify(msg), #exp), 0)))
kpeter@66
   213
kpeter@66
   214
#else
kpeter@66
   215
deba@108
   216
#  ifndef LEMON_ASSERT_HANDLER
kpeter@112
   217
#    define LEMON_ASSERT(exp, msg)  (static_cast<void>(0))
deba@108
   218
#    define LEMON_FIXME(msg) (static_cast<void>(0))
deba@118
   219
#    define LEMON_DEBUG(exp, msg) (static_cast<void>(0))
kpeter@66
   220
#  else
deba@118
   221
#    define LEMON_ASSERT(exp, msg)					\
deba@118
   222
       (static_cast<void> (!!(exp) ? 0 : (				\
deba@118
   223
        LEMON_ASSERT_HANDLER(__FILE__, __LINE__,                        \
deba@118
   224
			     LEMON_FUNCTION_NAME,			\
deba@118
   225
			     ::lemon::_assert_bits::cstringify(msg),	\
deba@118
   226
			     #exp), 0)))
deba@118
   227
#    define LEMON_FIXME(msg)						\
deba@108
   228
       (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, LEMON_FUNCTION_NAME,	\
deba@118
   229
			     ::lemon::_assert_bits::cstringify(msg),	\
deba@118
   230
			     static_cast<const char*>(0)))
deba@118
   231
deba@118
   232
#    if LEMON_ENABLE_DEBUG
deba@118
   233
#      define LEMON_DEBUG(exp, msg)
deba@118
   234
         (static_cast<void> (!!(exp) ? 0 : (         \
deba@118
   235
           LEMON_ASSERT_HANDLER(__FILE__, __LINE__,  \
deba@118
   236
                                LEMON_FUNCTION_NAME, \
deba@118
   237
				::lemon::_assert_bits::cstringify(msg),	\
deba@118
   238
				#exp), 0)))
deba@118
   239
#    else
deba@118
   240
#      define LEMON_DEBUG(exp, msg) (static_cast<void>(0))
deba@118
   241
#    endif
kpeter@66
   242
#  endif
deba@108
   243
kpeter@66
   244
#endif
kpeter@66
   245
deba@118
   246
#ifdef DOXYGEN
kpeter@66
   247
deba@118
   248
deba@118
   249
#else
deba@118
   250
deba@118
   251
deba@118
   252
#endif
deba@118
   253
deba@118
   254