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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 PDFium Authors. All rights reserved. 1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fpdfapi/parser/cpdf_array.h" 7 #include "core/fpdfapi/parser/cpdf_array.h"
8 8
9 #include <set> 9 #include <set>
10 10
11 #include "core/fpdfapi/parser/cpdf_name.h" 11 #include "core/fpdfapi/parser/cpdf_name.h"
12 #include "core/fpdfapi/parser/cpdf_number.h" 12 #include "core/fpdfapi/parser/cpdf_number.h"
13 #include "core/fpdfapi/parser/cpdf_reference.h" 13 #include "core/fpdfapi/parser/cpdf_reference.h"
14 #include "core/fpdfapi/parser/cpdf_stream.h" 14 #include "core/fpdfapi/parser/cpdf_stream.h"
15 #include "core/fpdfapi/parser/cpdf_string.h" 15 #include "core/fpdfapi/parser/cpdf_string.h"
16 #include "third_party/base/logging.h" 16 #include "third_party/base/logging.h"
17 #include "third_party/base/stl_util.h" 17 #include "third_party/base/stl_util.h"
18 18
19 CPDF_Array::CPDF_Array() {} 19 CPDF_Array::CPDF_Array() {}
20 20
21 CPDF_Array::~CPDF_Array() { 21 CPDF_Array::~CPDF_Array() {
22 // Mark the object as deleted so that it will not be deleted again 22 // Break cycles for cyclic references.
23 // in case of cyclic references.
24 m_ObjNum = kInvalidObjNum; 23 m_ObjNum = kInvalidObjNum;
25 for (auto& it : m_Objects) { 24 for (auto& it : m_Objects) {
26 if (it && it->GetObjNum() != kInvalidObjNum) 25 if (it && it->GetObjNum() == kInvalidObjNum)
27 delete it; 26 it.release();
28 } 27 }
29 } 28 }
30 29
31 CPDF_Object::Type CPDF_Array::GetType() const { 30 CPDF_Object::Type CPDF_Array::GetType() const {
32 return ARRAY; 31 return ARRAY;
33 } 32 }
34 33
35 bool CPDF_Array::IsArray() const { 34 bool CPDF_Array::IsArray() const {
36 return true; 35 return true;
37 } 36 }
38 37
39 CPDF_Array* CPDF_Array::AsArray() { 38 CPDF_Array* CPDF_Array::AsArray() {
40 return this; 39 return this;
41 } 40 }
42 41
43 const CPDF_Array* CPDF_Array::AsArray() const { 42 const CPDF_Array* CPDF_Array::AsArray() const {
44 return this; 43 return this;
45 } 44 }
46 45
47 std::unique_ptr<CPDF_Object> CPDF_Array::Clone() const { 46 std::unique_ptr<CPDF_Object> CPDF_Array::Clone() const {
48 return CloneObjectNonCyclic(false); 47 return CloneObjectNonCyclic(false);
49 } 48 }
50 49
51 std::unique_ptr<CPDF_Object> CPDF_Array::CloneNonCyclic( 50 std::unique_ptr<CPDF_Object> CPDF_Array::CloneNonCyclic(
52 bool bDirect, 51 bool bDirect,
53 std::set<const CPDF_Object*>* pVisited) const { 52 std::set<const CPDF_Object*>* pVisited) const {
54 pVisited->insert(this); 53 pVisited->insert(this);
55 auto pCopy = pdfium::MakeUnique<CPDF_Array>(); 54 auto pCopy = pdfium::MakeUnique<CPDF_Array>();
56 for (CPDF_Object* value : m_Objects) { 55 for (const auto& pValue : m_Objects) {
57 if (!pdfium::ContainsKey(*pVisited, value)) { 56 if (!pdfium::ContainsKey(*pVisited, pValue.get()))
58 pCopy->m_Objects.push_back( 57 pCopy->m_Objects.push_back(pValue->CloneNonCyclic(bDirect, pVisited));
59 value->CloneNonCyclic(bDirect, pVisited).release());
60 }
61 } 58 }
62 return std::move(pCopy); 59 return std::move(pCopy);
63 } 60 }
64 61
65 CFX_FloatRect CPDF_Array::GetRect() { 62 CFX_FloatRect CPDF_Array::GetRect() {
66 CFX_FloatRect rect; 63 CFX_FloatRect rect;
67 if (!IsArray() || m_Objects.size() != 4) 64 if (!IsArray() || m_Objects.size() != 4)
68 return rect; 65 return rect;
69 66
70 rect.left = GetNumberAt(0); 67 rect.left = GetNumberAt(0);
71 rect.bottom = GetNumberAt(1); 68 rect.bottom = GetNumberAt(1);
72 rect.right = GetNumberAt(2); 69 rect.right = GetNumberAt(2);
73 rect.top = GetNumberAt(3); 70 rect.top = GetNumberAt(3);
74 return rect; 71 return rect;
75 } 72 }
76 73
77 CFX_Matrix CPDF_Array::GetMatrix() { 74 CFX_Matrix CPDF_Array::GetMatrix() {
78 CFX_Matrix matrix; 75 CFX_Matrix matrix;
79 if (!IsArray() || m_Objects.size() != 6) 76 if (!IsArray() || m_Objects.size() != 6)
80 return matrix; 77 return matrix;
81 78
82 matrix.Set(GetNumberAt(0), GetNumberAt(1), GetNumberAt(2), GetNumberAt(3), 79 matrix.Set(GetNumberAt(0), GetNumberAt(1), GetNumberAt(2), GetNumberAt(3),
83 GetNumberAt(4), GetNumberAt(5)); 80 GetNumberAt(4), GetNumberAt(5));
84 return matrix; 81 return matrix;
85 } 82 }
86 83
87 CPDF_Object* CPDF_Array::GetObjectAt(size_t i) const { 84 CPDF_Object* CPDF_Array::GetObjectAt(size_t i) const {
88 if (i >= m_Objects.size()) 85 if (i >= m_Objects.size())
89 return nullptr; 86 return nullptr;
90 return m_Objects[i]; 87 return m_Objects[i].get();
91 } 88 }
92 89
93 CPDF_Object* CPDF_Array::GetDirectObjectAt(size_t i) const { 90 CPDF_Object* CPDF_Array::GetDirectObjectAt(size_t i) const {
94 if (i >= m_Objects.size()) 91 if (i >= m_Objects.size())
95 return nullptr; 92 return nullptr;
96 return m_Objects[i]->GetDirect(); 93 return m_Objects[i]->GetDirect();
97 } 94 }
98 95
99 CFX_ByteString CPDF_Array::GetStringAt(size_t i) const { 96 CFX_ByteString CPDF_Array::GetStringAt(size_t i) const {
100 if (i >= m_Objects.size()) 97 if (i >= m_Objects.size())
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 return ToArray(GetDirectObjectAt(i)); 130 return ToArray(GetDirectObjectAt(i));
134 } 131 }
135 132
136 void CPDF_Array::RemoveAt(size_t i, size_t nCount) { 133 void CPDF_Array::RemoveAt(size_t i, size_t nCount) {
137 if (i >= m_Objects.size()) 134 if (i >= m_Objects.size())
138 return; 135 return;
139 136
140 if (nCount <= 0 || nCount > m_Objects.size() - i) 137 if (nCount <= 0 || nCount > m_Objects.size() - i)
141 return; 138 return;
142 139
143 for (size_t j = 0; j < nCount; ++j)
144 delete m_Objects[i + j];
145
146 m_Objects.erase(m_Objects.begin() + i, m_Objects.begin() + i + nCount); 140 m_Objects.erase(m_Objects.begin() + i, m_Objects.begin() + i + nCount);
147 } 141 }
148 142
149 void CPDF_Array::ConvertToIndirectObjectAt(size_t i, 143 void CPDF_Array::ConvertToIndirectObjectAt(size_t i,
150 CPDF_IndirectObjectHolder* pHolder) { 144 CPDF_IndirectObjectHolder* pHolder) {
151 if (i >= m_Objects.size()) 145 if (i >= m_Objects.size())
152 return; 146 return;
153 147
154 CPDF_Object* pObj = m_Objects[i]; 148 if (!m_Objects[i] || m_Objects[i]->IsReference())
155 if (!pObj || pObj->IsReference())
156 return; 149 return;
157 150
158 CPDF_Object* pNew = pHolder->AddIndirectObject(pdfium::WrapUnique(pObj)); 151 CPDF_Object* pNew = pHolder->AddIndirectObject(std::move(m_Objects[i]));
159 m_Objects[i] = new CPDF_Reference(pHolder, pNew->GetObjNum()); 152 m_Objects[i] = pdfium::MakeUnique<CPDF_Reference>(pHolder, pNew->GetObjNum());
160 } 153 }
161 154
162 void CPDF_Array::SetAt(size_t i, CPDF_Object* pObj) { 155 CPDF_Object* CPDF_Array::SetAt(size_t i, std::unique_ptr<CPDF_Object> pObj) {
156 ASSERT(IsArray());
157 ASSERT(!pObj || pObj->IsInline());
158 if (i >= m_Objects.size()) {
159 ASSERT(false);
160 return nullptr;
161 }
162 CPDF_Object* pRet = pObj.get();
163 m_Objects[i] = std::move(pObj);
164 return pRet;
165 }
166
167 CPDF_Object* CPDF_Array::InsertAt(size_t index,
168 std::unique_ptr<CPDF_Object> pObj) {
163 ASSERT(IsArray()); 169 ASSERT(IsArray());
164 CHECK(!pObj || pObj->IsInline()); 170 CHECK(!pObj || pObj->IsInline());
165 if (i >= m_Objects.size()) { 171 CPDF_Object* pRet = pObj.get();
166 ASSERT(false); 172 if (index >= m_Objects.size()) {
167 return; 173 // Allocate space first.
174 m_Objects.resize(index + 1);
175 m_Objects[index] = std::move(pObj);
176 } else {
177 // Directly insert.
178 m_Objects.insert(m_Objects.begin() + index, std::move(pObj));
168 } 179 }
169 delete m_Objects[i]; 180 return pRet;
170 m_Objects[i] = pObj;
171 } 181 }
172 182
173 void CPDF_Array::InsertAt(size_t index, CPDF_Object* pObj) { 183 CPDF_Object* CPDF_Array::Add(std::unique_ptr<CPDF_Object> pObj) {
174 ASSERT(IsArray()); 184 ASSERT(IsArray());
175 CHECK(!pObj || pObj->IsInline()); 185 CHECK(!pObj || pObj->IsInline());
176 if (index >= m_Objects.size()) { 186 CPDF_Object* pRet = pObj.get();
177 // Allocate space first. 187 m_Objects.push_back(std::move(pObj));
178 m_Objects.resize(index + 1, nullptr); 188 return pRet;
179 m_Objects[index] = pObj;
180 } else {
181 // Directly insert.
182 m_Objects.insert(m_Objects.begin() + index, pObj);
183 }
184 } 189 }
185
186 void CPDF_Array::Add(CPDF_Object* pObj) {
187 ASSERT(IsArray());
188 CHECK(!pObj || pObj->IsInline());
189 m_Objects.push_back(pObj);
190 }
191
192 void CPDF_Array::AddName(const CFX_ByteString& str) {
193 Add(new CPDF_Name(str));
194 }
195
196 void CPDF_Array::AddString(const CFX_ByteString& str) {
197 Add(new CPDF_String(str, false));
198 }
199
200 void CPDF_Array::AddInteger(int i) {
201 Add(new CPDF_Number(i));
202 }
203
204 void CPDF_Array::AddNumber(FX_FLOAT f) {
205 Add(new CPDF_Number(f));
206 }
207
208 void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc,
209 uint32_t objnum) {
210 Add(new CPDF_Reference(pDoc, objnum));
211 }
212
213 void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc,
214 CPDF_Object* pObj) {
215 ASSERT(!pObj->IsInline());
216 Add(new CPDF_Reference(pDoc, pObj->GetObjNum()));
217 }
OLDNEW
« 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