PoDoFo  1.0.0-dev
PdfIndirectObjectList.h
1 
7 #ifndef PDF_INDIRECT_OBJECT_LIST_H
8 #define PDF_INDIRECT_OBJECT_LIST_H
9 
10 #include "PdfObject.h"
11 
12 namespace PoDoFo {
13 
14 class PdfObjectStreamProvider;
15 using PdfFreeObjectList = std::deque<PdfReference>;
16 
29 class PODOFO_API PdfIndirectObjectList final
30 {
31  friend class PdfDocument;
32  friend class PdfObject;
33  friend class PdfObjectOutputStream;
34  PODOFO_PRIVATE_FRIEND(class PdfObjectStreamParser);
35  PODOFO_PRIVATE_FRIEND(class PdfImmediateWriter);
36  PODOFO_PRIVATE_FRIEND(class PdfParser);
37  PODOFO_PRIVATE_FRIEND(class PdfWriter);
38  PODOFO_PRIVATE_FRIEND(class PdfParserTest);
39  PODOFO_PRIVATE_FRIEND(class PdfEncodingTest);
40  PODOFO_PRIVATE_FRIEND(class PdfEncryptTest);
41 
42 private:
43  // NOTE: For testing only
45 
46 public:
48 
49 public:
57  PdfObject& MustGetObject(const PdfReference& ref) const;
58 
64  PdfObject* GetObject(const PdfReference& ref) const;
65 
73  PdfObject& CreateDictionaryObject(const PdfName& type = PdfName::Null,
74  const PdfName& subtype = PdfName::Null);
75 
76  PdfObject& CreateArrayObject();
77 
84  PdfObject& CreateObject(const PdfObject& obj);
85  PdfObject& CreateObject(PdfObject&& obj);
86 
92  void CollectGarbage();
93 
94 public:
99  unsigned GetSize() const;
100 
108  inline unsigned GetObjectCount() const { return m_ObjectCount; }
109 
112  inline const PdfFreeObjectList& GetFreeObjects() const { return m_FreeObjects; }
113 
116  inline PdfDocument& GetDocument() const { return *m_Document; }
117 
118 private:
121  class PODOFO_API Observer
122  {
123  friend class PdfIndirectObjectList;
124  public:
125  virtual ~Observer() { }
126 
130  virtual void BeginAppendStream(PdfObjectStream& stream) = 0;
131 
135  virtual void EndAppendStream(PdfObjectStream& stream) = 0;
136  };
137 
140  class PODOFO_API StreamFactory
141  {
142  public:
143  virtual ~StreamFactory() { }
144 
151  virtual std::unique_ptr<PdfObjectStreamProvider> CreateStream() = 0;
152  };
153 
154  using ObjectNumSet = std::set<uint32_t>;
155  using ReferenceSet = std::set<PdfReference>;
156  using ObserverList = std::vector<Observer*>;
157  using ObjectList = std::set<PdfObject*, PdfObjectInequality>;
158 
159 public:
160 
161  // An incomplete set of container typedefs, just enough to handle
162  // the begin() and end() methods we wrap from the internal vector.
163  // TODO: proper wrapper iterator class.
164  using iterator = ObjectList::const_iterator;
165  using reverse_iterator = ObjectList::const_reverse_iterator;
166 
170  iterator begin() const;
171 
175  iterator end() const;
176 
177  reverse_iterator rbegin() const;
178 
179  reverse_iterator rend() const;
180 
181  size_t size() const;
182 
183 private:
184  PdfIndirectObjectList(PdfDocument& document);
185  PdfIndirectObjectList(PdfDocument& document, const PdfIndirectObjectList& rhs);
186 
187  PdfIndirectObjectList(const PdfIndirectObjectList&) = delete;
188  PdfIndirectObjectList& operator=(const PdfIndirectObjectList&) = delete;
189 
190 private:
198  std::unique_ptr<PdfObjectStreamProvider> CreateStream();
199 
211  std::unique_ptr<PdfObject> RemoveObject(const PdfReference& ref);
212 
217  std::unique_ptr<PdfObject> RemoveObject(const iterator& it);
218 
228  void Clear();
229 
233  void AttachObserver(Observer& observer);
234 
239  void DetachObserver(Observer& observer);
240 
244  void BeginAppendStream(PdfObjectStream& stream);
245 
249  void EndAppendStream(PdfObjectStream& stream);
250 
258  void PushObject(PdfObject* obj);
259 
269  bool TryAddFreeObject(const PdfReference& reference);
270 
281  int32_t SafeAddFreeObject(const PdfReference& reference);
282 
288  void AddFreeObject(const PdfReference& reference);
289 
294  void AddObjectStream(uint32_t objectNum);
295 
296  std::unique_ptr<PdfObject> RemoveObject(const PdfReference& ref, bool markAsFree);
297 
302  void SetStreamFactory(StreamFactory* factory);
303 
304 private:
305  void pushObject(const ObjectList::const_iterator& hintpos, ObjectList::node_type& node, PdfObject* obj);
306 
307  std::unique_ptr<PdfObject> removeObject(const iterator& it, bool markAsFree);
308 
309  void addNewObject(PdfObject* obj);
310 
314  PdfReference getNextFreeObject();
315 
316  int32_t tryAddFreeObject(uint32_t objnum, uint32_t gennum);
317 
318  void visitObject(const PdfObject& obj, std::unordered_set<PdfReference>& referencedObj);
319 
326  void tryIncrementObjectCount(const PdfReference& ref);
327 
328 private:
329  PdfDocument* m_Document;
330  ObjectList m_Objects;
331  unsigned m_ObjectCount;
332  PdfFreeObjectList m_FreeObjects;
333  ObjectNumSet m_unavailableObjects;
334  ObjectNumSet m_objectStreams;
335 
336  ObserverList m_observers;
337  StreamFactory* m_StreamFactory;
338 };
339 
340 };
341 
342 #endif // PDF_INDIRECT_OBJECT_LIST_H
PdfDocument is the core interface for working with PDF documents.
Definition: PdfDocument.h:108
A list of PdfObjects that constitutes the indirect object list of the document The PdfParser will rea...
Definition: PdfIndirectObjectList.h:30
const PdfFreeObjectList & GetFreeObjects() const
Definition: PdfIndirectObjectList.h:112
PdfDocument & GetDocument() const
Definition: PdfIndirectObjectList.h:116
unsigned GetObjectCount() const
Definition: PdfIndirectObjectList.h:108
This class represents a PdfName.
Definition: PdfName.h:24
static const PdfName Null
Null name, corresponds to "/".
Definition: PdfName.h:30
This class represents a PDF indirect Object in memory.
Definition: PdfObject.h:35
A reference is a pointer to a object in the PDF file of the form "4 0 R", where 4 is the object numbe...
Definition: PdfReference.h:24
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition: basetypes.h:16