xapian-core  1.4.18
omassert.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007,2008,2009,2012,2013,2015 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /* The "om" prefix is a historical vestige dating back to the "Open Muscat"
22  * code base upon which Xapian is partly based. It's preserved here only
23  * to avoid colliding with the ISO C "assert.h" header.
24  */
25 
26 #ifndef XAPIAN_INCLUDED_OMASSERT_H
27 #define XAPIAN_INCLUDED_OMASSERT_H
28 
29 #ifndef XAPIAN_ASSERTIONS
30 // The configure script should always define XAPIAN_ASSERTIONS if it defines
31 // XAPIAN_ASSERTIONS_PARANOID.
32 # ifdef XAPIAN_ASSERTIONS_PARANOID
33 # error XAPIAN_ASSERTIONS_PARANOID defined without XAPIAN_ASSERTIONS
34 # endif
35 #else
36 
37 #include <xapian/error.h>
38 
39 #include "str.h"
40 
41 #define XAPIAN_ASSERT_LOCATION__(LINE,MSG) __FILE__":"#LINE": "#MSG
42 #define XAPIAN_ASSERT_LOCATION_(LINE,MSG) XAPIAN_ASSERT_LOCATION__(LINE,MSG)
43 #define XAPIAN_ASSERT_LOCATION(MSG) XAPIAN_ASSERT_LOCATION_(__LINE__,MSG)
44 
45 // Expensive (or potentially expensive) assertions can be marked as "Paranoid"
46 // - these can be disabled separately from other assertions to allow a build
47 // with assertions which still has good performance.
48 #ifdef XAPIAN_ASSERTIONS_PARANOID
49 # define AssertParanoid(COND) Assert(COND)
50 # define AssertRelParanoid(A,REL,B) AssertRel(A,REL,B)
51 # define AssertEqParanoid(A,B) AssertEq(A,B)
52 # define AssertEqDoubleParanoid(A,B) AssertEqDouble(A,B)
53 #endif
54 
59 #define Assert(COND) \
60  do {\
61  if (rare(!(COND)))\
62  throw Xapian::AssertionError(XAPIAN_ASSERT_LOCATION(COND));\
63  } while (0)
64 
72 #define AssertRel(A,REL,B) \
73  do {\
74  if (rare(!((A) REL (B)))) {\
75  std::string xapian_assertion_msg(XAPIAN_ASSERT_LOCATION(A REL B));\
76  xapian_assertion_msg += " : values were ";\
77  xapian_assertion_msg += str(A);\
78  xapian_assertion_msg += " and ";\
79  xapian_assertion_msg += str(B);\
80  throw Xapian::AssertionError(xapian_assertion_msg);\
81  }\
82  } while (0)
83 
91 #define AssertEq(A,B) AssertRel(A,==,B)
92 
94 namespace Xapian {
95 namespace Internal {
96 bool within_DBL_EPSILON(double a, double b);
97 }
98 }
99 
101 #define AssertEqDouble(A,B) \
102  do {\
103  using Xapian::Internal::within_DBL_EPSILON;\
104  if (rare(!within_DBL_EPSILON(A, B))) {\
105  std::string xapian_assertion_msg(XAPIAN_ASSERT_LOCATION(within_DBL_EPSILON(A, B)));\
106  xapian_assertion_msg += " : values were ";\
107  xapian_assertion_msg += str(A);\
108  xapian_assertion_msg += " and ";\
109  xapian_assertion_msg += str(B);\
110  throw Xapian::AssertionError(xapian_assertion_msg);\
111  }\
112  } while (0)
113 
114 #endif
115 
116 // If assertions are disabled, set the macros to expand to (void)0 so that
117 // we get a compiler error in this case for assertions missing a trailing
118 // semicolon. This avoids one source of compile errors in debug builds
119 // which don't manifest in non-debug builds.
120 
121 #ifndef Assert
122 # define Assert(COND) (void)0
123 # define AssertRel(A,REL,B) (void)0
124 # define AssertEq(A,B) (void)0
125 # define AssertEqDouble(A,B) (void)0
126 #endif
127 
128 #ifndef AssertParanoid
129 # define AssertParanoid(COND) (void)0
130 # define AssertRelParanoid(A,REL,B) (void)0
131 # define AssertEqParanoid(A,B) (void)0
132 # define AssertEqDoubleParanoid(A,B) (void)0
133 #endif
134 
135 #endif // XAPIAN_INCLUDED_OMASSERT_H
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
Convert types to std::string.
Hierarchy of classes which Xapian can throw as exceptions.