1 \section{\class{wxWeakRef<T>
}}\label{wxweakref
}
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.
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
11 holding the reference.
13 wxWeakRef<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.
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
25 // Weak ref is used like an ordinary pointer
28 // Now the weak ref has been reset, so we don't risk accessing
29 // a dangling pointer:
33 wxWeakRef<T> works for any objects that are derived from
\helpref{wxTrackable
}{wxtrackable
}.
34 By default, wxEvtHandler and wxWindow derive from wxTrackable. However,
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:
40 class wxMyTrackableObject : public wxObject, public wxTrackable
{
41 // ... other members here
45 \wxheading{Predefined types
}
47 The following types of weak references are predefined:
50 typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
51 typedef wxWeakRef<wxWindow> wxWindowRef;
55 \wxheading{Derived from
}
59 \wxheading{Include files
}
65 \helpref{wxSharedPtr
}{wxsharedptr
},
\helpref{wxScopedPtr
}{wxscopedptrtemplate
}
67 \wxheading{Data structures
}
71 typedef T element_type
76 \latexignore{\rtfignore{\wxheading{Members
}}}
79 \membersection{wxWeakRef<T>::wxWeakRef<T>
}\label{wxweakrefwxweakref
}
81 \func{}{wxWeakRef<T>
}{\param{T*
}{pobj = NULL
}}
83 Constructor. The weak reference is initialized to
{\it pobj
}.
86 \membersection{wxWeakRef<T>::
\destruct{wxWeakRef<T>
}}\label{wxweakrefdtor
}
88 \func{}{\destruct{wxWeakRef<T>
}}{\void}
93 \membersection{wxWeakRef<T>::get
}\label{wxweakrefget
}
95 \constfunc{T *
}{get
}{\void}
97 Returns pointer to the tracked object or NULL.
99 \membersection{wxWeakRef<T>::operator T*
}\label{wxweakrefoperatorconvt
}
101 \constfunc{T*
}{operator*
}{\void}
103 Implicit conversion to T*. Returns pointer to the tracked
106 \membersection{wxWeakRef<T>::operator*
}\label{wxweakrefoperatorreft
}
108 \constfunc{T \&
}{operator*
}{\void}
110 Returns a reference to the tracked object. If the internal pointer is NULL
111 this method will cause an assert in debug mode.
114 \membersection{wxWeakRef<T>::operator->
}\label{wxweakrefoperatorderef
}
116 \func{T*
}{operator->
}{\void}
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.
123 \membersection{wxWeakRef<T>::operator=
}\label{wxweakrefoperatorassign
}
125 \func{T*
}{operator=
}{\param{T*
}{pobj
}}
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
}.
131 \membersection{wxWeakRef<T>::operator =
}\label{wxweakrefoperatorassign2
}
133 \func{T*
}{operator =
}{\param{wxWeakRef<T>\&
}{wr
}}
135 Release currently tracked object and start tracking the same object as
136 the wxWeakRef
{\it wr
}.
139 \membersection{wxWeakRef<T>::Release
}\label{wxweakrefrelease
}
141 \func{void
}{Release
}{\void}
143 Release currently tracked object and rests object reference.
146 \membersection{wxWeakRef<T>::OnObjectDestroy
}\label{wxweakrefonobjectdestroy
}
148 \func{virtual void
}{OnObjectDestroy
}{\void}
150 Called when the tracked object is destroyed. Be default sets
151 internal pointer to NULL.