From: Vadim Zeitlin Date: Fri, 12 Jun 2009 17:14:28 +0000 (+0000) Subject: reuse wxObjectRefData for various ref-counted classes (closes #10886) X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/92ffc98a0816dc6d046edb70d7fb063912391eb7 reuse wxObjectRefData for various ref-counted classes (closes #10886) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61025 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 11397b37de..49e9248266 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -110,16 +110,10 @@ class wxGridDirectionOperations; // class is not documented and is not public at all // ---------------------------------------------------------------------------- -class WXDLLIMPEXP_ADV wxGridCellWorker : public wxClientDataContainer +class WXDLLIMPEXP_ADV wxGridCellWorker : public wxClientDataContainer, public wxObjectRefData { public: - wxGridCellWorker() { m_nRef = 1; } - - // this class is ref counted: it is created with ref count of 1, so - // calling DecRef() once will delete it. Calling IncRef() allows to lock - // it until the matching DecRef() is called - void IncRef() { m_nRef++; } - void DecRef() { if ( --m_nRef == 0 ) delete this; } + wxGridCellWorker() { } // interpret renderer parameters: arbitrary string whose interpretatin is // left to the derived classes @@ -131,8 +125,6 @@ protected: virtual ~wxGridCellWorker(); private: - size_t m_nRef; - // suppress the stupid gcc warning about the class having private dtor and // no friends friend class wxGridCellWorkerDummyFriend; @@ -297,7 +289,7 @@ protected: // class may be returned by wxGridTable::GetAttr(). // ---------------------------------------------------------------------------- -class WXDLLIMPEXP_ADV wxGridCellAttr : public wxClientDataContainer +class WXDLLIMPEXP_ADV wxGridCellAttr : public wxClientDataContainer, public wxObjectRefData { public: enum wxAttrKind @@ -336,12 +328,6 @@ public: wxGridCellAttr *Clone() const; void MergeWith(wxGridCellAttr *mergefrom); - // this class is ref counted: it is created with ref count of 1, so - // calling DecRef() once will delete it. Calling IncRef() allows to lock - // it until the matching DecRef() is called - void IncRef() { m_nRef++; } - void DecRef() { if ( --m_nRef == 0 ) delete this; } - // setters void SetTextColour(const wxColour& colText) { m_colText = colText; } void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; } @@ -419,9 +405,6 @@ private: void Init(wxGridCellAttr *attrDefault = NULL); - // the ref count - when it goes to 0, we die - size_t m_nRef; - wxColour m_colText, m_colBack; wxFont m_font; diff --git a/include/wx/object.h b/include/wx/object.h index 9da3222c8e..8afcf85bf8 100644 --- a/include/wx/object.h +++ b/include/wx/object.h @@ -407,7 +407,7 @@ class WXDLLIMPEXP_BASE wxObjectRefData friend class WXDLLIMPEXP_FWD_BASE wxObject; public: - wxObjectRefData() : m_count(1) { } + wxObjectRefData(int refCount = 1) : m_count(refCount) { } int GetRefCount() const { return m_count; } diff --git a/include/wx/propgrid/property.h b/include/wx/propgrid/property.h index 106d983896..6e0e66683c 100644 --- a/include/wx/propgrid/property.h +++ b/include/wx/propgrid/property.h @@ -64,12 +64,12 @@ struct wxPGPaintData Base class for wxPropertyGrid cell renderers. */ -class WXDLLIMPEXP_PROPGRID wxPGCellRenderer +class WXDLLIMPEXP_PROPGRID wxPGCellRenderer : public wxObjectRefData { public: - wxPGCellRenderer( unsigned int refCount = 1 ) - : m_refCount(refCount) { } + wxPGCellRenderer( int refCount = 1 ) + : wxObjectRefData(refCount) { } virtual ~wxPGCellRenderer() { } // Render flags @@ -145,22 +145,6 @@ public: const wxRect& rect, const wxPGCell& cell, int flags ) const; - - void IncRef() - { - m_refCount++; - } - - void DecRef() - { - m_refCount--; - if ( !m_refCount ) - delete this; - } -protected: - -private: - unsigned int m_refCount; }; @@ -697,7 +681,7 @@ protected: typedef void* wxPGChoicesId; -class WXDLLIMPEXP_PROPGRID wxPGChoicesData +class WXDLLIMPEXP_PROPGRID wxPGChoicesData : public wxObjectRefData { friend class wxPGChoices; public: @@ -728,20 +712,9 @@ public: return m_items[i]; } - void DecRef() - { - m_refCount--; - wxASSERT( m_refCount >= 0 ); - if ( m_refCount == 0 ) - delete this; - } - private: wxVector m_items; - // So that multiple properties can use the same set - int m_refCount; - virtual ~wxPGChoicesData(); }; @@ -785,7 +758,7 @@ public: if ( a.m_data != wxPGChoicesEmptyData ) { m_data = a.m_data; - m_data->m_refCount++; + m_data->IncRef(); } } @@ -825,7 +798,7 @@ public: { wxASSERT(data); m_data = data; - data->m_refCount++; + data->IncRef(); } /** Destructor. */ @@ -998,8 +971,8 @@ public: // Returns data, increases refcount. wxPGChoicesData* GetData() { - wxASSERT( m_data->m_refCount != 0xFFFFFFF ); - m_data->m_refCount++; + wxASSERT( m_data->GetRefCount() != -1 ); + m_data->IncRef(); return m_data; } diff --git a/include/wx/propgrid/propgridpagestate.h b/include/wx/propgrid/propgridpagestate.h index 5e58483f1f..c6d8cde900 100644 --- a/include/wx/propgrid/propgridpagestate.h +++ b/include/wx/propgrid/propgridpagestate.h @@ -314,28 +314,16 @@ protected: /** Base class to derive new viterators. */ -class WXDLLIMPEXP_PROPGRID wxPGVIteratorBase +class WXDLLIMPEXP_PROPGRID wxPGVIteratorBase : public wxObjectRefData { friend class wxPGVIterator; public: - wxPGVIteratorBase() { m_refCount = 1; } + wxPGVIteratorBase() { } virtual void Next() = 0; - void IncRef() - { - m_refCount++; - } - void DecRef() - { - m_refCount--; - if ( m_refCount <= 0 ) - delete this; - } protected: virtual ~wxPGVIteratorBase() { } wxPropertyGridIterator m_it; -private: - int m_refCount; }; /** @class wxPGVIterator diff --git a/include/wx/variant.h b/include/wx/variant.h index 73e675eaef..d082a4f42b 100644 --- a/include/wx/variant.h +++ b/include/wx/variant.h @@ -55,11 +55,11 @@ * overloading wxVariant with unnecessary functionality. */ -class WXDLLIMPEXP_BASE wxVariantData +class WXDLLIMPEXP_BASE wxVariantData : public wxObjectRefData { friend class wxVariant; public: - wxVariantData() : m_count(1) { } + wxVariantData() { } // Override these to provide common functionality virtual bool Eq(wxVariantData& data) const = 0; @@ -81,23 +81,11 @@ public: // a copy of the data. virtual wxVariantData* Clone() const { return NULL; } - void IncRef() { m_count++; } - void DecRef() - { - if ( --m_count == 0 ) - delete this; - } - - int GetRefCount() const { return m_count; } - protected: // Protected dtor should make some incompatible code // break more louder. That is, they should do data->DecRef() // instead of delete data. virtual ~wxVariantData() { } - -private: - int m_count; }; /* diff --git a/src/common/variant.cpp b/src/common/variant.cpp index 661a6190e5..bed7a03f60 100644 --- a/src/common/variant.cpp +++ b/src/common/variant.cpp @@ -163,7 +163,7 @@ void wxVariant::Ref(const wxVariant& clone) if ( clone.m_data ) { m_data = clone.m_data; - m_data->m_count++; + m_data->IncRef(); } } @@ -172,7 +172,7 @@ void wxVariant::UnRef() { if ( m_data ) { - wxASSERT_MSG( m_data->m_count > 0, _T("invalid ref data count") ); + wxASSERT_MSG( m_data->GetRefCount() > 0, _T("invalid ref data count") ); m_data->DecRef(); m_data = NULL; diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 7b99052966..cc03db9a3f 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -197,8 +197,6 @@ wxGridCellWorker::~wxGridCellWorker() void wxGridCellAttr::Init(wxGridCellAttr *attrDefault) { - m_nRef = 1; - m_isReadOnly = Unset; m_renderer = NULL; diff --git a/src/propgrid/property.cpp b/src/propgrid/property.cpp index 8af53f28ab..7f2f5a66ed 100644 --- a/src/propgrid/property.cpp +++ b/src/propgrid/property.cpp @@ -2682,7 +2682,7 @@ void wxPGChoices::RemoveAt(size_t nIndex, size_t count) { AllocExclusive(); - wxASSERT( m_data->m_refCount != 0xFFFFFFF ); + wxASSERT( m_data->GetRefCount() != -1 ); m_data->m_items.erase(m_data->m_items.begin()+nIndex, m_data->m_items.begin()+nIndex+count); } @@ -2798,7 +2798,7 @@ void wxPGChoices::AllocExclusive() { EnsureData(); - if ( m_data->m_refCount != 1 ) + if ( m_data->GetRefCount() != 1 ) { wxPGChoicesData* data = new wxPGChoicesData(); data->CopyDataFrom(m_data); @@ -2816,7 +2816,7 @@ void wxPGChoices::AssignData( wxPGChoicesData* data ) if ( data != wxPGChoicesEmptyData ) { m_data = data; - data->m_refCount++; + data->IncRef(); } } diff --git a/src/propgrid/propgrid.cpp b/src/propgrid/propgrid.cpp index 83462b36a5..964afd9c90 100644 --- a/src/propgrid/propgrid.cpp +++ b/src/propgrid/propgrid.cpp @@ -5231,7 +5231,6 @@ wxPGChoiceEntry::wxPGChoiceEntry() wxPGChoicesData::wxPGChoicesData() { - m_refCount = 1; } wxPGChoicesData::~wxPGChoicesData()