]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/weakref.h
don't remove docs for wxThreadHelper::Create; list it in deprecated methods changelog...
[wxWidgets.git] / interface / wx / weakref.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: weakref.h
e25cd775 3// Purpose: interface of wxWeakRefDynamic<T>, wxWeakRef<T>
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
e25cd775 9
23324ae1 10/**
7c913512 11
73473b3e
RR
12 wxWeakRefDynamic<T> is a template class for weak references that is used in
13 the same way as wxWeakRef<T>. The only difference is that wxWeakRefDynamic
e25cd775
FM
14 defaults to using @c dynamic_cast for establishing the object reference
15 (while wxWeakRef defaults to @c static_cast).
7c913512 16
23324ae1
FM
17 So, wxWeakRef will detect a type mismatch during compile time and will
18 have a little better run-time performance. The role of wxWeakRefDynamic
7c913512
FM
19 is to handle objects which derived type one does not know.
20
e25cd775
FM
21 @note wxWeakRef<T> selects an implementation based on the static type of T.
22 If T does not have wxTrackable statically, it defaults to to a mixed-
23 mode operation, where it uses @c dynamic_cast as the last measure
24 (if available from the compiler and enabled when building wxWidgets).
7c913512 25
73473b3e 26 For general cases, wxWeakRef<T> is the better choice.
7c913512 27
e25cd775 28 For API documentation, see: wxWeakRef<T>.
7c913512 29
6c107bd2
FM
30 @tparam T
31 @todo docme
32
e25cd775 33 @nolibrary
d9a09273 34 @category{smartpointers}
23324ae1 35*/
5cb947fd 36template<typename T>
7c913512 37class wxWeakRefDynamic<T>
23324ae1
FM
38{
39public:
7c913512 40
23324ae1
FM
41};
42
43
e54c96f1 44
23324ae1 45/**
e25cd775
FM
46 wxWeakRef<T> is a template class for weak references to wxWidgets objects,
47 such as wxEvtHandler, wxWindow and wxObject.
48 A weak reference behaves much like an ordinary pointer, but when the object
49 pointed is destroyed, the weak reference is automatically reset to a @NULL pointer.
7c913512 50
73473b3e 51 wxWeakRef<T> can be used whenever one must keep a pointer to an object
23324ae1 52 that one does not directly own, and that may be destroyed before the object
7c913512
FM
53 holding the reference.
54
73473b3e 55 wxWeakRef<T> is a small object and the mechanism behind it is fast
7c913512
FM
56 (@b O(1)). So the overall cost of using it is small.
57
e25cd775
FM
58 Example:
59
e84fd6a1
RR
60 @code
61 wxWindow *wnd = new wxWindow( parent, wxID_ANY, "wxWindow" );
e25cd775 62 wxWeakRef<wxWindow> wr = wnd;
e84fd6a1
RR
63 wxWindowRef wr2 = wnd; // Same as above, but using a typedef
64 // Do things with window
65 wnd->Show( true );
e25cd775 66 // Weak ref is used like an ordinary pointer
e84fd6a1 67 wr->Show( false );
e25cd775 68 wnd->Destroy();
e84fd6a1
RR
69 // Now the weak ref has been reset, so we don't risk accessing
70 // a dangling pointer:
71 wxASSERT( wr==NULL );
72 @endcode
73
e25cd775
FM
74 wxWeakRef<T> works for any objects that are derived from wxTrackable.
75 By default, wxEvtHandler and wxWindow derive from wxTrackable.
76 However, wxObject does not, so types like wxFont and wxColour are not
77 trackable. The example below shows how to create a wxObject derived class
78 that is trackable:
e84fd6a1
RR
79
80 @code
81 class wxMyTrackableObject : public wxObject, public wxTrackable
e25cd775
FM
82 {
83 // ... other members here
84 };
e84fd6a1
RR
85 @endcode
86
87 The following types of weak references are predefined:
88
89 @code
90 typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
91 typedef wxWeakRef<wxWindow> wxWindowRef;
92 @endcode
93
6c107bd2
FM
94 @tparam T
95 @todo docme
96
e25cd775 97 @nolibrary
d9a09273 98 @category{smartpointers}
7c913512 99
73473b3e 100 @see wxSharedPtr<T>, wxScopedPtr<T>
23324ae1 101*/
5cb947fd 102template<typename T>
e25cd775 103class wxWeakRef<T> : public wxTrackerNode
23324ae1
FM
104{
105public:
106 /**
107 Constructor. The weak reference is initialized to @e pobj.
108 */
5cb947fd 109 wxWeakRef(T* pobj = NULL);
23324ae1 110
6dd21c54
RR
111 /**
112 Copy constructor.
113 */
114 wxWeakRef(const wxWeakRef<T>& wr);
e25cd775 115
23324ae1
FM
116 /**
117 Destructor.
118 */
5cb947fd 119 ~wxWeakRef();
23324ae1
FM
120
121 /**
122 Called when the tracked object is destroyed. Be default sets
e25cd775
FM
123 internal pointer to @NULL.
124 You need to call this method if you override it.
23324ae1
FM
125 */
126 virtual void OnObjectDestroy();
127
128 /**
129 Release currently tracked object and rests object reference.
130 */
131 void Release();
132
133 /**
134 Returns pointer to the tracked object or @NULL.
135 */
328f5751 136 T* get() const;
23324ae1
FM
137
138 /**
139 Release currently tracked object and start tracking the same object as
140 the wxWeakRef @e wr.
141 */
142 T* operator =(wxWeakRef<T>& wr);
143
144 /**
e25cd775
FM
145 Implicit conversion to T*.
146 Returns pointer to the tracked object or @NULL.
23324ae1 147 */
328f5751 148 T* operator*() const;
23324ae1
FM
149
150 /**
e25cd775
FM
151 Returns a reference to the tracked object.
152 If the internal pointer is @NULL this method will cause an assert in debug mode.
23324ae1 153 */
e25cd775 154 T& operator*() const;
23324ae1
FM
155
156 /**
e25cd775
FM
157 Smart pointer member access.
158 Returns a pointer to the tracked object.
159 If the internal pointer is @NULL this method will cause an assert in debug mode.
23324ae1
FM
160 */
161 T* operator-();
162
163 /**
164 Releases the currently tracked object and starts tracking @e pobj.
165 A weak reference may be reset by passing @e @NULL as @e pobj.
166 */
167 T* operator=(T* pobj);
168};
e54c96f1 169