]> git.saurik.com Git - wxWidgets.git/blob - include/wx/weakref.h
made wxHeaderColumnBase dtor virtual to avoid gcc warnings
[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
17 // Some compilers (VC6, Borland, otehrs?) have problem with template specialization.
18 // However, this is only used for optimization purposes (a smaller wxWeakRef pointer)
19 // (and the corner case of wxWeakRef<wxObject>). So for those compilers, we can fall
20 // back to the non-optimal case, where we use a the same type of weak ref (static one)
21 // in all cases. See defs.h for various setting these defines depending on compiler.
22
23 #if !defined(HAVE_PARTIAL_SPECIALIZATION) || !defined(HAVE_TEMPLATE_OVERLOAD_RESOLUTION)
24 #define USE_ONLY_STATIC_WEAKREF
25 #endif
26
27
28 #ifndef USE_ONLY_STATIC_WEAKREF
29
30 // Avoid including this for simpler compilers
31 #include "wx/meta/convertible.h"
32 #include "wx/meta/int2type.h"
33
34 template <class T>
35 struct wxIsStaticTrackable
36 {
37 enum { value = wxConvertibleTo<T, wxTrackable>::value };
38 };
39
40 #endif // !USE_ONLY_STATIC_WEAKREF
41
42
43 // Weak ref implementation when T has wxTrackable as a known base class
44 template <class T>
45 class wxWeakRefStatic : public wxTrackerNode
46 {
47 public:
48 wxWeakRefStatic() : m_pobj(NULL) { }
49
50 void Release()
51 {
52 // Release old object if any
53 if ( m_pobj )
54 {
55 // Remove ourselves from object tracker list
56 wxTrackable *pt = static_cast<wxTrackable*>(m_pobj);
57 pt->RemoveNode(this);
58 m_pobj = NULL;
59 }
60 }
61
62 virtual void OnObjectDestroy()
63 {
64 // Tracked object itself removes us from list of trackers
65 wxASSERT(m_pobj != NULL);
66 m_pobj = NULL;
67 }
68
69 protected:
70 void Assign(T* pobj)
71 {
72 if ( m_pobj == pobj )
73 return;
74
75 Release();
76
77 // Now set new trackable object
78 if ( pobj )
79 {
80 // Add ourselves to object tracker list
81 wxTrackable *pt = static_cast<wxTrackable*>(pobj);
82 pt->AddNode(this);
83 m_pobj = pobj;
84 }
85 }
86
87 void AssignCopy(const wxWeakRefStatic& wr)
88 {
89 Assign( wr.m_pobj );
90 }
91
92 T *m_pobj;
93 };
94
95
96
97 #ifndef USE_ONLY_STATIC_WEAKREF
98
99 template<class T,bool use_static>
100 struct wxWeakRefImpl;
101
102 // Intermediate class, to select the static case above.
103 template <class T>
104 struct wxWeakRefImpl<T, true> : public wxWeakRefStatic<T>
105 {
106 enum { value = 1 };
107 };
108
109 // Weak ref implementation when T does not have wxTrackable as known base class
110 template<class T>
111 struct wxWeakRefImpl<T, false> : public wxTrackerNode
112 {
113 void Release()
114 {
115 // Release old object if any
116 if ( m_pobj )
117 {
118 // Remove ourselves from object tracker list
119 m_ptbase->RemoveNode(this);
120 m_pobj = NULL;
121 m_ptbase = NULL;
122 }
123 }
124
125 virtual void OnObjectDestroy()
126 {
127 // Tracked object itself removes us from list of trackers
128 wxASSERT(m_pobj != NULL);
129 m_pobj = NULL;
130 m_ptbase = NULL;
131 }
132
133 protected:
134 wxWeakRefImpl() : m_pobj(NULL), m_ptbase(NULL) { }
135
136 // Assign receives most derived class here and can use that
137 template <class TDerived>
138 void Assign( TDerived* pobj )
139 {
140 AssignHelper( pobj, wxInt2Type<wxIsStaticTrackable<TDerived>::value>() );
141 }
142
143 template <class TDerived>
144 void AssignHelper(TDerived* pobj, wxInt2Type<true>)
145 {
146 wxTrackable *ptbase = static_cast<wxTrackable*>(pobj);
147 DoAssign( pobj, ptbase );
148 }
149
150 #ifdef HAVE_DYNAMIC_CAST
151 void AssignHelper(T* pobj, wxInt2Type<false>)
152 {
153 // A last way to get a trackable pointer
154 wxTrackable *ptbase = dynamic_cast<wxTrackable*>(pobj);
155 if ( ptbase )
156 {
157 DoAssign( pobj, ptbase );
158 }
159 else
160 {
161 wxFAIL_MSG( "Tracked class should inherit from wxTrackable" );
162
163 Release();
164 }
165 }
166 #endif // HAVE_DYNAMIC_CAST
167
168 void AssignCopy(const wxWeakRefImpl& wr)
169 {
170 DoAssign(wr.m_pobj, wr.m_ptbase);
171 }
172
173 void DoAssign( T* pobj, wxTrackable *ptbase ) {
174 if( m_pobj==pobj ) return;
175 Release();
176
177 // Now set new trackable object
178 if( pobj )
179 {
180 // Add ourselves to object tracker list
181 wxASSERT( ptbase );
182 ptbase->AddNode( this );
183 m_pobj = pobj;
184 m_ptbase = ptbase;
185 }
186 }
187
188 T *m_pobj;
189 wxTrackable *m_ptbase;
190 };
191
192 #endif // #ifndef USE_ONLY_STATIC_WEAKREF
193
194
195
196 // A weak reference to an object of type T, where T has base wxTrackable
197 // (usually statically but if not dynamic_cast<> is tried).
198 template <class T>
199 class wxWeakRef : public
200 #ifdef USE_ONLY_STATIC_WEAKREF
201 wxWeakRefStatic<T>
202 #else
203 wxWeakRefImpl<T, wxIsStaticTrackable<T>::value>
204 #endif
205 {
206 public:
207 // Default ctor
208 wxWeakRef() { }
209
210 // When we have the full type here, static_cast<> will always work
211 // (or give a straight compiler error).
212 template <class TDerived>
213 wxWeakRef(TDerived* pobj)
214 {
215 Assign(pobj);
216 }
217
218 // We need this copy ctor, since otherwise a default compiler (binary) copy
219 // happens (if embedded as an object member).
220 wxWeakRef(const wxWeakRef<T>& wr)
221 {
222 Assign(wr.get());
223 }
224
225 template <class TDerived>
226 wxWeakRef<T>& operator=(TDerived* pobj)
227 {
228 this->Assign(pobj);
229 return *this;
230 }
231
232 wxWeakRef<T>& operator=(const wxWeakRef<T>& wr)
233 {
234 AssignCopy(wr);
235 return *this;
236 }
237
238 virtual ~wxWeakRef() { this->Release(); }
239
240 // Smart pointer functions
241 T& operator*() const { return *this->m_pobj; }
242 T* operator->() const { return this->m_pobj; }
243
244 T* get() const { return this->m_pobj; }
245 operator T*() const { return this->m_pobj; }
246 };
247
248
249 #ifdef HAVE_DYNAMIC_CAST
250
251 // Weak ref implementation assign objects are queried for wxTrackable
252 // using dynamic_cast<>
253 template <class T>
254 class wxWeakRefDynamic : public wxTrackerNode
255 {
256 public:
257 wxWeakRefDynamic() : m_pobj(NULL) { }
258
259 wxWeakRefDynamic(T* pobj) : m_pobj(pobj)
260 {
261 Assign(pobj);
262 }
263
264 wxWeakRefDynamic(const wxWeakRef<T>& wr)
265 {
266 Assign(wr.get());
267 }
268
269 virtual ~wxWeakRefDynamic() { Release(); }
270
271 // Smart pointer functions
272 T& operator*() const { wxASSERT(m_pobj); return *m_pobj; }
273 T* operator->() const { wxASSERT(m_pobj); return m_pobj; }
274
275 T* get() const { return m_pobj; }
276 operator T* () const { return m_pobj; }
277
278 T* operator = (T* pobj) { Assign(pobj); return m_pobj; }
279
280 // Assign from another weak ref, point to same object
281 T* operator = (const wxWeakRef<T> &wr) { Assign( wr.get() ); return m_pobj; }
282
283 void Release()
284 {
285 // Release old object if any
286 if( m_pobj )
287 {
288 // Remove ourselves from object tracker list
289 wxTrackable *pt = dynamic_cast<wxTrackable*>(m_pobj);
290 wxASSERT(pt);
291 pt->RemoveNode(this);
292 m_pobj = NULL;
293 }
294 }
295
296 virtual void OnObjectDestroy()
297 {
298 wxASSERT_MSG(m_pobj, "tracked object should have removed us itself");
299
300 m_pobj = NULL;
301 }
302
303 protected:
304 void Assign(T *pobj)
305 {
306 if ( m_pobj == pobj )
307 return;
308
309 Release();
310
311 // Now set new trackable object
312 if ( pobj )
313 {
314 // Add ourselves to object tracker list
315 wxTrackable *pt = dynamic_cast<wxTrackable*>(pobj);
316 if ( pt )
317 {
318 pt->AddNode(this);
319 m_pobj = pobj;
320 }
321 else
322 {
323 // If the object we want to track does not support wxTackable, then
324 // log a message and keep the NULL object pointer.
325 wxFAIL_MSG( "Tracked class should inherit from wxTrackable" );
326 }
327 }
328 }
329
330 T *m_pobj;
331 };
332
333 #endif // #ifdef HAVE_DYNAMIC_CAST
334
335
336 // Provide some basic types of weak references
337 class WXDLLIMPEXP_FWD_BASE wxEvtHandler;
338 class WXDLLIMPEXP_FWD_CORE wxWindow;
339
340
341 typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
342 typedef wxWeakRef<wxWindow> wxWindowRef;
343
344 #endif // _WX_WEAKREF_H_
345