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