Use wxObjectDataPtr to manage wxPrintData::m_nativeData

Remove code allocating and freeing this pointer directly and use
wxObjectDataPtr instead.

This required adding wxRefCounter-like methods to wxPrintData, which
can't inherit from wxRefCounter because it uses a different name for its
field storing the reference counter, which is public and so can't be
renamed without breaking compatibility.
This commit is contained in:
Vadim Zeitlin 2023-03-22 18:10:18 +01:00
parent 22a09c2fb5
commit ecf3d73695
4 changed files with 15 additions and 26 deletions

View file

@ -113,7 +113,7 @@ public:
void ConvertToNative();
void ConvertFromNative();
// Holds the native print data
wxPrintNativeDataBase *GetNativeData() const { return m_nativeData; }
wxPrintNativeDataBase *GetNativeData() const { return m_nativeData.get(); }
private:
wxPrintBin m_bin;
@ -136,7 +136,7 @@ private:
std::vector<char> m_privData;
wxPrintNativeDataBase *m_nativeData;
wxObjectDataPtr<wxPrintNativeDataBase> m_nativeData;
private:
wxDECLARE_DYNAMIC_CLASS(wxPrintData);

View file

@ -159,8 +159,8 @@ public:
class WXDLLIMPEXP_CORE wxPrintNativeDataBase: public wxObject
{
public:
wxPrintNativeDataBase();
virtual ~wxPrintNativeDataBase() {}
wxPrintNativeDataBase() = default;
virtual ~wxPrintNativeDataBase();
virtual bool TransferTo( wxPrintData &data ) = 0;
virtual bool TransferFrom( const wxPrintData &data ) = 0;
@ -172,7 +172,14 @@ public:
virtual bool Ok() const { return IsOk(); }
virtual bool IsOk() const = 0;
int m_ref;
// Internal implementation details, do not use.
// For historical reasons, this class doesn't use wxRefCounter, but provides
// the same methods, so that it could still be used with wxObjectDataPtr.
void IncRef() { m_ref++; }
void DecRef() { if ( !--m_ref) delete this; }
int m_ref = 1;
private:
wxDECLARE_CLASS(wxPrintNativeDataBase);

View file

@ -79,7 +79,6 @@ wxPrintData::wxPrintData()
wxPrintData::wxPrintData(const wxPrintData& printData)
: wxObject()
{
m_nativeData = nullptr;
(*this) = printData;
}
@ -96,12 +95,7 @@ void wxPrintData::SetPrivData( char *privData, int len )
}
}
wxPrintData::~wxPrintData()
{
m_nativeData->m_ref--;
if (m_nativeData->m_ref == 0)
delete m_nativeData;
}
wxPrintData::~wxPrintData() = default;
void wxPrintData::ConvertToNative()
{
@ -133,16 +127,7 @@ wxPrintData& wxPrintData::operator=(const wxPrintData& data)
m_printMode = data.m_printMode;
m_filename = data.m_filename;
// UnRef old m_nativeData
if (m_nativeData)
{
m_nativeData->m_ref--;
if (m_nativeData->m_ref == 0)
delete m_nativeData;
}
// Set Ref new one
m_nativeData = data.GetNativeData();
m_nativeData->m_ref++;
m_nativeData = data.m_nativeData;
m_privData = data.m_privData;

View file

@ -288,10 +288,7 @@ wxPrintNativeDataBase *wxNativePrintFactory::CreatePrintNativeData()
wxIMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase, wxObject);
wxPrintNativeDataBase::wxPrintNativeDataBase()
{
m_ref = 1;
}
wxPrintNativeDataBase::~wxPrintNativeDataBase() = default;
//----------------------------------------------------------------------------
// wxPrintFactoryModule