• 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_OBJECT_H_
8 #define CORE_FPDFAPI_PARSER_CPDF_OBJECT_H_
9 
10 #include <memory>
11 #include <set>
12 #include <type_traits>
13 
14 #include "core/fxcrt/fx_string.h"
15 #include "core/fxcrt/fx_system.h"
16 
17 class CPDF_Array;
18 class CPDF_Boolean;
19 class CPDF_Dictionary;
20 class CPDF_Name;
21 class CPDF_Null;
22 class CPDF_Number;
23 class CPDF_Reference;
24 class CPDF_Stream;
25 class CPDF_String;
26 class IFX_ArchiveStream;
27 
28 class CPDF_Object {
29  public:
30   static const uint32_t kInvalidObjNum = static_cast<uint32_t>(-1);
31   enum Type {
32     BOOLEAN = 1,
33     NUMBER,
34     STRING,
35     NAME,
36     ARRAY,
37     DICTIONARY,
38     STREAM,
39     NULLOBJ,
40     REFERENCE
41   };
42 
43   virtual ~CPDF_Object();
44 
45   virtual Type GetType() const = 0;
GetObjNum()46   uint32_t GetObjNum() const { return m_ObjNum; }
SetObjNum(uint32_t objnum)47   void SetObjNum(uint32_t objnum) { m_ObjNum = objnum; }
GetGenNum()48   uint32_t GetGenNum() const { return m_GenNum; }
SetGenNum(uint32_t gennum)49   void SetGenNum(uint32_t gennum) { m_GenNum = gennum; }
IsInline()50   bool IsInline() const { return m_ObjNum == 0; }
51 
52   // Create a deep copy of the object.
53   virtual std::unique_ptr<CPDF_Object> Clone() const = 0;
54 
55   // Create a deep copy of the object except any reference object be
56   // copied to the object it points to directly.
57   virtual std::unique_ptr<CPDF_Object> CloneDirectObject() const;
58 
59   virtual CPDF_Object* GetDirect() const;
60   virtual ByteString GetString() const;
61   virtual WideString GetUnicodeText() const;
62   virtual float GetNumber() const;
63   virtual int GetInteger() const;
64   virtual CPDF_Dictionary* GetDict() const;
65 
66   virtual void SetString(const ByteString& str);
67 
68   virtual bool IsArray() const;
69   virtual bool IsBoolean() const;
70   virtual bool IsDictionary() const;
71   virtual bool IsName() const;
72   virtual bool IsNumber() const;
73   virtual bool IsReference() const;
74   virtual bool IsStream() const;
75   virtual bool IsString() const;
76   virtual bool IsNull() const;
77 
78   virtual CPDF_Array* AsArray();
79   virtual const CPDF_Array* AsArray() const;
80   virtual CPDF_Boolean* AsBoolean();
81   virtual const CPDF_Boolean* AsBoolean() const;
82   virtual CPDF_Dictionary* AsDictionary();
83   virtual const CPDF_Dictionary* AsDictionary() const;
84   virtual CPDF_Name* AsName();
85   virtual const CPDF_Name* AsName() const;
86   virtual CPDF_Number* AsNumber();
87   virtual const CPDF_Number* AsNumber() const;
88   virtual CPDF_Reference* AsReference();
89   virtual const CPDF_Reference* AsReference() const;
90   virtual CPDF_Stream* AsStream();
91   virtual const CPDF_Stream* AsStream() const;
92   virtual CPDF_String* AsString();
93   virtual const CPDF_String* AsString() const;
94 
95   virtual bool WriteTo(IFX_ArchiveStream* archive) const = 0;
96 
97   // Create a deep copy of the object with the option to either
98   // copy a reference object or directly copy the object it refers to
99   // when |bDirect| is true.
100   // Also check cyclic reference against |pVisited|, no copy if it is found.
101   // Complex objects should implement their own CloneNonCyclic()
102   // function to properly check for possible loop.
103   virtual std::unique_ptr<CPDF_Object> CloneNonCyclic(
104       bool bDirect,
105       std::set<const CPDF_Object*>* pVisited) const;
106 
107  protected:
CPDF_Object()108   CPDF_Object() : m_ObjNum(0), m_GenNum(0) {}
109 
110   std::unique_ptr<CPDF_Object> CloneObjectNonCyclic(bool bDirect) const;
111 
112   uint32_t m_ObjNum;
113 
114  private:
CPDF_Object(const CPDF_Object & src)115   CPDF_Object(const CPDF_Object& src) {}
116 
117   uint32_t m_GenNum;
118 };
119 
120 template <typename T>
121 struct CanInternStrings {
122   static const bool value = std::is_same<T, CPDF_Array>::value ||
123                             std::is_same<T, CPDF_Dictionary>::value ||
124                             std::is_same<T, CPDF_Name>::value ||
125                             std::is_same<T, CPDF_String>::value;
126 };
127 
128 #endif  // CORE_FPDFAPI_PARSER_CPDF_OBJECT_H_
129