1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWeakRef - Generic weak references for wxWidgets
4 // Author: Arne Steinarson
7 // Copyright: (c) 2007 Arne Steinarson
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_WEAKREF_H_
12 #define _WX_WEAKREF_H_
14 #include <wx/tracker.h>
16 // A weak reference to an object of type T, where T has type wxTrackable
17 // as one of its base classes (in a static or dynamic sense).
19 class wxWeakRef
: public wxTrackerNode
22 typedef T element_type
;
24 wxWeakRef(T
*pobj
= NULL
) : m_pobj(NULL
) { Assign(pobj
); }
26 virtual ~wxWeakRef() { Assign(NULL
); }
28 // test for pointer validity: defining conversion to unspecified_bool_type
29 // and not more obvious bool to avoid implicit conversions to integer types
30 typedef T
*(wxWeakRef
<T
>::*unspecified_bool_type
)() const;
31 operator unspecified_bool_type() const
33 return m_pobj
? &wxWeakRef
<T
>::get
: NULL
;
43 wxASSERT(m_pobj
!= NULL
);
49 wxASSERT(m_pobj
!= NULL
);
59 // Assign from another weak ref, point to same object
60 T
* operator = (const wxWeakRef
<T
> &wr
)
66 virtual void OnObjectDestroy()
68 // Tracked object itself removes us from list of trackers
74 friend class wxTrackableBase
;
75 friend class wxEvtHandler
;
77 virtual wxTrackerNodeType
GetType() { return WeakRef
; }
84 // First release old object if any
87 // Remove ourselves from object tracker list
88 // This does static_cast if available, otherwise it tries dynamic cast
89 wxTrackableBase
*pt
= wxTrackableCaster
<T
,wxHasBase
<T
,wxTrackableBase
>::value
>::Cast(m_pobj
);
95 // Now set new trackable object
98 wxTrackableBase
*pt
= wxTrackableCaster
<T
,wxHasBase
<T
,wxTrackableBase
>::value
>::Cast(pobj
);
106 // If the tracked we want to track does not support wxTackableBase, then
107 // log a message and keep the NULL object pointer.
108 wxLogWarning( _T("wxWeakRef::Assign - Type does not provide wxTrackableBase - resetting tracked object") );
116 // Provide some basic types of weak references
117 class WXDLLIMPEXP_FWD_BASE wxObject
;
118 class WXDLLIMPEXP_FWD_BASE wxEvtHandler
;
119 class WXDLLIMPEXP_FWD_CORE wxWindow
;
121 typedef wxWeakRef
<wxObject
> wxObjectRef
;
122 typedef wxWeakRef
<wxEvtHandler
> wxEvtHandlerRef
;
123 typedef wxWeakRef
<wxWindow
> wxWindowRef
;
125 #endif // _WX_WEAKREF_H_