]>
Commit | Line | Data |
---|---|---|
db7035e4 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/weakref.h | |
3 | // Purpose: wxWeakRef - Generic weak references for wxWidgets | |
4 | // Author: Arne Steinarson | |
cc6ceca7 | 5 | // Created: 27 Dec 07 |
9dd5ff5b | 6 | // RCS-ID: $Id$ |
db7035e4 VZ |
7 | // Copyright: (c) 2007 Arne Steinarson |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_WEAKREF_H_ | |
12 | #define _WX_WEAKREF_H_ | |
13 | ||
cc6ceca7 | 14 | #include "wx/tracker.h" |
db7035e4 | 15 | |
26c66bc6 RR |
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 | |
cc6ceca7 VZ |
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 | ||
26c66bc6 RR |
40 | #endif // !USE_ONLY_STATIC_WEAKREF |
41 | ||
42 | ||
cc6ceca7 VZ |
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 | protected: | |
63 | void Assign(T* pobj) | |
64 | { | |
65 | if ( m_pobj == pobj ) | |
66 | return; | |
67 | ||
68 | Release(); | |
69 | ||
70 | // Now set new trackable object | |
71 | if ( pobj ) | |
72 | { | |
73 | // Add ourselves to object tracker list | |
74 | wxTrackable *pt = static_cast<wxTrackable*>(pobj); | |
75 | pt->AddNode(this); | |
76 | m_pobj = pobj; | |
77 | } | |
78 | } | |
79 | ||
42d9ad79 | 80 | void AssignCopy(const wxWeakRefStatic& wr) |
cc6ceca7 VZ |
81 | { |
82 | Assign( wr.m_pobj ); | |
83 | } | |
84 | ||
85 | virtual void OnObjectDestroy() | |
86 | { | |
87 | // Tracked object itself removes us from list of trackers | |
88 | wxASSERT( m_pobj!=NULL ); | |
89 | m_pobj = NULL; | |
90 | } | |
91 | ||
92 | T *m_pobj; | |
93 | }; | |
94 | ||
cc6ceca7 | 95 | |
7005cf44 | 96 | |
26c66bc6 | 97 | #ifndef USE_ONLY_STATIC_WEAKREF |
7005cf44 | 98 | |
cc6ceca7 VZ |
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 | |
db7035e4 | 110 | template<class T> |
cc6ceca7 VZ |
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 | protected: | |
126 | wxWeakRefImpl() : m_pobj(NULL), m_ptbase(NULL) { } | |
127 | ||
128 | // Assign receives most derived class here and can use that | |
129 | template <class TDerived> | |
130 | void Assign( TDerived* pobj ) | |
131 | { | |
132 | AssignHelper( pobj, wxInt2Type<wxIsStaticTrackable<TDerived>::value>() ); | |
133 | } | |
134 | ||
135 | template <class TDerived> | |
136 | void AssignHelper(TDerived* pobj, wxInt2Type<true>) | |
137 | { | |
138 | wxTrackable *ptbase = static_cast<wxTrackable*>(pobj); | |
139 | DoAssign( pobj, ptbase ); | |
140 | } | |
141 | ||
142 | #ifdef HAVE_DYNAMIC_CAST | |
143 | void AssignHelper(T* pobj, wxInt2Type<false>) | |
144 | { | |
145 | // A last way to get a trackable pointer | |
146 | wxTrackable *ptbase = dynamic_cast<wxTrackable*>(pobj); | |
147 | if ( ptbase ) | |
148 | { | |
149 | DoAssign( pobj, ptbase ); | |
150 | } | |
151 | else | |
152 | { | |
153 | wxFAIL_MSG( "Tracked class should inherit from wxTrackable" ); | |
154 | ||
155 | Release(); | |
156 | } | |
157 | } | |
158 | #endif // HAVE_DYNAMIC_CAST | |
159 | ||
160 | void AssignCopy(const wxWeakRefImpl& wr) | |
161 | { | |
42d9ad79 | 162 | DoAssign(wr.m_pobj, wr.m_ptbase); |
cc6ceca7 VZ |
163 | } |
164 | ||
165 | void DoAssign( T* pobj, wxTrackable *ptbase ) { | |
166 | if( m_pobj==pobj ) return; | |
167 | Release(); | |
168 | ||
169 | // Now set new trackable object | |
170 | if( pobj ) | |
171 | { | |
172 | // Add ourselves to object tracker list | |
173 | wxASSERT( ptbase ); | |
174 | ptbase->AddNode( this ); | |
175 | m_pobj = pobj; | |
176 | m_ptbase = ptbase; | |
177 | } | |
178 | } | |
179 | ||
180 | virtual void OnObjectDestroy() | |
181 | { | |
182 | // Tracked object itself removes us from list of trackers | |
183 | wxASSERT( m_pobj!=NULL ); | |
184 | m_pobj = NULL; | |
185 | m_ptbase = NULL; | |
186 | } | |
187 | ||
188 | T *m_pobj; | |
189 | wxTrackable *m_ptbase; | |
190 | }; | |
191 | ||
26c66bc6 | 192 | #endif // #ifndef USE_ONLY_STATIC_WEAKREF |
7005cf44 | 193 | |
cc6ceca7 VZ |
194 | |
195 | ||
26c66bc6 | 196 | // A weak reference to an object of type T, where T has base wxTrackable |
cc6ceca7 VZ |
197 | // (usually statically but if not dynamic_cast<> is tried). |
198 | template <class T> | |
199 | class wxWeakRef : public | |
26c66bc6 | 200 | #ifdef USE_ONLY_STATIC_WEAKREF |
cc6ceca7 | 201 | wxWeakRefStatic<T> |
7005cf44 VZ |
202 | #else |
203 | wxWeakRefImpl<T, wxIsStaticTrackable<T>::value> | |
cc6ceca7 | 204 | #endif |
db7035e4 VZ |
205 | { |
206 | public: | |
cc6ceca7 VZ |
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 | ||
26c66bc6 RR |
218 | // We need this copy ctor, since otherwise a default compiler (binary) copy |
219 | // happens (if embedded as an object member). | |
6dd21c54 RR |
220 | wxWeakRef(const wxWeakRef<T>& wr) |
221 | { | |
222 | Assign(wr.get()); | |
223 | } | |
224 | ||
cc6ceca7 VZ |
225 | template <class TDerived> |
226 | wxWeakRef<T>& operator=(TDerived* pobj) | |
227 | { | |
074574b4 | 228 | this->Assign(pobj); |
cc6ceca7 VZ |
229 | return *this; |
230 | } | |
231 | ||
232 | wxWeakRef<T>& operator=(const wxWeakRef<T>& wr) | |
233 | { | |
234 | AssignCopy(wr); | |
235 | return *this; | |
236 | } | |
db7035e4 | 237 | |
34bfda8a | 238 | virtual ~wxWeakRef() { this->Release(); } |
cc6ceca7 VZ |
239 | |
240 | // Smart pointer functions | |
26c66bc6 RR |
241 | T& operator*() const { return *this->m_pobj; } |
242 | T* operator->() const { return this->m_pobj; } | |
cc6ceca7 | 243 | |
26c66bc6 RR |
244 | T* get() const { return this->m_pobj; } |
245 | operator T*() const { return this->m_pobj; } | |
cc6ceca7 VZ |
246 | }; |
247 | ||
248 | ||
26c66bc6 RR |
249 | #ifdef HAVE_DYNAMIC_CAST |
250 | ||
cc6ceca7 VZ |
251 | // Weak ref implementation assign objects are queried for wxTrackable |
252 | // using dynamic_cast<> | |
42d9ad79 | 253 | template <class T> |
cc6ceca7 VZ |
254 | class wxWeakRefDynamic : public wxTrackerNode |
255 | { | |
256 | public: | |
257 | wxWeakRefDynamic() : m_pobj(NULL) { } | |
1e9192d0 | 258 | |
cc6ceca7 | 259 | wxWeakRefDynamic(T* pobj) : m_pobj(pobj) |
db7035e4 VZ |
260 | { |
261 | Assign(pobj); | |
db7035e4 VZ |
262 | } |
263 | ||
26c66bc6 RR |
264 | wxWeakRefDynamic(const wxWeakRef<T>& wr) |
265 | { | |
266 | Assign(wr.get()); | |
267 | } | |
268 | ||
cc6ceca7 VZ |
269 | virtual ~wxWeakRefDynamic() { Release(); } |
270 | ||
271 | // Smart pointer functions | |
26c66bc6 RR |
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; } | |
db7035e4 | 277 | |
26c66bc6 RR |
278 | T* operator = (T* pobj) { Assign(pobj); return m_pobj; } |
279 | ||
cc6ceca7 | 280 | // Assign from another weak ref, point to same object |
26c66bc6 | 281 | T* operator = (const wxWeakRef<T> &wr) { Assign( wr.get() ); return m_pobj; } |
db7035e4 | 282 | |
cc6ceca7 | 283 | void Release() |
db7035e4 | 284 | { |
cc6ceca7 VZ |
285 | // Release old object if any |
286 | if( m_pobj ) | |
db7035e4 VZ |
287 | { |
288 | // Remove ourselves from object tracker list | |
cc6ceca7 | 289 | wxTrackable *pt = dynamic_cast<wxTrackable*>(m_pobj); |
9dd5ff5b RR |
290 | wxASSERT(pt); |
291 | pt->RemoveNode(this); | |
db7035e4 VZ |
292 | m_pobj = NULL; |
293 | } | |
cc6ceca7 VZ |
294 | } |
295 | ||
296 | protected: | |
297 | void Assign(T *pobj) | |
298 | { | |
299 | if ( m_pobj == pobj ) | |
300 | return; | |
301 | ||
302 | Release(); | |
db7035e4 VZ |
303 | |
304 | // Now set new trackable object | |
305 | if ( pobj ) | |
306 | { | |
cc6ceca7 VZ |
307 | // Add ourselves to object tracker list |
308 | wxTrackable *pt = dynamic_cast<wxTrackable*>(pobj); | |
309 | if ( pt ) | |
9dd5ff5b | 310 | { |
cc6ceca7 | 311 | pt->AddNode(this); |
9dd5ff5b RR |
312 | m_pobj = pobj; |
313 | } | |
314 | else | |
315 | { | |
cc6ceca7 VZ |
316 | // If the object we want to track does not support wxTackable, then |
317 | // log a message and keep the NULL object pointer. | |
318 | wxFAIL_MSG( "Tracked class should inherit from wxTrackable" ); | |
9dd5ff5b | 319 | } |
db7035e4 VZ |
320 | } |
321 | } | |
322 | ||
cc6ceca7 VZ |
323 | virtual void OnObjectDestroy() |
324 | { | |
325 | wxASSERT_MSG( m_pobj, "tracked object should have removed us itself" ); | |
326 | ||
327 | m_pobj = NULL; | |
328 | } | |
329 | ||
db7035e4 VZ |
330 | T *m_pobj; |
331 | }; | |
332 | ||
26c66bc6 RR |
333 | #endif // #ifdef HAVE_DYNAMIC_CAST |
334 | ||
335 | ||
db7035e4 | 336 | // Provide some basic types of weak references |
db7035e4 | 337 | class WXDLLIMPEXP_FWD_BASE wxEvtHandler; |
5a0c7d66 | 338 | class WXDLLIMPEXP_FWD_CORE wxWindow; |
db7035e4 | 339 | |
26c66bc6 | 340 | |
db7035e4 VZ |
341 | typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef; |
342 | typedef wxWeakRef<wxWindow> wxWindowRef; | |
343 | ||
344 | #endif // _WX_WEAKREF_H_ | |
345 |