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

Side by Side Diff: core/fpdfapi/parser/cpdf_dictionary.h

Issue 2510223002: Make CPDF_Dictionary use unique pointers. (Closed)
Patch Set: Plug leaks 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
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 #ifndef CORE_FPDFAPI_PARSER_CPDF_DICTIONARY_H_ 7 #ifndef CORE_FPDFAPI_PARSER_CPDF_DICTIONARY_H_
8 #define CORE_FPDFAPI_PARSER_CPDF_DICTIONARY_H_ 8 #define CORE_FPDFAPI_PARSER_CPDF_DICTIONARY_H_
9 9
10 #include <map> 10 #include <map>
11 #include <set> 11 #include <set>
12 12
13 #include "core/fpdfapi/parser/cpdf_object.h" 13 #include "core/fpdfapi/parser/cpdf_object.h"
14 #include "core/fxcrt/cfx_string_pool_template.h" 14 #include "core/fxcrt/cfx_string_pool_template.h"
15 #include "core/fxcrt/cfx_weak_ptr.h" 15 #include "core/fxcrt/cfx_weak_ptr.h"
16 #include "core/fxcrt/fx_coordinates.h" 16 #include "core/fxcrt/fx_coordinates.h"
17 #include "core/fxcrt/fx_string.h" 17 #include "core/fxcrt/fx_string.h"
18 #include "third_party/base/ptr_util.h" 18 #include "third_party/base/ptr_util.h"
19 19
20 class CPDF_IndirectObjectHolder; 20 class CPDF_IndirectObjectHolder;
21 21
22 class CPDF_Dictionary : public CPDF_Object { 22 class CPDF_Dictionary : public CPDF_Object {
23 public: 23 public:
24 using iterator = std::map<CFX_ByteString, CPDF_Object*>::iterator; 24 using const_iterator =
25 using const_iterator = std::map<CFX_ByteString, CPDF_Object*>::const_iterator; 25 std::map<CFX_ByteString, std::unique_ptr<CPDF_Object>>::const_iterator;
26 26
27 CPDF_Dictionary(); 27 CPDF_Dictionary();
28 explicit CPDF_Dictionary(const CFX_WeakPtr<CFX_ByteStringPool>& pPool); 28 explicit CPDF_Dictionary(const CFX_WeakPtr<CFX_ByteStringPool>& pPool);
29 ~CPDF_Dictionary() override; 29 ~CPDF_Dictionary() override;
30 30
31 // CPDF_Object: 31 // CPDF_Object:
32 Type GetType() const override; 32 Type GetType() const override;
33 std::unique_ptr<CPDF_Object> Clone() const override; 33 std::unique_ptr<CPDF_Object> Clone() const override;
34 CPDF_Dictionary* GetDict() const override; 34 CPDF_Dictionary* GetDict() const override;
35 bool IsDictionary() const override; 35 bool IsDictionary() const override;
(...skipping 17 matching lines...) Expand all
53 CFX_FloatRect GetRectFor(const CFX_ByteString& key) const; 53 CFX_FloatRect GetRectFor(const CFX_ByteString& key) const;
54 CFX_Matrix GetMatrixFor(const CFX_ByteString& key) const; 54 CFX_Matrix GetMatrixFor(const CFX_ByteString& key) const;
55 FX_FLOAT GetFloatFor(const CFX_ByteString& key) const { 55 FX_FLOAT GetFloatFor(const CFX_ByteString& key) const {
56 return GetNumberFor(key); 56 return GetNumberFor(key);
57 } 57 }
58 58
59 bool KeyExist(const CFX_ByteString& key) const; 59 bool KeyExist(const CFX_ByteString& key) const;
60 bool IsSignatureDict() const; 60 bool IsSignatureDict() const;
61 61
62 // Set* functions invalidate iterators for the element with the key |key|. 62 // Set* functions invalidate iterators for the element with the key |key|.
63 void SetFor(const CFX_ByteString& key, CPDF_Object* pObj); 63 // Takes ownership of |pObj|, returns an unowned pointer to it.
64 void SetBooleanFor(const CFX_ByteString& key, bool bValue); 64 CPDF_Object* SetFor(const CFX_ByteString& key,
65 void SetNameFor(const CFX_ByteString& key, const CFX_ByteString& name); 65 std::unique_ptr<CPDF_Object> pObj);
66 void SetStringFor(const CFX_ByteString& key, const CFX_ByteString& str);
67 void SetIntegerFor(const CFX_ByteString& key, int i);
68 void SetNumberFor(const CFX_ByteString& key, FX_FLOAT f);
69 void SetReferenceFor(const CFX_ByteString& key,
70 CPDF_IndirectObjectHolder* pDoc,
71 uint32_t objnum);
72 void SetReferenceFor(const CFX_ByteString& key,
73 CPDF_IndirectObjectHolder* pDoc,
74 CPDF_Object* pObj);
75 66
67 // Creates a new object owned by the dictionary and returns an unowned
68 // pointer to it.
69 template <typename T, typename... Args>
70 typename std::enable_if<!CanInternStrings<T>::value, T*>::type SetNewFor(
71 const CFX_ByteString& key,
72 Args... args) {
73 return static_cast<T*>(SetFor(key, pdfium::MakeUnique<T>(args...)));
74 }
75 template <typename T, typename... Args>
76 typename std::enable_if<CanInternStrings<T>::value, T*>::type SetNewFor(
77 const CFX_ByteString& key,
78 Args... args) {
79 return static_cast<T*>(
80 SetFor(key, pdfium::MakeUnique<T>(m_pPool, args...)));
81 }
82
83 // Convenience functions to convert native objects to array form.
76 void SetRectFor(const CFX_ByteString& key, const CFX_FloatRect& rect); 84 void SetRectFor(const CFX_ByteString& key, const CFX_FloatRect& rect);
77 void SetMatrixFor(const CFX_ByteString& key, const CFX_Matrix& matrix); 85 void SetMatrixFor(const CFX_ByteString& key, const CFX_Matrix& matrix);
78 86
79 void ConvertToIndirectObjectFor(const CFX_ByteString& key, 87 void ConvertToIndirectObjectFor(const CFX_ByteString& key,
80 CPDF_IndirectObjectHolder* pHolder); 88 CPDF_IndirectObjectHolder* pHolder);
81 89
82 // Invalidates iterators for the element with the key |key|. 90 // Invalidates iterators for the element with the key |key|.
83 void RemoveFor(const CFX_ByteString& key); 91 void RemoveFor(const CFX_ByteString& key);
84 92
85 // Invalidates iterators for the element with the key |oldkey|. 93 // Invalidates iterators for the element with the key |oldkey|.
86 void ReplaceKey(const CFX_ByteString& oldkey, const CFX_ByteString& newkey); 94 void ReplaceKey(const CFX_ByteString& oldkey, const CFX_ByteString& newkey);
87 95
88 iterator begin() { return m_Map.begin(); }
89 iterator end() { return m_Map.end(); }
90 const_iterator begin() const { return m_Map.begin(); } 96 const_iterator begin() const { return m_Map.begin(); }
91 const_iterator end() const { return m_Map.end(); } 97 const_iterator end() const { return m_Map.end(); }
92 98
93 CFX_WeakPtr<CFX_ByteStringPool> GetByteStringPool() const { return m_pPool; } 99 CFX_WeakPtr<CFX_ByteStringPool> GetByteStringPool() const { return m_pPool; }
94 100
95 protected: 101 protected:
96 CFX_ByteString MaybeIntern(const CFX_ByteString& str); 102 CFX_ByteString MaybeIntern(const CFX_ByteString& str);
97 std::unique_ptr<CPDF_Object> CloneNonCyclic( 103 std::unique_ptr<CPDF_Object> CloneNonCyclic(
98 bool bDirect, 104 bool bDirect,
99 std::set<const CPDF_Object*>* visited) const override; 105 std::set<const CPDF_Object*>* visited) const override;
100 106
101 CFX_WeakPtr<CFX_ByteStringPool> m_pPool; 107 CFX_WeakPtr<CFX_ByteStringPool> m_pPool;
102 std::map<CFX_ByteString, CPDF_Object*> m_Map; 108 std::map<CFX_ByteString, std::unique_ptr<CPDF_Object>> m_Map;
103 }; 109 };
104 110
105 inline CPDF_Dictionary* ToDictionary(CPDF_Object* obj) { 111 inline CPDF_Dictionary* ToDictionary(CPDF_Object* obj) {
106 return obj ? obj->AsDictionary() : nullptr; 112 return obj ? obj->AsDictionary() : nullptr;
107 } 113 }
108 114
109 inline const CPDF_Dictionary* ToDictionary(const CPDF_Object* obj) { 115 inline const CPDF_Dictionary* ToDictionary(const CPDF_Object* obj) {
110 return obj ? obj->AsDictionary() : nullptr; 116 return obj ? obj->AsDictionary() : nullptr;
111 } 117 }
112 118
113 inline std::unique_ptr<CPDF_Dictionary> ToDictionary( 119 inline std::unique_ptr<CPDF_Dictionary> ToDictionary(
114 std::unique_ptr<CPDF_Object> obj) { 120 std::unique_ptr<CPDF_Object> obj) {
115 CPDF_Dictionary* pDict = ToDictionary(obj.get()); 121 CPDF_Dictionary* pDict = ToDictionary(obj.get());
116 if (!pDict) 122 if (!pDict)
117 return nullptr; 123 return nullptr;
118 obj.release(); 124 obj.release();
119 return std::unique_ptr<CPDF_Dictionary>(pDict); 125 return std::unique_ptr<CPDF_Dictionary>(pDict);
120 } 126 }
121 127
122 #endif // CORE_FPDFAPI_PARSER_CPDF_DICTIONARY_H_ 128 #endif // CORE_FPDFAPI_PARSER_CPDF_DICTIONARY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698