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