]> git.saurik.com Git - wxWidgets.git/blob - include/wx/debug.h
Use wxWebKitCtrlNameStr to adhere to the convention used by other wx classes.
[wxWidgets.git] / include / wx / debug.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/debug.h
3 // Purpose: Misc debug functions and macros
4 // Author: Vadim Zeitlin
5 // Created: 29/01/98
6 // RCS-ID: $Id$
7 // Copyright: (c) 1998-2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_DEBUG_H_
12 #define _WX_DEBUG_H_
13
14 #if !defined(__WXPALMOS5__) && !defined(__WXWINCE__)
15 #include <assert.h>
16 #endif // systems without assert.h
17
18 #include <limits.h> // for CHAR_BIT used below
19
20 #include "wx/chartype.h" // for __TFILE__ and wxChar
21 #include "wx/cpp.h" // for __WXFUNCTION__
22
23 class WXDLLIMPEXP_FWD_BASE wxString;
24 class WXDLLIMPEXP_FWD_BASE wxCStrData;
25
26 // ----------------------------------------------------------------------------
27 // Defines controlling the debugging macros
28 // ----------------------------------------------------------------------------
29
30 /*
31 wxWidgets can be built with several different levels of debug support
32 specified by the value of wxDEBUG_LEVEL constant:
33
34 0: No assertion macros at all, this should only be used when optimizing
35 for resource-constrained systems (typically embedded ones).
36 1: Default level, most of the assertions are enabled.
37 2: Maximal (at least for now): asserts which are "expensive"
38 (performance-wise) or only make sense for finding errors in wxWidgets
39 itself, as opposed to bugs in applications using it, are also enabled.
40 */
41
42 // unless wxDEBUG_LEVEL is predefined (by configure or via wx/setup.h under
43 // Windows), use the default
44 #if !defined(wxDEBUG_LEVEL)
45 #define wxDEBUG_LEVEL 1
46 #endif // !defined(wxDEBUG_LEVEL)
47
48 /*
49 __WXDEBUG__ is defined when wxDEBUG_LEVEL != 0. This is done mostly for
50 compatibility but it also provides a simpler way to check if asserts and
51 debug logging is enabled at all.
52 */
53 #if wxDEBUG_LEVEL > 0
54 #ifndef __WXDEBUG__
55 #define __WXDEBUG__
56 #endif
57 #else
58 #undef __WXDEBUG__
59 #endif
60
61 // Finally there is also a very old WXDEBUG macro not used anywhere at all, it
62 // is only defined for compatibility.
63 #ifdef __WXDEBUG__
64 #if !defined(WXDEBUG) || !WXDEBUG
65 #undef WXDEBUG
66 #define WXDEBUG 1
67 #endif // !WXDEBUG
68 #endif // __WXDEBUG__
69
70 // ----------------------------------------------------------------------------
71 // Handling assertion failures
72 // ----------------------------------------------------------------------------
73
74 /*
75 Type for the function called in case of assert failure, see
76 wxSetAssertHandler().
77 */
78 typedef void (*wxAssertHandler_t)(const wxString& file,
79 int line,
80 const wxString& func,
81 const wxString& cond,
82 const wxString& msg);
83
84 #if wxDEBUG_LEVEL
85
86 // the global assert handler function, if it is NULL asserts don't check their
87 // conditions
88 extern WXDLLIMPEXP_DATA_BASE(wxAssertHandler_t) wxTheAssertHandler;
89
90 /*
91 Sets the function to be called in case of assertion failure.
92
93 The default assert handler forwards to wxApp::OnAssertFailure() whose
94 default behaviour is, in turn, to show the standard assertion failure
95 dialog if a wxApp object exists or shows the same dialog itself directly
96 otherwise.
97
98 While usually it is enough -- and more convenient -- to just override
99 OnAssertFailure(), to handle all assertion failures, including those
100 occurring even before wxApp object creation or after its destruction you
101 need to provide your assertion handler function.
102
103 This function also provides a simple way to disable all asserts: simply
104 pass NULL pointer to it. Doing this will result in not even evaluating
105 assert conditions at all, avoiding almost all run-time cost of asserts.
106
107 Notice that this function is not MT-safe, so you should call it before
108 starting any other threads.
109
110 The return value of this function is the previous assertion handler. It can
111 be called after any pre-processing by your handler and can also be restored
112 later if you uninstall your handler.
113 */
114 inline wxAssertHandler_t wxSetAssertHandler(wxAssertHandler_t handler)
115 {
116 const wxAssertHandler_t old = wxTheAssertHandler;
117 wxTheAssertHandler = handler;
118 return old;
119 }
120
121 /*
122 Reset the default assert handler.
123
124 This may be used to enable asserts, which are disabled by default in this
125 case, for programs built in release build (NDEBUG defined).
126 */
127 extern void WXDLLIMPEXP_BASE wxSetDefaultAssertHandler();
128
129 #else // !wxDEBUG_LEVEL
130
131 // provide empty stubs in case assertions are completely disabled
132 //
133 // NB: can't use WXUNUSED() here as we're included from wx/defs.h before it is
134 // defined
135 inline wxAssertHandler_t wxSetAssertHandler(wxAssertHandler_t /* handler */)
136 {
137 return NULL;
138 }
139
140 inline void wxSetDefaultAssertHandler() { }
141
142 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
143
144 // simply a synonym for wxSetAssertHandler(NULL)
145 inline void wxDisableAsserts() { wxSetAssertHandler(NULL); }
146
147 /*
148 A macro which disables asserts for applications compiled in release build.
149
150 By default, IMPLEMENT_APP (or rather IMPLEMENT_WXWIN_MAIN) disable the
151 asserts in the applications compiled in the release build by calling this.
152 It does nothing if NDEBUG is not defined.
153 */
154 #ifdef NDEBUG
155 #define wxDISABLE_ASSERTS_IN_RELEASE_BUILD() wxDisableAsserts()
156 #else
157 #define wxDISABLE_ASSERTS_IN_RELEASE_BUILD()
158 #endif
159
160 #if wxDEBUG_LEVEL
161
162 /*
163 wxOnAssert() is used by the debugging macros defined below. Different
164 overloads are needed because these macros can be used with or without _T().
165
166 All of them are implemented in src/common/appcmn.cpp and unconditionally
167 call wxTheAssertHandler so the caller must check that it is non-NULL
168 (assert macros do it).
169 */
170
171 #if wxUSE_UNICODE
172
173 // these overloads are the ones typically used by debugging macros: we have to
174 // provide wxChar* msg version because it's common to use _T() in the macros
175 // and finally, we can't use const wx(char)* msg = NULL, because that would
176 // be ambiguous
177 //
178 // also notice that these functions can't be inline as wxString is not defined
179 // yet (and can't be as wxString code itself may use assertions)
180 extern void WXDLLIMPEXP_BASE wxOnAssert(const char *file,
181 int line,
182 const char *func,
183 const char *cond);
184
185 extern void WXDLLIMPEXP_BASE wxOnAssert(const char *file,
186 int line,
187 const char *func,
188 const char *cond,
189 const char *msg);
190
191 extern void WXDLLIMPEXP_BASE wxOnAssert(const char *file,
192 int line,
193 const char *func,
194 const char *cond,
195 const wxChar *msg);
196 #endif /* wxUSE_UNICODE */
197
198 // this version is for compatibility with wx 2.8 Unicode build only, we don't
199 // use it ourselves any more except in ANSI-only build in which case it is all
200 // we need
201 extern void WXDLLIMPEXP_BASE wxOnAssert(const wxChar *file,
202 int line,
203 const char *func,
204 const wxChar *cond,
205 const wxChar *msg = NULL);
206
207 // these overloads work when msg passed to debug macro is a string and we
208 // also have to provide wxCStrData overload to resolve ambiguity which would
209 // otherwise arise from wxASSERT( s.c_str() )
210 extern void WXDLLIMPEXP_BASE wxOnAssert(const wxString& file,
211 int line,
212 const wxString& func,
213 const wxString& cond,
214 const wxString& msg);
215
216 extern void WXDLLIMPEXP_BASE wxOnAssert(const wxString& file,
217 int line,
218 const wxString& func,
219 const wxString& cond);
220
221 extern void WXDLLIMPEXP_BASE wxOnAssert(const char *file,
222 int line,
223 const char *func,
224 const char *cond,
225 const wxCStrData& msg);
226
227 extern void WXDLLIMPEXP_BASE wxOnAssert(const char *file,
228 int line,
229 const char *func,
230 const char *cond,
231 const wxString& msg);
232
233 #endif // wxDEBUG_LEVEL
234
235
236 // ----------------------------------------------------------------------------
237 // Debugging macros
238 // ----------------------------------------------------------------------------
239
240 /*
241 Assertion macros: check if the condition is true and call assert handler
242 (which will by default notify the user about failure) if it isn't.
243
244 wxASSERT and wxFAIL macros as well as wxTrap() function do nothing at all
245 if wxDEBUG_LEVEL is 0 however they do check their conditions at default
246 debug level 1, unlike the previous wxWidgets versions.
247
248 wxASSERT_LEVEL_2 is meant to be used for "expensive" asserts which should
249 normally be disabled because they have a big impact on performance and so
250 this macro only does anything if wxDEBUG_LEVEL >= 2.
251 */
252 #if wxDEBUG_LEVEL
253 // call this function to break into the debugger unconditionally (assuming
254 // the program is running under debugger, of course)
255 extern void WXDLLIMPEXP_BASE wxTrap();
256
257 // assert checks if the condition is true and calls the assert handler with
258 // the provided message if it isn't
259 //
260 // NB: the macro is defined like this to ensure that nested if/else
261 // statements containing it are compiled in the same way whether it is
262 // defined as empty or not; also notice that we can't use ";" instead
263 // of "{}" as some compilers warn about "possible unwanted ;" then
264 #define wxASSERT_MSG(cond, msg) \
265 if ( !wxTheAssertHandler || (cond) ) \
266 {} \
267 else \
268 wxOnAssert(__FILE__, __LINE__, __WXFUNCTION__, #cond, msg)
269
270 // a version without any additional message, don't use unless condition
271 // itself is fully self-explanatory
272 #define wxASSERT(cond) wxASSERT_MSG(cond, (const char*)NULL)
273
274 // wxFAIL is a special form of assert: it always triggers (and so is
275 // usually used in normally unreachable code)
276 #define wxFAIL_COND_MSG(cond, msg) \
277 if ( !wxTheAssertHandler ) \
278 {} \
279 else \
280 wxOnAssert(__FILE__, __LINE__, __WXFUNCTION__, cond, msg)
281 #define wxFAIL_MSG(msg) wxFAIL_COND_MSG("Assert failure", msg)
282 #define wxFAIL wxFAIL_MSG((const char*)NULL)
283 #else // !wxDEBUG_LEVEL
284 #define wxTrap()
285
286 #define wxASSERT(cond)
287 #define wxASSERT_MSG(cond, msg)
288 #define wxFAIL
289 #define wxFAIL_MSG(msg)
290 #define wxFAIL_COND_MSG(cond, msg)
291 #endif // wxDEBUG_LEVEL
292
293 #if wxDEBUG_LEVEL >= 2
294 #define wxASSERT_LEVEL_2_MSG(cond, msg) wxASSERT_MSG(cond, msg)
295 #define wxASSERT_LEVEL_2(cond) wxASSERT(cond)
296 #else // wxDEBUG_LEVEL < 2
297 #define wxASSERT_LEVEL_2_MSG(cond, msg)
298 #define wxASSERT_LEVEL_2(cond)
299 #endif
300
301
302 /*
303 wxCHECK macros always check their conditions, setting debug level to 0 only
304 makes them silent in case of failure, otherwise -- including at default
305 debug level 1 -- they call the assert handler if the condition is false
306
307 They are supposed to be used only in invalid situation: for example, an
308 invalid parameter (e.g. a NULL pointer) is passed to a function. Instead of
309 dereferencing it and causing core dump the function might use
310
311 wxCHECK_RET( p != NULL, "pointer can't be NULL" )
312 */
313
314 // the generic macro: takes the condition to check, the statement to be execute
315 // in case the condition is false and the message to pass to the assert handler
316 #define wxCHECK2_MSG(cond, op, msg) \
317 if ( cond ) \
318 {} \
319 else \
320 { \
321 wxFAIL_COND_MSG(#cond, msg); \
322 op; \
323 } \
324 struct wxDummyCheckStruct /* just to force a semicolon */
325
326 // check which returns with the specified return code if the condition fails
327 #define wxCHECK_MSG(cond, rc, msg) wxCHECK2_MSG(cond, return rc, msg)
328
329 // check that expression is true, "return" if not (also FAILs in debug mode)
330 #define wxCHECK(cond, rc) wxCHECK_MSG(cond, rc, (const char*)NULL)
331
332 // check that expression is true, perform op if not
333 #define wxCHECK2(cond, op) wxCHECK2_MSG(cond, op, (const char*)NULL)
334
335 // special form of wxCHECK2: as wxCHECK, but for use in void functions
336 //
337 // NB: there is only one form (with msg parameter) and it's intentional:
338 // there is no other way to tell the caller what exactly went wrong
339 // from the void function (of course, the function shouldn't be void
340 // to begin with...)
341 #define wxCHECK_RET(cond, msg) wxCHECK2_MSG(cond, return, msg)
342
343
344 // ----------------------------------------------------------------------------
345 // Compile time asserts
346 //
347 // Unlike the normal assert and related macros above which are checked during
348 // the program run-time the macros below will result in a compilation error if
349 // the condition they check is false. This is usually used to check the
350 // expressions containing sizeof()s which cannot be tested with the
351 // preprocessor. If you can use the #if's, do use them as you can give a more
352 // detailed error message then.
353 // ----------------------------------------------------------------------------
354
355 /*
356 How this works (you don't have to understand it to be able to use the
357 macros): we rely on the fact that it is invalid to define a named bit field
358 in a struct of width 0. All the rest are just the hacks to minimize the
359 possibility of the compiler warnings when compiling this macro: in
360 particular, this is why we define a struct and not an object (which would
361 result in a warning about unused variable) and a named struct (otherwise we'd
362 get a warning about an unnamed struct not used to define an object!).
363 */
364
365 #define wxMAKE_UNIQUE_ASSERT_NAME wxMAKE_UNIQUE_NAME(wxAssert_)
366
367 /*
368 The second argument of this macro must be a valid C++ identifier and not a
369 string. I.e. you should use it like this:
370
371 wxCOMPILE_TIME_ASSERT( sizeof(int) >= 2, YourIntsAreTooSmall );
372
373 It may be used both within a function and in the global scope.
374 */
375 #if defined(__WATCOMC__)
376 /* avoid "unused symbol" warning */
377 #define wxCOMPILE_TIME_ASSERT(expr, msg) \
378 class wxMAKE_UNIQUE_ASSERT_NAME { \
379 unsigned int msg: expr; \
380 wxMAKE_UNIQUE_ASSERT_NAME() { wxUnusedVar(msg); } \
381 }
382 #else
383 #define wxCOMPILE_TIME_ASSERT(expr, msg) \
384 struct wxMAKE_UNIQUE_ASSERT_NAME { unsigned int msg: expr; }
385 #endif
386
387 /*
388 When using VC++ 6 with "Edit and Continue" on, the compiler completely
389 mishandles __LINE__ and so wxCOMPILE_TIME_ASSERT() doesn't work, provide a
390 way to make "unique" assert names by specifying a unique prefix explicitly
391 */
392 #define wxMAKE_UNIQUE_ASSERT_NAME2(text) wxCONCAT(wxAssert_, text)
393
394 #define wxCOMPILE_TIME_ASSERT2(expr, msg, text) \
395 struct wxMAKE_UNIQUE_ASSERT_NAME2(text) { unsigned int msg: expr; }
396
397 // helpers for wxCOMPILE_TIME_ASSERT below, for private use only
398 #define wxMAKE_BITSIZE_MSG(type, size) type ## SmallerThan ## size ## Bits
399
400 // a special case of compile time assert: check that the size of the given type
401 // is at least the given number of bits
402 #define wxASSERT_MIN_BITSIZE(type, size) \
403 wxCOMPILE_TIME_ASSERT(sizeof(type) * CHAR_BIT >= size, \
404 wxMAKE_BITSIZE_MSG(type, size))
405
406
407 // ----------------------------------------------------------------------------
408 // other miscellaneous debugger-related functions
409 // ----------------------------------------------------------------------------
410
411 /*
412 Return true if we're running under debugger.
413
414 Currently this only really works under Win32 and Mac in CodeWarrior builds,
415 it always returns false in other cases.
416 */
417 #if defined(__WXMAC__) || defined(__WIN32__)
418 extern bool WXDLLIMPEXP_BASE wxIsDebuggerRunning();
419 #else // !Mac
420 inline bool wxIsDebuggerRunning() { return false; }
421 #endif // Mac/!Mac
422
423 // An assert helper used to avoid warning when testing constant expressions,
424 // i.e. wxASSERT( sizeof(int) == 4 ) can generate a compiler warning about
425 // expression being always true, but not using
426 // wxASSERT( wxAssertIsEqual(sizeof(int), 4) )
427 //
428 // NB: this is made obsolete by wxCOMPILE_TIME_ASSERT() and should no
429 // longer be used.
430 extern bool WXDLLIMPEXP_BASE wxAssertIsEqual(int x, int y);
431
432 // Use of wxFalse instead of false suppresses compiler warnings about testing
433 // constant expression
434 extern WXDLLIMPEXP_DATA_BASE(const bool) wxFalse;
435
436 #define wxAssertFailure wxFalse
437
438 // This is similar to WXUNUSED() and useful for parameters which are only used
439 // in assertions.
440 #if wxDEBUG_LEVEL
441 #define WXUNUSED_UNLESS_DEBUG(param) param
442 #else
443 #define WXUNUSED_UNLESS_DEBUG(param) WXUNUSED(param)
444 #endif
445
446
447 #endif // _WX_DEBUG_H_