Add wxObjectDataPtr::release()

This makes it possible to use wxObjectDataPtr inside functions returning
raw pointers owned by the caller, such as custom GetAttr() in the grid
sample.
This commit is contained in:
Vadim Zeitlin 2020-03-31 02:43:15 +02:00
parent 15b5a1865c
commit 06af121e9c
4 changed files with 28 additions and 14 deletions

View File

@ -313,6 +313,13 @@ public:
m_ptr = ptr;
}
T* release()
{
T* const ptr = m_ptr;
m_ptr = NULL;
return ptr;
}
wxObjectDataPtr& operator=(const wxObjectDataPtr &tocopy)
{
if (m_ptr)

View File

@ -608,6 +608,21 @@ public:
*/
void reset(T *ptr);
/**
Release the owned pointer, making caller responsible for decrementing
its reference count.
This method should be used only for interoperating with the existing
code working with raw pointers, typically when returning a raw pointer
from a function.
After calling this function, this object becomes invalid, i.e. it
doesn't hold any valid pointer value any more.
@since 3.1.4
*/
T* release();
/**
Conversion to a boolean expression (in a variant which is not
convertable to anything but a boolean expression).

View File

@ -1596,41 +1596,34 @@ void GridFrame::OnBugsTable(wxCommandEvent& )
// ----------------------------------------------------------------------------
MyGridCellAttrProvider::MyGridCellAttrProvider()
: m_attrForOddRows(new wxGridCellAttr)
{
m_attrForOddRows = new wxGridCellAttr;
m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
}
MyGridCellAttrProvider::~MyGridCellAttrProvider()
{
m_attrForOddRows->DecRef();
}
wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col,
wxGridCellAttr::wxAttrKind kind /* = wxGridCellAttr::Any */) const
{
wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col, kind);
wxObjectDataPtr<wxGridCellAttr>
attr(wxGridCellAttrProvider::GetAttr(row, col, kind));
if ( row % 2 )
{
if ( !attr )
{
attr = m_attrForOddRows;
attr->IncRef();
}
else
{
if ( !attr->HasBackgroundColour() )
{
wxGridCellAttr *attrNew = attr->Clone();
attr->DecRef();
attr = attrNew;
attr = attr->Clone();
attr->SetBackgroundColour(*wxLIGHT_GREY);
}
}
}
return attr;
return attr.release();
}
void GridFrame::OnVTable(wxCommandEvent& )

View File

@ -296,13 +296,12 @@ class MyGridCellAttrProvider : public wxGridCellAttrProvider
{
public:
MyGridCellAttrProvider();
virtual ~MyGridCellAttrProvider();
virtual wxGridCellAttr *GetAttr(int row, int col,
wxGridCellAttr::wxAttrKind kind) const wxOVERRIDE;
private:
wxGridCellAttr *m_attrForOddRows;
wxObjectDataPtr<wxGridCellAttr> m_attrForOddRows;
};
// ----------------------------------------------------------------------------