#include <wx/object.h>
Derive classes from this to store your own data. When retrieving information from a wxObject's reference data, you will need to cast to your own derived class.
// include file // ------------ class MyCar : public wxObject { public: MyCar() { } MyCar( int price ); bool IsOk() const { return m_refData != NULL; } bool operator == ( const MyCar& car ) const; bool operator != (const MyCar& car) const { return !(*this == car); } void SetPrice( int price ); int GetPrice() const; protected: virtual wxObjectRefData *CreateRefData() const; virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const; DECLARE_DYNAMIC_CLASS(MyCar) }; // implementation // -------------- // the reference data class is typically a private class only visible in the // implementation source file of the refcounted class. class MyCarRefData : public wxObjectRefData { public: MyCarRefData() { m_price = 0; } MyCarRefData( const MyCarRefData& data ) : wxObjectRefData() { // copy refcounted data; this is usually a time- and memory-consuming operation // and is only done when two (or more) MyCar instances need to unshare a // common instance of MyCarRefData m_price = data.m_price; } bool operator == (const MyCarRefData& data) const { return m_price == data.m_price; } private: // in real world, reference counting is usually used only when // the wxObjectRefData-derived class holds data very memory-consuming; // in this example the various MyCar instances may share a MyCarRefData // instance which however only takes 4 bytes for this integer! int m_price; }; #define M_CARDATA ((MyCarRefData *)m_refData) IMPLEMENT_DYNAMIC_CLASS(MyCar,wxObject) MyCar::MyCar( int price ) { // here we init the MyCar internal data: m_refData = new MyCarRefData(); M_CARDATA->m_price = price; } wxObjectRefData *MyCar::CreateRefData() const { return new MyCarRefData; } wxObjectRefData *MyCar::CloneRefData(const wxObjectRefData *data) const { return new MyCarRefData(*(MyCarRefData *)data); } bool MyCar::operator == ( const MyCar& car ) const { if (m_refData == car.m_refData) return true; if (!m_refData || !car.m_refData) return false; // here we use the MyCarRefData::operator==() function. // Note however that this comparison may be very slow if the // reference data contains a lot of data to be compared. return ( *(MyCarRefData*)m_refData == *(MyCarRefData*)car.m_refData ); } void MyCar::SetPrice( int price ) { // since this function modifies one of the MyCar internal property, // we need to be sure that the other MyCar instances which share the // same MyCarRefData instance are not affected by this call. // I.e. it's very important to call UnShare() in all setters of // refcounted classes! UnShare(); M_CARDATA->m_price = price; } int MyCar::GetPrice() const { wxCHECK_MSG( IsOk(), -1, "invalid car" ); return M_CARDATA->m_price; }
Public Member Functions | |
wxObjectRefData () | |
Default constructor. | |
void | DecRef () |
Decrements the reference count associated with this shared data and, if it reaches zero, destroys this instance of wxObjectRefData releasing its memory. | |
int | GetRefCount () const |
Returns the reference count associated with this shared data. | |
void | IncRef () |
Increments the reference count associated with this shared data. | |
Protected Member Functions | |
virtual | ~wxObjectRefData () |
Destructor. |
virtual wxObjectRefData::~wxObjectRefData | ( | ) | [protected, virtual] |
Destructor.
It's declared protected
so that wxObjectRefData instances will never be destroyed directly but only as result of a DecRef() call.
wxObjectRefData::wxObjectRefData | ( | ) |
Default constructor.
Initialises the internal reference count to 1.
void wxObjectRefData::DecRef | ( | ) |
Decrements the reference count associated with this shared data and, if it reaches zero, destroys this instance of wxObjectRefData releasing its memory.
Please note that after calling this function, the caller should absolutely avoid to use the pointer to this instance since it may not be valid anymore.
int wxObjectRefData::GetRefCount | ( | ) | const |
Returns the reference count associated with this shared data.
When this goes to zero during a DecRef() call, the object will auto-free itself.
void wxObjectRefData::IncRef | ( | ) |
Increments the reference count associated with this shared data.
![]() |
[ top ] |