wxObjectDataPtr< T > Class Reference
[Runtime Type Information (RTTI)Smart Pointers]

#include <wx/object.h>

List of all members.


Detailed Description

This is helper template class primarily written to avoid memory leaks because of missing calls to wxObjectRefData::DecRef().

Despite the name this template can actually be used as a smart pointer for any class implementing the reference counting interface which only consists of the two methods T::IncRef() and T::DecRef().

The difference to wxSharedPtr<T> is that wxObjectDataPtr<T> relies on the reference counting to be in the class pointed to where instead wxSharedPtr<T> implements the reference counting itself.

Example:

    class MyCarRefData: public wxObjectRefData
    {
    public:
        MyCarRefData()  { m_price = 0; }

        MyCarRefData( const MyCarRefData& data )
            : wxObjectRefData()
        {
            m_price = data.m_price;
        }

        void SetPrice( int price )  { m_price = price; }
        int GetPrice()              { return m_price; }

    protected:
        int m_price;
    };

    class MyCar
    {
    public:
        MyCar( int price ) : m_data( new MyCarRefData )
        {
            m_data->SetPrice( price );
        }

        MyCar& operator =( const MyCar& tocopy )
        {
            m_data = tocopy.m_data;
            return *this;
        }

        bool operator == ( const MyCar& other ) const
        {
            if (m_data.get() == other.m_data.get()) return true;
            return (m_data->GetPrice() == other.m_data->GetPrice());
        }

        void SetPrice( int price )
        {
           UnShare();
           m_data->SetPrice( price );
        }

        int GetPrice() const
        {
           return m_data->GetPrice();
        }

        wxObjectDataPtr<MyCarRefData> m_data;

    protected:
        void UnShare()
        {
            if (m_data->GetRefCount() == 1)
                return;

            m_data.reset( new MyCarRefData( *m_data ) );
        }
    };

Library:  wxBase

Category:  Runtime Type Information (RTTI), Smart Pointers

See also:
wxObject, wxObjectRefData, Reference Counting, wxSharedPtr<T>, wxScopedPtr<T>, wxWeakRef<T>

Public Member Functions

 wxObjectDataPtr (T *ptr=NULL)
 wxObjectDataPtr (const wxObjectDataPtr< T > &tocopy)
 ~wxObjectDataPtr ()
T * get () const
void reset (T *ptr)
 operator unspecified_bool_type () const
T & operator* () const
T * operator-> () const
wxObjectDataPtr< T > & operator= (const wxObjectDataPtr< T > &tocopy)
wxObjectDataPtr< T > & operator= (T *ptr)

Constructor & Destructor Documentation

wxObjectDataPtr< T >::wxObjectDataPtr ( T *  ptr = NULL  ) 

Constructor.

ptr is a pointer to the reference counted object to which this class points. If ptr is not NULL T::IncRef() will be called on the object.

wxObjectDataPtr< T >::wxObjectDataPtr ( const wxObjectDataPtr< T > &  tocopy  ) 

This copy constructor increases the count of the reference counted object to which tocopy points and then this class will point to, as well.

wxObjectDataPtr< T >::~wxObjectDataPtr (  ) 

Decreases the reference count of the object to which this class points.


Member Function Documentation

T* wxObjectDataPtr< T >::get (  )  const

Gets a pointer to the reference counted object to which this class points.

void wxObjectDataPtr< T >::reset ( T *  ptr  ) 

Reset this class to ptr which points to a reference counted object and calls T::DecRef() on the previously owned object.

wxObjectDataPtr< T >::operator unspecified_bool_type (  )  const

Conversion to a boolean expression (in a variant which is not convertable to anything but a boolean expression).

If this class contains a valid pointer it will return true, if it contains a NULL pointer it will return false.

T& wxObjectDataPtr< T >::operator* (  )  const

Returns a reference to the object.

If the internal pointer is NULL this method will cause an assert in debug mode.

T* wxObjectDataPtr< T >::operator-> (  )  const

Returns a pointer to the reference counted object to which this class points.

If this the internal pointer is NULL, this method will assert in debug mode.

wxObjectDataPtr<T>& wxObjectDataPtr< T >::operator= ( const wxObjectDataPtr< T > &  tocopy  ) 

Assignment operator.

wxObjectDataPtr<T>& wxObjectDataPtr< T >::operator= ( T *  ptr  ) 

Assignment operator.



wxWidgets logo

[ top ]