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()); \
85 #define IMP_NOT_IMPLEMENTED \
87 IMP::base::handle_error("This method is not implemented."); \
88 throw IMP::base::InternalException("Not implemented"); \
108 #define IMP_IF_CHECK(level)
121 #define IMP_CHECK_CODE(expr)
143 #define IMP_INTERNAL_CHECK(expr, message)
153 #define IMP_INTERNAL_CHECK_FLOAT_EQUAL(expra, exprb, message)
172 #define IMP_USAGE_CHECK(expr, message)
182 #define IMP_USAGE_CHECK_FLOAT_EQUAL(expra, exprb, message)
188 #define IMP_CHECK_VARIABLE(variable)
194 #define IMP_USAGE_CHECK_VARIABLE(variable)
199 #define IMP_INTERNAL_CHECK_VARIABLE(variable)
203 #if IMP_HAS_CHECKS == IMP_INTERNAL
204 #define IMP_CHECK_VARIABLE(variable)
205 #define IMP_USAGE_CHECK_VARIABLE(variable)
206 #define IMP_INTERNAL_CHECK_VARIABLE(variable)
207 #elif IMP_HAS_CHECKS == IMP_USAGE
208 #define IMP_CHECK_VARIABLE(variable)
209 #define IMP_USAGE_CHECK_VARIABLE(variable)
210 #define IMP_INTERNAL_CHECK_VARIABLE(variable) IMP_UNUSED(variable)
212 #define IMP_CHECK_VARIABLE(variable) IMP_UNUSED(variable)
213 #define IMP_USAGE_CHECK_VARIABLE(variable) IMP_UNUSED(variable)
214 #define IMP_INTERNAL_CHECK_VARIABLE(variable) IMP_UNUSED(variable)
217 #if IMP_HAS_CHECKS > IMP_NONE
218 #define IMP_IF_CHECK(level) \
219 using IMP::base::NONE; \
220 using IMP::base::USAGE; \
221 using IMP::base::USAGE_AND_INTERNAL; \
222 if (level <= ::IMP::base::get_check_level())
224 #define IMP_CHECK_CODE(expr) expr
226 #if IMP_BASE_HAS_LOG4CXX
227 #define IMP_BASE_CONTEXT
229 #define IMP_BASE_CONTEXT << IMP::base::get_context_message()
232 #else // IMP_HAS_CHECKS == IMP_NONE
233 #define IMP_IF_CHECK(level) if (0)
234 #define IMP_CHECK_CODE(expr)
235 #endif // IMP_HAS_CHECKS
237 #if IMP_HAS_CHECKS >= IMP_INTERNAL
238 #define IMP_INTERNAL_CHECK(expr, message) \
240 if (IMP::base::get_check_level() >= IMP::base::USAGE_AND_INTERNAL && \
242 std::ostringstream imp_check_oss; \
243 imp_check_oss << "Internal check failure: " << message << std::endl \
244 << " File \"" << __FILE__ << "\", line " \
245 << __LINE__ IMP_BASE_CONTEXT << std::endl; \
246 IMP::base::handle_error(imp_check_oss.str().c_str()); \
247 throw IMP::base::InternalException(imp_check_oss.str().c_str()); \
251 #define IMP_INTERNAL_CHECK_FLOAT_EQUAL(expra, exprb, message) \
252 IMP_INTERNAL_CHECK( \
253 std::abs((expra) - (exprb)) < .1 * std::abs((expra) + (exprb)) + .1, \
254 (expra) << " != " << (exprb) << " - " << message)
256 #define IMP_INTERNAL_CHECK(expr, message)
257 #define IMP_INTERNAL_CHECK_FLOAT_EQUAL(expra, exprb, message)
260 #if IMP_HAS_CHECKS >= IMP_USAGE
261 #define IMP_USAGE_CHECK(expr, message) \
263 if (IMP::base::get_check_level() >= IMP::base::USAGE && !(expr)) { \
264 std::ostringstream imp_check_oss; \
265 imp_check_oss << "Usage check failure: " << message IMP_BASE_CONTEXT \
267 IMP::base::handle_error(imp_check_oss.str().c_str()); \
268 throw IMP::base::UsageException(imp_check_oss.str().c_str()); \
271 #define IMP_USAGE_CHECK_FLOAT_EQUAL(expra, exprb, message) \
273 std::abs((expra) - (exprb)) < .1 * std::abs((expra) + (exprb)) + .1, \
274 expra << " != " << exprb << " - " << message)
276 #define IMP_USAGE_CHECK(expr, message)
277 #define IMP_USAGE_CHECK_FLOAT_EQUAL(expra, exprb, message)
280 #endif // IMP_DOXYGEN
282 #if defined(IMP_DOXYGEN) || IMP_HAS_CHECKS == IMP_NONE
284 #define IMP_CHECK_OBJECT(obj) IMP_UNUSED(obj)
285 #define IMP_CHECK_OBJECT_IF_NOT_nullptr(obj) IMP_UNUSED(obj)
288 #define IMP_CHECK_OBJECT(obj) \
291 IMP_INTERNAL_CHECK((obj), "nullptr object"); \
292 IMP_INTERNAL_CHECK((obj)->get_is_valid(), \
293 "Check object " << static_cast<const void *>(obj) \
294 << " was previously freed"); \
297 #define IMP_CHECK_OBJECT_IF_NOT_nullptr(obj) \
300 IMP_INTERNAL_CHECK((obj)->get_is_valid(), \
301 "Check object " << static_cast<const void *>(obj) \
302 << " was previously freed"); \
Various compiler workarounds.
Exception definitions and assertions.