diff options
author | tsepez <tsepez@chromium.org> | 2016-05-18 10:16:43 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-05-18 10:16:43 -0700 |
commit | 3b093630d55ad97807bcc710227b03819944605b (patch) | |
tree | db2148d92e0e56b13a6084af203529e0b3238d88 /core/fpdfdoc | |
parent | 5c9b65e603ee970b1a5cfbdf09273885a31e877b (diff) | |
download | pdfium-3b093630d55ad97807bcc710227b03819944605b.tar.xz |
Use std::vector in CPDF_StructTreeImpl
Allows use of CFX_RetainPtrs in place of explicit refcounting.
Review-Url: https://codereview.chromium.org/1985253002
Diffstat (limited to 'core/fpdfdoc')
-rw-r--r-- | core/fpdfdoc/doc_tagged.cpp | 112 | ||||
-rw-r--r-- | core/fpdfdoc/tagged_int.h | 13 |
2 files changed, 54 insertions, 71 deletions
diff --git a/core/fpdfdoc/doc_tagged.cpp b/core/fpdfdoc/doc_tagged.cpp index cfa9e4663c..b5b89a017e 100644 --- a/core/fpdfdoc/doc_tagged.cpp +++ b/core/fpdfdoc/doc_tagged.cpp @@ -53,19 +53,14 @@ CPDF_StructTreeImpl::CPDF_StructTreeImpl(const CPDF_Document* pDoc) m_pRoleMap(m_pTreeRoot ? m_pTreeRoot->GetDictBy("RoleMap") : nullptr), m_pPage(nullptr) {} -CPDF_StructTreeImpl::~CPDF_StructTreeImpl() { - for (int i = 0; i < m_Kids.GetSize(); i++) { - if (m_Kids[i]) - m_Kids[i]->Release(); - } -} +CPDF_StructTreeImpl::~CPDF_StructTreeImpl() {} int CPDF_StructTreeImpl::CountTopElements() const { - return m_Kids.GetSize(); + return pdfium::CollectionSize<int>(m_Kids); } IPDF_StructElement* CPDF_StructTreeImpl::GetTopElement(int i) const { - return m_Kids.GetAt(i); + return m_Kids[i].Get(); } void CPDF_StructTreeImpl::LoadDocTree() { @@ -76,23 +71,23 @@ void CPDF_StructTreeImpl::LoadDocTree() { CPDF_Object* pKids = m_pTreeRoot->GetDirectObjectBy("K"); if (!pKids) return; + if (CPDF_Dictionary* pDict = pKids->AsDictionary()) { - CPDF_StructElementImpl* pStructElementImpl = - new CPDF_StructElementImpl(this, nullptr, pDict); - m_Kids.Add(pStructElementImpl); + m_Kids.push_back(CFX_RetainPtr<CPDF_StructElementImpl>( + new CPDF_StructElementImpl(this, nullptr, pDict))); return; } + CPDF_Array* pArray = pKids->AsArray(); if (!pArray) return; for (size_t i = 0; i < pArray->GetCount(); i++) { - CPDF_Dictionary* pKid = pArray->GetDictAt(i); - CPDF_StructElementImpl* pStructElementImpl = - new CPDF_StructElementImpl(this, nullptr, pKid); - m_Kids.Add(pStructElementImpl); + m_Kids.push_back(CFX_RetainPtr<CPDF_StructElementImpl>( + new CPDF_StructElementImpl(this, nullptr, pArray->GetDictAt(i)))); } } + void CPDF_StructTreeImpl::LoadPageTree(const CPDF_Dictionary* pPageDict) { m_pPage = pPageDict; if (!m_pTreeRoot) @@ -110,32 +105,28 @@ void CPDF_StructTreeImpl::LoadPageTree(const CPDF_Dictionary* pPageDict) { else return; - uint32_t i; - m_Kids.SetSize(dwKids); - for (i = 0; i < dwKids; i++) { - m_Kids[i] = NULL; - } - std::map<CPDF_Dictionary*, CPDF_StructElementImpl*> element_map; + m_Kids.clear(); + m_Kids.resize(dwKids); CPDF_Dictionary* pParentTree = m_pTreeRoot->GetDictBy("ParentTree"); - if (!pParentTree) { + if (!pParentTree) return; - } + CPDF_NumberTree parent_tree(pParentTree); int parents_id = pPageDict->GetIntegerBy("StructParents", -1); - if (parents_id >= 0) { - CPDF_Array* pParentArray = ToArray(parent_tree.LookupValue(parents_id)); - if (!pParentArray) - return; + if (parents_id < 0) + return; - for (i = 0; i < pParentArray->GetCount(); i++) { - CPDF_Dictionary* pParent = pParentArray->GetDictAt(i); - if (!pParent) { - continue; - } + CPDF_Array* pParentArray = ToArray(parent_tree.LookupValue(parents_id)); + if (!pParentArray) + return; + + std::map<CPDF_Dictionary*, CPDF_StructElementImpl*> element_map; + for (size_t i = 0; i < pParentArray->GetCount(); i++) { + if (CPDF_Dictionary* pParent = pParentArray->GetDictAt(i)) AddPageNode(pParent, element_map); - } } } + CPDF_StructElementImpl* CPDF_StructTreeImpl::AddPageNode( CPDF_Dictionary* pDict, std::map<CPDF_Dictionary*, CPDF_StructElementImpl*>& map, @@ -160,14 +151,12 @@ CPDF_StructElementImpl* CPDF_StructTreeImpl::AddPageNode( CPDF_StructElementImpl* pParentElement = AddPageNode(pParent, map, nLevel + 1); FX_BOOL bSave = FALSE; - for (int i = 0; i < pParentElement->m_Kids.GetSize(); i++) { - if (pParentElement->m_Kids[i].m_Type != CPDF_StructKid::Element) { + for (CPDF_StructKid& kid : pParentElement->m_Kids) { + if (kid.m_Type != CPDF_StructKid::Element) continue; - } - if (pParentElement->m_Kids[i].m_Element.m_pDict != pDict) { + if (kid.m_Element.m_pDict != pDict) continue; - } - pParentElement->m_Kids[i].m_Element.m_pElement = pElement->Retain(); + kid.m_Element.m_pElement = pElement->Retain(); bSave = TRUE; } if (!bSave) { @@ -180,33 +169,22 @@ CPDF_StructElementImpl* CPDF_StructTreeImpl::AddPageNode( FX_BOOL CPDF_StructTreeImpl::AddTopLevelNode(CPDF_Dictionary* pDict, CPDF_StructElementImpl* pElement) { CPDF_Object* pObj = m_pTreeRoot->GetDirectObjectBy("K"); - if (!pObj) { + if (!pObj) return FALSE; - } + if (pObj->IsDictionary()) { - if (pObj->GetObjNum() == pDict->GetObjNum()) { - if (m_Kids[0]) { - m_Kids[0]->Release(); - } - m_Kids[0] = pElement->Retain(); - } else { + if (pObj->GetObjNum() != pDict->GetObjNum()) return FALSE; - } + m_Kids[0].Reset(pElement); } if (CPDF_Array* pTopKids = pObj->AsArray()) { - uint32_t i; - FX_BOOL bSave = FALSE; - for (i = 0; i < pTopKids->GetCount(); i++) { + bool bSave = false; + for (size_t i = 0; i < pTopKids->GetCount(); i++) { CPDF_Reference* pKidRef = ToReference(pTopKids->GetObjectAt(i)); - if (!pKidRef) - continue; - if (pKidRef->GetRefObjNum() != pDict->GetObjNum()) - continue; - - if (m_Kids[i]) - m_Kids[i]->Release(); - m_Kids[i] = pElement->Retain(); - bSave = TRUE; + if (pKidRef && pKidRef->GetRefObjNum() == pDict->GetObjNum()) { + m_Kids[i].Reset(pElement); + bSave = true; + } } if (!bSave) return FALSE; @@ -231,13 +209,12 @@ CPDF_StructElementImpl::CPDF_StructElementImpl(CPDF_StructTreeImpl* pTree, } CPDF_StructElementImpl::~CPDF_StructElementImpl() { - for (int i = 0; i < m_Kids.GetSize(); i++) { - if (m_Kids[i].m_Type == CPDF_StructKid::Element && - m_Kids[i].m_Element.m_pElement) { - ((CPDF_StructElementImpl*)m_Kids[i].m_Element.m_pElement)->Release(); - } + for (CPDF_StructKid& kid : m_Kids) { + if (kid.m_Type == CPDF_StructKid::Element && kid.m_Element.m_pElement) + static_cast<CPDF_StructElementImpl*>(kid.m_Element.m_pElement)->Release(); } } + CPDF_StructElementImpl* CPDF_StructElementImpl::Retain() { m_RefCount++; return this; @@ -257,14 +234,15 @@ void CPDF_StructElementImpl::LoadKids(CPDF_Dictionary* pDict) { if (!pKids) return; + m_Kids.clear(); if (CPDF_Array* pArray = pKids->AsArray()) { - m_Kids.SetSize(pArray->GetCount()); + m_Kids.resize(pArray->GetCount()); for (uint32_t i = 0; i < pArray->GetCount(); i++) { CPDF_Object* pKid = pArray->GetDirectObjectAt(i); LoadKid(PageObjNum, pKid, &m_Kids[i]); } } else { - m_Kids.SetSize(1); + m_Kids.resize(1); LoadKid(PageObjNum, pKids, &m_Kids[0]); } } diff --git a/core/fpdfdoc/tagged_int.h b/core/fpdfdoc/tagged_int.h index 7b9a32fa84..d508211441 100644 --- a/core/fpdfdoc/tagged_int.h +++ b/core/fpdfdoc/tagged_int.h @@ -8,8 +8,12 @@ #define CORE_FPDFDOC_TAGGED_INT_H_ #include <map> +#include <memory> +#include <vector> #include "core/fpdfdoc/include/fpdf_tagged.h" +#include "core/fxcrt/include/cfx_retain_ptr.h" +#include "third_party/base/stl_util.h" class CPDF_StructElementImpl; @@ -35,7 +39,8 @@ class CPDF_StructTreeImpl final : public IPDF_StructTree { const CPDF_Dictionary* const m_pTreeRoot; const CPDF_Dictionary* const m_pRoleMap; const CPDF_Dictionary* m_pPage; - CFX_ArrayTemplate<CPDF_StructElementImpl*> m_Kids; + std::vector<CFX_RetainPtr<CPDF_StructElementImpl>> m_Kids; + friend class CPDF_StructElementImpl; }; @@ -50,9 +55,9 @@ class CPDF_StructElementImpl final : public IPDF_StructElement { const CFX_ByteString& GetType() const override { return m_Type; } IPDF_StructElement* GetParent() const override { return m_pParent; } CPDF_Dictionary* GetDict() const override { return m_pDict; } - int CountKids() const override { return m_Kids.GetSize(); } + int CountKids() const override { return pdfium::CollectionSize<int>(m_Kids); } const CPDF_StructKid& GetKid(int index) const override { - return m_Kids.GetData()[index]; + return m_Kids[index]; } CPDF_Object* GetAttr(const CFX_ByteStringC& owner, const CFX_ByteStringC& name, @@ -96,7 +101,7 @@ class CPDF_StructElementImpl final : public IPDF_StructElement { CPDF_StructElementImpl* const m_pParent; CPDF_Dictionary* const m_pDict; CFX_ByteString m_Type; - CFX_ArrayTemplate<CPDF_StructKid> m_Kids; + std::vector<CPDF_StructKid> m_Kids; friend class CPDF_StructTreeImpl; }; |