Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1195)

Unified Diff: core/fpdfapi/parser/cpdf_array.cpp

Issue 2498223005: Make CPDF_Array take unique_ptrs (Closed)
Patch Set: nits Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « core/fpdfapi/parser/cpdf_array.h ('k') | core/fpdfapi/parser/cpdf_array_unittest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/fpdfapi/parser/cpdf_array.cpp
diff --git a/core/fpdfapi/parser/cpdf_array.cpp b/core/fpdfapi/parser/cpdf_array.cpp
index f8ec46acc9da7578f7284f8cf6917c00952c49c1..3edb3bb0efa958cfc53d0775a092a74befe5279a 100644
--- a/core/fpdfapi/parser/cpdf_array.cpp
+++ b/core/fpdfapi/parser/cpdf_array.cpp
@@ -19,12 +19,11 @@
CPDF_Array::CPDF_Array() {}
CPDF_Array::~CPDF_Array() {
- // Mark the object as deleted so that it will not be deleted again
- // in case of cyclic references.
+ // Break cycles for cyclic references.
m_ObjNum = kInvalidObjNum;
for (auto& it : m_Objects) {
- if (it && it->GetObjNum() != kInvalidObjNum)
- delete it;
+ if (it && it->GetObjNum() == kInvalidObjNum)
+ it.release();
}
}
@@ -53,11 +52,9 @@ std::unique_ptr<CPDF_Object> CPDF_Array::CloneNonCyclic(
std::set<const CPDF_Object*>* pVisited) const {
pVisited->insert(this);
auto pCopy = pdfium::MakeUnique<CPDF_Array>();
- for (CPDF_Object* value : m_Objects) {
- if (!pdfium::ContainsKey(*pVisited, value)) {
- pCopy->m_Objects.push_back(
- value->CloneNonCyclic(bDirect, pVisited).release());
- }
+ for (const auto& pValue : m_Objects) {
+ if (!pdfium::ContainsKey(*pVisited, pValue.get()))
+ pCopy->m_Objects.push_back(pValue->CloneNonCyclic(bDirect, pVisited));
}
return std::move(pCopy);
}
@@ -87,7 +84,7 @@ CFX_Matrix CPDF_Array::GetMatrix() {
CPDF_Object* CPDF_Array::GetObjectAt(size_t i) const {
if (i >= m_Objects.size())
return nullptr;
- return m_Objects[i];
+ return m_Objects[i].get();
}
CPDF_Object* CPDF_Array::GetDirectObjectAt(size_t i) const {
@@ -140,9 +137,6 @@ void CPDF_Array::RemoveAt(size_t i, size_t nCount) {
if (nCount <= 0 || nCount > m_Objects.size() - i)
return;
- for (size_t j = 0; j < nCount; ++j)
- delete m_Objects[i + j];
-
m_Objects.erase(m_Objects.begin() + i, m_Objects.begin() + i + nCount);
}
@@ -151,67 +145,45 @@ void CPDF_Array::ConvertToIndirectObjectAt(size_t i,
if (i >= m_Objects.size())
return;
- CPDF_Object* pObj = m_Objects[i];
- if (!pObj || pObj->IsReference())
+ if (!m_Objects[i] || m_Objects[i]->IsReference())
return;
- CPDF_Object* pNew = pHolder->AddIndirectObject(pdfium::WrapUnique(pObj));
- m_Objects[i] = new CPDF_Reference(pHolder, pNew->GetObjNum());
+ CPDF_Object* pNew = pHolder->AddIndirectObject(std::move(m_Objects[i]));
+ m_Objects[i] = pdfium::MakeUnique<CPDF_Reference>(pHolder, pNew->GetObjNum());
}
-void CPDF_Array::SetAt(size_t i, CPDF_Object* pObj) {
+CPDF_Object* CPDF_Array::SetAt(size_t i, std::unique_ptr<CPDF_Object> pObj) {
ASSERT(IsArray());
- CHECK(!pObj || pObj->IsInline());
+ ASSERT(!pObj || pObj->IsInline());
if (i >= m_Objects.size()) {
ASSERT(false);
- return;
+ return nullptr;
}
- delete m_Objects[i];
- m_Objects[i] = pObj;
+ CPDF_Object* pRet = pObj.get();
+ m_Objects[i] = std::move(pObj);
+ return pRet;
}
-void CPDF_Array::InsertAt(size_t index, CPDF_Object* pObj) {
+CPDF_Object* CPDF_Array::InsertAt(size_t index,
+ std::unique_ptr<CPDF_Object> pObj) {
ASSERT(IsArray());
CHECK(!pObj || pObj->IsInline());
+ CPDF_Object* pRet = pObj.get();
if (index >= m_Objects.size()) {
// Allocate space first.
- m_Objects.resize(index + 1, nullptr);
- m_Objects[index] = pObj;
+ m_Objects.resize(index + 1);
+ m_Objects[index] = std::move(pObj);
} else {
// Directly insert.
- m_Objects.insert(m_Objects.begin() + index, pObj);
+ m_Objects.insert(m_Objects.begin() + index, std::move(pObj));
}
+ return pRet;
}
-void CPDF_Array::Add(CPDF_Object* pObj) {
+CPDF_Object* CPDF_Array::Add(std::unique_ptr<CPDF_Object> pObj) {
ASSERT(IsArray());
CHECK(!pObj || pObj->IsInline());
- m_Objects.push_back(pObj);
-}
-
-void CPDF_Array::AddName(const CFX_ByteString& str) {
- Add(new CPDF_Name(str));
-}
-
-void CPDF_Array::AddString(const CFX_ByteString& str) {
- Add(new CPDF_String(str, false));
-}
-
-void CPDF_Array::AddInteger(int i) {
- Add(new CPDF_Number(i));
-}
-
-void CPDF_Array::AddNumber(FX_FLOAT f) {
- Add(new CPDF_Number(f));
-}
-
-void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc,
- uint32_t objnum) {
- Add(new CPDF_Reference(pDoc, objnum));
-}
-
-void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc,
- CPDF_Object* pObj) {
- ASSERT(!pObj->IsInline());
- Add(new CPDF_Reference(pDoc, pObj->GetObjNum()));
+ CPDF_Object* pRet = pObj.get();
+ m_Objects.push_back(std::move(pObj));
+ return pRet;
}
« no previous file with comments | « core/fpdfapi/parser/cpdf_array.h ('k') | core/fpdfapi/parser/cpdf_array_unittest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698