lemon/assert.h
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 09 Sep 2008 20:52:45 +0100
changeset 258 0310c8984732
parent 212 1ae84dea7d09
child 266 112ed801139d
permissions -rw-r--r--
Merge
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@66
     2
 *
alpar@209
     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,
alpar@209
    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,
alpar@209
    43
                                const char *function, const char* message,
alpar@209
    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 {
alpar@209
    51
alpar@209
    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;
alpar@209
    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
kpeter@212
    69
#if (defined(LEMON_ASSERT_LOG) ? 1 : 0) +               \
kpeter@212
    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
kpeter@212
    75
#if ((defined(LEMON_ASSERT_LOG) ? 1 : 0) +              \
kpeter@212
    76
     (defined(LEMON_ASSERT_ABORT) ? 1 : 0) +            \
kpeter@212
    77
     (defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) == 1 ||     \
kpeter@212
    78
     defined(LEMON_ENABLE_ASSERTS)) &&                  \
kpeter@212
    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
ladanyi@142
   106
#  if defined __GNUC__
ladanyi@142
   107
#    define LEMON_FUNCTION_NAME (__PRETTY_FUNCTION__)
ladanyi@142
   108
#  elif defined _MSC_VER
ladanyi@142
   109
#    define LEMON_FUNCTION_NAME (__FUNCSIG__)
ladanyi@142
   110
#  else
ladanyi@142
   111
#    define LEMON_FUNCTION_NAME (__func__)
ladanyi@142
   112
#  endif
kpeter@66
   113
#endif
kpeter@66
   114
kpeter@66
   115
#ifdef DOXYGEN
kpeter@66
   116
deba@108
   117
/// \ingroup exceptions
deba@108
   118
///
kpeter@112
   119
/// \brief Macro for assertion with customizable message
kpeter@66
   120
///
deba@118
   121
/// Macro for assertion with customizable message.  \param exp An
deba@118
   122
/// expression that must be convertible to \c bool.  If it is \c
deba@118
   123
/// false, then an assertion is raised. The concrete behaviour depends
deba@118
   124
/// on the settings of the assertion system.  \param msg A <tt>const
deba@118
   125
/// char*</tt> parameter, which can be used to provide information
deba@118
   126
/// about the circumstances of the failed assertion.
kpeter@66
   127
///
deba@118
   128
/// The assertions are enabled in the default behaviour.
deba@118
   129
/// You can disable them with the following code:
kpeter@66
   130
/// \code
deba@118
   131
/// #define LEMON_DISABLE_ASSERTS
kpeter@66
   132
/// \endcode
deba@108
   133
/// or with compilation parameters:
deba@108
   134
/// \code
deba@118
   135
/// g++ -DLEMON_DISABLE_ASSERTS
deba@118
   136
/// make CXXFLAGS='-DLEMON_DISABLE_ASSERTS'
deba@108
   137
/// \endcode
deba@118
   138
/// The checking is also disabled when the standard macro \c NDEBUG is defined.
alpar@209
   139
///
kpeter@112
   140
/// The LEMON assertion system has a wide range of customization
kpeter@112
   141
/// properties. As a default behaviour the failed assertion prints a
kpeter@112
   142
/// short log message to the standard error and aborts the execution.
kpeter@66
   143
///
alpar@209
   144
/// The following modes can be used in the assertion system:
kpeter@66
   145
///
kpeter@112
   146
/// - \c LEMON_ASSERT_LOG The failed assertion prints a short log
kpeter@112
   147
///   message to the standard error and continues the execution.
deba@118
   148
/// - \c LEMON_ASSERT_ABORT This mode is similar to the \c
deba@118
   149
///   LEMON_ASSERT_LOG, but it aborts the program. It is the default
deba@118
   150
///   behaviour.
kpeter@112
   151
/// - \c LEMON_ASSERT_CUSTOM The user can define own assertion handler
deba@118
   152
///   function.
deba@108
   153
///   \code
alpar@210
   154
///     void custom_assert_handler(const char* file, int line,
alpar@210
   155
///                                const char* function, const char* message,
alpar@210
   156
///                                const char* assertion);
deba@108
   157
///   \endcode
deba@118
   158
///   The name of the function should be defined as the \c
alpar@209
   159
///   LEMON_CUSTOM_ASSERT_HANDLER macro name.
deba@108
   160
///   \code
deba@108
   161
///     #define LEMON_CUSTOM_ASSERT_HANDLER custom_assert_handler
deba@108
   162
///   \endcode
deba@118
   163
///   Whenever an assertion is occured, the custom assertion
deba@118
   164
///   handler is called with appropiate parameters.
kpeter@66
   165
///
kpeter@112
   166
/// The assertion mode can also be changed within one compilation unit.
kpeter@112
   167
/// If the macros are redefined with other settings and the
kpeter@112
   168
/// \ref lemon/assert.h "assert.h" file is reincluded, then the
kpeter@112
   169
/// behaviour is changed appropiately to the new settings.
alpar@209
   170
