#include <wx/weakref.h>
wxWeakRef<T> is a template class for weak references to wxWidgets objects, such as wxEvtHandler, wxWindow and wxObject.
A weak reference behaves much like an ordinary pointer, but when the object pointed is destroyed, the weak reference is automatically reset to a NULL pointer.
wxWeakRef<T> can be used whenever one must keep a pointer to an object that one does not directly own, and that may be destroyed before the object holding the reference.
wxWeakRef<T> is a small object and the mechanism behind it is fast (O(1)). So the overall cost of using it is small.
Example:
wxWeakRef<T> works for any objects that are derived from wxTrackable. By default, wxEvtHandler and wxWindow derive from wxTrackable. However, wxObject does not, so types like wxFont and wxColour are not trackable. The example below shows how to create a wxObject derived class that is trackable:
The following types of weak references are predefined:
T | The type to which the smart pointer points to. |
Public Types | |
typedef T | element_type |
Type of the element stored by this reference. More... | |
Public Member Functions | |
wxWeakRef (T *pobj=NULL) | |
Constructor. More... | |
wxWeakRef (const wxWeakRef< T > &wr) | |
Copy constructor. More... | |
virtual | ~wxWeakRef () |
Destructor. More... | |
virtual void | OnObjectDestroy () |
Called when the tracked object is destroyed. More... | |
void | Release () |
Release currently tracked object and rests object reference. More... | |
T * | get () const |
Returns pointer to the tracked object or NULL. More... | |
T * | operator= (wxWeakRef< T > &wr) |
Release currently tracked object and start tracking the same object as the wxWeakRef wr. More... | |
T * | operator* () const |
Implicit conversion to T*. More... | |
T & | operator* () const |
Returns a reference to the tracked object. More... | |
T * | operator-> () |
Smart pointer member access. More... | |
T * | operator= (T *pobj) |
Releases the currently tracked object and starts tracking pobj. More... | |
typedef T wxWeakRef< T >::element_type |
Type of the element stored by this reference.
wxWeakRef< T >::wxWeakRef | ( | T * | pobj = NULL | ) |
Constructor.
The weak reference is initialized to pobj.
wxWeakRef< T >::wxWeakRef | ( | const wxWeakRef< T > & | wr | ) |
Copy constructor.
T* wxWeakRef< T >::get | ( | ) | const |
Returns pointer to the tracked object or NULL.
|
virtual |
Called when the tracked object is destroyed.
Be default sets internal pointer to NULL. You need to call this method if you override it.
T* wxWeakRef< T >::operator* | ( | ) | const |
Implicit conversion to T*.
Returns pointer to the tracked object or NULL.
T& wxWeakRef< T >::operator* | ( | ) | const |
Returns a reference to the tracked object.
If the internal pointer is NULL this method will cause an assert in debug mode.
T* wxWeakRef< T >::operator-> | ( | ) |
Smart pointer member access.
Returns a pointer to the tracked object. If the internal pointer is NULL this method will cause an assert in debug mode.
Release currently tracked object and start tracking the same object as the wxWeakRef wr.
T* wxWeakRef< T >::operator= | ( | T * | pobj | ) |
Releases the currently tracked object and starts tracking pobj.
A weak reference may be reset by passing NULL as pobj.
void wxWeakRef< T >::Release | ( | ) |
Release currently tracked object and rests object reference.