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_INDIRECT_OBJECT_HOLDER_H_ | 7 #ifndef CORE_FPDFAPI_PARSER_CPDF_INDIRECT_OBJECT_HOLDER_H_ |
8 #define CORE_FPDFAPI_PARSER_CPDF_INDIRECT_OBJECT_HOLDER_H_ | 8 #define CORE_FPDFAPI_PARSER_CPDF_INDIRECT_OBJECT_HOLDER_H_ |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <type_traits> | |
12 | 13 |
13 #include "core/fxcrt/cfx_string_pool_template.h" | 14 #include "core/fxcrt/cfx_string_pool_template.h" |
14 #include "core/fxcrt/cfx_weak_ptr.h" | 15 #include "core/fxcrt/cfx_weak_ptr.h" |
15 #include "core/fxcrt/fx_system.h" | 16 #include "core/fxcrt/fx_system.h" |
16 #include "third_party/base/ptr_util.h" | 17 #include "third_party/base/ptr_util.h" |
17 | 18 |
18 class CPDF_Object; | 19 class CPDF_Object; |
20 class CPDF_Name; | |
19 | 21 |
20 class CPDF_IndirectObjectHolder { | 22 class CPDF_IndirectObjectHolder { |
21 public: | 23 public: |
22 using const_iterator = | 24 using const_iterator = |
23 std::map<uint32_t, std::unique_ptr<CPDF_Object>>::const_iterator; | 25 std::map<uint32_t, std::unique_ptr<CPDF_Object>>::const_iterator; |
24 | 26 |
25 CPDF_IndirectObjectHolder(); | 27 CPDF_IndirectObjectHolder(); |
26 virtual ~CPDF_IndirectObjectHolder(); | 28 virtual ~CPDF_IndirectObjectHolder(); |
27 | 29 |
28 CPDF_Object* GetIndirectObject(uint32_t objnum) const; | 30 CPDF_Object* GetIndirectObject(uint32_t objnum) const; |
29 CPDF_Object* GetOrParseIndirectObject(uint32_t objnum); | 31 CPDF_Object* GetOrParseIndirectObject(uint32_t objnum); |
30 void DeleteIndirectObject(uint32_t objnum); | 32 void DeleteIndirectObject(uint32_t objnum); |
31 | 33 |
32 // Creates and adds a new object owned by the indirect object holder, | 34 // Creates and adds a new object owned by the indirect object holder, |
33 // and returns an unowned pointer to it. | 35 // and returns an unowned pointer to it. We have a special case to |
36 // handle objects that can intern strings from our ByteStringPool. | |
34 template <typename T, typename... Args> | 37 template <typename T, typename... Args> |
35 T* NewIndirect(Args... args) { | 38 typename std::enable_if<!std::is_same<T, CPDF_Name>::value, T*>::type |
39 NewIndirect(Args... args) { | |
36 return static_cast<T*>(AddIndirectObject(pdfium::MakeUnique<T>(args...))); | 40 return static_cast<T*>(AddIndirectObject(pdfium::MakeUnique<T>(args...))); |
37 } | 41 } |
38 | 42 |
43 template <typename T, typename... Args> | |
44 typename std::enable_if<std::is_same<T, CPDF_Name>::value, T*>::type | |
45 NewIndirect(Args... args) { | |
46 return static_cast<T*>( | |
47 AddIndirectObject(pdfium::MakeUnique<T>(m_pByteStringPool, args...))); | |
Tom Sepez
2016/11/16 22:36:36
Here, the indirect object holder injects its own b
| |
48 } | |
49 | |
39 // Takes ownership of |pObj|, returns unowned pointer to it. | 50 // Takes ownership of |pObj|, returns unowned pointer to it. |
40 CPDF_Object* AddIndirectObject(std::unique_ptr<CPDF_Object> pObj); | 51 CPDF_Object* AddIndirectObject(std::unique_ptr<CPDF_Object> pObj); |
41 | 52 |
42 // Always takes ownership of |pObj|, return true if higher generation number. | 53 // Always takes ownership of |pObj|, return true if higher generation number. |
43 bool ReplaceIndirectObjectIfHigherGeneration( | 54 bool ReplaceIndirectObjectIfHigherGeneration( |
44 uint32_t objnum, | 55 uint32_t objnum, |
45 std::unique_ptr<CPDF_Object> pObj); | 56 std::unique_ptr<CPDF_Object> pObj); |
46 | 57 |
47 uint32_t GetLastObjNum() const { return m_LastObjNum; } | 58 uint32_t GetLastObjNum() const { return m_LastObjNum; } |
48 void SetLastObjNum(uint32_t objnum) { m_LastObjNum = objnum; } | 59 void SetLastObjNum(uint32_t objnum) { m_LastObjNum = objnum; } |
49 | 60 |
50 CFX_WeakPtr<CFX_ByteStringPool> GetByteStringPool() const { | 61 CFX_WeakPtr<CFX_ByteStringPool> GetByteStringPool() const { |
51 return m_pByteStringPool; | 62 return m_pByteStringPool; |
52 } | 63 } |
53 | 64 |
54 const_iterator begin() const { return m_IndirectObjs.begin(); } | 65 const_iterator begin() const { return m_IndirectObjs.begin(); } |
55 const_iterator end() const { return m_IndirectObjs.end(); } | 66 const_iterator end() const { return m_IndirectObjs.end(); } |
56 | 67 |
57 protected: | 68 protected: |
58 virtual std::unique_ptr<CPDF_Object> ParseIndirectObject(uint32_t objnum); | 69 virtual std::unique_ptr<CPDF_Object> ParseIndirectObject(uint32_t objnum); |
59 | 70 |
60 private: | 71 private: |
61 uint32_t m_LastObjNum; | 72 uint32_t m_LastObjNum; |
62 std::map<uint32_t, std::unique_ptr<CPDF_Object>> m_IndirectObjs; | 73 std::map<uint32_t, std::unique_ptr<CPDF_Object>> m_IndirectObjs; |
63 CFX_WeakPtr<CFX_ByteStringPool> m_pByteStringPool; | 74 CFX_WeakPtr<CFX_ByteStringPool> m_pByteStringPool; |
64 }; | 75 }; |
65 | 76 |
66 #endif // CORE_FPDFAPI_PARSER_CPDF_INDIRECT_OBJECT_HOLDER_H_ | 77 #endif // CORE_FPDFAPI_PARSER_CPDF_INDIRECT_OBJECT_HOLDER_H_ |
OLD | NEW |