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