]> git.saurik.com Git - wxWidgets.git/commitdiff
made wxDELETE[A]() template functions instead of macros; they now also check that...
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 2 Aug 2008 22:49:01 +0000 (22:49 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 2 Aug 2008 22:49:01 +0000 (22:49 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@54940 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/defs.h
tests/Makefile.in
tests/makefile.bcc
tests/makefile.gcc
tests/makefile.vc
tests/makefile.wat
tests/misc/misctests.cpp [new file with mode: 0644]
tests/test.bkl
tests/test_test.dsp
tests/test_vc7_test.vcproj
tests/test_vc8_test.vcproj

index 36be2abf2fd3e5d8f0b94ca8165eb2d375922718..7c89f53ef76afd4a7192933ae1373f69d945321e 100644 (file)
@@ -611,15 +611,33 @@ typedef short int WXTYPE;
 #include <stddef.h>
 #endif
 
-/*  delete pointer if it is not NULL and NULL it afterwards */
-/*  (checking that it's !NULL before passing it to delete is just a */
-/*   a question of style, because delete will do it itself anyhow, but it might */
-/*   be considered as an error by some overzealous debugging implementations of */
-/*   the library, so we do it ourselves) */
-#define wxDELETE(p)      if ( (p) != NULL ) { delete p; p = NULL; }
-
-/*  delete an array and NULL it (see comments above) */
-#define wxDELETEA(p)     if ( (p) ) { delete [] (p); p = NULL; }
+#ifdef __cplusplus
+    // delete pointer if it is not NULL and NULL it afterwards
+    template <typename T>
+    inline void wxDELETE(T*& ptr)
+    {
+        typedef char TypeIsCompleteCheck[sizeof(T)];
+
+        if ( ptr != NULL )
+        {
+            delete ptr;
+            ptr = NULL;
+        }
+    }
+
+    // delete an array and NULL it (see comments above)
+    template <typename T>
+    inline void wxDELETEA(T*& ptr)
+    {
+        typedef char TypeIsCompleteCheck[sizeof(T)];
+
+        if ( ptr != NULL )
+        {
+            delete [] ptr;
+            ptr = NULL;
+        }
+    }
+#endif /*__cplusplus*/
 
 /*  size of statically declared array */
 #define WXSIZEOF(array)   (sizeof(array)/sizeof(array[0]))
index 7de6fe5e8817717609cd39b65b200c6a6888d576..e10f222a21212d6e2989d38a39b99b40fbd1512d 100644 (file)
@@ -71,6 +71,7 @@ TEST_OBJECTS =  \
        test_longlongtest.o \
        test_convautotest.o \
        test_mbconvtest.o \
+       test_misctests.o \
        test_ipc.o \
        test_regextest.o \
        test_wxregextest.o \
@@ -395,6 +396,9 @@ test_convautotest.o: $(srcdir)/mbconv/convautotest.cpp $(TEST_ODEP)
 test_mbconvtest.o: $(srcdir)/mbconv/mbconvtest.cpp $(TEST_ODEP)
        $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/mbconv/mbconvtest.cpp
 
+test_misctests.o: $(srcdir)/misc/misctests.cpp $(TEST_ODEP)
+       $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/misc/misctests.cpp
+
 test_ipc.o: $(srcdir)/net/ipc.cpp $(TEST_ODEP)
        $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/net/ipc.cpp
 
index f5d622f02e47add812a7036f845e3a9b81603b3e..6915d58ff47009c19ba2a97a475b1212890cdfa0 100644 (file)
@@ -57,6 +57,7 @@ TEST_OBJECTS =  \
        $(OBJS)\test_longlongtest.obj \
        $(OBJS)\test_convautotest.obj \
        $(OBJS)\test_mbconvtest.obj \
+       $(OBJS)\test_misctests.obj \
        $(OBJS)\test_ipc.obj \
        $(OBJS)\test_regextest.obj \
        $(OBJS)\test_wxregextest.obj \
@@ -424,6 +425,9 @@ $(OBJS)\test_convautotest.obj: .\mbconv\convautotest.cpp
 $(OBJS)\test_mbconvtest.obj: .\mbconv\mbconvtest.cpp
        $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\mbconv\mbconvtest.cpp
 
+$(OBJS)\test_misctests.obj: .\misc\misctests.cpp
+       $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\misc\misctests.cpp
+
 $(OBJS)\test_ipc.obj: .\net\ipc.cpp
        $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\net\ipc.cpp
 
index e53f54bb2eed751b352a6f2d148bc7123b48092f..2d220fceff5730b649167d362e317f406c1cbe6e 100644 (file)
@@ -49,6 +49,7 @@ TEST_OBJECTS =  \
        $(OBJS)\test_longlongtest.o \
        $(OBJS)\test_convautotest.o \
        $(OBJS)\test_mbconvtest.o \
+       $(OBJS)\test_misctests.o \
        $(OBJS)\test_ipc.o \
        $(OBJS)\test_regextest.o \
        $(OBJS)\test_wxregextest.o \
@@ -402,6 +403,9 @@ $(OBJS)\test_convautotest.o: ./mbconv/convautotest.cpp
 $(OBJS)\test_mbconvtest.o: ./mbconv/mbconvtest.cpp
        $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
 
+$(OBJS)\test_misctests.o: ./misc/misctests.cpp
+       $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
+
 $(OBJS)\test_ipc.o: ./net/ipc.cpp
        $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
 
index 225bda8f92672f2f930b0ce063e948f45d02cbcb..280abafcf0b216d5814c446e8feaad4ea5b108bc 100644 (file)
@@ -50,6 +50,7 @@ TEST_OBJECTS =  \
        $(OBJS)\test_longlongtest.obj \
        $(OBJS)\test_convautotest.obj \
        $(OBJS)\test_mbconvtest.obj \
+       $(OBJS)\test_misctests.obj \
        $(OBJS)\test_ipc.obj \
        $(OBJS)\test_regextest.obj \
        $(OBJS)\test_wxregextest.obj \
@@ -509,6 +510,9 @@ $(OBJS)\test_convautotest.obj: .\mbconv\convautotest.cpp
 $(OBJS)\test_mbconvtest.obj: .\mbconv\mbconvtest.cpp
        $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\mbconv\mbconvtest.cpp
 
+$(OBJS)\test_misctests.obj: .\misc\misctests.cpp
+       $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\misc\misctests.cpp
+
 $(OBJS)\test_ipc.obj: .\net\ipc.cpp
        $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\net\ipc.cpp
 
index c177331470876d3b65b860f9f6af609fa831563d..ece7379d06723e0d9f724128762797a5bbf58825 100644 (file)
@@ -262,6 +262,7 @@ TEST_OBJECTS =  &
        $(OBJS)\test_longlongtest.obj &
        $(OBJS)\test_convautotest.obj &
        $(OBJS)\test_mbconvtest.obj &
+       $(OBJS)\test_misctests.obj &
        $(OBJS)\test_ipc.obj &
        $(OBJS)\test_regextest.obj &
        $(OBJS)\test_wxregextest.obj &
@@ -455,6 +456,9 @@ $(OBJS)\test_convautotest.obj :  .AUTODEPEND .\mbconv\convautotest.cpp
 $(OBJS)\test_mbconvtest.obj :  .AUTODEPEND .\mbconv\mbconvtest.cpp
        $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
 
+$(OBJS)\test_misctests.obj :  .AUTODEPEND .\misc\misctests.cpp
+       $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
+
 $(OBJS)\test_ipc.obj :  .AUTODEPEND .\net\ipc.cpp
        $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
 
diff --git a/tests/misc/misctests.cpp b/tests/misc/misctests.cpp
new file mode 100644 (file)
index 0000000..75b1b55
--- /dev/null
@@ -0,0 +1,71 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        tests/misc/misctests.cpp
+// Purpose:     test miscellaneous stuff
+// Author:      Peter Most
+// Created:     2008-07-10
+// RCS-ID:      $Id$
+// Copyright:   (c) 2008 Peter Most
+///////////////////////////////////////////////////////////////////////////////
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
+#include "testprec.h"
+
+#ifdef __BORLANDC__
+    #pragma hdrstop
+#endif
+
+#include "wx/defs.h"
+
+// ----------------------------------------------------------------------------
+// test class
+// ----------------------------------------------------------------------------
+
+class MiscTestCase : public CppUnit::TestCase
+{
+public:
+    MiscTestCase() { }
+
+private:
+    CPPUNIT_TEST_SUITE( MiscTestCase );
+        CPPUNIT_TEST( Delete );
+    CPPUNIT_TEST_SUITE_END();
+
+    void Delete();
+
+    DECLARE_NO_COPY_CLASS(MiscTestCase)
+};
+
+// register in the unnamed registry so that these tests are run by default
+CPPUNIT_TEST_SUITE_REGISTRATION( MiscTestCase );
+
+// also include in it's own registry so that these tests can be run alone
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscTestCase, "MiscTestCase" );
+
+void MiscTestCase::Delete()
+{
+    // Allocate some arbitrary memory to get a valid pointer:
+    long *pointer = new long;
+    CPPUNIT_ASSERT( pointer != NULL );
+
+    // Check that wxDELETE sets the pointer to NULL:
+    wxDELETE( pointer );
+    CPPUNIT_ASSERT( pointer == NULL );
+
+    // Allocate some arbitrary array to get a valid pointer:
+    long *array = new long[ 3 ];
+    CPPUNIT_ASSERT( array != NULL );
+
+    // Check that wxDELETEA sets the pointer to NULL:
+    wxDELETE( array );
+    CPPUNIT_ASSERT( array == NULL );
+
+    // this results in compilation error, as it should
+#if 0
+    struct SomeUnknownStruct *p = NULL;
+    wxDELETE(p);
+#endif
+}
+
index 34e49912ab0c68550acbec9507c1eb56a86c55f2..0d15a28298fcb72a161a6b0d742f0a4d7de9a6a4 100644 (file)
@@ -43,6 +43,7 @@
             longlong/longlongtest.cpp
             mbconv/convautotest.cpp
             mbconv/mbconvtest.cpp
+            misc/misctests.cpp
             net/ipc.cpp
             regex/regextest.cpp
             regex/wxregextest.cpp
index f7d86fa6864f9b6f3340f6dc29c5c2edeb21f032..febddb5fa55ab439793fc44802ab506ce36e3768 100644 (file)
@@ -361,6 +361,10 @@ SOURCE=.\streams\memstream.cpp
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=.\misc\misctests.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=.\thread\queue.cpp\r
 # End Source File\r
 # Begin Source File\r
index 63fdfa870177f1407439bcc6f323a94150e48af8..ca8b6b72685ab308f9c974327363ba7f08916530 100644 (file)
                                RelativePath=".\mbconv\mbconvtest.cpp"/>\r
                        <File\r
                                RelativePath=".\streams\memstream.cpp"/>\r
+                       <File\r
+                               RelativePath=".\misc\misctests.cpp"/>\r
                        <File\r
                                RelativePath=".\thread\queue.cpp"/>\r
                        <File\r
index d20c74ab47aaa24ca699d52ac404692079910b3a..8633de6d4101f4bb7e7018c3d3b573fe7f65c97f 100644 (file)
                        <File\r
                                RelativePath=".\streams\memstream.cpp"\r
                        />\r
+                       <File\r
+                               RelativePath=".\misc\misctests.cpp"\r
+                       />\r
                        <File\r
                                RelativePath=".\thread\queue.cpp"\r
                        />\r