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 #include "wx/meta/convertible.h"
17 #include "wx/meta/int2type.h"
20 struct wxIsStaticTrackable
22 enum { value
= wxConvertibleTo
<T
, wxTrackable
>::value
};
25 // Weak ref implementation when T has wxTrackable as a known base class
27 class wxWeakRefStatic
: public wxTrackerNode
30 wxWeakRefStatic() : m_pobj(NULL
) { }
34 // Release old object if any
37 // Remove ourselves from object tracker list
38 wxTrackable
*pt
= static_cast<wxTrackable
*>(m_pobj
);
52 // Now set new trackable object
55 // Add ourselves to object tracker list
56 wxTrackable
*pt
= static_cast<wxTrackable
*>(pobj
);
62 void AssignCopy(const wxWeakRefStatic
& wr
)
67 virtual void OnObjectDestroy()
69 // Tracked object itself removes us from list of trackers
70 wxASSERT( m_pobj
!=NULL
);
77 #ifdef HAVE_PARTIAL_SPECIALIZATION
79 template<class T
,bool use_static
>
82 // Intermediate class, to select the static case above.
84 struct wxWeakRefImpl
<T
, true> : public wxWeakRefStatic
<T
>
89 // Weak ref implementation when T does not have wxTrackable as known base class
91 struct wxWeakRefImpl
<T
, false> : public wxTrackerNode
95 // Release old object if any
98 // Remove ourselves from object tracker list
99 m_ptbase
->RemoveNode(this);
106 wxWeakRefImpl() : m_pobj(NULL
), m_ptbase(NULL
) { }
108 // Assign receives most derived class here and can use that
109 template <class TDerived
>
110 void Assign( TDerived
* pobj
)
112 AssignHelper( pobj
, wxInt2Type
<wxIsStaticTrackable
<TDerived
>::value
>() );
115 template <class TDerived
>
116 void AssignHelper(TDerived
* pobj
, wxInt2Type
<true>)
118 wxTrackable
*ptbase
= static_cast<wxTrackable
*>(pobj
);
119 DoAssign( pobj
, ptbase
);
122 #ifdef HAVE_DYNAMIC_CAST
123 void AssignHelper(T
* pobj
, wxInt2Type
<false>)
125 // A last way to get a trackable pointer
126 wxTrackable
*ptbase
= dynamic_cast<wxTrackable
*>(pobj
);
129 DoAssign( pobj
, ptbase
);
133 wxFAIL_MSG( "Tracked class should inherit from wxTrackable" );
138 #endif // HAVE_DYNAMIC_CAST
140 void AssignCopy(const wxWeakRefImpl
& wr
)
142 DoAssign(wr
.m_pobj
, wr
.m_ptbase
);
145 void DoAssign( T
* pobj
, wxTrackable
*ptbase
) {
146 if( m_pobj
==pobj
) return;
149 // Now set new trackable object
152 // Add ourselves to object tracker list
154 ptbase
->AddNode( this );
160 virtual void OnObjectDestroy()
162 // Tracked object itself removes us from list of trackers
163 wxASSERT( m_pobj
!=NULL
);
169 wxTrackable
*m_ptbase
;
172 #endif // HAVE_PARTIAL_SPECIALIZATION
175 // A weak reference to an object of type T, where T has type wxTrackable
176 // (usually statically but if not dynamic_cast<> is tried).
178 class wxWeakRef
: public
179 #ifdef HAVE_PARTIAL_SPECIALIZATION
180 wxWeakRefImpl
<T
, wxIsStaticTrackable
<T
>::value
>
189 // When we have the full type here, static_cast<> will always work
190 // (or give a straight compiler error).
191 template <class TDerived
>
192 wxWeakRef(TDerived
* pobj
)
197 template <class TDerived
>
198 wxWeakRef
<T
>& operator=(TDerived
* pobj
)
204 wxWeakRef
<T
>& operator=(const wxWeakRef
<T
>& wr
)
210 virtual ~wxWeakRef() { this->Release(); }
212 // Smart pointer functions
213 T
& operator*() const { return *this->m_pobj
; }
214 T
* operator->() const { return this->m_pobj
; }
216 T
* get() const { return this->m_pobj
; }
217 operator T
*() const { return get(); }
221 // Weak ref implementation assign objects are queried for wxTrackable
222 // using dynamic_cast<>
224 class wxWeakRefDynamic
: public wxTrackerNode
227 wxWeakRefDynamic() : m_pobj(NULL
) { }
229 wxWeakRefDynamic(T
* pobj
) : m_pobj(pobj
)
234 virtual ~wxWeakRefDynamic() { Release(); }
236 // Smart pointer functions
237 T
& operator * (){ wxASSERT(this->m_pobj
); return *m_pobj
; }
238 T
* operator -> (){ wxASSERT(this->m_pobj
); return m_pobj
; }
239 T
* operator = (T
* pobj
) { Assign(pobj
); return m_pobj
; }
241 T
* get(){ return this->m_pobj
; }
243 // operator T* (){ return this->m_pobj; }
245 // test for pointer validity: defining conversion to unspecified_bool_type
246 // and not more obvious bool to avoid implicit conversions to integer types
247 typedef T
*(wxWeakRef
<T
>::*unspecified_bool_type
)() const;
248 operator unspecified_bool_type() const
250 return m_pobj
? &wxWeakRef
<T
>::get
: NULL
;
253 // Assign from another weak ref, point to same object
254 T
* operator = (const wxWeakRef
<T
> &wr
) { Assign( wr
.get() ); return this->m_pobj
; }
258 // Release old object if any
261 // Remove ourselves from object tracker list
262 wxTrackable
*pt
= dynamic_cast<wxTrackable
*>(m_pobj
);
264 pt
->RemoveNode(this);
272 if ( m_pobj
== pobj
)
277 // Now set new trackable object
280 // Add ourselves to object tracker list
281 wxTrackable
*pt
= dynamic_cast<wxTrackable
*>(pobj
);
289 // If the object we want to track does not support wxTackable, then
290 // log a message and keep the NULL object pointer.
291 wxFAIL_MSG( "Tracked class should inherit from wxTrackable" );
296 virtual void OnObjectDestroy()
298 wxASSERT_MSG( m_pobj
, "tracked object should have removed us itself" );
306 // Provide some basic types of weak references
307 class WXDLLIMPEXP_FWD_BASE wxEvtHandler
;
308 class WXDLLIMPEXP_FWD_BASE wxWindow
;
310 typedef wxWeakRef
<wxEvtHandler
> wxEvtHandlerRef
;
311 typedef wxWeakRef
<wxWindow
> wxWindowRef
;
313 #endif // _WX_WEAKREF_H_