1 | /*************************************************************************** |
---|
2 | * blitz/bzdebug.h Debugging macros |
---|
3 | * |
---|
4 | * $Id: bzdebug.h,v 1.6 2004/10/06 21:58:33 julianc Exp $ |
---|
5 | * |
---|
6 | * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org> |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * Suggestions: blitz-dev@oonumerics.org |
---|
19 | * Bugs: blitz-bugs@oonumerics.org |
---|
20 | * |
---|
21 | * For more information, please see the Blitz++ Home Page: |
---|
22 | * http://oonumerics.org/blitz/ |
---|
23 | * |
---|
24 | ***************************************************************************/ |
---|
25 | |
---|
26 | #ifndef BZ_DEBUG_H |
---|
27 | #define BZ_DEBUG_H |
---|
28 | |
---|
29 | #ifdef BZ_HAVE_STDLIB_H |
---|
30 | #include <stdlib.h> |
---|
31 | #endif |
---|
32 | #include <assert.h> |
---|
33 | |
---|
34 | #ifdef BZ_HAVE_RTTI |
---|
35 | #include <typeinfo> |
---|
36 | #endif |
---|
37 | |
---|
38 | BZ_NAMESPACE(blitz) |
---|
39 | |
---|
40 | /* |
---|
41 | * These globals are used by the Blitz++ testsuite. The _bz_global |
---|
42 | * modifier ensures that they will reside in libblitz.a, but appear |
---|
43 | * "extern" elsewhere. |
---|
44 | */ |
---|
45 | |
---|
46 | _bz_global bool assertFailMode BZ_GLOBAL_INIT(false); |
---|
47 | _bz_global int assertFailCount BZ_GLOBAL_INIT(0); |
---|
48 | _bz_global int assertSuccessCount BZ_GLOBAL_INIT(0); |
---|
49 | |
---|
50 | |
---|
51 | #if defined(BZ_TESTSUITE) |
---|
52 | /* |
---|
53 | * In testsuite mode, these routines allow a test suite to check |
---|
54 | * that precondition checking is being done properly. A typical |
---|
55 | * use looks like this: |
---|
56 | * |
---|
57 | * beginCheckAssert(); |
---|
58 | * // Some operation which should cause an assert to fail |
---|
59 | * endCheckAssert(); |
---|
60 | * |
---|
61 | * The routine beginCheckAssert() sets a flag which results in |
---|
62 | * failed asserts being silently tallied. If no asserts have |
---|
63 | * failed by the time endCheckAssert() is invoked, the program |
---|
64 | * halts and issues an error code. |
---|
65 | * |
---|
66 | * In normal operation (i.e. when beginCheckAssert() has not |
---|
67 | * been called), failed preconditions will cause the program |
---|
68 | * to halt and issue an error code. -- TV 980226 |
---|
69 | */ |
---|
70 | |
---|
71 | inline void checkAssert(bool condition, const char* where=0, |
---|
72 | int line=0) |
---|
73 | { |
---|
74 | if (assertFailMode == true) |
---|
75 | { |
---|
76 | if (condition == true) |
---|
77 | ++assertSuccessCount; |
---|
78 | else |
---|
79 | ++assertFailCount; |
---|
80 | } |
---|
81 | else { |
---|
82 | if (!condition) |
---|
83 | { |
---|
84 | cerr << "Unexpected assert failure!" << endl; |
---|
85 | if (where) |
---|
86 | cerr << where << ":" << line << endl; |
---|
87 | cerr.flush(); |
---|
88 | assert(0); |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | inline void beginCheckAssert() |
---|
94 | { |
---|
95 | assertFailMode = true; |
---|
96 | assertSuccessCount = 0; |
---|
97 | assertFailCount = 0; |
---|
98 | } |
---|
99 | |
---|
100 | inline void endCheckAssert() |
---|
101 | { |
---|
102 | assert(assertFailMode == true); |
---|
103 | assertFailMode = false; |
---|
104 | if (assertFailCount == 0) |
---|
105 | { |
---|
106 | cerr << "Assert check failed!" << endl; |
---|
107 | assert(0); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | #define BZASSERT(X) checkAssert(X, __FILE__, __LINE__) |
---|
112 | #define BZPRECONDITION(X) checkAssert(X, __FILE__, __LINE__) |
---|
113 | #define BZPOSTCONDITION(X) checkAssert(X, __FILE__, __LINE__) |
---|
114 | #define BZSTATECHECK(X,Y) checkAssert(X == Y, __FILE__, __LINE__) |
---|
115 | #define BZPRECHECK(X,Y) \ |
---|
116 | { \ |
---|
117 | if ((assertFailMode == false) && (!(X))) \ |
---|
118 | cerr << Y << endl; \ |
---|
119 | checkAssert(X, __FILE__, __LINE__); \ |
---|
120 | } |
---|
121 | |
---|
122 | #define BZ_DEBUG_MESSAGE(X) \ |
---|
123 | { \ |
---|
124 | if (assertFailMode == false) \ |
---|
125 | { \ |
---|
126 | cout << __FILE__ << ":" << __LINE__ << " " << X << endl; \ |
---|
127 | } \ |
---|
128 | } |
---|
129 | |
---|
130 | #define BZ_DEBUG_PARAM(X) X |
---|
131 | #define BZ_PRE_FAIL checkAssert(0) |
---|
132 | #define BZ_ASM_DEBUG_MARKER |
---|
133 | |
---|
134 | #elif defined(BZ_DEBUG) |
---|
135 | |
---|
136 | #define BZASSERT(X) assert(X) |
---|
137 | #define BZPRECONDITION(X) assert(X) |
---|
138 | #define BZPOSTCONDITION(X) assert(X) |
---|
139 | #define BZSTATECHECK(X,Y) assert(X == Y) |
---|
140 | #define BZPRECHECK(X,Y) \ |
---|
141 | { if (!(X)) \ |
---|
142 | { cerr << "[Blitz++] Precondition failure: Module " << __FILE__ \ |
---|
143 | << " line " << __LINE__ << endl \ |
---|
144 | << Y << endl; \ |
---|
145 | cerr.flush(); \ |
---|
146 | assert(0); \ |
---|
147 | } \ |
---|
148 | } |
---|
149 | |
---|
150 | #define BZ_DEBUG_MESSAGE(X) \ |
---|
151 | { cout << __FILE__ << ":" << __LINE__ << " " << X << endl; } |
---|
152 | |
---|
153 | #define BZ_DEBUG_PARAM(X) X |
---|
154 | #define BZ_PRE_FAIL assert(0) |
---|
155 | |
---|
156 | // This routine doesn't exist anywhere; it's used to mark a |
---|
157 | // position of interest in assembler (.s) files |
---|
158 | void _bz_debug_marker(); |
---|
159 | #define BZ_ASM_DEBUG_MARKER _bz_debug_marker(); |
---|
160 | |
---|
161 | #else // !BZ_TESTSUITE && !BZ_DEBUG |
---|
162 | |
---|
163 | #define BZASSERT(X) |
---|
164 | #define BZPRECONDITION(X) |
---|
165 | #define BZPOSTCONDITION(X) |
---|
166 | #define BZSTATECHECK(X,Y) |
---|
167 | #define BZPRECHECK(X,Y) |
---|
168 | #define BZ_DEBUG_MESSAGE(X) |
---|
169 | #define BZ_DEBUG_PARAM(X) |
---|
170 | #define BZ_PRE_FAIL |
---|
171 | #define BZ_ASM_DEBUG_MARKER |
---|
172 | |
---|
173 | #endif // !BZ_TESTSUITE && !BZ_DEBUG |
---|
174 | |
---|
175 | #define BZ_NOT_IMPLEMENTED() { cerr << "[Blitz++] Not implemented: module " \ |
---|
176 | << __FILE__ << " line " << __LINE__ << endl; \ |
---|
177 | exit(1); } |
---|
178 | |
---|
179 | #ifdef BZ_HAVE_RTTI |
---|
180 | #define BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(X) typeid(X).name() |
---|
181 | #else |
---|
182 | |
---|
183 | template<typename T> |
---|
184 | class _bz_stringLiteralForNumericType { |
---|
185 | public: |
---|
186 | static const char* string() |
---|
187 | { return "unknown"; } |
---|
188 | }; |
---|
189 | |
---|
190 | #define BZ_DECL_SLFNT(X,Y) \ |
---|
191 | template<> \ |
---|
192 | class _bz_stringLiteralForNumericType< X > { \ |
---|
193 | public: \ |
---|
194 | static const char* string() \ |
---|
195 | { return Y; } \ |
---|
196 | } |
---|
197 | |
---|
198 | #ifdef BZ_HAVE_BOOL |
---|
199 | BZ_DECL_SLFNT(bool, "bool"); |
---|
200 | #endif |
---|
201 | |
---|
202 | BZ_DECL_SLFNT(char, "char"); |
---|
203 | BZ_DECL_SLFNT(unsigned char, "unsigned char"); |
---|
204 | BZ_DECL_SLFNT(short int, "short int"); |
---|
205 | BZ_DECL_SLFNT(short unsigned int, "short unsigned int"); |
---|
206 | BZ_DECL_SLFNT(int, "int"); |
---|
207 | BZ_DECL_SLFNT(unsigned int, "unsigned int"); |
---|
208 | BZ_DECL_SLFNT(long, "long"); |
---|
209 | BZ_DECL_SLFNT(unsigned long, "unsigned long"); |
---|
210 | BZ_DECL_SLFNT(float, "float"); |
---|
211 | BZ_DECL_SLFNT(double, "double"); |
---|
212 | BZ_DECL_SLFNT(long double, "long double"); |
---|
213 | |
---|
214 | #ifdef BZ_HAVE_COMPLEX |
---|
215 | BZ_DECL_SLFNT(complex<float>, "complex<float>"); |
---|
216 | BZ_DECL_SLFNT(complex<double>, "complex<double>"); |
---|
217 | BZ_DECL_SLFNT(complex<long double>, "complex<long double>"); |
---|
218 | #endif |
---|
219 | |
---|
220 | #define BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(X) \ |
---|
221 | _bz_stringLiteralForNumericType<X>::string() |
---|
222 | |
---|
223 | #endif // !BZ_HAVE_RTTI |
---|
224 | |
---|
225 | BZ_NAMESPACE_END |
---|
226 | |
---|
227 | #endif // BZ_DEBUG_H |
---|