#include <wx/object.h>
This is a helper template class primarily written to avoid memory leaks because of missing calls to wxRefCounter::DecRef() and 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.
Below is an example illustrating how to implement reference counted data using wxRefCounter and wxObjectDataPtr<T> with copy-on-write semantics.
Public Member Functions | |
wxObjectDataPtr (T *ptr=NULL) | |
Constructor. More... | |
~wxObjectDataPtr () | |
Decreases the reference count of the object to which this class points. More... | |
T * | get () const |
Gets a pointer to the reference counted object to which this class points. More... | |
void | reset (T *ptr) |
Reset this class to ptr which points to a reference counted object and calls T::DecRef() on the previously owned object. More... | |
T * | release () |
Release the owned pointer, making caller responsible for decrementing its reference count. More... | |
operator unspecified_bool_type () const | |
Conversion to a boolean expression (in a variant which is not convertible to anything but a boolean expression). More... | |
T & | operator* () const |
Returns a reference to the object. More... | |
T * | operator-> () const |
Returns a pointer to the reference counted object to which this class points. More... | |
template<typename U > | |
wxObjectDataPtr (const wxObjectDataPtr< U > &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. More... | |
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. More... | |
template<typename U > | |
wxObjectDataPtr< T > & | operator= (const wxObjectDataPtr< U > &tocopy) |
Assignment operator. More... | |
wxObjectDataPtr< T > & | operator= (const wxObjectDataPtr< T > &tocopy) |
Assignment operator. More... | |
wxObjectDataPtr< T > & | operator= (T *ptr) |
Assignment operator. More... | |
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< U > & | 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.
Using U different from T
is only supported since wxWidgets 3.1.5.
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.
Using U different from T
is only supported since wxWidgets 3.1.5.
wxObjectDataPtr< T >::~wxObjectDataPtr | ( | ) |
Decreases the reference count of the object to which this class points.
T* wxObjectDataPtr< T >::get | ( | ) | const |
Gets a pointer to the reference counted object to which this class points.
wxObjectDataPtr< T >::operator unspecified_bool_type | ( | ) | const |
Conversion to a boolean expression (in a variant which is not convertible 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.
Using U different from T
is only supported since wxWidgets 3.1.5.
wxObjectDataPtr<T>& wxObjectDataPtr< T >::operator= | ( | const wxObjectDataPtr< U > & | tocopy | ) |
Assignment operator.
Using U different from T
is only supported since wxWidgets 3.1.5.
wxObjectDataPtr<T>& wxObjectDataPtr< T >::operator= | ( | T * | ptr | ) |
Assignment operator.
Using U different from T
is only supported since wxWidgets 3.1.5.
T* wxObjectDataPtr< T >::release | ( | ) |
Release the owned pointer, making caller responsible for decrementing its reference count.
This method should be used only for interoperating with the existing code working with raw pointers, typically when returning a raw pointer from a function.
After calling this function, this object becomes invalid, i.e. it doesn't hold any valid pointer value any more.
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.