[1660792 ] wxObject::Dec/IncRef and wxObjectDataPtr

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44521 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robert Roebling
2007-02-17 09:16:35 +00:00
parent c71b212607
commit 4a11340a1c
3 changed files with 110 additions and 9 deletions

View File

@@ -392,14 +392,83 @@ class WXDLLIMPEXP_BASE wxObjectRefData
public:
wxObjectRefData() : m_count(1) { }
virtual ~wxObjectRefData() { }
int GetRefCount() const { return m_count; }
void IncRef() { m_count++; }
void DecRef();
protected:
// this object should never be destroyed directly but only as a
// result of a DecRef() call:
virtual ~wxObjectRefData() { }
private:
// our refcount:
int m_count;
};
// ----------------------------------------------------------------------------
// wxObjectDataPtr: helper class to avoid memleaks because of missing calls
// to wxObjectRefData::DecRef
// ----------------------------------------------------------------------------
template <class T>
class wxObjectDataPtr
{
public:
typedef T element_type;
wxEXPLICIT wxObjectDataPtr(T *ptr = NULL) : m_ptr(ptr) {}
// copy ctor
wxObjectDataPtr(const wxObjectDataPtr<T> &tocopy)
: m_ptr(tocopy.m_ptr)
{
if (m_ptr)
m_ptr->IncRef();
}
~wxObjectDataPtr()
{
if (m_ptr)
m_ptr->DecRef();
}
T *get() const { return m_ptr; }
T *operator->() const { return get(); }
void reset(T *ptr)
{
if (m_ptr)
m_ptr->DecRef();
m_ptr = ptr;
}
wxObjectDataPtr& operator=(const wxObjectDataPtr &tocopy)
{
if (m_ptr)
m_ptr->DecRef();
m_ptr = tocopy.m_ptr;
if (m_ptr)
m_ptr->IncRef();
return *this;
}
wxObjectDataPtr& operator=(T *ptr)
{
if (m_ptr)
m_ptr->DecRef();
m_ptr = ptr;
if (m_ptr)
m_ptr->IncRef();
return *this;
}
private:
T *m_ptr;
};
// ----------------------------------------------------------------------------
// wxObject: the root class of wxWidgets object hierarchy
// ----------------------------------------------------------------------------
@@ -520,7 +589,8 @@ public:
#ifdef _MSC_VER
return (wxClassInfo*) m_classInfo;
#else
return wx_const_cast(wxClassInfo *, m_classInfo);
wxDynamicClassInfo *nonconst = wx_const_cast(wxDynamicClassInfo *, m_classInfo);
return wx_static_cast(wxClassInfo *, nonconst);
#endif
}