#include </home/zeitlin/src/wx/github/interface/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.
例:
wxWindow *wnd = new wxWindow( parent, wxID_ANY, "wxWindow" ); wxWeakRef<wxWindow> wr = wnd; wxWindowRef wr2 = wnd; // Same as above, but using a typedef // Do things with window wnd->Show( true ); // Weak ref is used like an ordinary pointer wr->Show( false ); wnd->Destroy(); // Now the weak ref has been reset, so we don't risk accessing // a dangling pointer: wxASSERT( wr==NULL );
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:
class wxMyTrackableObject : public wxObject, public wxTrackable { // ... other members here };
The following types of weak references are predefined:
typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef; typedef wxWeakRef<wxWindow> wxWindowRef;
T | The type to which the smart pointer points to. |
Public Types | |
typedef T | element_type |
Type of the element stored by this reference. | |
Public Member Functions | |
wxWeakRef (T *pobj=NULL) | |
Constructor. | |
wxWeakRef (const wxWeakRef< T > &wr) | |
Copy constructor. | |
virtual | ~wxWeakRef () |
Destructor. | |
virtual void | OnObjectDestroy () |
Called when the tracked object is destroyed. | |
void | Release () |
Release currently tracked object and rests object reference. | |
T * | get () const |
Returns pointer to the tracked object or NULL. | |
T * | operator= (wxWeakRef< T > &wr) |
Release currently tracked object and start tracking the same object as the wxWeakRef wr. | |
T * | operator* () const |
Implicit conversion to T*. | |
T & | operator* () const |
Returns a reference to the tracked object. | |
T * | operator-> () |
Smart pointer member access. | |
T * | operator= (T *pobj) |
Releases the currently tracked object and starts tracking pobj. |
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.
virtual wxWeakRef< T >::~wxWeakRef | ( | ) | [virtual] |
Destructor.
T* wxWeakRef< T >::get | ( | ) | const |
Returns pointer to the tracked object or NULL.
virtual void wxWeakRef< T >::OnObjectDestroy | ( | ) | [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.
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.
T* wxWeakRef< T >::operator= | ( | wxWeakRef< T > & | wr | ) |
Release currently tracked object and start tracking the same object as the wxWeakRef wr.
void wxWeakRef< T >::Release | ( | ) |
Release currently tracked object and rests object reference.