9 #ifndef IMPBASE_CHECK_MACROS_H
10 #define IMPBASE_CHECK_MACROS_H
12 #include <IMP/base/base_config.h>
18 #if !defined(IMP_HAS_CHECKS)
19 #error "IMP_HAS_CHECKS is not defined, compilation is broken"
22 #if !defined(IMP_NONE)
23 #error "IMP_NONE is not defined, compilation is broken"
30 #define IMP_CATCH_AND_TERMINATE(expr) \
34 catch (const IMP::base::Exception &e) { \
35 std::cerr << "Application terminated with error :" << e.what() \
50 #define IMP_THROW(message, exception_name) \
53 using namespace IMP::base; \
54 std::ostringstream imp_throw_oss; \
55 imp_throw_oss << message << std::endl; \
56 throw exception_name(imp_throw_oss.str().c_str()); \
63 #define IMP_ALWAYS_CHECK(condition, message, exception_name) \
65 IMP_THROW(message, exception_name); \
74 #define IMP_FAILURE(message) \
76 std::ostringstream imp_failure_oss; \
77 imp_failure_oss << message << std::endl; \
78 IMP::base::handle_error(imp_failure_oss.str().c_str()); \
79 throw IMP::base::InternalException(imp_failure_oss.str().c_str()); \
83 #define IMP_NOT_IMPLEMENTED \
85 IMP::base::handle_error("This method is not implemented."); \
86 throw IMP::base::InternalException("Not implemented"); \
106 #define IMP_IF_CHECK(level)
119 #define IMP_CHECK_CODE(expr)
141 #define IMP_INTERNAL_CHECK(expr, message)
151 #define IMP_INTERNAL_CHECK_FLOAT_EQUAL(expra, exprb, message)
170 #define IMP_USAGE_CHECK(expr, message)
180 #define IMP_USAGE_CHECK_FLOAT_EQUAL(expra, exprb, message)
186 #define IMP_CHECK_VARIABLE(variable)
192 #define IMP_USAGE_CHECK_VARIABLE(variable)
197 #define IMP_INTERNAL_CHECK_VARIABLE(variable)
201 #if IMP_HAS_CHECKS == IMP_INTERNAL
202 #define IMP_CHECK_VARIABLE(variable)
203 #define IMP_USAGE_CHECK_VARIABLE(variable)
204 #define IMP_INTERNAL_CHECK_VARIABLE(variable)
205 #elif IMP_HAS_CHECKS == IMP_USAGE
206 #define IMP_CHECK_VARIABLE(variable)
207 #define IMP_USAGE_CHECK_VARIABLE(variable)
208 #define IMP_INTERNAL_CHECK_VARIABLE(variable) IMP_UNUSED(variable)
210 #define IMP_CHECK_VARIABLE(variable) IMP_UNUSED(variable)
211 #define IMP_USAGE_CHECK_VARIABLE(variable) IMP_UNUSED(variable)
212 #define IMP_INTERNAL_CHECK_VARIABLE(variable) IMP_UNUSED(variable)
215 #if IMP_HAS_CHECKS > IMP_NONE
216 #define IMP_IF_CHECK(level) \
217 using IMP::base::NONE; \
218 using IMP::base::USAGE; \
219 using IMP::base::USAGE_AND_INTERNAL; \
220 if (level <= ::IMP::base::get_check_level())
222 #define IMP_CHECK_CODE(expr) expr
224 #if IMP_BASE_HAS_LOG4CXX
225 #define IMP_BASE_CONTEXT
227 #define IMP_BASE_CONTEXT << IMP::base::get_context_message()
230 #else // IMP_HAS_CHECKS == IMP_NONE
231 #define IMP_IF_CHECK(level) if (0)
232 #define IMP_CHECK_CODE(expr)
233 #endif // IMP_HAS_CHECKS
235 #if IMP_HAS_CHECKS >= IMP_INTERNAL
236 #define IMP_INTERNAL_CHECK(expr, message) \
238 if (IMP::base::get_check_level() >= IMP::base::USAGE_AND_INTERNAL && \
240 std::ostringstream imp_check_oss; \
241 imp_check_oss << "Internal check failure: " << message << std::endl \
242 << " File \"" << __FILE__ << "\", line " \
243 << __LINE__ IMP_BASE_CONTEXT << std::endl; \
244 IMP::base::handle_error(imp_check_oss.str().c_str()); \
245 throw IMP::base::InternalException(imp_check_oss.str().c_str()); \
249 #define IMP_INTERNAL_CHECK_FLOAT_EQUAL(expra, exprb, message) \
250 IMP_INTERNAL_CHECK( \
251 std::abs((expra) - (exprb)) < .1 * std::abs((expra) + (exprb)) + .1, \
252 (expra) << " != " << (exprb) << " - " << message)
254 #define IMP_INTERNAL_CHECK(expr, message)
255 #define IMP_INTERNAL_CHECK_FLOAT_EQUAL(expra, exprb, message)
258 #if IMP_HAS_CHECKS >= IMP_USAGE
259 #define IMP_USAGE_CHECK(expr, message) \
261 if (IMP::base::get_check_level() >= IMP::base::USAGE && !(expr)) { \
262 std::ostringstream imp_check_oss; \
263 imp_check_oss << "Usage check failure: " << message IMP_BASE_CONTEXT \
265 IMP::base::handle_error(imp_check_oss.str().c_str()); \
266 throw IMP::base::UsageException(imp_check_oss.str().c_str()); \
269 #define IMP_USAGE_CHECK_FLOAT_EQUAL(expra, exprb, message) \
271 std::abs((expra) - (exprb)) < .1 * std::abs((expra) + (exprb)) + .1, \
272 expra << " != " << exprb << " - " << message)
274 #define IMP_USAGE_CHECK(expr, message)
275 #define IMP_USAGE_CHECK_FLOAT_EQUAL(expra, exprb, message)
278 #endif // IMP_DOXYGEN
280 #if defined(IMP_DOXYGEN) || IMP_HAS_CHECKS == IMP_NONE
282 #define IMP_CHECK_OBJECT(obj) IMP_UNUSED(obj)
283 #define IMP_CHECK_OBJECT_IF_NOT_nullptr(obj) IMP_UNUSED(obj)
286 #define IMP_CHECK_OBJECT(obj) \
289 IMP_INTERNAL_CHECK((obj), "nullptr object"); \
290 IMP_INTERNAL_CHECK((obj)->get_is_valid(), \
291 "Check object " << static_cast<const void *>(obj) \
292 << " was previously freed"); \
295 #define IMP_CHECK_OBJECT_IF_NOT_nullptr(obj) \
298 IMP_INTERNAL_CHECK((obj)->get_is_valid(), \
299 "Check object " << static_cast<const void *>(obj) \
300 << " was previously freed"); \
Exception definitions and assertions.
Various compiler workarounds.