This class is just a typedef to wxRefCounter and is used by wxObject.
Derive classes from this to store your own data in wxObject-derived classes. When retrieving information from a wxObject's reference data, you will need to cast to your own derived class.
Below is an example illustrating how to store reference counted data in a class derived from wxObject including copy-on-write semantics.
Example
{
public:
MyCar() { }
MyCar( int price );
bool IsOk() const { return m_refData != NULL; }
bool operator != (
const MyCar& car)
const {
return !(*
this == car); }
void SetPrice( int price );
int GetPrice() const;
protected:
};
{
public:
MyCarRefData()
{
m_price = 0;
}
MyCarRefData( const MyCarRefData& data )
{
m_price = data.m_price;
}
{
return m_price == data.m_price;
}
private:
int m_price;
};
#define M_CARDATA ((MyCarRefData *)m_refData)
MyCar::MyCar( int price )
{
m_refData = new MyCarRefData();
M_CARDATA->m_price = price;
}
{
return new MyCarRefData;
}
{
return new MyCarRefData(*(MyCarRefData *)data);
}
{
if (m_refData == car.m_refData)
return true;
if (!m_refData || !car.m_refData)
return false;
return ( *(MyCarRefData*)m_refData == *(MyCarRefData*)car.m_refData );
}
void MyCar::SetPrice( int price )
{
UnShare();
M_CARDATA->m_price = price;
}
int MyCar::GetPrice() const
{
return M_CARDATA->m_price;
}
- See Also
- wxObject, wxObjectDataPtr<T>, Reference Counting