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_ARRAY_H_ | 7 #ifndef CORE_FPDFAPI_PARSER_CPDF_ARRAY_H_ |
8 #define CORE_FPDFAPI_PARSER_CPDF_ARRAY_H_ | 8 #define CORE_FPDFAPI_PARSER_CPDF_ARRAY_H_ |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <set> | 11 #include <set> |
| 12 #include <type_traits> |
12 #include <vector> | 13 #include <vector> |
13 | 14 |
14 #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h" | 15 #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h" |
15 #include "core/fpdfapi/parser/cpdf_object.h" | 16 #include "core/fpdfapi/parser/cpdf_object.h" |
16 #include "core/fxcrt/fx_basic.h" | 17 #include "core/fxcrt/fx_basic.h" |
17 #include "core/fxcrt/fx_coordinates.h" | 18 #include "core/fxcrt/fx_coordinates.h" |
18 #include "third_party/base/ptr_util.h" | 19 #include "third_party/base/ptr_util.h" |
19 | 20 |
20 class CPDF_Array : public CPDF_Object { | 21 class CPDF_Array : public CPDF_Object { |
21 public: | 22 public: |
22 using const_iterator = | 23 using const_iterator = |
23 std::vector<std::unique_ptr<CPDF_Object>>::const_iterator; | 24 std::vector<std::unique_ptr<CPDF_Object>>::const_iterator; |
24 | 25 |
25 CPDF_Array(); | 26 CPDF_Array(); |
| 27 explicit CPDF_Array(const CFX_WeakPtr<CFX_ByteStringPool>& pPool); |
26 ~CPDF_Array() override; | 28 ~CPDF_Array() override; |
27 | 29 |
28 // CPDF_Object: | 30 // CPDF_Object: |
29 Type GetType() const override; | 31 Type GetType() const override; |
30 std::unique_ptr<CPDF_Object> Clone() const override; | 32 std::unique_ptr<CPDF_Object> Clone() const override; |
31 bool IsArray() const override; | 33 bool IsArray() const override; |
32 CPDF_Array* AsArray() override; | 34 CPDF_Array* AsArray() override; |
33 const CPDF_Array* AsArray() const override; | 35 const CPDF_Array* AsArray() const override; |
34 | 36 |
35 bool IsEmpty() const { return m_Objects.empty(); } | 37 bool IsEmpty() const { return m_Objects.empty(); } |
36 size_t GetCount() const { return m_Objects.size(); } | 38 size_t GetCount() const { return m_Objects.size(); } |
37 CPDF_Object* GetObjectAt(size_t index) const; | 39 CPDF_Object* GetObjectAt(size_t index) const; |
38 CPDF_Object* GetDirectObjectAt(size_t index) const; | 40 CPDF_Object* GetDirectObjectAt(size_t index) const; |
39 CFX_ByteString GetStringAt(size_t index) const; | 41 CFX_ByteString GetStringAt(size_t index) const; |
40 int GetIntegerAt(size_t index) const; | 42 int GetIntegerAt(size_t index) const; |
41 FX_FLOAT GetNumberAt(size_t index) const; | 43 FX_FLOAT GetNumberAt(size_t index) const; |
42 CPDF_Dictionary* GetDictAt(size_t index) const; | 44 CPDF_Dictionary* GetDictAt(size_t index) const; |
43 CPDF_Stream* GetStreamAt(size_t index) const; | 45 CPDF_Stream* GetStreamAt(size_t index) const; |
44 CPDF_Array* GetArrayAt(size_t index) const; | 46 CPDF_Array* GetArrayAt(size_t index) const; |
45 FX_FLOAT GetFloatAt(size_t index) const { return GetNumberAt(index); } | 47 FX_FLOAT GetFloatAt(size_t index) const { return GetNumberAt(index); } |
46 CFX_Matrix GetMatrix(); | 48 CFX_Matrix GetMatrix(); |
47 CFX_FloatRect GetRect(); | 49 CFX_FloatRect GetRect(); |
48 | 50 |
49 // Takes ownership of |pObj|, returns unowned pointer to it. | 51 // Takes ownership of |pObj|, returns unowned pointer to it. |
50 CPDF_Object* Add(std::unique_ptr<CPDF_Object> pObj); | 52 CPDF_Object* Add(std::unique_ptr<CPDF_Object> pObj); |
51 CPDF_Object* SetAt(size_t index, std::unique_ptr<CPDF_Object> pObj); | 53 CPDF_Object* SetAt(size_t index, std::unique_ptr<CPDF_Object> pObj); |
52 CPDF_Object* InsertAt(size_t index, std::unique_ptr<CPDF_Object> pObj); | 54 CPDF_Object* InsertAt(size_t index, std::unique_ptr<CPDF_Object> pObj); |
53 | 55 |
54 // Creates object owned by the array, returns unowned pointer to it. | 56 // Creates object owned by the array, returns unowned pointer to it. |
| 57 // We have special cases for objects that can intern strings from |
| 58 // a ByteStringPool. |
55 template <typename T, typename... Args> | 59 template <typename T, typename... Args> |
56 T* AddNew(Args... args) { | 60 typename std::enable_if<!CanInternStrings<T>::value, T*>::type AddNew( |
| 61 Args... args) { |
57 return static_cast<T*>(Add(pdfium::MakeUnique<T>(args...))); | 62 return static_cast<T*>(Add(pdfium::MakeUnique<T>(args...))); |
58 } | 63 } |
59 template <typename T, typename... Args> | 64 template <typename T, typename... Args> |
60 T* SetNewAt(size_t index, Args... args) { | 65 typename std::enable_if<CanInternStrings<T>::value, T*>::type AddNew( |
| 66 Args... args) { |
| 67 return static_cast<T*>(Add(pdfium::MakeUnique<T>(m_pPool, args...))); |
| 68 } |
| 69 template <typename T, typename... Args> |
| 70 typename std::enable_if<!CanInternStrings<T>::value, T*>::type SetNewAt( |
| 71 size_t index, |
| 72 Args... args) { |
61 return static_cast<T*>(SetAt(index, pdfium::MakeUnique<T>(args...))); | 73 return static_cast<T*>(SetAt(index, pdfium::MakeUnique<T>(args...))); |
62 } | 74 } |
63 template <typename T, typename... Args> | 75 template <typename T, typename... Args> |
64 T* InsertNewAt(size_t index, Args... args) { | 76 typename std::enable_if<CanInternStrings<T>::value, T*>::type SetNewAt( |
| 77 size_t index, |
| 78 Args... args) { |
| 79 return static_cast<T*>( |
| 80 SetAt(index, pdfium::MakeUnique<T>(m_pPool, args...))); |
| 81 } |
| 82 template <typename T, typename... Args> |
| 83 typename std::enable_if<!CanInternStrings<T>::value, T*>::type InsertNewAt( |
| 84 size_t index, |
| 85 Args... args) { |
65 return static_cast<T*>(InsertAt(index, pdfium::MakeUnique<T>(args...))); | 86 return static_cast<T*>(InsertAt(index, pdfium::MakeUnique<T>(args...))); |
66 } | 87 } |
| 88 template <typename T, typename... Args> |
| 89 typename std::enable_if<CanInternStrings<T>::value, T*>::type InsertNewAt( |
| 90 size_t index, |
| 91 Args... args) { |
| 92 return static_cast<T*>( |
| 93 InsertAt(index, pdfium::MakeUnique<T>(m_pPool, args...))); |
| 94 } |
67 | 95 |
68 void RemoveAt(size_t index, size_t nCount = 1); | 96 void RemoveAt(size_t index, size_t nCount = 1); |
69 void ConvertToIndirectObjectAt(size_t index, CPDF_IndirectObjectHolder* pDoc); | 97 void ConvertToIndirectObjectAt(size_t index, CPDF_IndirectObjectHolder* pDoc); |
70 | 98 |
71 const_iterator begin() const { return m_Objects.begin(); } | 99 const_iterator begin() const { return m_Objects.begin(); } |
72 const_iterator end() const { return m_Objects.end(); } | 100 const_iterator end() const { return m_Objects.end(); } |
73 | 101 |
74 protected: | 102 protected: |
75 std::unique_ptr<CPDF_Object> CloneNonCyclic( | 103 std::unique_ptr<CPDF_Object> CloneNonCyclic( |
76 bool bDirect, | 104 bool bDirect, |
77 std::set<const CPDF_Object*>* pVisited) const override; | 105 std::set<const CPDF_Object*>* pVisited) const override; |
78 | 106 |
79 std::vector<std::unique_ptr<CPDF_Object>> m_Objects; | 107 std::vector<std::unique_ptr<CPDF_Object>> m_Objects; |
| 108 CFX_WeakPtr<CFX_ByteStringPool> m_pPool; |
80 }; | 109 }; |
81 | 110 |
82 inline CPDF_Array* ToArray(CPDF_Object* obj) { | 111 inline CPDF_Array* ToArray(CPDF_Object* obj) { |
83 return obj ? obj->AsArray() : nullptr; | 112 return obj ? obj->AsArray() : nullptr; |
84 } | 113 } |
85 | 114 |
86 inline const CPDF_Array* ToArray(const CPDF_Object* obj) { | 115 inline const CPDF_Array* ToArray(const CPDF_Object* obj) { |
87 return obj ? obj->AsArray() : nullptr; | 116 return obj ? obj->AsArray() : nullptr; |
88 } | 117 } |
89 | 118 |
90 inline std::unique_ptr<CPDF_Array> ToArray(std::unique_ptr<CPDF_Object> obj) { | 119 inline std::unique_ptr<CPDF_Array> ToArray(std::unique_ptr<CPDF_Object> obj) { |
91 CPDF_Array* pArray = ToArray(obj.get()); | 120 CPDF_Array* pArray = ToArray(obj.get()); |
92 if (!pArray) | 121 if (!pArray) |
93 return nullptr; | 122 return nullptr; |
94 obj.release(); | 123 obj.release(); |
95 return std::unique_ptr<CPDF_Array>(pArray); | 124 return std::unique_ptr<CPDF_Array>(pArray); |
96 } | 125 } |
97 | 126 |
98 #endif // CORE_FPDFAPI_PARSER_CPDF_ARRAY_H_ | 127 #endif // CORE_FPDFAPI_PARSER_CPDF_ARRAY_H_ |
OLD | NEW |