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
This commit is contained in:
Vadim Zeitlin
2009-06-12 17:14:28 +00:00
parent 58e914f015
commit 92ffc98a08
9 changed files with 21 additions and 92 deletions

View File

@@ -110,16 +110,10 @@ class wxGridDirectionOperations;
// class is not documented and is not public at all // 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: public:
wxGridCellWorker() { m_nRef = 1; } wxGridCellWorker() { }
// 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; }
// interpret renderer parameters: arbitrary string whose interpretatin is // interpret renderer parameters: arbitrary string whose interpretatin is
// left to the derived classes // left to the derived classes
@@ -131,8 +125,6 @@ protected:
virtual ~wxGridCellWorker(); virtual ~wxGridCellWorker();
private: private:
size_t m_nRef;
// suppress the stupid gcc warning about the class having private dtor and // suppress the stupid gcc warning about the class having private dtor and
// no friends // no friends
friend class wxGridCellWorkerDummyFriend; friend class wxGridCellWorkerDummyFriend;
@@ -297,7 +289,7 @@ protected:
// class may be returned by wxGridTable::GetAttr(). // class may be returned by wxGridTable::GetAttr().
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class WXDLLIMPEXP_ADV wxGridCellAttr : public wxClientDataContainer class WXDLLIMPEXP_ADV wxGridCellAttr : public wxClientDataContainer, public wxObjectRefData
{ {
public: public:
enum wxAttrKind enum wxAttrKind
@@ -336,12 +328,6 @@ public:
wxGridCellAttr *Clone() const; wxGridCellAttr *Clone() const;
void MergeWith(wxGridCellAttr *mergefrom); 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 // setters
void SetTextColour(const wxColour& colText) { m_colText = colText; } void SetTextColour(const wxColour& colText) { m_colText = colText; }
void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; } void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
@@ -419,9 +405,6 @@ private:
void Init(wxGridCellAttr *attrDefault = NULL); void Init(wxGridCellAttr *attrDefault = NULL);
// the ref count - when it goes to 0, we die
size_t m_nRef;
wxColour m_colText, wxColour m_colText,
m_colBack; m_colBack;
wxFont m_font; wxFont m_font;

View File

@@ -407,7 +407,7 @@ class WXDLLIMPEXP_BASE wxObjectRefData
friend class WXDLLIMPEXP_FWD_BASE wxObject; friend class WXDLLIMPEXP_FWD_BASE wxObject;
public: public:
wxObjectRefData() : m_count(1) { } wxObjectRefData(int refCount = 1) : m_count(refCount) { }
int GetRefCount() const { return m_count; } int GetRefCount() const { return m_count; }

View File

@@ -64,12 +64,12 @@ struct wxPGPaintData
Base class for wxPropertyGrid cell renderers. Base class for wxPropertyGrid cell renderers.
*/ */
class WXDLLIMPEXP_PROPGRID wxPGCellRenderer class WXDLLIMPEXP_PROPGRID wxPGCellRenderer : public wxObjectRefData
{ {
public: public:
wxPGCellRenderer( unsigned int refCount = 1 ) wxPGCellRenderer( int refCount = 1 )
: m_refCount(refCount) { } : wxObjectRefData(refCount) { }
virtual ~wxPGCellRenderer() { } virtual ~wxPGCellRenderer() { }
// Render flags // Render flags
@@ -145,22 +145,6 @@ public:
const wxRect& rect, const wxRect& rect,
const wxPGCell& cell, const wxPGCell& cell,
int flags ) const; 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; typedef void* wxPGChoicesId;
class WXDLLIMPEXP_PROPGRID wxPGChoicesData class WXDLLIMPEXP_PROPGRID wxPGChoicesData : public wxObjectRefData
{ {
friend class wxPGChoices; friend class wxPGChoices;
public: public:
@@ -728,20 +712,9 @@ public:
return m_items[i]; return m_items[i];
} }
void DecRef()
{
m_refCount--;
wxASSERT( m_refCount >= 0 );
if ( m_refCount == 0 )
delete this;
}
private: private:
wxVector<wxPGChoiceEntry> m_items; wxVector<wxPGChoiceEntry> m_items;
// So that multiple properties can use the same set
int m_refCount;
virtual ~wxPGChoicesData(); virtual ~wxPGChoicesData();
}; };
@@ -785,7 +758,7 @@ public:
if ( a.m_data != wxPGChoicesEmptyData ) if ( a.m_data != wxPGChoicesEmptyData )
{ {
m_data = a.m_data; m_data = a.m_data;
m_data->m_refCount++; m_data->IncRef();
} }
} }
@@ -825,7 +798,7 @@ public:
{ {
wxASSERT(data); wxASSERT(data);
m_data = data; m_data = data;
data->m_refCount++; data->IncRef();
} }
/** Destructor. */ /** Destructor. */
@@ -998,8 +971,8 @@ public:
// Returns data, increases refcount. // Returns data, increases refcount.
wxPGChoicesData* GetData() wxPGChoicesData* GetData()
{ {
wxASSERT( m_data->m_refCount != 0xFFFFFFF ); wxASSERT( m_data->GetRefCount() != -1 );
m_data->m_refCount++; m_data->IncRef();
return m_data; return m_data;
} }

