From 0b049be4b7e9d8ddd352bc92e003adc2cb241dbf Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 1 Jan 2024 20:47:58 +0100 Subject: [PATCH] Use unique_ptr<> in wxXmlDocument Replace manual use of raw pointer with std::unique_ptr<>. No real changes. --- include/wx/xml/xml.h | 12 +++++++----- src/xml/xml.cpp | 18 ++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/wx/xml/xml.h b/include/wx/xml/xml.h index d262a2a42b..7f24e71258 100644 --- a/include/wx/xml/xml.h +++ b/include/wx/xml/xml.h @@ -21,6 +21,8 @@ #include "wx/textbuf.h" #include "wx/versioninfo.h" +#include + #ifdef WXMAKINGDLL_XML #define WXDLLIMPEXP_XML WXEXPORT #elif defined(WXUSINGDLL) @@ -231,7 +233,7 @@ public: const wxString& encoding = wxT("UTF-8")); wxXmlDocument(wxInputStream& stream, const wxString& encoding = wxT("UTF-8")); - virtual ~wxXmlDocument() { delete m_docNode; } + ~wxXmlDocument() = default; wxXmlDocument(const wxXmlDocument& doc); wxXmlDocument& operator=(const wxXmlDocument& doc); @@ -252,7 +254,7 @@ public: // Returns root node of the document. wxXmlNode *GetRoot() const; // Returns the document node. - wxXmlNode *GetDocumentNode() const { return m_docNode; } + wxXmlNode *GetDocumentNode() const { return m_docNode.get(); } // Returns version of document (may be empty). @@ -267,8 +269,8 @@ public: wxString GetEOL() const { return m_eol; } // Write-access methods: - wxXmlNode *DetachDocumentNode() { wxXmlNode *old=m_docNode; m_docNode=nullptr; return old; } - void SetDocumentNode(wxXmlNode *node) { delete m_docNode; m_docNode = node; } + wxXmlNode *DetachDocumentNode() { return m_docNode.release(); } + void SetDocumentNode(wxXmlNode *node) { m_docNode.reset(node); } wxXmlNode *DetachRoot(); void SetRoot(wxXmlNode *node); void SetVersion(const wxString& version) { m_version = version; } @@ -283,7 +285,7 @@ private: wxString m_version; wxString m_fileEncoding; wxXmlDoctype m_doctype; - wxXmlNode *m_docNode = nullptr; + std::unique_ptr m_docNode; wxTextFileType m_fileType = wxTextFileType_Unix; wxString m_eol = wxS("\n"); diff --git a/src/xml/xml.cpp b/src/xml/xml.cpp index d9231e2122..b1971eb3a1 100644 --- a/src/xml/xml.cpp +++ b/src/xml/xml.cpp @@ -464,8 +464,6 @@ wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc) wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc) { - delete m_docNode; - DoCopy(doc); return *this; } @@ -479,9 +477,9 @@ void wxXmlDocument::DoCopy(const wxXmlDocument& doc) m_eol = doc.m_eol; if (doc.m_docNode) - m_docNode = new wxXmlNode(*doc.m_docNode); + m_docNode.reset(new wxXmlNode(*doc.m_docNode)); else - m_docNode = nullptr; + m_docNode.reset(); } bool wxXmlDocument::Load(const wxString& filename, const wxString& encoding, int flags) @@ -502,7 +500,7 @@ bool wxXmlDocument::Save(const wxString& filename, int indentstep) const wxXmlNode *wxXmlDocument::GetRoot() const { - wxXmlNode *node = m_docNode; + wxXmlNode *node = m_docNode.get(); if (node) { node = m_docNode->GetChildren(); @@ -514,7 +512,7 @@ wxXmlNode *wxXmlDocument::GetRoot() const wxXmlNode *wxXmlDocument::DetachRoot() { - wxXmlNode *node = m_docNode; + wxXmlNode *node = m_docNode.get(); if (node) { node = m_docNode->GetChildren(); @@ -547,7 +545,7 @@ void wxXmlDocument::SetRoot(wxXmlNode *root) "Can only set an element type node as root" ); } - wxXmlNode *node = m_docNode; + wxXmlNode *node = m_docNode.get(); if (node) { node = m_docNode->GetChildren(); @@ -569,11 +567,11 @@ void wxXmlDocument::SetRoot(wxXmlNode *root) } else { - m_docNode = new wxXmlNode(wxXML_DOCUMENT_NODE, wxEmptyString); + m_docNode.reset(new wxXmlNode(wxXML_DOCUMENT_NODE, wxEmptyString)); m_docNode->SetChildren(root); } if (root) - root->SetParent(m_docNode); + root->SetParent(m_docNode.get()); } void wxXmlDocument::SetFileType(wxTextFileType fileType) @@ -585,7 +583,7 @@ void wxXmlDocument::SetFileType(wxTextFileType fileType) void wxXmlDocument::AppendToProlog(wxXmlNode *node) { if (!m_docNode) - m_docNode = new wxXmlNode(wxXML_DOCUMENT_NODE, wxEmptyString); + m_docNode.reset(new wxXmlNode(wxXML_DOCUMENT_NODE, wxEmptyString)); if (IsOk()) m_docNode->InsertChild( node, GetRoot() ); else