| Version: 3.3.1

wxObjectRefData Class Reference
Class List by Category » Runtime Type Information (RTTI)

`#include <wx/object.h>`

Inheritance diagram for wxObjectRefData:
[legend]

## Detailed Description

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

// include file
// ------------
class MyCar : public wxObject
{
public:
MyCar() = default;
MyCar( int price );
bool IsOk() const { return m_refData != nullptr; }
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;
wxDECLARE_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)
wxIMPLEMENT_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;
}
wxObject
This is the root class of many of the wxWidgets classes.
Definition: object.h:233
wxObjectRefData
This class is just a typedef to wxRefCounter and is used by wxObject.
wxCHECK_MSG
#define wxCHECK_MSG(condition, retValue, message)
Checks that the condition is true, returns with the given return value if not (stops execution in deb...
Definition: debug.h:180
wxIMPLEMENT_DYNAMIC_CLASS
#define wxIMPLEMENT_DYNAMIC_CLASS(className, baseClassName)
Used in a C++ implementation file to complete the declaration of a class that has run-time type infor...
Definition: object.h:788
wxDECLARE_DYNAMIC_CLASS
#define wxDECLARE_DYNAMIC_CLASS(className)
Used inside a class declaration to make the class known to wxWidgets RTTI system and also declare tha...
Definition: object.h:730
operator==
bool operator==(const wxTreeItemId &left, const wxTreeItemId &right)
operator!=
bool operator!=(const wxTreeItemId &left, const wxTreeItemId &right)

##

Library: wxBase
Category: Runtime Type Information (RTTI)

See also
  wxObject, wxObjectDataPtr<T>, Reference Counting