View File

@@ -314,28 +314,16 @@ protected:
/** Base class to derive new viterators. /** Base class to derive new viterators.
*/ */
class WXDLLIMPEXP_PROPGRID wxPGVIteratorBase class WXDLLIMPEXP_PROPGRID wxPGVIteratorBase : public wxObjectRefData
{ {
friend class wxPGVIterator; friend class wxPGVIterator;
public: public:
wxPGVIteratorBase() { m_refCount = 1; } wxPGVIteratorBase() { }
virtual void Next() = 0; virtual void Next() = 0;
void IncRef()
{
m_refCount++;
}
void DecRef()
{
m_refCount--;
if ( m_refCount <= 0 )
delete this;
}
protected: protected:
virtual ~wxPGVIteratorBase() { } virtual ~wxPGVIteratorBase() { }
wxPropertyGridIterator m_it; wxPropertyGridIterator m_it;
private:
int m_refCount;
}; };
/** @class wxPGVIterator /** @class wxPGVIterator

View File

@@ -55,11 +55,11 @@
* overloading wxVariant with unnecessary functionality. * overloading wxVariant with unnecessary functionality.
*/ */
class WXDLLIMPEXP_BASE wxVariantData class WXDLLIMPEXP_BASE wxVariantData : public wxObjectRefData
{ {
friend class wxVariant; friend class wxVariant;
public: public:
wxVariantData() : m_count(1) { } wxVariantData() { }
// Override these to provide common functionality // Override these to provide common functionality
virtual bool Eq(wxVariantData& data) const = 0; virtual bool Eq(wxVariantData& data) const = 0;
@@ -81,23 +81,11 @@ public:
// a copy of the data. // a copy of the data.
virtual wxVariantData* Clone() const { return NULL; } 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:
// Protected dtor should make some incompatible code // Protected dtor should make some incompatible code
// break more louder. That is, they should do data->DecRef() // break more louder. That is, they should do data->DecRef()
// instead of delete data. // instead of delete data.
virtual ~wxVariantData() { } virtual ~wxVariantData() { }
private:
int m_count;
}; };
/* /*

View File

@@ -163,7 +163,7 @@ void wxVariant::Ref(const wxVariant& clone)
if ( clone.m_data ) if ( clone.m_data )
{ {
m_data = 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 ) 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->DecRef();
m_data = NULL; m_data = NULL;

View File

@@ -197,8 +197,6 @@ wxGridCellWorker::~wxGridCellWorker()
void wxGridCellAttr::Init(wxGridCellAttr *attrDefault) void wxGridCellAttr::Init(wxGridCellAttr *attrDefault)
{ {
m_nRef = 1;
m_isReadOnly = Unset; m_isReadOnly = Unset;
m_renderer = NULL; m_renderer = NULL;

View File

@@ -2682,7 +2682,7 @@ void wxPGChoices::RemoveAt(size_t nIndex, size_t count)
{ {
AllocExclusive(); 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.erase(m_data->m_items.begin()+nIndex,
m_data->m_items.begin()+nIndex+count); m_data->m_items.begin()+nIndex+count);
} }
@@ -2798,7 +2798,7 @@ void wxPGChoices::AllocExclusive()
{ {
EnsureData(); EnsureData();
if ( m_data->m_refCount != 1 ) if ( m_data->GetRefCount() != 1 )
{ {
wxPGChoicesData* data = new wxPGChoicesData(); wxPGChoicesData* data = new wxPGChoicesData();
data->CopyDataFrom(m_data); data->CopyDataFrom(m_data);
@@ -2816,7 +2816,7 @@ void wxPGChoices::AssignData( wxPGChoicesData* data )
if ( data != wxPGChoicesEmptyData ) if ( data != wxPGChoicesEmptyData )
{ {
m_data = data; m_data = data;
data->m_refCount++; data->IncRef();
} }
} }

View File

@@ -5231,7 +5231,6 @@ wxPGChoiceEntry::wxPGChoiceEntry()
wxPGChoicesData::wxPGChoicesData() wxPGChoicesData::wxPGChoicesData()
{ {
m_refCount = 1;
} }
wxPGChoicesData::~wxPGChoicesData() wxPGChoicesData::~wxPGChoicesData()