compilation fixes for standard-conformant compilers and added back implicit conversio...
[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() { this->Release(); }
211
212 // Smart pointer functions
213 T& operator*() const { return *this->m_pobj; }
214 T* operator->() const { return this->m_pobj; }
215
216 T* get() const { return this->m_pobj; }
217 operator T*() const { return get(); }
218 };
219
220
221 // Weak ref implementation assign objects are queried for wxTrackable
222 // using dynamic_cast<>
223 template<class T>
224 class wxWeakRefDynamic : public wxTrackerNode
225 {
226 public:
227 wxWeakRefDynamic() : m_pobj(NULL) { }
228
229 wxWeakRefDynamic(T* pobj) : m_pobj(pobj)
230 {
231 Assign(pobj);
232 }
233
234 virtual ~wxWeakRefDynamic() { Release(); }
235
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; }
240
241 T* get(){ return this->m_pobj; }
242
243 // operator T* (){ return this->m_pobj; }
244
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
249 {
250 return m_pobj ? &wxWeakRef<T>::get : NULL;
251 }
252
253 // Assign from another weak ref, point to same object
254 T* operator = (const wxWeakRef<T> &wr) { Assign( wr.get() ); return this->m_pobj; }
255
256 void Release()
257 {
258 // Release old object if any
259 if( m_pobj )
260 {
261 // Remove ourselves from object tracker list
262 wxTrackable *pt = dynamic_cast<wxTrackable*>(m_pobj);
263 wxASSERT(pt);
264 pt->RemoveNode(this);
265 m_pobj = NULL;
266 }
267 }
268
269 protected:
270 void Assign(T *pobj)
271 {
272 if ( m_pobj == pobj )
273 return;
274
275 Release();
276
277 // Now set new trackable object
278 if ( pobj )
279 {
280 // Add ourselves to object tracker list
281 wxTrackable *pt = dynamic_cast<wxTrackable*>(pobj);
282 if ( pt )
283 {
284 pt->AddNode(this);
285 m_pobj = pobj;
286 }
287 else
288 {
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" );
292 }
293 }
294 }
295
296 void AssignCopy(const wxWeakRefDynamic& wr)
297 {
298 Assign(wr.m_pobj);
299 }
300
301 virtual void OnObjectDestroy()
302 {
303 wxASSERT_MSG( m_pobj, "tracked object should have removed us itself" );
304
305 m_pobj = NULL;
306 }
307
308 T *m_pobj;
309 };
310
311 // Provide some basic types of weak references
312 class WXDLLIMPEXP_FWD_BASE wxEvtHandler;
313 class WXDLLIMPEXP_FWD_BASE wxWindow;
314
315 typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
316 typedef wxWeakRef<wxWindow> wxWindowRef;
317
318 #endif // _WX_WEAKREF_H_
319