• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef CORE_FPDFAPI_PARSER_CPDF_INDIRECT_OBJECT_HOLDER_H_
8 #define CORE_FPDFAPI_PARSER_CPDF_INDIRECT_OBJECT_HOLDER_H_
9 
10 #include <map>
11 #include <memory>
12 #include <type_traits>
13 #include <utility>
14 #include <vector>
15 
16 #include "core/fpdfapi/parser/cpdf_object.h"
17 #include "core/fxcrt/fx_system.h"
18 #include "core/fxcrt/string_pool_template.h"
19 #include "core/fxcrt/weak_ptr.h"
20 #include "third_party/base/ptr_util.h"
21 
22 class CPDF_IndirectObjectHolder {
23  public:
24   using const_iterator =
25       std::map<uint32_t, RetainPtr<CPDF_Object>>::const_iterator;
26 
27   CPDF_IndirectObjectHolder();
28   virtual ~CPDF_IndirectObjectHolder();
29 
30   CPDF_Object* GetIndirectObject(uint32_t objnum) const;
31   virtual CPDF_Object* GetOrParseIndirectObject(uint32_t objnum);
32   void DeleteIndirectObject(uint32_t objnum);
33 
34   // Creates and adds a new object owned by the indirect object holder,
35   // and returns an unowned pointer to it.  We have a special case to
36   // handle objects that can intern strings from our ByteStringPool.
37   template <typename T, typename... Args>
NewIndirect(Args &&...args)38   typename std::enable_if<!CanInternStrings<T>::value, T*>::type NewIndirect(
39       Args&&... args) {
40     return static_cast<T*>(
41         AddIndirectObject(pdfium::MakeRetain<T>(std::forward<Args>(args)...)));
42   }
43   template <typename T, typename... Args>
NewIndirect(Args &&...args)44   typename std::enable_if<CanInternStrings<T>::value, T*>::type NewIndirect(
45       Args&&... args) {
46     return static_cast<T*>(AddIndirectObject(
47         pdfium::MakeRetain<T>(m_pByteStringPool, std::forward<Args>(args)...)));
48   }
49 
50   // Creates and adds a new object not owned by the indirect object holder,
51   // but which can intern strings from it.
52   template <typename T, typename... Args>
New(Args &&...args)53   typename std::enable_if<CanInternStrings<T>::value, RetainPtr<T>>::type New(
54       Args&&... args) {
55     return pdfium::MakeRetain<T>(m_pByteStringPool,
56                                  std::forward<Args>(args)...);
57   }
58 
59   // Takes ownership of |pObj|, returns unowned pointer to it.
60   CPDF_Object* AddIndirectObject(RetainPtr<CPDF_Object> pObj);
61 
62   // Always takes ownership of |pObj|, return true if higher generation number.
63   bool ReplaceIndirectObjectIfHigherGeneration(uint32_t objnum,
64                                                RetainPtr<CPDF_Object> pObj);
65 
GetLastObjNum()66   uint32_t GetLastObjNum() const { return m_LastObjNum; }
SetLastObjNum(uint32_t objnum)67   void SetLastObjNum(uint32_t objnum) { m_LastObjNum = objnum; }
68 
GetByteStringPool()69   WeakPtr<ByteStringPool> GetByteStringPool() const {
70     return m_pByteStringPool;
71   }
72 
begin()73   const_iterator begin() const { return m_IndirectObjs.begin(); }
end()74   const_iterator end() const { return m_IndirectObjs.end(); }
75 
76  protected:
77   virtual RetainPtr<CPDF_Object> ParseIndirectObject(uint32_t objnum);
78 
79  private:
80   uint32_t m_LastObjNum;
81   std::map<uint32_t, RetainPtr<CPDF_Object>> m_IndirectObjs;
82   WeakPtr<ByteStringPool> m_pByteStringPool;
83 };
84 
85 #endif  // CORE_FPDFAPI_PARSER_CPDF_INDIRECT_OBJECT_HOLDER_H_
86