From 0b18e1599dc9d07355c4ab6a069de33a536f7ba8 Mon Sep 17 00:00:00 2001 From: Nicolas Pena Date: Wed, 5 Apr 2017 16:34:50 -0400 Subject: Use unique_ptr in the children of CFieldTree::Node MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I149718bbdb7195223012150d6162d73cbeb3b8cc Reviewed-on: https://pdfium-review.googlesource.com/3813 Reviewed-by: Tom Sepez Reviewed-by: dsinclair Commit-Queue: Nicolás Peña --- core/fpdfdoc/cpdf_interform.cpp | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) (limited to 'core/fpdfdoc') diff --git a/core/fpdfdoc/cpdf_interform.cpp b/core/fpdfdoc/cpdf_interform.cpp index 0af050d0b2..1192e6e556 100644 --- a/core/fpdfdoc/cpdf_interform.cpp +++ b/core/fpdfdoc/cpdf_interform.cpp @@ -397,12 +397,14 @@ class CFieldTree { explicit Node(const CFX_WideString& short_name) : m_ShortName(short_name) {} ~Node() {} - void AddChildNode(Node* pNode) { m_Children.push_back(pNode); } + void AddChildNode(std::unique_ptr pNode) { + m_Children.push_back(std::move(pNode)); + } size_t GetChildrenCount() const { return m_Children.size(); } - Node* GetChildAt(size_t i) { return m_Children[i]; } - const Node* GetChildAt(size_t i) const { return m_Children[i]; } + Node* GetChildAt(size_t i) { return m_Children[i].get(); } + const Node* GetChildAt(size_t i) const { return m_Children[i].get(); } CPDF_FormField* GetFieldAtIndex(size_t index) { size_t nFieldsToGo = index; @@ -448,7 +450,7 @@ class CFieldTree { return count; } - std::vector m_Children; + std::vector> m_Children; CFX_WideString m_ShortName; std::unique_ptr m_pField; }; @@ -459,11 +461,9 @@ class CFieldTree { bool SetField(const CFX_WideString& full_name, std::unique_ptr pField); CPDF_FormField* GetField(const CFX_WideString& full_name); - void RemoveAll(); Node* FindNode(const CFX_WideString& full_name); Node* AddChild(Node* pParent, const CFX_WideString& short_name); - void RemoveNode(Node* pNode, int nLevel = 0); Node* Lookup(Node* pParent, const CFX_WideString& short_name); @@ -472,29 +472,17 @@ class CFieldTree { CFieldTree::CFieldTree() {} -CFieldTree::~CFieldTree() { - RemoveAll(); -} +CFieldTree::~CFieldTree() {} CFieldTree::Node* CFieldTree::AddChild(Node* pParent, const CFX_WideString& short_name) { if (!pParent) return nullptr; - Node* pNode = new Node(short_name); - pParent->AddChildNode(pNode); - return pNode; -} - -void CFieldTree::RemoveNode(Node* pNode, int nLevel) { - if (!pNode) - return; - - if (nLevel <= nMaxRecursion) { - for (size_t i = 0; i < pNode->GetChildrenCount(); ++i) - RemoveNode(pNode->GetChildAt(i), nLevel + 1); - } - delete pNode; + auto pNew = pdfium::MakeUnique(short_name); + Node* pChild = pNew.get(); + pParent->AddChildNode(std::move(pNew)); + return pChild; } CFieldTree::Node* CFieldTree::Lookup(Node* pParent, @@ -510,11 +498,6 @@ CFieldTree::Node* CFieldTree::Lookup(Node* pParent, return nullptr; } -void CFieldTree::RemoveAll() { - for (size_t i = 0; i < m_Root.GetChildrenCount(); ++i) - RemoveNode(m_Root.GetChildAt(i)); -} - bool CFieldTree::SetField(const CFX_WideString& full_name, std::unique_ptr pField) { if (full_name.IsEmpty()) -- cgit v1.2.3