]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/debug.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Misc debug functions and macros
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
17 #include "wx/wxchar.h"
19 // ----------------------------------------------------------------------------
20 // Defines controlling the debugging macros
21 // ----------------------------------------------------------------------------
23 // if _DEBUG is defined (MS VC++ and others use it in debug builds), define
28 #endif // !__WXDEBUG__
31 // if NDEBUG is defined (<assert.h> uses it), undef __WXDEBUG__ and WXDEBUG
37 // if __WXDEBUG__ is defined, make sure that WXDEBUG is defined and >= 1
39 #if !defined(WXDEBUG) || !WXDEBUG
45 // ----------------------------------------------------------------------------
48 // All debugging macros rely on ASSERT() which in turn calls user-defined
49 // OnAssert() function. To keep things simple, it's called even when the
50 // expression is TRUE (i.e. everything is ok) and by default does nothing: just
51 // returns the same value back. But if you redefine it to do something more sexy
52 // (popping up a message box in your favourite GUI, sending you e-mail or
53 // whatever) it will affect all ASSERTs, FAILs and CHECKs in your code.
55 // Warning: if you don't like advices on programming style, don't read
58 // Extensive use of these macros is recommended! Remember that ASSERTs are
59 // disabled in final (without __WXDEBUG__ defined) build, so they add strictly
60 // nothing to your program's code. On the other hand, CHECK macros do stay
61 // even in release builds, but in general are not much of a burden, while
62 // a judicious use of them might increase your program's stability.
63 // ----------------------------------------------------------------------------
65 // Macros which are completely disabled in 'release' mode
67 // NB: these functions are implemented in src/common/appcmn.cpp
70 this function may be redefined to do something non trivial and is called
71 whenever one of debugging macros fails (i.e. condition is false in an
75 szFile and nLine - file name and line number of the ASSERT
76 szMsg - optional message explaining the reason
78 extern void WXDLLEXPORT
wxOnAssert(const wxChar
*szFile
,
80 const wxChar
*szMsg
= NULL
);
82 // call this function to break into the debugger uncodnitionally (assuming
83 // the program is running under debugger, of course)
84 extern void WXDLLEXPORT
wxTrap();
87 notice the usage of else at the end of wxASSERT macro: this ensures that
95 works like expected: if there were no "else", the one in the code above
96 would be matched with a wrong "if"
99 // generic assert macro
100 #define wxASSERT(cond) if ( !(cond) ) wxOnAssert(__TFILE__, __LINE__); else
102 // assert with additional message explaining it's cause
103 #define wxASSERT_MSG(cond, msg) \
104 if ( !(cond) ) wxOnAssert(__TFILE__, __LINE__, msg); else
106 // an assert helper used to avoid warning when testing constant expressions,
107 // i.e. wxASSERT( sizeof(int) == 4 ) can generate a compiler warning about
108 // expression being always true, but not using
109 // wxASSERT( wxAssertIsEqual(sizeof(int), 4) )
110 extern bool WXDLLEXPORT
wxAssertIsEqual(int x
, int y
);
114 // nothing to do in release modes (hopefully at this moment there are
116 #define wxASSERT(cond)
117 #define wxASSERT_MSG(x, m)
120 // Use of wxFalse instead of FALSE suppresses compiler warnings about testing
121 // constant expression
122 WXDLLEXPORT_DATA(extern const bool) wxFalse
;
124 // special form of assert: always triggers it (in debug mode)
125 #define wxFAIL wxASSERT(wxFalse)
127 // FAIL with some message
128 #define wxFAIL_MSG(msg) wxASSERT_MSG(wxFalse, msg)
130 // NB: the following macros work also in release mode!
133 These macros must be used only in invalid situation: for example, an
134 invalid parameter (NULL pointer) is passed to a function. Instead of
135 dereferencing it and causing core dump the function might try using
136 CHECK( p != NULL ) or CHECK( p != NULL, return LogError("p is NULL!!") )
139 // check that expression is true, "return" if not (also FAILs in debug mode)
140 #define wxCHECK(x, rc) if (!(x)) {wxFAIL; return rc; }
142 // as wxCHECK but with a message explaining why we fail
143 #define wxCHECK_MSG(x, rc, msg) if (!(x)) {wxFAIL_MSG(msg); return rc; }
145 // check that expression is true, perform op if not
146 #define wxCHECK2(x, op) if (!(x)) {wxFAIL; op; }
148 // as wxCHECK2 but with a message explaining why we fail
149 #define wxCHECK2_MSG(x, op, msg) if (!(x)) {wxFAIL_MSG(msg); op; }
151 // special form of wxCHECK2: as wxCHECK, but for use in void functions
153 // NB: there is only one form (with msg parameter) and it's intentional:
154 // there is no other way to tell the caller what exactly went wrong
155 // from the void function (of course, the function shouldn't be void
157 #define wxCHECK_RET(x, msg) if (!(x)) {wxFAIL_MSG(msg); return; }
159 #endif // _WX_DEBUG_H_