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 *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
26 // Weak ref is used like an ordinary pointer
29 // Now the weak ref has been reset, so we don't risk accessing
30 // a dangling pointer:
34 wxWeakRef<T> works for any objects that are derived from
\helpref{wxTrackable
}{wxtrackable
}.
35 By default, wxEvtHandler and wxWindow derive from wxTrackable. However,
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:
41 class wxMyTrackableObject : public wxObject, public wxTrackable
{
42 // ... other members here
46 \wxheading{Predefined types
}
48 The following types of weak references are predefined:
51 typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
52 typedef wxWeakRef<wxWindow> wxWindowRef;
56 \wxheading{Derived from
}
60 \wxheading{Include files
}
66 \helpref{wxSharedPtr
}{wxsharedptr
},
\helpref{wxScopedPtr
}{wxscopedptrtemplate
}
68 \wxheading{Data structures
}
72 typedef T element_type
77 \latexignore{\rtfignore{\wxheading{Members
}}}
80 \membersection{wxWeakRef<T>::wxWeakRef<T>
}\label{wxweakrefwxweakref
}
82 \func{}{wxWeakRef<T>
}{\param{T*
}{pobj = NULL
}}
84 Constructor. The weak reference is initialized to
{\it pobj
}.
87 \membersection{wxWeakRef<T>::
\destruct{wxWeakRef<T>
}}\label{wxweakrefdtor
}
89 \func{}{\destruct{wxWeakRef<T>
}}{\void}
94 \membersection{wxWeakRef<T>::get
}\label{wxweakrefget
}
96 \constfunc{T *
}{get
}{\void}
98 Returns pointer to the tracked object or NULL.
100 \membersection{wxWeakRef<T>::operator unspecified
\_bool\_type}\label{wxweakrefoperatorbool
}
102 \constfunc{}{operator unspecified
\_bool\_type}{\void}
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
}.
110 \membersection{wxWeakRef<T>::operator*
}\label{wxweakrefoperatorreft
}
112 \constfunc{T \&
}{operator*
}{\void}
114 Returns a reference to the tracked object. If the internal pointer is NULL
115 this method will cause an assert in debug mode.
118 \membersection{wxWeakRef<T>::operator->
}\label{wxweakrefoperatorderef
}
120 \func{T*
}{operator->
}{\void}
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.
127 \membersection{wxWeakRef<T>::operator=
}\label{wxweakrefoperatorassign
}
129 \func{T*
}{operator=
}{\param{T*
}{pobj
}}
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
}.
135 \membersection{wxWeakRef<T>::operator =
}\label{wxweakrefoperatorassign2
}
137 \func{T*
}{operator =
}{\param{wxWeakRef<T>\&
}{wr
}}
139 Release currently tracked object and start tracking the same object as
140 the wxWeakRef
{\it wr
}.
143 \membersection{wxWeakRef<T>::Release
}\label{wxweakrefrelease
}
145 \func{void
}{Release
}{\void}
147 Release currently tracked object and rests object reference.
150 \membersection{wxWeakRef<T>::OnObjectDestroy
}\label{wxweakrefonobjectdestroy
}
152 \func{virtual void
}{OnObjectDestroy
}{\void}
154 Called when the tracked object is destroyed. Be default sets
155 internal pointer to NULL.