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