improvements to wxWeakRef and related classes
[wxWidgets.git] / include / wx / weakref.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/weakref.h
3 // Purpose: wxWeakRef - Generic weak references for wxWidgets
4 // Author: Arne Steinarson
5 // Created: 27 Dec 07
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Arne Steinarson
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_WEAKREF_H_
12 #define _WX_WEAKREF_H_
13
14 #include "wx/tracker.h"
15
16 #include "wx/meta/convertible.h"
17 #include "wx/meta/int2type.h"
18
19 template <class T>
20 struct wxIsStaticTrackable
21 {
22 enum { value = wxConvertibleTo<T, wxTrackable>::value };
23 };
24
25 // Weak ref implementation when T has wxTrackable as a known base class
26 template <class T>
27 class wxWeakRefStatic : public wxTrackerNode
28 {
29 public:
30 wxWeakRefStatic() : m_pobj(NULL) { }
31
32 void Release()
33 {
34 // Release old object if any
35 if ( m_pobj )
36 {
37 // Remove ourselves from object tracker list
38 wxTrackable *pt = static_cast<wxTrackable*>(m_pobj);
39 pt->RemoveNode(this);
40 m_pobj = NULL;
41 }
42 }
43
44 protected:
45 void Assign(T* pobj)
46 {
47 if ( m_pobj == pobj )
48 return;
49
50 Release();
51
52 // Now set new trackable object
53 if ( pobj )
54 {
55 // Add ourselves to object tracker list
56 wxTrackable *pt = static_cast<wxTrackable*>(pobj);
57 pt->AddNode(this);
58 m_pobj = pobj;
59 }
60 }
61
62 void AssignCopy( const wxWeakRefStatic& wr )
63 {
64 Assign( wr.m_pobj );
65 }
66
67 virtual void OnObjectDestroy()
68 {
69 // Tracked object itself removes us from list of trackers
70 wxASSERT( m_pobj!=NULL );
71 m_pobj = NULL;
72 }
73
74 T *m_pobj;
75 };
76
77 #ifdef HAVE_PARTIAL_SPECIALIZATION
78
79 template<class T,bool use_static>
80 struct wxWeakRefImpl;
81
82 // Intermediate class, to select the static case above.
83 template <class T>
84 struct wxWeakRefImpl<T, true> : public wxWeakRefStatic<T>
85 {
86 enum { value = 1 };
87 };
88
89 // Weak ref implementation when T does not have wxTrackable as known base class
90 template<class T>
91 struct wxWeakRefImpl<T, false> : public wxTrackerNode
92 {
93 void Release()
94 {
95 // Release old object if any
96 if ( m_pobj )
97 {
98 // Remove ourselves from object tracker list
99 m_ptbase->RemoveNode(this);
100 m_pobj = NULL;
101 m_ptbase = NULL;
102 }
103 }
104
105 protected:
106 wxWeakRefImpl() : m_pobj(NULL), m_ptbase(NULL) { }
107
108 // Assign receives most derived class here and can use that
109 template <class TDerived>
110 void Assign( TDerived* pobj )
111 {
112 AssignHelper( pobj, wxInt2Type<wxIsStaticTrackable<TDerived>::value>() );
113 }
114
115 template <class TDerived>
116 void AssignHelper(TDerived* pobj, wxInt2Type<true>)
117 {
118 wxTrackable *ptbase = static_cast<wxTrackable*>(pobj);
119 DoAssign( pobj, ptbase );
120 }
121
122 #ifdef HAVE_DYNAMIC_CAST
123 void AssignHelper(T* pobj, wxInt2Type<false>)
124 {
125 // A last way to get a trackable pointer
126 wxTrackable *ptbase = dynamic_cast<wxTrackable*>(pobj);
127 if ( ptbase )
128 {
129 DoAssign( pobj, ptbase );
130 }
131 else
132 {
133 wxFAIL_MSG( "Tracked class should inherit from wxTrackable" );
134
135 Release();
136 }
137 }
138 #endif // HAVE_DYNAMIC_CAST
139
140 void AssignCopy(const wxWeakRefImpl& wr)
141 {
142 DoAssign( wr.m_pobj, wr.m_ptbase );
143 }
144
145 void DoAssign( T* pobj, wxTrackable *ptbase ) {
146 if( m_pobj==pobj ) return;
147 Release();
148
149 // Now set new trackable object
150 if( pobj )
151 {
152 // Add ourselves to object tracker list
153 wxASSERT( ptbase );
154 ptbase->AddNode( this );
155 m_pobj = pobj;
156 m_ptbase = ptbase;
157 }
158 }
159
160 virtual void OnObjectDestroy()
161 {
162 // Tracked object itself removes us from list of trackers
163 wxASSERT( m_pobj!=NULL );
164 m_pobj = NULL;
165 m_ptbase = NULL;
166 }
167
168 T *m_pobj;
169 wxTrackable *m_ptbase;
170 };
171
172 #endif // HAVE_PARTIAL_SPECIALIZATION
173
174
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).
177 template <class T>
178 class wxWeakRef : public
179 #ifdef HAVE_PARTIAL_SPECIALIZATION
180 wxWeakRefImpl<T, wxIsStaticTrackable<T>::value>
181 #else
182 wxWeakRefStatic<T>
183 #endif
184 {
185 public:
186 // Default ctor
187 wxWeakRef() { }
188
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)
193 {
194 Assign(pobj);
195 }
196
197 template <class TDerived>
198 wxWeakRef<T>& operator=(TDerived* pobj)
199 {
200 Assign(pobj);
201 return *this;
202 }
203
204 wxWeakRef<T>& operator=(const wxWeakRef<T>& wr)
205 {
206 AssignCopy(wr);
207 return *this;
208 }
209
210 virtual ~wxWeakRef() { Release(); }
211
212 // Smart pointer functions
213 T& operator*() const { return *m_pobj; }
214 T* operator->() const { return m_pobj; }
215
216 T* get() const { return m_pobj; }
217
218 // test for pointer validity: defining conversion to unspecified_bool_type
219 // and not more obvious bool to avoid implicit conversions to integer types
220 typedef T *(wxWeakRef<T>::*unspecified_bool_type)() const;
221 operator unspecified_bool_type() const
222 {
223 return this->m_pobj ? &wxWeakRef<T>::get : NULL;
224 }
225 };
226
227
228 // Weak ref implementation assign objects are queried for wxTrackable
229 // using dynamic_cast<>
230 template<class T>
231 class wxWeakRefDynamic : public wxTrackerNode
232 {
233 public:
234 wxWeakRefDynamic() : m_pobj(NULL) { }
235
236 wxWeakRefDynamic(T* pobj) : m_pobj(pobj)
237 {
238 Assign(pobj);
239 }
240
241 virtual ~wxWeakRefDynamic() { Release(); }
242
243 // Smart pointer functions
244 T& operator * (){ wxASSERT(this->m_pobj); return *m_pobj; }
245 T* operator -> (){ wxASSERT(this->m_pobj); return m_pobj; }
246 T* operator = (T* pobj) { Assign(pobj); return m_pobj; }
247
248 T* get(){ return this->m_pobj; }
249
250 // operator T* (){ return this->m_pobj; }
251
252 // test for pointer validity: defining conversion to unspecified_bool_type
253 // and not more obvious bool to avoid implicit conversions to integer types
254 typedef T *(wxWeakRef<T>::*unspecified_bool_type)() const;
255 operator unspecified_bool_type() const
256 {
257 return m_pobj ? &wxWeakRef<T>::get : NULL;
258 }
259
260 // Assign from another weak ref, point to same object
261 T* operator = (const wxWeakRef<T> &wr) { Assign( wr.get() ); return this->m_pobj; }
262
263 void Release()
264 {
265 // Release old object if any
266 if( m_pobj )
267 {
268 // Remove ourselves from object tracker list
269 wxTrackable *pt = dynamic_cast<wxTrackable*>(m_pobj);
270 wxASSERT(pt);
271 pt->RemoveNode(this);
272 m_pobj = NULL;
273 }
274 }
275
276 protected:
277 void Assign(T *pobj)
278 {
279 if ( m_pobj == pobj )
280 return;
281
282 Release();
283
284 // Now set new trackable object
285 if ( pobj )
286 {
287 // Add ourselves to object tracker list
288 wxTrackable *pt = dynamic_cast<wxTrackable*>(pobj);
289 if ( pt )
290 {
291 pt->AddNode(this);
292 m_pobj = pobj;
293 }
294 else
295 {
296 // If the object we want to track does not support wxTackable, then
297 // log a message and keep the NULL object pointer.
298 wxFAIL_MSG( "Tracked class should inherit from wxTrackable" );
299 }
300 }
301 }
302
303 void AssignCopy(const wxWeakRefDynamic& wr)
304 {
305 Assign(wr.m_pobj);
306 }
307
308 virtual void OnObjectDestroy()
309 {
310 wxASSERT_MSG( m_pobj, "tracked object should have removed us itself" );
311
312 m_pobj = NULL;
313 }
314
315 T *m_pobj;
316 };
317
318 // Provide some basic types of weak references
319 class WXDLLIMPEXP_FWD_BASE wxEvtHandler;
320 class WXDLLIMPEXP_FWD_BASE wxWindow;
321
322 typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
323 typedef wxWeakRef<wxWindow> wxWindowRef;
324
325 #endif // _WX_WEAKREF_H_
326