Home | History | Annotate | Download | only in parser
      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 
     27 class CPDF_Object {
     28  public:
     29   static const uint32_t kInvalidObjNum = static_cast<uint32_t>(-1);
     30   enum Type {
     31     BOOLEAN = 1,
     32     NUMBER,
     33     STRING,
     34     NAME,
     35     ARRAY,
     36     DICTIONARY,
     37     STREAM,
     38     NULLOBJ,
     39     REFERENCE
     40   };
     41 
     42   virtual ~CPDF_Object();
     43 
     44   virtual Type GetType() const = 0;
     45   uint32_t GetObjNum() const { return m_ObjNum; }
     46   uint32_t GetGenNum() const { return m_GenNum; }
     47   bool IsInline() const { return m_ObjNum == 0; }
     48 
     49   // Create a deep copy of the object.
     50   virtual std::unique_ptr<CPDF_Object> Clone() const = 0;
     51 
     52   // Create a deep copy of the object except any reference object be
     53   // copied to the object it points to directly.
     54   virtual std::unique_ptr<CPDF_Object> CloneDirectObject() const;
     55 
     56   virtual CPDF_Object* GetDirect() const;
     57   virtual CFX_ByteString GetString() const;
     58   virtual CFX_WideString GetUnicodeText() const;
     59   virtual FX_FLOAT GetNumber() const;
     60   virtual int GetInteger() const;
     61   virtual CPDF_Dictionary* GetDict() const;
     62 
     63   virtual void SetString(const CFX_ByteString& str);
     64 
     65   virtual bool IsArray() const;
     66   virtual bool IsBoolean() const;
     67   virtual bool IsDictionary() const;
     68   virtual bool IsName() const;
     69   virtual bool IsNumber() const;
     70   virtual bool IsReference() const;
     71   virtual bool IsStream() const;
     72   virtual bool IsString() const;
     73 
     74   virtual CPDF_Array* AsArray();
     75   virtual const CPDF_Array* AsArray() const;
     76   virtual CPDF_Boolean* AsBoolean();
     77   virtual const CPDF_Boolean* AsBoolean() const;
     78   virtual CPDF_Dictionary* AsDictionary();
     79   virtual const CPDF_Dictionary* AsDictionary() const;
     80   virtual CPDF_Name* AsName();
     81   virtual const CPDF_Name* AsName() const;
     82   virtual CPDF_Number* AsNumber();
     83   virtual const CPDF_Number* AsNumber() const;
     84   virtual CPDF_Reference* AsReference();
     85   virtual const CPDF_Reference* AsReference() const;
     86   virtual CPDF_Stream* AsStream();
     87   virtual const CPDF_Stream* AsStream() const;
     88   virtual CPDF_String* AsString();
     89   virtual const CPDF_String* AsString() const;
     90 
     91  protected:
     92   friend class CPDF_Array;
     93   friend class CPDF_Dictionary;
     94   friend class CPDF_IndirectObjectHolder;
     95   friend class CPDF_Parser;
     96   friend class CPDF_Reference;
     97   friend class CPDF_Stream;
     98 
     99   CPDF_Object() : m_ObjNum(0), m_GenNum(0) {}
    100 
    101   std::unique_ptr<CPDF_Object> CloneObjectNonCyclic(bool bDirect) const;
    102 
    103   // Create a deep copy of the object with the option to either
    104   // copy a reference object or directly copy the object it refers to
    105   // when |bDirect| is true.
    106   // Also check cyclic reference against |pVisited|, no copy if it is found.
    107   // Complex objects should implement their own CloneNonCyclic()
    108   // function to properly check for possible loop.
    109   virtual std::unique_ptr<CPDF_Object> CloneNonCyclic(
    110       bool bDirect,
    111       std::set<const CPDF_Object*>* pVisited) const;
    112 
    113   uint32_t m_ObjNum;
    114   uint32_t m_GenNum;
    115 
    116  private:
    117   CPDF_Object(const CPDF_Object& src) {}
    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