Version: 2.9.4
Public Types | Public Member Functions
wxWeakRef< T > Class Template Reference

#include </home/zeitlin/src/wx/github/interface/wx/weakref.h>


Detailed Description

template<typename T>
class wxWeakRef< T >

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;
Template Parameters:
TThe type to which the smart pointer points to.

Library:  None; this class implementation is entirely header-based.
Category:  Smart Pointers
参照:
wxSharedPtr<T>, wxScopedPtr<T>

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.

List of all members.


Member Typedef Documentation

template<typename T >
typedef T wxWeakRef< T >::element_type

Type of the element stored by this reference.


Constructor & Destructor Documentation

template<typename T >
wxWeakRef< T >::wxWeakRef ( T *  pobj = NULL)

Constructor.

The weak reference is initialized to pobj.

template<typename T >
wxWeakRef< T >::wxWeakRef ( const wxWeakRef< T > &  wr)

Copy constructor.

template<typename T >
virtual wxWeakRef< T >::~wxWeakRef ( ) [virtual]

Destructor.


Member Function Documentation

template<typename T >
T* wxWeakRef< T >::get ( ) const

Returns pointer to the tracked object or NULL.

template<typename T >
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.

template<typename T >
T* wxWeakRef< T >::operator* ( ) const

Implicit conversion to T*.

Returns pointer to the tracked object or NULL.

template<typename T >
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.

template<typename T >
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.

template<typename T >
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.

template<typename T >
T* wxWeakRef< T >::operator= ( wxWeakRef< T > &  wr)

Release currently tracked object and start tracking the same object as the wxWeakRef wr.

template<typename T >
void wxWeakRef< T >::Release ( )

Release currently tracked object and rests object reference.

 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines