Add non-template wxWeakRef<T> constructor from T*.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 10 Apr 2010 18:13:29 +0000 (18:13 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 10 Apr 2010 18:13:29 +0000 (18:13 +0000)
We need a non-template ctor to make the code initializing wxWeakRef with
literal NULL to compile.

Closes #11916.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63934 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/weakref.h
tests/weakref/weakref.cpp

index 96ed48ee6f35619419f784d489cd13febc91723f..df31c2fdc4abd612c3d8c9859d1c5d07935857b2 100644 (file)
@@ -211,6 +211,13 @@ public:
     // Default ctor
     wxWeakRef() { }
 
+    // Ctor from the object of this type: this is needed as the template ctor
+    // below is not used by at least g++4 when a literal NULL is used
+    wxWeakRef(T *pobj)
+    {
+        Assign(pobj);
+    }
+
     // When we have the full type here, static_cast<> will always work
     // (or give a straight compiler error).
     template <class TDerived>
index 2c0eea61be63b4f0199c6ebc4cbf21564a7d37c5..3550d138055c243ff392d29fff8114c8ae85eabe 100644 (file)
@@ -79,6 +79,10 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WeakRefTestCase, "WeakRefTestCase" );
 void WeakRefTestCase::DeclareTest()
 {
     {
+        // Not initializing or initializing with NULL should work too
+        wxWeakRef<wxEvtHandler> wroDef;
+        wxWeakRef<wxEvtHandler> wro0(NULL);
+
         wxObject o; // Should not work
         wxEvtHandler eh;
         wxObjectTrackable ot;
@@ -123,6 +127,19 @@ void WeakRefTestCase::AssignTest()
     // Should be reset now
     CPPUNIT_ASSERT( !wro1 );
     CPPUNIT_ASSERT( !wro2 );
+
+    // Explicitly resetting should work too
+    wxEvtHandler eh;
+    wxObjectTrackable ot;
+
+    wro1 = &eh;
+    wro2 = &ot;
+
+    wro1 = NULL;
+    wro2 = NULL;
+
+    CPPUNIT_ASSERT( !wro1 );
+    CPPUNIT_ASSERT( !wro2 );
 }
 
 void WeakRefTestCase::AssignWeakRefTest()