# wxObjectRefData Class Reference
[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() { }
            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;

            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;
        }

##

Library: wxBase

Category: Runtime Type Information (RTTI)

See also:
  wxObject, wxObjectDataPtr<T>, Reference Counting

|  | [ top ]