]> git.saurik.com Git - wxWidgets.git/blame - include/wx/weakref.h
Last part from weak ref patch for event sink disconnection
[wxWidgets.git] / include / wx / weakref.h
CommitLineData
db7035e4
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/weakref.h
3// Purpose: wxWeakRef - Generic weak references for wxWidgets
4// Author: Arne Steinarson
5// Created: 2007-12-27
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
14#include <wx/tracker.h>
15
16// A weak reference to an object of type T, where T has type wxTrackable
17// as one of its base classes (in a static or dynamic sense).
18template<class T>
19class wxWeakRef : public wxTrackerNode
20{
21public:
9dd5ff5b
RR
22 typedef T element_type;
23
db7035e4
VZ
24 wxWeakRef(T *pobj = NULL) : m_pobj(NULL) { Assign(pobj); }
25
26 virtual ~wxWeakRef() { Assign(NULL); }
27
1e9192d0
VZ
28 // test for pointer validity: defining conversion to unspecified_bool_type
29 // and not more obvious bool to avoid implicit conversions to integer types
30 typedef T *(wxWeakRef<T>::*unspecified_bool_type)() const;
31 operator unspecified_bool_type() const
32 {
33 return m_pobj ? &wxWeakRef<T>::get : NULL;
34 }
35
9dd5ff5b
RR
36 T * get() const
37 {
38 return m_pobj;
39 }
40
41 T* operator->()
42 {
43 wxASSERT(m_pobj != NULL);
44 return m_pobj;
45 }
46
47 T& operator*() const
48 {
49 wxASSERT(m_pobj != NULL);
50 return *m_pobj;
51 }
52
db7035e4
VZ
53 T* operator=(T *pobj)
54 {
55 Assign(pobj);
56 return m_pobj;
57 }
58
9dd5ff5b
RR
59 // Assign from another weak ref, point to same object
60 T* operator = (const wxWeakRef<T> &wr)
61 {
62 Assign(wr);
63 return m_pobj;
64 }
65
db7035e4
VZ
66 virtual void OnObjectDestroy()
67 {
68 // Tracked object itself removes us from list of trackers
69 wxASSERT( m_pobj );
70 m_pobj = NULL;
71 }
72
db7035e4 73protected:
9dd5ff5b
RR
74 friend class wxTrackableBase;
75 friend class wxEvtHandler;
db7035e4 76
9dd5ff5b
RR
77 virtual wxTrackerNodeType GetType() { return WeakRef; }
78
db7035e4
VZ
79 void Assign(T* pobj)
80 {
81 if ( m_pobj == pobj )
82 return;
83
84 // First release old object if any
85 if ( m_pobj )
86 {
87 // Remove ourselves from object tracker list
9dd5ff5b
RR
88 // This does static_cast if available, otherwise it tries dynamic cast
89 wxTrackableBase *pt = wxTrackableCaster<T,wxHasBase<T,wxTrackableBase>::value >::Cast(m_pobj);
90 wxASSERT(pt);
91 pt->RemoveNode(this);
db7035e4
VZ
92 m_pobj = NULL;
93 }
94
95 // Now set new trackable object
96 if ( pobj )
97 {
9dd5ff5b
RR
98 wxTrackableBase *pt = wxTrackableCaster<T,wxHasBase<T,wxTrackableBase>::value >::Cast(pobj);
99 if( pt )
100 {
101 pt->AddNode( this );
102 m_pobj = pobj;
103 }
104 else
105 {
106 // If the tracked we want to track does not support wxTackableBase, then
107 // log a message and keep the NULL object pointer.
108 wxLogWarning( _T("wxWeakRef::Assign - Type does not provide wxTrackableBase - resetting tracked object") );
109 }
db7035e4
VZ
110 }
111 }
112
113 T *m_pobj;
114};
115
116// Provide some basic types of weak references
117class WXDLLIMPEXP_FWD_BASE wxObject;
118class WXDLLIMPEXP_FWD_BASE wxEvtHandler;
119class WXDLLIMPEXP_FWD_CORE wxWindow;
120
121typedef wxWeakRef<wxObject> wxObjectRef;
122typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
123typedef wxWeakRef<wxWindow> wxWindowRef;
124
125#endif // _WX_WEAKREF_H_
126