]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/weakref.tex
Remove double entry of wxTrackable
[wxWidgets.git] / docs / latex / wx / weakref.tex
CommitLineData
a6acecec
RR
1\section{\class{wxWeakRef<T>}}\label{wxweakref}
2
b3a029f0
VZ
3wxWeakRef is a template class for weak references to wxWidgets objects,
4such as \helpref{wxEvtHandler}{wxevthandler}, \helpref{wxWindow}{wxwindow} and
5\helpref{wxObject}{wxobject}. A weak reference behaves much like an ordinary
6pointer, but when the object pointed is destroyed, the weak reference is
7automatically reset to a NULL pointer.
9dd5ff5b 8
cc6ceca7
VZ
9wxWeakRef<T> can be used whenever one must keep a pointer to an object
10that one does not directly own, and that may be destroyed before the object
9dd5ff5b
RR
11holding the reference.
12
cc6ceca7 13wxWeakRef<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
34wxWeakRef<T> works for any objects that are derived from \helpref{wxTrackable}{wxtrackable}.
35By default, wxEvtHandler and wxWindow derive from wxTrackable. However,
b3a029f0
VZ
36wxObject does not, so types like \helpref{wxFont}{wxfont} and
37\helpref{wxColour}{wxcolour} are not trackable. The example below shows how to
38create 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
48The following types of weak references are predefined:
49
50\begin{verbatim}
9dd5ff5b
RR
51typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
52typedef wxWeakRef<wxWindow> wxWindowRef;
a6acecec
RR
53\end{verbatim}
54
9dd5ff5b 55
a6acecec
RR
56\wxheading{Derived from}
57
58wxTrackerNode
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}
72typedef 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
84Constructor. 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
91Destructor.
92
a6acecec 93
9dd5ff5b
RR
94\membersection{wxWeakRef<T>::get}\label{wxweakrefget}
95
96\constfunc{T *}{get}{\void}
97
98Returns 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
104Conversion to a boolean expression (in a variant which is not
105convertable to anything but a boolean expression). If this class
106contains a valid pointer it will return {\it true}, if it contains
107a 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
114Returns a reference to the tracked object. If the internal pointer is NULL
115this 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
122Smart pointer member access. Returns a pointer to the
123tracked object. If the internal pointer is NULL this
124method 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
131Releases the currently tracked object and starts tracking {\it pobj}.
132A 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
139Release currently tracked object and start tracking the same object as
140the wxWeakRef {\it wr}.
a6acecec 141
a6acecec 142
cc6ceca7
VZ
143\membersection{wxWeakRef<T>::Release}\label{wxweakrefrelease}
144
145\func{void}{Release}{\void}
146
147Release 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
154Called when the tracked object is destroyed. Be default sets
155internal pointer to NULL.
156