]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/weakref.tex
Added wxRect::Intersect() documentation (patch 1868322 from Laurent Humbertclaude).
[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
RR
8
9wxWeakref<T> can be used whenever one must keep a pointer to an object
10that does not directly own, and that may be destroyed before the object
11holding the reference.
12
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
72636c15
RR
34wxWeakref<T> works for any objects that are derived from
35\helpref{wxTrackableBase}{wxtrackablebase} or \helpref{wxTrackable}{wxtrackable}.
36By default, wxEvtHandler and wxWindow derive from wxTrackableBase. However,
b3a029f0
VZ
37wxObject does not, so types like \helpref{wxFont}{wxfont} and
38\helpref{wxColour}{wxcolour} are not trackable. The example below shows how to
39create a wxObject derived class that is trackable:
a6acecec
RR
40
41\begin{verbatim}
9dd5ff5b
RR
42 class wxMyTrackableObject : public wxObject, public wxTrackable {
43 // ... other members here
44 };
45\end{verbatim}
46
b3a029f0
VZ
47\textbf{Note:} Custom trackable objects should derive from wxTrackable
48if one wants to reference them from a \texttt{wxWeakRef<wxObject>}. The
9dd5ff5b
RR
49difference between the two base classes is that wxTrackableBase
50has no virtual member functions (no VTable), and thus cannot be detected
b3a029f0 51through \texttt{dynamic\_cast<>}.
9dd5ff5b 52
9dd5ff5b 53\wxheading{Predefined types}
a6acecec 54
9dd5ff5b
RR
55The following types of weak references are predefined:
56
57\begin{verbatim}
58typedef wxWeakRef<wxObject> wxObjectRef;
59typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
60typedef wxWeakRef<wxWindow> wxWindowRef;
a6acecec
RR
61\end{verbatim}
62
9dd5ff5b 63
a6acecec
RR
64\wxheading{Derived from}
65
66wxTrackerNode
67
68\wxheading{Include files}
69
70<weakref.h>
71
a60b0ddc
RR
72\wxheading{See also}
73
74\helpref{wxSharedPtr}{wxsharedptr}, \helpref{wxScopedPtr}{wxscopedptrtemplate}
75
a6acecec
RR
76\wxheading{Data structures}
77
72636c15
RR
78{\small%
79\begin{verbatim}
80typedef T element_type
81\end{verbatim}
82}%
83
84
a6acecec
RR
85\latexignore{\rtfignore{\wxheading{Members}}}
86
87
88\membersection{wxWeakRef<T>::wxWeakRef<T>}\label{wxweakrefwxweakref}
89
90\func{}{wxWeakRef<T>}{\param{T* }{pobj = NULL}}
91
9dd5ff5b
RR
92Constructor. The weak reference is initialized to {\it pobj}.
93
a6acecec
RR
94
95\membersection{wxWeakRef<T>::\destruct{wxWeakRef<T>}}\label{wxweakrefdtor}
96
97\func{}{\destruct{wxWeakRef<T>}}{\void}
98
99Destructor.
100
a6acecec 101
9dd5ff5b
RR
102\membersection{wxWeakRef<T>::get}\label{wxweakrefget}
103
104\constfunc{T *}{get}{\void}
105
106Returns pointer to the tracked object or NULL.
107
a60b0ddc
RR
108\membersection{wxWeakRef<T>::operator unspecified\_bool\_type}\label{wxweakrefoperatorbool}
109
110\constfunc{}{operator unspecified\_bool\_type}{\void}
111
112Conversion to a boolean expression (in a variant which is not
113convertable to anything but a boolean expression). If this class
114contains a valid pointer it will return {\it true}, if it contains
115a NULL pointer it will return {\it false}.
116
9dd5ff5b
RR
117
118\membersection{wxWeakRef<T>::operator*}\label{wxweakrefoperatorreft}
119
120\constfunc{T \&}{operator*}{\void}
121
122Returns a reference to the tracked object. If the internal pointer is NULL
123this method will cause an assert in debug mode.
a6acecec 124
a6acecec
RR
125
126\membersection{wxWeakRef<T>::operator->}\label{wxweakrefoperatorderef}
127
128\func{T*}{operator->}{\void}
129
9dd5ff5b
RR
130Smart pointer member access. Returns a pointer to the
131tracked object. If the internal pointer is NULL this
132method will cause an assert in debug mode.
133
a6acecec
RR
134
135\membersection{wxWeakRef<T>::operator=}\label{wxweakrefoperatorassign}
136
137\func{T* operator}{operator=}{\param{T* }{pobj}}
138
9dd5ff5b
RR
139Releases the currently tracked object and starts tracking {\it pobj}.
140A weak reference may be reset by passing {\it NULL} as {\it pobj}.
a6acecec 141
a6acecec 142
9dd5ff5b 143\membersection{wxWeakRef<T>::operator =}\label{wxweakrefoperatorassign2}
a6acecec 144
9dd5ff5b 145\func{T* operator}{operator =}{\param{wxWeakRef<T>\& }{wr}}
a6acecec 146
9dd5ff5b
RR
147Release currently tracked object and start tracking the same object as
148the wxWeakRef {\it wr}.
a6acecec 149
a6acecec
RR
150
151\membersection{wxWeakRef<T>::OnObjectDestroy}\label{wxweakrefonobjectdestroy}
152
153\func{virtual void}{OnObjectDestroy}{\void}
154
155Called when the tracked object is destroyed. Be default sets
156internal pointer to NULL.
157