]>
Commit | Line | Data |
---|---|---|
a6acecec RR |
1 | \section{\class{wxWeakRef<T>}}\label{wxweakref} |
2 | ||
b3a029f0 VZ |
3 | wxWeakRef is a template class for weak references to wxWidgets objects, |
4 | such as \helpref{wxEvtHandler}{wxevthandler}, \helpref{wxWindow}{wxwindow} and | |
5 | \helpref{wxObject}{wxobject}. A weak reference behaves much like an ordinary | |
6 | pointer, but when the object pointed is destroyed, the weak reference is | |
7 | automatically reset to a NULL pointer. | |
9dd5ff5b | 8 | |
cc6ceca7 VZ |
9 | wxWeakRef<T> can be used whenever one must keep a pointer to an object |
10 | that one does not directly own, and that may be destroyed before the object | |
9dd5ff5b RR |
11 | holding the reference. |
12 | ||
cc6ceca7 | 13 | wxWeakRef<T> is a small object and the mechanism behind it is fast |
b3a029f0 | 14 | (\textbf{O(1)}). So the overall cost of using it is small. |
9dd5ff5b RR |
15 | |
16 | ||
17 | \wxheading{Example} | |
18 | ||
19 | \begin{verbatim} | |
9dd5ff5b RR |
20 | wxWindow *wnd = new wxWindow( parent, wxID_ANY, "wxWindow" ); |
21 | wxWeakRef<wxWindow> wr = wnd; | |
22 | wxWindowRef wr2 = wnd; // Same as above, but using a typedef | |
23 | // Do things with window | |
24 | wnd->Show( true ); | |
25 | // Weak ref is used like an ordinary pointer | |
26 | wr->Show( false ); | |
27 | wnd->Destroy(); | |
28 | // Now the weak ref has been reset, so we don't risk accessing | |
29 | // a dangling pointer: | |
30 | wxASSERT( wr==NULL ); | |
31 | \end{verbatim} | |
32 | ||
cc6ceca7 VZ |
33 | wxWeakRef<T> works for any objects that are derived from \helpref{wxTrackable}{wxtrackable}. |
34 | By default, wxEvtHandler and wxWindow derive from wxTrackable. However, | |
b3a029f0 VZ |
35 | wxObject does not, so types like \helpref{wxFont}{wxfont} and |
36 | \helpref{wxColour}{wxcolour} are not trackable. The example below shows how to | |
37 | create a wxObject derived class that is trackable: | |
a6acecec RR |
38 | |
39 | \begin{verbatim} | |
9dd5ff5b RR |
40 | class wxMyTrackableObject : public wxObject, public wxTrackable { |
41 | // ... other members here | |
42 | }; | |
43 | \end{verbatim} | |
44 | ||
9dd5ff5b | 45 | \wxheading{Predefined types} |
a6acecec | 46 | |
9dd5ff5b RR |
47 | The following types of weak references are predefined: |
48 | ||
49 | \begin{verbatim} | |
9dd5ff5b RR |
50 | typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef; |
51 | typedef wxWeakRef<wxWindow> wxWindowRef; | |
a6acecec RR |
52 | \end{verbatim} |
53 | ||
9dd5ff5b | 54 | |
a6acecec RR |
55 | \wxheading{Derived from} |
56 | ||
57 | wxTrackerNode | |
58 | ||
59 | \wxheading{Include files} | |
60 | ||
61 | <weakref.h> | |
62 | ||
a60b0ddc RR |
63 | \wxheading{See also} |
64 | ||
65 | \helpref{wxSharedPtr}{wxsharedptr}, \helpref{wxScopedPtr}{wxscopedptrtemplate} | |
66 | ||
a6acecec RR |
67 | \wxheading{Data structures} |
68 | ||
72636c15 RR |
69 | {\small% |
70 | \begin{verbatim} | |
71 | typedef T element_type | |
72 | \end{verbatim} | |
73 | }% | |
74 | ||
75 | ||
a6acecec RR |
76 | \latexignore{\rtfignore{\wxheading{Members}}} |
77 | ||
78 | ||
79 | \membersection{wxWeakRef<T>::wxWeakRef<T>}\label{wxweakrefwxweakref} | |
80 | ||
81 | \func{}{wxWeakRef<T>}{\param{T* }{pobj = NULL}} | |
82 | ||
9dd5ff5b RR |
83 | Constructor. The weak reference is initialized to {\it pobj}. |
84 | ||
a6acecec RR |
85 | |
86 | \membersection{wxWeakRef<T>::\destruct{wxWeakRef<T>}}\label{wxweakrefdtor} | |
87 | ||
88 | \func{}{\destruct{wxWeakRef<T>}}{\void} | |
89 | ||
90 | Destructor. | |
91 | ||
a6acecec | 92 | |
9dd5ff5b RR |
93 | \membersection{wxWeakRef<T>::get}\label{wxweakrefget} |
94 | ||
95 | \constfunc{T *}{get}{\void} | |
96 | ||
97 | Returns pointer to the tracked object or NULL. | |
98 | ||
e9cec363 | 99 | \membersection{wxWeakRef<T>::operator T*}\label{wxweakrefoperatorconvt} |
a60b0ddc | 100 | |
e9cec363 | 101 | \constfunc{T*}{operator*}{\void} |
a60b0ddc | 102 | |
e9cec363 RR |
103 | Implicit conversion to T*. Returns pointer to the tracked |
104 | object or NULL. | |
9dd5ff5b RR |
105 | |
106 | \membersection{wxWeakRef<T>::operator*}\label{wxweakrefoperatorreft} | |
107 | ||
108 | \constfunc{T \&}{operator*}{\void} | |
109 | ||
110 | Returns a reference to the tracked object. If the internal pointer is NULL | |
111 | this method will cause an assert in debug mode. | |
a6acecec | 112 | |
a6acecec RR |
113 | |
114 | \membersection{wxWeakRef<T>::operator->}\label{wxweakrefoperatorderef} | |
115 | ||
116 | \func{T*}{operator->}{\void} | |
117 | ||
9dd5ff5b RR |
118 | Smart pointer member access. Returns a pointer to the |
119 | tracked object. If the internal pointer is NULL this | |
120 | method will cause an assert in debug mode. | |
121 | ||
a6acecec RR |
122 | |
123 | \membersection{wxWeakRef<T>::operator=}\label{wxweakrefoperatorassign} | |
124 | ||
cc6ceca7 | 125 | \func{T*}{operator=}{\param{T* }{pobj}} |
a6acecec | 126 | |
9dd5ff5b RR |
127 | Releases the currently tracked object and starts tracking {\it pobj}. |
128 | A weak reference may be reset by passing {\it NULL} as {\it pobj}. | |
a6acecec | 129 | |
a6acecec | 130 | |
9dd5ff5b | 131 | \membersection{wxWeakRef<T>::operator =}\label{wxweakrefoperatorassign2} |
a6acecec | 132 | |
cc6ceca7 | 133 | \func{T*}{operator =}{\param{wxWeakRef<T>\& }{wr}} |
a6acecec | 134 | |
9dd5ff5b RR |
135 | Release currently tracked object and start tracking the same object as |
136 | the wxWeakRef {\it wr}. | |
a6acecec | 137 | |
a6acecec | 138 | |
cc6ceca7 VZ |
139 | \membersection{wxWeakRef<T>::Release}\label{wxweakrefrelease} |
140 | ||
141 | \func{void}{Release}{\void} | |
142 | ||
143 | Release currently tracked object and rests object reference. | |
144 | ||
145 | ||
a6acecec RR |
146 | \membersection{wxWeakRef<T>::OnObjectDestroy}\label{wxweakrefonobjectdestroy} |
147 | ||
148 | \func{virtual void}{OnObjectDestroy}{\void} | |
149 | ||
150 | Called when the tracked object is destroyed. Be default sets | |
151 | internal pointer to NULL. | |
152 |