00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef XAPIAN_INCLUDED_OMASSERT_H
00027 #define XAPIAN_INCLUDED_OMASSERT_H
00028
00040 #define CompileTimeAssert(COND)\
00041 do {\
00042 typedef int xapian_compile_time_check_[(COND) ? 1 : -1];\
00043 } while (0)
00044
00045 #ifndef XAPIAN_ASSERTIONS
00046
00047
00048 # ifdef XAPIAN_ASSERTIONS_PARANOID
00049 # error XAPIAN_ASSERTIONS_PARANOID defined without XAPIAN_ASSERTIONS
00050 # endif
00051 #else
00052
00053 #include <xapian/error.h>
00054
00055 #include "str.h"
00056 #include "utils.h"
00057
00058 #define XAPIAN_ASSERT_LOCATION__(LINE,MSG) __FILE__":"#LINE": "#MSG
00059 #define XAPIAN_ASSERT_LOCATION_(LINE,MSG) XAPIAN_ASSERT_LOCATION__(LINE,MSG)
00060 #define XAPIAN_ASSERT_LOCATION(MSG) XAPIAN_ASSERT_LOCATION_(__LINE__,MSG)
00061
00062
00063
00064
00065 #ifdef XAPIAN_ASSERTIONS_PARANOID
00066 # define AssertParanoid(COND) Assert(COND)
00067 # define AssertRelParanoid(A,REL,B) AssertRel(A,REL,B)
00068 # define AssertEqParanoid(A,B) AssertEq(A,B)
00069 # define AssertEqDoubleParanoid(A,B) AssertEqDouble(A,B)
00070 #endif
00071
00076 #define Assert(COND) \
00077 do {\
00078 if (rare(!(COND)))\
00079 throw Xapian::AssertionError(XAPIAN_ASSERT_LOCATION(COND));\
00080 } while (0)
00081
00089 #define AssertRel(A,REL,B) \
00090 do {\
00091 if (rare(!((A) REL (B)))) {\
00092 std::string xapian_assertion_msg(XAPIAN_ASSERT_LOCATION(A REL B));\
00093 xapian_assertion_msg += " : values were ";\
00094 xapian_assertion_msg += str(A);\
00095 xapian_assertion_msg += " and ";\
00096 xapian_assertion_msg += str(B);\
00097 throw Xapian::AssertionError(xapian_assertion_msg);\
00098 }\
00099 } while (0)
00100
00108 #define AssertEq(A,B) AssertRel(A,==,B)
00109
00111 #define AssertEqDouble(A,B) \
00112 do {\
00113 using Xapian::Internal::within_DBL_EPSILON;\
00114 if (rare(within_DBL_EPSILON(A,B))) {\
00115 std::string xapian_assertion_msg(XAPIAN_ASSERT_LOCATION(within_DBL_EPSILON(A,B)));\
00116 xapian_assertion_msg += " : values were ";\
00117 xapian_assertion_msg += str(A);\
00118 xapian_assertion_msg += " and ";\
00119 xapian_assertion_msg += str(B);\
00120 throw Xapian::AssertionError(xapian_assertion_msg);\
00121 }\
00122 } while (0)
00123
00124 #endif
00125
00126
00127
00128
00129
00130
00131 #ifndef Assert
00132 # define Assert(COND) (void)0
00133 # define AssertRel(A,REL,B) (void)0
00134 # define AssertEq(A,B) (void)0
00135 # define AssertEqDouble(A,B) (void)0
00136 #endif
00137
00138 #ifndef AssertParanoid
00139 # define AssertParanoid(COND) (void)0
00140 # define AssertRelParanoid(A,REL,B) (void)0
00141 # define AssertEqParanoid(A,B) (void)0
00142 # define AssertEqDoubleParanoid(A,B) (void)0
00143 #endif
00144
00159 #define STATIC_ASSERT(COND) \
00160 do { \
00161 char xapian_static_assert_failed[(COND) ? 1 : -1]; \
00162 (void)xapian_static_assert_failed; \
00163 } while (0)
00164
00166 #define STATIC_ASSERT_UNSIGNED_TYPE(TYPE) \
00167 STATIC_ASSERT(static_cast<TYPE>(-1) > 0)
00168
00180 #define STATIC_ASSERT_TYPE_DOMINATES(T1, T2) \
00181 STATIC_ASSERT(static_cast<T1>(-1) > 0 ? \
00182 (static_cast<T2>(-1) > 0 && sizeof(T1) >= sizeof(T2)) : \
00183 (sizeof(T1) >= sizeof(T2) + (static_cast<T2>(-1) > 0)))
00184
00185 #endif // XAPIAN_INCLUDED_OMASSERT_H