From 7f2468e96a3887eb767ed6fcf452834a4c7b7a28 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 15 Apr 2010 22:47:37 +0000 Subject: [PATCH] Fix using weak references to incomplete classes. wxWeakRef can work with forward-declared classes provided dynamic_cast<> is available but this wasn't detected as being the case due to the use of the obsolete HAVE_DYNAMIC_CAST in its code. Replace HAVE_DYNAMIC_CAST with !wxNO_RTTI to fix this. Also add a unit test checking that this does work. Closes #11916. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63986 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/weakref.h | 8 ++++---- tests/weakref/weakref.cpp | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/include/wx/weakref.h b/include/wx/weakref.h index b767cafd0a..08878903f4 100644 --- a/include/wx/weakref.h +++ b/include/wx/weakref.h @@ -149,7 +149,7 @@ protected: DoAssign( pobj, ptbase ); } -#ifdef HAVE_DYNAMIC_CAST +#ifndef wxNO_RTTI void AssignHelper(T* pobj, wxInt2Type) { // A last way to get a trackable pointer @@ -165,7 +165,7 @@ protected: Release(); } } -#endif // HAVE_DYNAMIC_CAST +#endif // RTTI enabled void AssignCopy(const wxWeakRefImpl& wr) { @@ -254,7 +254,7 @@ public: }; -#ifdef HAVE_DYNAMIC_CAST +#ifndef wxNO_RTTI // Weak ref implementation assign objects are queried for wxTrackable // using dynamic_cast<> @@ -338,7 +338,7 @@ protected: T *m_pobj; }; -#endif // #ifdef HAVE_DYNAMIC_CAST +#endif // RTTI enabled // Provide some basic types of weak references diff --git a/tests/weakref/weakref.cpp b/tests/weakref/weakref.cpp index ef1b8ad0f8..163a16f401 100644 --- a/tests/weakref/weakref.cpp +++ b/tests/weakref/weakref.cpp @@ -76,6 +76,14 @@ CPPUNIT_TEST_SUITE_REGISTRATION( WeakRefTestCase ); // also include in it's own registry so that these tests can be run alone CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WeakRefTestCase, "WeakRefTestCase" ); + +// Test weak reference to an incomplete type, this should work if the type is +// fully defined before it is used (but currently doesn't, see #11916) +struct ForwardDeclaredClass; +wxWeakRef g_incompleteWeakRef; + +struct ForwardDeclaredClass : wxEvtHandler { }; + void WeakRefTestCase::DeclareTest() { { @@ -110,6 +118,17 @@ void WeakRefTestCase::DeclareTest() CPPUNIT_ASSERT( wreh.get() == &eh ); CPPUNIT_ASSERT( wrot.get() == &ot ); } + + // This test requires a working dynamic_cast<> +#ifndef wxNO_RTTI + { + ForwardDeclaredClass fdc; + g_incompleteWeakRef = &fdc; + CPPUNIT_ASSERT( g_incompleteWeakRef ); + } + + CPPUNIT_ASSERT( !g_incompleteWeakRef ); +#endif // RTTI enabled } void WeakRefTestCase::AssignTest() -- 2.45.2