common/omassert.h

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2007 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 /* The "om" prefix is a historical vestige dating back to the "Open Muscat"
00022  * code base upon which Xapian is partly based.  It's preserved here only
00023  * to avoid colliding with the ISO C "assert.h" header.
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_DEBUG
00046 // The configure script should always define XAPIAN_DEBUG if it defines
00047 // XAPIAN_DEBUG_PARANOID.
00048 # ifdef XAPIAN_DEBUG_PARANOID
00049 #  error XAPIAN_DEBUG_PARANOID defined without XAPIAN_DEBUG
00050 # endif
00051 #else
00052 
00053 #include <xapian/error.h>
00054 
00055 #include "utils.h" // For om_to_string() and within_DBL_EPSILON().
00056 
00057 #define XAPIAN_ASSERT_LOCATION__(LINE,MSG) __FILE__":"#LINE": "#MSG
00058 #define XAPIAN_ASSERT_LOCATION_(LINE,MSG) XAPIAN_ASSERT_LOCATION__(LINE,MSG)
00059 #define XAPIAN_ASSERT_LOCATION(MSG) XAPIAN_ASSERT_LOCATION_(__LINE__,MSG)
00060 
00061 // Expensive (or potentially expensive) assertions can be marked as "Paranoid"
00062 // - these can be disabled separately from other assertions to allow a build
00063 // with assertions which still has good performance.
00064 #ifdef XAPIAN_DEBUG_PARANOID
00065 # define AssertParanoid(COND) Assert(COND)
00066 # define AssertRelParanoid(A,REL,B) AssertRel(A,REL,B)
00067 # define AssertEqParanoid(A,B) AssertEq(A,B)
00068 # define AssertEqDoubleParanoid(A,B) AssertEqDouble(A,B)
00069 #endif
00070 
00075 #define Assert(COND) \
00076     do {\
00077         if (rare(!(COND)))\
00078             throw Xapian::AssertionError(XAPIAN_ASSERT_LOCATION(COND));\
00079     } while (0)
00080 
00088 #define AssertRel(A,REL,B) \
00089     do {\
00090         if (rare(!((A) REL (B)))) {\
00091             std::string xapian_assertion_msg(XAPIAN_ASSERT_LOCATION(A REL B));\
00092             xapian_assertion_msg += " : values were ";\
00093             xapian_assertion_msg += om_tostring(A);\
00094             xapian_assertion_msg += " and ";\
00095             xapian_assertion_msg += om_tostring(B);\
00096             throw Xapian::AssertionError(xapian_assertion_msg);\
00097         }\
00098     } while (0)
00099 
00107 #define AssertEq(A,B) AssertRel(A,==,B)
00108 
00110 #define AssertEqDouble(A,B) \
00111     do {\
00112         using Xapian::Internal::within_DBL_EPSILON;\
00113         if (rare(within_DBL_EPSILON(A,B))) {\
00114             std::string xapian_assertion_msg(XAPIAN_ASSERT_LOCATION(within_DBL_EPSILON(A,B)));\
00115             xapian_assertion_msg += " : values were ";\
00116             xapian_assertion_msg += om_tostring(A);\
00117             xapian_assertion_msg += " and ";\
00118             xapian_assertion_msg += om_tostring(B);\
00119             throw Xapian::AssertionError(xapian_assertion_msg);\
00120         }\
00121     } while (0)
00122 
00123 #endif
00124 
00125 // If assertions are disabled, set the macros to expand to (void)0 so that
00126 // we get a compiler error in this case for assertions missing a trailing
00127 // semicolon.  This avoids one source of compile errors in debug builds
00128 // which don't manifest in non-debug builds.
00129 
00130 #ifndef Assert
00131 # define Assert(COND) (void)0
00132 # define AssertRel(A,REL,B) (void)0
00133 # define AssertEq(A,B) (void)0
00134 # define AssertEqDouble(A,B) (void)0
00135 #endif
00136 
00137 #ifndef AssertParanoid
00138 # define AssertParanoid(COND) (void)0
00139 # define AssertRelParanoid(A,REL,B) (void)0
00140 # define AssertEqParanoid(A,B) (void)0
00141 # define AssertEqDoubleParanoid(A,B) (void)0
00142 #endif
00143 
00144 #endif // XAPIAN_INCLUDED_OMASSERT_H

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.