wxWidgets: wxObjectDataPtr< T > Class Template Reference

| Version: 3.1.5

Loading...
Searching...
No Matches
Public Member Functions | List of all members
wxObjectDataPtr< T > Class Template Reference
Class List by Category » Runtime Type Information (RTTI) | Class List by Category » Smart Pointers

`#include </cygdrive/c/Users/Hamis/Cygwin-packaging/wxWidgets3.1/wxWidgets3.1-3.1.5-4.x86_64/src/wxWidgets-3.1.5/interface/wx/object.h>`

## Detailed Description

template<class T>
class wxObjectDataPtr< T >

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.

# Example

class MyCarRefData: public wxRefCounter
{
public:
MyCarRefData( int price = 0 ) : m_price(price) { }
MyCarRefData( const MyCarRefData& data ) : m_price(data.m_price) { }
void SetPrice( int price ) { m_price = price; }
int GetPrice() const { return m_price; }
protected:
int m_price;
};
class MyCar
{
public:
// initializes this MyCar assigning to the
// internal data pointer a new instance of MyCarRefData
MyCar( int price = 0 ) : m_data( new MyCarRefData(price) )
{
}
MyCar& operator =( const MyCar& tocopy )
{
// shallow copy: this is just a fast copy of pointers; the real
// memory-consuming data which typically is stored inside
// MyCarRefData is not copied here!
m_data = tocopy.m_data;
return *this;
}
bool operator == ( const MyCar& other ) const
{
if (m_data.get() == other.m_data.get())
return true; // this instance and the 'other' one share the
// same MyCarRefData data...
return (m_data.GetPrice() == other.m_data.GetPrice());
}
void SetPrice( int price )
{
// make sure changes to this class do not affect other instances
// currently sharing our same refcounted data:
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 ) );
}
};
wxRefCounter
This class is used to manage reference-counting providing a simple interface and a counter.
Definition object.h:163
operator==
bool operator==(const wxString &s1, const wxString &s2)
Comparison operator for string types.

##

Library: wxBase

<>< =''>:</>&;&;< =''>\ , \ </></>

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

## Public Member Functions
| wxObjectDataPtr (T *ptr=NULL)
| Constructor.
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.
| wxObjectDataPtr (const wxObjectDataPtr< T > &tocopy)
| ~wxObjectDataPtr ()
| Decreases the reference count of the object to which this class points.
T * | get () const
| Gets a pointer to the reference counted object to which this class points.
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.
T * | release ()
| Release the owned pointer, making caller responsible for decrementing its reference count.
| operator unspecified_bool_type () const
| Conversion to a boolean expression (in a variant which is not convertible to anything but a boolean expression).
T & | operator* () const
| Returns a reference to the object.
T * | operator-> () const
| Returns a pointer to the reference counted object to which this class points.
template<typename U>
wxObjectDataPtr< T > & | operator= (const wxObjectDataPtr< U > &tocopy)
| Assignment operator.
wxObjectDataPtr< T > & | operator= (const wxObjectDataPtr< T > &tocopy)
wxObjectDataPtr< T > & | operator= (T *ptr)

## Constructor & Destructor Documentation

## ◆ wxObjectDataPtr() [1/3]

template<class T>

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() [2/3]

template<class T>
template<typename U>

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() [3/3]

template<class T>

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

## ◆ ~wxObjectDataPtr()

template<class T>

wxObjectDataPtr< T >::~wxObjectDataPtr | ( |  | ) |

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

## Member Function Documentation

## ◆ get()

template<class T>

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

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

## ◆ operator unspecified_bool_type()

template<class T>

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.

## ◆ operator*()

template<class T>

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.

## ◆ operator->()

template<class T>

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.

## ◆ operator=() [1/3]

template<class T>

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

## ◆ operator=() [2/3]

template<class T>
template<typename U>

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

Assignment operator.

Using U different from `T` is only supported since wxWidgets 3.1.5.

## ◆ operator=() [3/3]

template<class T>

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

## ◆ release()

template<class T>

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.

Since
  3.1.4

## ◆ reset()

template<class T>

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.