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