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 <set> | 11 #include <set> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h" | 14 #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h" |
14 #include "core/fpdfapi/parser/cpdf_object.h" | 15 #include "core/fpdfapi/parser/cpdf_object.h" |
15 #include "core/fxcrt/fx_basic.h" | 16 #include "core/fxcrt/fx_basic.h" |
16 #include "core/fxcrt/fx_coordinates.h" | 17 #include "core/fxcrt/fx_coordinates.h" |
| 18 #include "third_party/base/ptr_util.h" |
17 | 19 |
18 class CPDF_Array : public CPDF_Object { | 20 class CPDF_Array : public CPDF_Object { |
19 public: | 21 public: |
20 using iterator = std::vector<CPDF_Object*>::iterator; | 22 using const_iterator = |
21 using const_iterator = std::vector<CPDF_Object*>::const_iterator; | 23 std::vector<std::unique_ptr<CPDF_Object>>::const_iterator; |
22 | 24 |
23 CPDF_Array(); | 25 CPDF_Array(); |
24 ~CPDF_Array() override; | 26 ~CPDF_Array() override; |
25 | 27 |
26 // CPDF_Object: | 28 // CPDF_Object: |
27 Type GetType() const override; | 29 Type GetType() const override; |
28 std::unique_ptr<CPDF_Object> Clone() const override; | 30 std::unique_ptr<CPDF_Object> Clone() const override; |
29 bool IsArray() const override; | 31 bool IsArray() const override; |
30 CPDF_Array* AsArray() override; | 32 CPDF_Array* AsArray() override; |
31 const CPDF_Array* AsArray() const override; | 33 const CPDF_Array* AsArray() const override; |
32 | 34 |
33 bool IsEmpty() const { return m_Objects.empty(); } | 35 bool IsEmpty() const { return m_Objects.empty(); } |
34 size_t GetCount() const { return m_Objects.size(); } | 36 size_t GetCount() const { return m_Objects.size(); } |
35 CPDF_Object* GetObjectAt(size_t index) const; | 37 CPDF_Object* GetObjectAt(size_t index) const; |
36 CPDF_Object* GetDirectObjectAt(size_t index) const; | 38 CPDF_Object* GetDirectObjectAt(size_t index) const; |
37 CFX_ByteString GetStringAt(size_t index) const; | 39 CFX_ByteString GetStringAt(size_t index) const; |
38 int GetIntegerAt(size_t index) const; | 40 int GetIntegerAt(size_t index) const; |
39 FX_FLOAT GetNumberAt(size_t index) const; | 41 FX_FLOAT GetNumberAt(size_t index) const; |
40 CPDF_Dictionary* GetDictAt(size_t index) const; | 42 CPDF_Dictionary* GetDictAt(size_t index) const; |
41 CPDF_Stream* GetStreamAt(size_t index) const; | 43 CPDF_Stream* GetStreamAt(size_t index) const; |
42 CPDF_Array* GetArrayAt(size_t index) const; | 44 CPDF_Array* GetArrayAt(size_t index) const; |
43 FX_FLOAT GetFloatAt(size_t index) const { return GetNumberAt(index); } | 45 FX_FLOAT GetFloatAt(size_t index) const { return GetNumberAt(index); } |
44 CFX_Matrix GetMatrix(); | 46 CFX_Matrix GetMatrix(); |
45 CFX_FloatRect GetRect(); | 47 CFX_FloatRect GetRect(); |
46 | 48 |
47 void SetAt(size_t index, CPDF_Object* pObj); | 49 // Takes ownership of |pObj|, returns unowned pointer to it. |
48 void InsertAt(size_t index, CPDF_Object* pObj); | 50 CPDF_Object* Add(std::unique_ptr<CPDF_Object> pObj); |
| 51 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); |
| 53 |
| 54 // Creates object owned by the array, returns unowned pointer to it. |
| 55 template <typename T, typename... Args> |
| 56 T* AddNew(Args... args) { |
| 57 return static_cast<T*>(Add(pdfium::MakeUnique<T>(args...))); |
| 58 } |
| 59 template <typename T, typename... Args> |
| 60 T* SetNewAt(size_t index, Args... args) { |
| 61 return static_cast<T*>(SetAt(index, pdfium::MakeUnique<T>(args...))); |
| 62 } |
| 63 template <typename T, typename... Args> |
| 64 T* InsertNewAt(size_t index, Args... args) { |
| 65 return static_cast<T*>(InsertAt(index, pdfium::MakeUnique<T>(args...))); |
| 66 } |
| 67 |
49 void RemoveAt(size_t index, size_t nCount = 1); | 68 void RemoveAt(size_t index, size_t nCount = 1); |
50 void ConvertToIndirectObjectAt(size_t index, CPDF_IndirectObjectHolder* pDoc); | 69 void ConvertToIndirectObjectAt(size_t index, CPDF_IndirectObjectHolder* pDoc); |
51 | 70 |
52 void Add(CPDF_Object* pObj); | |
53 void AddNumber(FX_FLOAT f); | |
54 void AddInteger(int i); | |
55 void AddString(const CFX_ByteString& str); | |
56 void AddName(const CFX_ByteString& str); | |
57 void AddReference(CPDF_IndirectObjectHolder* pDoc, uint32_t objnum); | |
58 void AddReference(CPDF_IndirectObjectHolder* pDoc, CPDF_Object* pObj); | |
59 | |
60 iterator begin() { return m_Objects.begin(); } | |
61 iterator end() { return m_Objects.end(); } | |
62 const_iterator begin() const { return m_Objects.begin(); } | 71 const_iterator begin() const { return m_Objects.begin(); } |
63 const_iterator end() const { return m_Objects.end(); } | 72 const_iterator end() const { return m_Objects.end(); } |
64 | 73 |
65 protected: | 74 protected: |
66 std::unique_ptr<CPDF_Object> CloneNonCyclic( | 75 std::unique_ptr<CPDF_Object> CloneNonCyclic( |
67 bool bDirect, | 76 bool bDirect, |
68 std::set<const CPDF_Object*>* pVisited) const override; | 77 std::set<const CPDF_Object*>* pVisited) const override; |
69 | 78 |
70 std::vector<CPDF_Object*> m_Objects; | 79 std::vector<std::unique_ptr<CPDF_Object>> m_Objects; |
71 }; | 80 }; |
72 | 81 |
73 inline CPDF_Array* ToArray(CPDF_Object* obj) { | 82 inline CPDF_Array* ToArray(CPDF_Object* obj) { |
74 return obj ? obj->AsArray() : nullptr; | 83 return obj ? obj->AsArray() : nullptr; |
75 } | 84 } |
76 | 85 |
77 inline const CPDF_Array* ToArray(const CPDF_Object* obj) { | 86 inline const CPDF_Array* ToArray(const CPDF_Object* obj) { |
78 return obj ? obj->AsArray() : nullptr; | 87 return obj ? obj->AsArray() : nullptr; |
79 } | 88 } |
80 | 89 |
81 inline std::unique_ptr<CPDF_Array> ToArray(std::unique_ptr<CPDF_Object> obj) { | 90 inline std::unique_ptr<CPDF_Array> ToArray(std::unique_ptr<CPDF_Object> obj) { |
82 CPDF_Array* pArray = ToArray(obj.get()); | 91 CPDF_Array* pArray = ToArray(obj.get()); |
83 if (!pArray) | 92 if (!pArray) |
84 return nullptr; | 93 return nullptr; |
85 obj.release(); | 94 obj.release(); |
86 return std::unique_ptr<CPDF_Array>(pArray); | 95 return std::unique_ptr<CPDF_Array>(pArray); |
87 } | 96 } |
88 | 97 |
89 #endif // CORE_FPDFAPI_PARSER_CPDF_ARRAY_H_ | 98 #endif // CORE_FPDFAPI_PARSER_CPDF_ARRAY_H_ |
OLD | NEW |