]> git.saurik.com Git - wxWidgets.git/blame_incremental - docs/latex/wx/weakref.tex
Sort of autoresize if only 1 column is there
[wxWidgets.git] / docs / latex / wx / weakref.tex
... / ...
CommitLineData
1\section{\class{wxWeakRef<T>}}\label{wxweakref}
2
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.
8
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
11holding the reference.
12
13wxWeakRef<T> is a small object and the mechanism behind it is fast
14(\textbf{O(1)}). So the overall cost of using it is small.
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
34wxWeakRef<T> works for any objects that are derived from \helpref{wxTrackable}{wxtrackable}.
35By default, wxEvtHandler and wxWindow derive from wxTrackable. However,
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:
39
40\begin{verbatim}
41 class wxMyTrackableObject : public wxObject, public wxTrackable {
42 // ... other members here
43 };
44\end{verbatim}
45
46\wxheading{Predefined types}
47
48The following types of weak references are predefined:
49
50\begin{verbatim}
51typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
52typedef wxWeakRef<wxWindow> wxWindowRef;
53\end{verbatim}
54
55
56\wxheading{Derived from}
57
58wxTrackerNode
59
60\wxheading{Include files}
61
62<weakref.h>
63
64\wxheading{See also}
65
66\helpref{wxSharedPtr}{wxsharedptr}, \helpref{wxScopedPtr}{wxscopedptrtemplate}
67
68\wxheading{Data structures}
69
70{\small%
71\begin{verbatim}
72typedef T element_type
73\end{verbatim}
74}%
75
76
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
84Constructor. The weak reference is initialized to {\it pobj}.
85
86
87\membersection{wxWeakRef<T>::\destruct{wxWeakRef<T>}}\label{wxweakrefdtor}
88
89\func{}{\destruct{wxWeakRef<T>}}{\void}
90
91Destructor.
92
93
94\membersection{wxWeakRef<T>::get}\label{wxweakrefget}
95
96\constfunc{T *}{get}{\void}
97
98Returns pointer to the tracked object or NULL.
99
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
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.
116
117
118\membersection{wxWeakRef<T>::operator->}\label{wxweakrefoperatorderef}
119
120\func{T*}{operator->}{\void}
121
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
126
127\membersection{wxWeakRef<T>::operator=}\label{wxweakrefoperatorassign}
128
129\func{T*}{operator=}{\param{T* }{pobj}}
130
131Releases the currently tracked object and starts tracking {\it pobj}.
132A weak reference may be reset by passing {\it NULL} as {\it pobj}.
133
134
135\membersection{wxWeakRef<T>::operator =}\label{wxweakrefoperatorassign2}
136
137\func{T*}{operator =}{\param{wxWeakRef<T>\& }{wr}}
138
139Release currently tracked object and start tracking the same object as
140the wxWeakRef {\it wr}.
141
142
143\membersection{wxWeakRef<T>::Release}\label{wxweakrefrelease}
144
145\func{void}{Release}{\void}
146
147Release currently tracked object and rests object reference.
148
149
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