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

Side by Side Diff: core/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp

Issue 2353013003: Re-land "Make CPDF_IndirectObjectHolder use unique_ptr to objects"" (Closed)
Patch Set: Created 4 years, 3 months 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
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/fpdf_parser/include/cpdf_indirect_object_holder.h" 7 #include "core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h"
8 8
9 #include "core/fpdfapi/fpdf_parser/include/cpdf_object.h" 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_object.h"
10 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h" 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h"
11 11
12 CPDF_IndirectObjectHolder::CPDF_IndirectObjectHolder() : m_LastObjNum(0) {} 12 CPDF_IndirectObjectHolder::CPDF_IndirectObjectHolder() : m_LastObjNum(0) {}
13 13
14 CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() { 14 CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() {}
15 for (const auto& pair : m_IndirectObjs)
16 delete pair.second;
17 }
18 15
19 CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject( 16 CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject(
20 uint32_t objnum) const { 17 uint32_t objnum) const {
21 auto it = m_IndirectObjs.find(objnum); 18 auto it = m_IndirectObjs.find(objnum);
22 return it != m_IndirectObjs.end() ? it->second : nullptr; 19 return it != m_IndirectObjs.end() ? it->second.get() : nullptr;
23 } 20 }
24 21
25 CPDF_Object* CPDF_IndirectObjectHolder::GetOrParseIndirectObject( 22 CPDF_Object* CPDF_IndirectObjectHolder::GetOrParseIndirectObject(
26 uint32_t objnum) { 23 uint32_t objnum) {
27 if (objnum == 0) 24 if (objnum == 0)
28 return nullptr; 25 return nullptr;
29 26
30 CPDF_Object* pObj = GetIndirectObject(objnum); 27 CPDF_Object* pObj = GetIndirectObject(objnum);
31 if (pObj) 28 if (pObj)
32 return pObj->GetObjNum() != CPDF_Object::kInvalidObjNum ? pObj : nullptr; 29 return pObj->GetObjNum() != CPDF_Object::kInvalidObjNum ? pObj : nullptr;
33 30
34 pObj = ParseIndirectObject(objnum); 31 pObj = ParseIndirectObject(objnum);
35 if (!pObj) 32 if (!pObj)
36 return nullptr; 33 return nullptr;
37 34
38 pObj->m_ObjNum = objnum; 35 pObj->m_ObjNum = objnum;
39 m_LastObjNum = std::max(m_LastObjNum, objnum); 36 m_LastObjNum = std::max(m_LastObjNum, objnum);
40 if (m_IndirectObjs[objnum]) 37 m_IndirectObjs[objnum].reset(pObj);
41 delete m_IndirectObjs[objnum];
42
43 m_IndirectObjs[objnum] = pObj;
44 return pObj; 38 return pObj;
45 } 39 }
46 40
47 CPDF_Object* CPDF_IndirectObjectHolder::ParseIndirectObject(uint32_t objnum) { 41 CPDF_Object* CPDF_IndirectObjectHolder::ParseIndirectObject(uint32_t objnum) {
48 return nullptr; 42 return nullptr;
49 } 43 }
50 44
51 uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { 45 uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) {
52 if (pObj->m_ObjNum) 46 if (pObj->m_ObjNum)
53 return pObj->m_ObjNum; 47 return pObj->m_ObjNum;
54 48
55 m_LastObjNum++; 49 m_LastObjNum++;
56 m_IndirectObjs[m_LastObjNum] = pObj; 50 m_IndirectObjs[m_LastObjNum].reset(pObj);
57 pObj->m_ObjNum = m_LastObjNum; 51 pObj->m_ObjNum = m_LastObjNum;
58 return m_LastObjNum; 52 return m_LastObjNum;
59 } 53 }
60 54
61 bool CPDF_IndirectObjectHolder::ReplaceIndirectObjectIfHigherGeneration( 55 bool CPDF_IndirectObjectHolder::ReplaceIndirectObjectIfHigherGeneration(
62 uint32_t objnum, 56 uint32_t objnum,
63 CPDF_Object* pObj) { 57 CPDF_Object* pObj) {
64 if (!objnum || !pObj) 58 if (!objnum || !pObj)
65 return false; 59 return false;
66 60
67 CPDF_Object* pOldObj = GetIndirectObject(objnum); 61 CPDF_Object* pOldObj = GetIndirectObject(objnum);
68 if (pOldObj) { 62 if (pOldObj && pObj->GetGenNum() <= pOldObj->GetGenNum()) {
69 if (pObj->GetGenNum() <= pOldObj->GetGenNum()) { 63 delete pObj;
70 delete pObj; 64 return false;
71 return false;
72 }
73 delete pOldObj;
74 } 65 }
75 pObj->m_ObjNum = objnum; 66 pObj->m_ObjNum = objnum;
76 m_IndirectObjs[objnum] = pObj; 67 m_IndirectObjs[objnum].reset(pObj);
77 m_LastObjNum = std::max(m_LastObjNum, objnum); 68 m_LastObjNum = std::max(m_LastObjNum, objnum);
78 return true; 69 return true;
79 } 70 }
80 71
81 void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { 72 void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) {
82 CPDF_Object* pObj = GetIndirectObject(objnum); 73 CPDF_Object* pObj = GetIndirectObject(objnum);
83 if (!pObj || pObj->GetObjNum() == CPDF_Object::kInvalidObjNum) 74 if (!pObj || pObj->GetObjNum() == CPDF_Object::kInvalidObjNum)
84 return; 75 return;
85 76
86 delete pObj;
87 m_IndirectObjs.erase(objnum); 77 m_IndirectObjs.erase(objnum);
88 } 78 }
OLDNEW
« no previous file with comments | « core/fpdfapi/fpdf_parser/cfdf_document.cpp ('k') | core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698