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 wxWeakRef(T
*pobj
= NULL
) : m_pobj(NULL
) { Assign(pobj
); }
24 virtual ~wxWeakRef() { Assign(NULL
); }
26 // Smart pointer functions
27 operator T
*(){ return m_pobj
; }
28 T
* operator->(){ return m_pobj
; }
35 virtual void OnObjectDestroy()
37 // Tracked object itself removes us from list of trackers
42 virtual wxTrackerNodeType
GetType() { return WeakRef
; }
45 wxTrackableBase
*GetTrackable(T
*pobj
)
47 // this uses static_cast if possible or dynamic_cast otherwise
48 return wxTrackableCaster
<T
, wxHasBase
<T
, wxTrackableBase
>::value
>
57 // First release old object if any
60 // Remove ourselves from object tracker list
61 GetTrackable(m_pobj
)->RemoveNode(this);
65 // Now set new trackable object
68 wxTrackableBase
* const pt
= GetTrackable(pobj
);
69 wxCHECK_RET( pt
, "type must derive from wxTrackableBase" );
79 // Provide some basic types of weak references
80 class WXDLLIMPEXP_FWD_BASE wxObject
;
81 class WXDLLIMPEXP_FWD_BASE wxEvtHandler
;
82 class WXDLLIMPEXP_FWD_CORE wxWindow
;
84 typedef wxWeakRef
<wxObject
> wxObjectRef
;
85 typedef wxWeakRef
<wxEvtHandler
> wxEvtHandlerRef
;
86 typedef wxWeakRef
<wxWindow
> wxWindowRef
;
88 #endif // _WX_WEAKREF_H_