X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/db7035e48a4ccc6265fa01949cb92db3c6b6c17f..f8b1df0b15f843b8f47efb9bee38f1de0fa95020:/include/wx/weakref.h diff --git a/include/wx/weakref.h b/include/wx/weakref.h index 7d8ac8c5b9..3d4c445774 100644 --- a/include/wx/weakref.h +++ b/include/wx/weakref.h @@ -3,7 +3,7 @@ // Purpose: wxWeakRef - Generic weak references for wxWidgets // Author: Arne Steinarson // Created: 2007-12-27 -// RCS-ID: $Id:$ +// RCS-ID: $Id$ // Copyright: (c) 2007 Arne Steinarson // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -19,19 +19,50 @@ template class wxWeakRef : public wxTrackerNode { public: + typedef T element_type; + wxWeakRef(T *pobj = NULL) : m_pobj(NULL) { Assign(pobj); } virtual ~wxWeakRef() { Assign(NULL); } - // Smart pointer functions - operator T*(){ return m_pobj; } - T* operator->(){ return m_pobj; } + // test for pointer validity: defining conversion to unspecified_bool_type + // and not more obvious bool to avoid implicit conversions to integer types + typedef T *(wxWeakRef::*unspecified_bool_type)() const; + operator unspecified_bool_type() const + { + return m_pobj ? &wxWeakRef::get : NULL; + } + + T * get() const + { + return m_pobj; + } + + T* operator->() + { + wxASSERT(m_pobj != NULL); + return m_pobj; + } + + T& operator*() const + { + wxASSERT(m_pobj != NULL); + return *m_pobj; + } + T* operator=(T *pobj) { Assign(pobj); return m_pobj; } + // Assign from another weak ref, point to same object + T* operator = (const wxWeakRef &wr) + { + Assign(wr); + return m_pobj; + } + virtual void OnObjectDestroy() { // Tracked object itself removes us from list of trackers @@ -39,16 +70,12 @@ public: m_pobj = NULL; } - virtual wxTrackerNodeType GetType() { return WeakRef; } - protected: - wxTrackableBase *GetTrackable(T *pobj) - { - // this uses static_cast if possible or dynamic_cast otherwise - return wxTrackableCaster::value> - ::Cast(pobj); - } + friend class wxTrackableBase; + friend class wxEvtHandler; + virtual wxTrackerNodeType GetType() { return WeakRef; } + void Assign(T* pobj) { if ( m_pobj == pobj ) @@ -58,18 +85,28 @@ protected: if ( m_pobj ) { // Remove ourselves from object tracker list - GetTrackable(m_pobj)->RemoveNode(this); + // This does static_cast if available, otherwise it tries dynamic cast + wxTrackableBase *pt = wxTrackableCaster::value >::Cast(m_pobj); + wxASSERT(pt); + pt->RemoveNode(this); m_pobj = NULL; } // Now set new trackable object if ( pobj ) { - wxTrackableBase * const pt = GetTrackable(pobj); - wxCHECK_RET( pt, "type must derive from wxTrackableBase" ); - - pt->AddNode(this); - m_pobj = pobj; + wxTrackableBase *pt = wxTrackableCaster::value >::Cast(pobj); + if( pt ) + { + pt->AddNode( this ); + m_pobj = pobj; + } + else + { + // If the tracked we want to track does not support wxTackableBase, then + // log a message and keep the NULL object pointer. + wxLogWarning( _T("wxWeakRef::Assign - Type does not provide wxTrackableBase - resetting tracked object") ); + } } }