From aad65f130d9cb77d0e9f2b7b580c54712386f77a Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Thu, 13 Jun 2002 19:37:43 +0000
Subject: [PATCH] new wxASSERT implementation using wxAssert() helper function

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15827 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/changes.txt           |  4 ++++
 docs/latex/wx/app.tex      |  4 +++-
 docs/latex/wx/function.tex |  2 +-
 include/wx/app.h           |  8 +++++++-
 include/wx/debug.h         | 34 ++++++++++++++++++++--------------
 src/common/appcmn.cpp      | 35 ++++++++++++++++++++---------------
 6 files changed, 55 insertions(+), 32 deletions(-)

diff --git a/docs/changes.txt b/docs/changes.txt
index f9e4e4b40f..aa4c3338fd 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -34,6 +34,10 @@ wxBase:
   a function returning a pointer would stop compiling now (but this change
   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):
 
 ! the event type constants are not constants any more but are dynamically
diff --git a/docs/latex/wx/app.tex b/docs/latex/wx/app.tex
index fb6340665d..f082563254 100644
--- a/docs/latex/wx/app.tex
+++ b/docs/latex/wx/app.tex
@@ -238,7 +238,7 @@ Returns 0 under X, and the wParam of the WM\_QUIT message under Windows.
 
 \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
 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{cond}{the condition of the failed assert in string form}
+
 \docparam{msg}{the message specified as argument to 
 \helpref{wxASSERT\_MSG}{wxassertmsg} or \helpref{wxFAIL\_MSG}{wxfailmsg}, will
 be {\tt NULL} if just \helpref{wxASSERT}{wxassert} or \helpref{wxFAIL}{wxfail} 
diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex
index 5527dddcf2..188d6394a8 100644
--- a/docs/latex/wx/function.tex
+++ b/docs/latex/wx/function.tex
@@ -3435,7 +3435,7 @@ compilation error messages if the condition they check fail.
 
 \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
 is false in an assertion). It is only defined in the debug mode, in release
diff --git a/include/wx/app.h b/include/wx/app.h
index 8a355c4151..fc770fe7f5 100644
--- a/include/wx/app.h
+++ b/include/wx/app.h
@@ -330,8 +330,14 @@ public:
     // this function is called when an assert failure occurs, the base class
     // version does the normal processing (i.e. shows the usual assert failure
     // dialog box)
+    //
+    // the arguments are the place where the assert occured, the text of the
+    // assert itself and the user-specified message
 #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__
 
     // check that the wxBuildOptions object (constructed in the application
diff --git a/include/wx/debug.h b/include/wx/debug.h
index 436a05ab29..ee32d3a0eb 100644
--- a/include/wx/debug.h
+++ b/include/wx/debug.h
@@ -78,36 +78,42 @@
   */
   extern void WXDLLEXPORT wxOnAssert(const wxChar *szFile,
                                      int nLine,
+                                     const wxChar *szCond,
                                      const wxChar *szMsg = NULL);
 
   // call this function to break into the debugger uncodnitionally (assuming
   // the program is running under debugger, of course)
   extern void WXDLLEXPORT wxTrap();
 
-  /*
-    notice the usage of else at the end of wxASSERT macro: this ensures that
-    the following code
-
-        if ( ... )
-            wxASSERT(...);
-        else
-            ...
-
-    works like expected: if there were no "else", the one in the code above
-    would be matched with a wrong "if"
-  */
+  // helper function used to implement wxASSERT and wxASSERT_MSG
+  //
+  // note using "int" and not "bool" for cond to avoid VC++ warnings about
+  // implicit conversions when doing "wxAssert( pointer )" and also use of
+  // "!!cond" below to ensure that everything is converted to int
+  inline void WXDLLEXPORT wxAssert(int cond,
+                                   const wxChar *szFile,
+                                   int nLine,
+                                   const wxChar *szCond,
+                                   const wxChar *szMsg = NULL)
+  {
+      if ( !cond )
+          wxOnAssert(szFile, nLine, szCond, szMsg);
+  }
 
   // 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
   #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,
   // i.e. wxASSERT( sizeof(int) == 4 ) can generate a compiler warning about
   // expression being always true, but not using
   // 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);
 #else
   #define wxTrap()
diff --git a/src/common/appcmn.cpp b/src/common/appcmn.cpp
index 3e0ecff929..665e19c19a 100644
--- a/src/common/appcmn.cpp
+++ b/src/common/appcmn.cpp
@@ -424,23 +424,22 @@ void wxTrap()
 
 // show the assert modal dialog
 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
     static bool s_bNoAsserts = FALSE;
 
     wxChar szBuf[4096];
 
-    // make life easier for people using VC++ IDE: clicking on the message
-    // will take us immediately to the place of the failed assert
+    // make life easier for people using VC++ IDE by using this format: like
+    // this, clicking on the message will take us immediately to the place of
+    // the failed assert
     wxSnprintf(szBuf, WXSIZEOF(szBuf),
-#ifdef __VISUALC__
-               wxT("%s(%d): assert failed"),
-#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);
+               wxT("%s(%d): assert \"%s\" failed"),
+               szFile, nLine, szCond);
 
     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
-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
     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
         // behaviour
-        ShowAssertDialog(szFile, nLine, szMsg);
+        ShowAssertDialog(szFile, nLine, szCond, szMsg);
     }
     else
     {
         // let the app process it as it wants
-        wxTheApp->OnAssert(szFile, nLine, szMsg);
+        wxTheApp->OnAssert(szFile, nLine, szCond, szMsg);
     }
 
     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
-- 
2.50.0

