2008-01-06 13:01:28 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/weakref.h
|
|
|
|
// Purpose: wxWeakRef - Generic weak references for wxWidgets
|
|
|
|
// Author: Arne Steinarson
|
|
|
|
// Created: 2007-12-27
|
2008-01-08 04:55:21 -05:00
|
|
|
// RCS-ID: $Id$
|
2008-01-06 13:01:28 -05:00
|
|
|
// Copyright: (c) 2007 Arne Steinarson
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_WEAKREF_H_
|
|
|
|
#define _WX_WEAKREF_H_
|
|
|
|
|
|
|
|
#include <wx/tracker.h>
|
|
|
|
|
|
|
|
// A weak reference to an object of type T, where T has type wxTrackable
|
|
|
|
// as one of its base classes (in a static or dynamic sense).
|
|
|
|
template<class T>
|
|
|
|
class wxWeakRef : public wxTrackerNode
|
|
|
|
{
|
|
|
|
public:
|
2008-01-08 04:55:21 -05:00
|
|
|
typedef T element_type;
|
|
|
|
|
2008-01-06 13:01:28 -05:00
|
|
|
wxWeakRef(T *pobj = NULL) : m_pobj(NULL) { Assign(pobj); }
|
|
|
|
|
|
|
|
virtual ~wxWeakRef() { Assign(NULL); }
|
|
|
|
|
2008-01-08 04:55:21 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-01-06 13:01:28 -05:00
|
|
|
T* operator=(T *pobj)
|
|
|
|
{
|
|
|
|
Assign(pobj);
|
|
|
|
return m_pobj;
|
|
|
|
}
|
|
|
|
|
2008-01-08 04:55:21 -05:00
|
|
|
// Assign from another weak ref, point to same object
|
|
|
|
T* operator = (const wxWeakRef<T> &wr)
|
|
|
|
{
|
|
|
|
Assign(wr);
|
|
|
|
return m_pobj;
|
|
|
|
}
|
|
|
|
|
2008-01-06 13:01:28 -05:00
|
|
|
virtual void OnObjectDestroy()
|
|
|
|
{
|
|
|
|
// Tracked object itself removes us from list of trackers
|
|
|
|
wxASSERT( m_pobj );
|
|
|
|
m_pobj = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2008-01-08 04:55:21 -05:00
|
|
|
friend class wxTrackableBase;
|
|
|
|
friend class wxEvtHandler;
|
2008-01-06 13:01:28 -05:00
|
|
|
|
2008-01-08 04:55:21 -05:00
|
|
|
virtual wxTrackerNodeType GetType() { return WeakRef; }
|
|
|
|
|
2008-01-06 13:01:28 -05:00
|
|
|
void Assign(T* pobj)
|
|
|
|
{
|
|
|
|
if ( m_pobj == pobj )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// First release old object if any
|
|
|
|
if ( m_pobj )
|
|
|
|
{
|
|
|
|
// Remove ourselves from object tracker list
|
2008-01-08 04:55:21 -05:00
|
|
|
// This does static_cast if available, otherwise it tries dynamic cast
|
|
|
|
wxTrackableBase *pt = wxTrackableCaster<T,wxHasBase<T,wxTrackableBase>::value >::Cast(m_pobj);
|
|
|
|
wxASSERT(pt);
|
|
|
|
pt->RemoveNode(this);
|
2008-01-06 13:01:28 -05:00
|
|
|
m_pobj = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now set new trackable object
|
|
|
|
if ( pobj )
|
|
|
|
{
|
2008-01-08 04:55:21 -05:00
|
|
|
wxTrackableBase *pt = wxTrackableCaster<T,wxHasBase<T,wxTrackableBase>::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") );
|
|
|
|
}
|
2008-01-06 13:01:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T *m_pobj;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Provide some basic types of weak references
|
|
|
|
class WXDLLIMPEXP_FWD_BASE wxObject;
|
|
|
|
class WXDLLIMPEXP_FWD_BASE wxEvtHandler;
|
|
|
|
class WXDLLIMPEXP_FWD_CORE wxWindow;
|
|
|
|
|
|
|
|
typedef wxWeakRef<wxObject> wxObjectRef;
|
|
|
|
typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
|
|
|
|
typedef wxWeakRef<wxWindow> wxWindowRef;
|
|
|
|
|
|
|
|
#endif // _WX_WEAKREF_H_
|
|
|
|
|