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