new wxASSERT implementation using wxAssert() helper function

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15827 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2002-06-13 19:37:43 +00:00
parent b7b35e5063
commit aad65f130d
6 changed files with 55 additions and 32 deletions

View File

@@ -34,6 +34,10 @@ wxBase:
a function returning a pointer would stop compiling now (but this change a function returning a pointer would stop compiling now (but this change
is not supposed to have any effects on valid code) is not supposed to have any effects on valid code)
- another minor change: wxApp::OnAssert() has a new "cond" argument, you
must modify YourApp::OnAssert() signature if you were using it to override
the default assert handling.
All (GUI): All (GUI):
! the event type constants are not constants any more but are dynamically ! the event type constants are not constants any more but are dynamically

View File

@@ -238,7 +238,7 @@ Returns 0 under X, and the wParam of the WM\_QUIT message under Windows.
\membersection{wxApp::OnAssert}\label{wxapponassert} \membersection{wxApp::OnAssert}\label{wxapponassert}
\func{void}{OnAssert}{\param{const wxChar }{*file}, \param{int }{line}, \param{const wxChar }{*msg}} \func{void}{OnAssert}{\param{const wxChar }{*file}, \param{int }{line}, \param{const wxChar }{*cond}, \param{const wxChar }{*msg}}
This function is called when an assert failure occurs, i.e. the condition This function is called when an assert failure occurs, i.e. the condition
specified in \helpref{wxASSERT}{wxassert} macro evaluated to {\tt FALSE}. specified in \helpref{wxASSERT}{wxassert} macro evaluated to {\tt FALSE}.
@@ -254,6 +254,8 @@ the user to stop the program, continue or ignore all subsequent asserts.
\docparam{line}{the line number in this file where the assert occured} \docparam{line}{the line number in this file where the assert occured}
\docparam{cond}{the condition of the failed assert in string form}
\docparam{msg}{the message specified as argument to \docparam{msg}{the message specified as argument to
\helpref{wxASSERT\_MSG}{wxassertmsg} or \helpref{wxFAIL\_MSG}{wxfailmsg}, will \helpref{wxASSERT\_MSG}{wxassertmsg} or \helpref{wxFAIL\_MSG}{wxfailmsg}, will
be {\tt NULL} if just \helpref{wxASSERT}{wxassert} or \helpref{wxFAIL}{wxfail} be {\tt NULL} if just \helpref{wxASSERT}{wxassert} or \helpref{wxFAIL}{wxfail}

View File

@@ -3435,7 +3435,7 @@ compilation error messages if the condition they check fail.
\membersection{::wxOnAssert}\label{wxonassert} \membersection{::wxOnAssert}\label{wxonassert}
\func{void}{wxOnAssert}{\param{const char *}{fileName}, \param{int}{ lineNumber}, \param{const char *}{msg = NULL}} \func{void}{wxOnAssert}{\param{const char *}{fileName}, \param{int}{ lineNumber}, \param{const char *}{cond}, \param{const char *}{msg = NULL}}
This function is called whenever one of debugging macros fails (i.e. condition This function is called whenever one of debugging macros fails (i.e. condition
is false in an assertion). It is only defined in the debug mode, in release is false in an assertion). It is only defined in the debug mode, in release

View File

@@ -330,8 +330,14 @@ public:
// this function is called when an assert failure occurs, the base class // this function is called when an assert failure occurs, the base class
// version does the normal processing (i.e. shows the usual assert failure // version does the normal processing (i.e. shows the usual assert failure
// dialog box) // dialog box)
//
// the arguments are the place where the assert occured, the text of the
// assert itself and the user-specified message
#ifdef __WXDEBUG__ #ifdef __WXDEBUG__
virtual void OnAssert(const wxChar *file, int line, const wxChar *msg); virtual void OnAssert(const wxChar *file,
int line,
const wxChar *cond,
const wxChar *msg);
#endif // __WXDEBUG__ #endif // __WXDEBUG__
// check that the wxBuildOptions object (constructed in the application // check that the wxBuildOptions object (constructed in the application

View File

@@ -78,36 +78,42 @@
*/ */
extern void WXDLLEXPORT wxOnAssert(const wxChar *szFile, extern void WXDLLEXPORT wxOnAssert(const wxChar *szFile,
int nLine, int nLine,
const wxChar *szCond,
const wxChar *szMsg = NULL); const wxChar *szMsg = NULL);
// call this function to break into the debugger uncodnitionally (assuming // call this function to break into the debugger uncodnitionally (assuming
// the program is running under debugger, of course) // the program is running under debugger, of course)
extern void WXDLLEXPORT wxTrap(); extern void WXDLLEXPORT wxTrap();
/* // helper function used to implement wxASSERT and wxASSERT_MSG
notice the usage of else at the end of wxASSERT macro: this ensures that //
the following code // note using "int" and not "bool" for cond to avoid VC++ warnings about
// implicit conversions when doing "wxAssert( pointer )" and also use of
if ( ... ) // "!!cond" below to ensure that everything is converted to int
wxASSERT(...); inline void WXDLLEXPORT wxAssert(int cond,
else const wxChar *szFile,
... int nLine,
const wxChar *szCond,
works like expected: if there were no "else", the one in the code above const wxChar *szMsg = NULL)
would be matched with a wrong "if" {
*/ if ( !cond )
wxOnAssert(szFile, nLine, szCond, szMsg);
}
// generic assert macro // generic assert macro
#define wxASSERT(cond) if ( !(cond) ) wxOnAssert(__TFILE__, __LINE__); else #define wxASSERT(cond) wxAssert(!!(cond), __TFILE__, __LINE__, _T(#cond))
// assert with additional message explaining it's cause // assert with additional message explaining it's cause
#define wxASSERT_MSG(cond, msg) \ #define wxASSERT_MSG(cond, msg) \
if ( !(cond) ) wxOnAssert(__TFILE__, __LINE__, msg); else wxAssert(!!(cond), __TFILE__, __LINE__, _T(#cond), msg)
// an assert helper used to avoid warning when testing constant expressions, // an assert helper used to avoid warning when testing constant expressions,
// i.e. wxASSERT( sizeof(int) == 4 ) can generate a compiler warning about // i.e. wxASSERT( sizeof(int) == 4 ) can generate a compiler warning about
// expression being always true, but not using // expression being always true, but not using
// wxASSERT( wxAssertIsEqual(sizeof(int), 4) ) // wxASSERT( wxAssertIsEqual(sizeof(int), 4) )
//
// NB: this is made obsolete by wxCOMPILE_TIME_ASSERT() and shouldn't be
// used any longer
extern bool WXDLLEXPORT wxAssertIsEqual(int x, int y); extern bool WXDLLEXPORT wxAssertIsEqual(int x, int y);
#else #else
#define wxTrap() #define wxTrap()

View File

@@ -424,23 +424,22 @@ void wxTrap()
// show the assert modal dialog // show the assert modal dialog
static static
void ShowAssertDialog(const wxChar *szFile, int nLine, const wxChar *szMsg) void ShowAssertDialog(const wxChar *szFile,
int nLine,
const wxChar *szCond,
const wxChar *szMsg)
{ {
// this variable can be set to true to suppress "assert failure" messages // this variable can be set to true to suppress "assert failure" messages
static bool s_bNoAsserts = FALSE; static bool s_bNoAsserts = FALSE;
wxChar szBuf[4096]; wxChar szBuf[4096];
// make life easier for people using VC++ IDE: clicking on the message // make life easier for people using VC++ IDE by using this format: like
// will take us immediately to the place of the failed assert // this, clicking on the message will take us immediately to the place of
// the failed assert
wxSnprintf(szBuf, WXSIZEOF(szBuf), wxSnprintf(szBuf, WXSIZEOF(szBuf),
#ifdef __VISUALC__ wxT("%s(%d): assert \"%s\" failed"),
wxT("%s(%d): assert failed"), szFile, nLine, szCond);
#else // !VC++
// make the error message more clear for all the others
wxT("Assert failed in file %s at line %d"),
#endif // VC/!VC
szFile, nLine);
if ( szMsg != NULL ) if ( szMsg != NULL )
{ {
@@ -501,7 +500,10 @@ void ShowAssertDialog(const wxChar *szFile, int nLine, const wxChar *szMsg)
} }
// this function is called when an assert fails // this function is called when an assert fails
void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg) void wxOnAssert(const wxChar *szFile,
int nLine,
const wxChar *szCond,
const wxChar *szMsg)
{ {
// FIXME MT-unsafe // FIXME MT-unsafe
static bool s_bInAssert = FALSE; static bool s_bInAssert = FALSE;
@@ -522,20 +524,23 @@ void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg)
{ {
// by default, show the assert dialog box - we can't customize this // by default, show the assert dialog box - we can't customize this
// behaviour // behaviour
ShowAssertDialog(szFile, nLine, szMsg); ShowAssertDialog(szFile, nLine, szCond, szMsg);
} }
else else
{ {
// let the app process it as it wants // let the app process it as it wants
wxTheApp->OnAssert(szFile, nLine, szMsg); wxTheApp->OnAssert(szFile, nLine, szCond, szMsg);
} }
s_bInAssert = FALSE; s_bInAssert = FALSE;
} }
void wxAppBase::OnAssert(const wxChar *file, int line, const wxChar *msg) void wxAppBase::OnAssert(const wxChar *file,
int line,
const wxChar *cond,
const wxChar *msg)
{ {
ShowAssertDialog(file, line, msg); ShowAssertDialog(file, line, cond, msg);
} }
#endif //WXDEBUG #endif //WXDEBUG