#  define LEMON_ASSERT(exp, msg)                                        \
kpeter@212
   171
  (static_cast<void> (!!(exp) ? 0 : (                                   \
kpeter@212
   172
    LEMON_ASSERT_HANDLER(__FILE__, __LINE__,                            \
kpeter@212
   173
                         LEMON_FUNCTION_NAME,                           \
alpar@209
   174
                         ::lemon::_assert_bits::cstringify(msg), #exp), 0)))
deba@108
   175
deba@108
   176
/// \ingroup exceptions
kpeter@66
   177
///
deba@108
   178
/// \brief Macro for mark not yet implemented features.
deba@108
   179
///
deba@108
   180
/// Macro for mark not yet implemented features and outstanding bugs.
deba@108
   181
/// It is close to be the shortcut of the following code:
deba@108
   182
/// \code
deba@108
   183
///   LEMON_ASSERT(false, msg);
deba@108
   184
/// \endcode
deba@118
   185
///
alpar@209
   186
/// \see LEMON_ASSERT
kpeter@212
   187
#  define LEMON_FIXME(msg)                                              \
alpar@209
   188
  (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, LEMON_FUNCTION_NAME,        \
kpeter@212
   189
                        ::lemon::_assert_bits::cstringify(msg),         \
alpar@209
   190
                        static_cast<const char*>(0)))
deba@118
   191
deba@118
   192
/// \ingroup exceptions
deba@118
   193
///
deba@118
   194
/// \brief Macro for internal assertions
deba@118
   195
///
deba@118
   196
/// Macro for internal assertions, it is used in the library to check
deba@118
   197
/// the consistency of results of algorithms, several pre- and
deba@118
   198
/// postconditions and invariants. The checking is disabled by
deba@118
   199
/// default, but it can be turned on with the macro \c
deba@118
   200
/// LEMON_ENABLE_DEBUG.
deba@118
   201
/// \code
deba@118
   202
/// #define LEMON_ENABLE_DEBUG
deba@118
   203
/// \endcode
deba@118
   204
/// or with compilation parameters:
deba@118
   205
/// \code
deba@118
   206
/// g++ -DLEMON_ENABLE_DEBUG
deba@118
   207
/// make CXXFLAGS='-DLEMON_ENABLE_DEBUG'
deba@118
   208
/// \endcode
deba@118
   209
///
deba@118
   210
/// This macro works like the \c LEMON_ASSERT macro, therefore the
deba@118
   211
/// current behaviour depends on the settings of \c LEMON_ASSERT
deba@118
   212
/// macro.
deba@118
   213
///
alpar@209
   214
/// \see LEMON_ASSERT
kpeter@212
   215
#  define LEMON_DEBUG(exp, msg)                                         \
kpeter@212
   216
  (static_cast<void> (!!(exp) ? 0 : (                                   \
deba@118
   217
    LEMON_ASSERT_HANDLER(__FILE__, __LINE__,                            \
kpeter@212
   218
                         LEMON_FUNCTION_NAME,                           \
alpar@209
   219
                         ::lemon::_assert_bits::cstringify(msg), #exp), 0)))
kpeter@66
   220
kpeter@66
   221
#else
kpeter@66
   222
deba@108
   223
#  ifndef LEMON_ASSERT_HANDLER
kpeter@112
   224
#    define LEMON_ASSERT(exp, msg)  (static_cast<void>(0))
deba@108
   225
#    define LEMON_FIXME(msg) (static_cast<void>(0))
deba@118
   226
#    define LEMON_DEBUG(exp, msg) (static_cast<void>(0))
kpeter@66
   227
#  else
kpeter@212
   228
#    define LEMON_ASSERT(exp, msg)                                      \
kpeter@212
   229
       (static_cast<void> (!!(exp) ? 0 : (                              \
deba@118
   230
        LEMON_ASSERT_HANDLER(__FILE__, __LINE__,                        \
kpeter@212
   231
                             LEMON_FUNCTION_NAME,                       \
kpeter@212
   232
                             ::lemon::_assert_bits::cstringify(msg),    \
alpar@209
   233
                             #exp), 0)))
kpeter@212
   234
#    define LEMON_FIXME(msg)                                            \
kpeter@212
   235
       (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, LEMON_FUNCTION_NAME,   \
kpeter@212
   236
                             ::lemon::_assert_bits::cstringify(msg),    \
alpar@209
   237
                             static_cast<const char*>(0)))
deba@118
   238
deba@118
   239
#    if LEMON_ENABLE_DEBUG
kpeter@218
   240
#      define LEMON_DEBUG(exp, msg)                                     \
kpeter@212
   241
         (static_cast<void> (!!(exp) ? 0 : (                            \
kpeter@212
   242
           LEMON_ASSERT_HANDLER(__FILE__, __LINE__,                     \
kpeter@212
   243
                                LEMON_FUNCTION_NAME,                    \
kpeter@212
   244
                                ::lemon::_assert_bits::cstringify(msg), \
alpar@209
   245
                                #exp), 0)))
deba@118
   246
#    else
deba@118
   247
#      define LEMON_DEBUG(exp, msg) (static_cast<void>(0))
deba@118
   248
#    endif
kpeter@66
   249
#  endif
deba@108
   250
kpeter@66
   251
#endif