cpdf_dictionary.h revision d904c1ec7e8d1d86ed56f0dd252435d12cd345ae
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_DICTIONARY_H_ 8#define CORE_FPDFAPI_PARSER_CPDF_DICTIONARY_H_ 9 10#include <map> 11#include <memory> 12#include <set> 13#include <utility> 14 15#include "core/fpdfapi/parser/cpdf_object.h" 16#include "core/fxcrt/fx_coordinates.h" 17#include "core/fxcrt/fx_string.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 22class CPDF_IndirectObjectHolder; 23 24class CPDF_Dictionary : public CPDF_Object { 25 public: 26 using const_iterator = 27 std::map<ByteString, std::unique_ptr<CPDF_Object>>::const_iterator; 28 29 CPDF_Dictionary(); 30 explicit CPDF_Dictionary(const WeakPtr<ByteStringPool>& pPool); 31 ~CPDF_Dictionary() override; 32 33 // CPDF_Object: 34 Type GetType() const override; 35 std::unique_ptr<CPDF_Object> Clone() const override; 36 CPDF_Dictionary* GetDict() const override; 37 bool IsDictionary() const override; 38 CPDF_Dictionary* AsDictionary() override; 39 const CPDF_Dictionary* AsDictionary() const override; 40 bool WriteTo(IFX_ArchiveStream* archive) const override; 41 42 size_t GetCount() const { return m_Map.size(); } 43 CPDF_Object* GetObjectFor(const ByteString& key) const; 44 CPDF_Object* GetDirectObjectFor(const ByteString& key) const; 45 ByteString GetStringFor(const ByteString& key) const; 46 ByteString GetStringFor(const ByteString& key, 47 const ByteString& default_str) const; 48 WideString GetUnicodeTextFor(const ByteString& key) const; 49 int GetIntegerFor(const ByteString& key) const; 50 int GetIntegerFor(const ByteString& key, int default_int) const; 51 bool GetBooleanFor(const ByteString& key, bool bDefault = false) const; 52 float GetNumberFor(const ByteString& key) const; 53 CPDF_Dictionary* GetDictFor(const ByteString& key) const; 54 CPDF_Stream* GetStreamFor(const ByteString& key) const; 55 CPDF_Array* GetArrayFor(const ByteString& key) const; 56 CFX_FloatRect GetRectFor(const ByteString& key) const; 57 CFX_Matrix GetMatrixFor(const ByteString& key) const; 58 float GetFloatFor(const ByteString& key) const { return GetNumberFor(key); } 59 60 bool KeyExist(const ByteString& key) const; 61 bool IsSignatureDict() const; 62 63 // Set* functions invalidate iterators for the element with the key |key|. 64 // Takes ownership of |pObj|, returns an unowned pointer to it. 65 CPDF_Object* SetFor(const ByteString& key, std::unique_ptr<CPDF_Object> pObj); 66 67 // Creates a new object owned by the dictionary and returns an unowned 68 // pointer to it. 69 template <typename T, typename... Args> 70 typename std::enable_if<!CanInternStrings<T>::value, T*>::type SetNewFor( 71 const ByteString& key, 72 Args&&... args) { 73 return static_cast<T*>( 74 SetFor(key, pdfium::MakeUnique<T>(std::forward<Args>(args)...))); 75 } 76 template <typename T, typename... Args> 77 typename std::enable_if<CanInternStrings<T>::value, T*>::type SetNewFor( 78 const ByteString& key, 79 Args&&... args) { 80 return static_cast<T*>(SetFor( 81 key, pdfium::MakeUnique<T>(m_pPool, std::forward<Args>(args)...))); 82 } 83 84 // Convenience functions to convert native objects to array form. 85 void SetRectFor(const ByteString& key, const CFX_FloatRect& rect); 86 void SetMatrixFor(const ByteString& key, const CFX_Matrix& matrix); 87 88 void ConvertToIndirectObjectFor(const ByteString& key, 89 CPDF_IndirectObjectHolder* pHolder); 90 91 // Invalidates iterators for the element with the key |key|. 92 std::unique_ptr<CPDF_Object> RemoveFor(const ByteString& key); 93 94 // Invalidates iterators for the element with the key |oldkey|. 95 void ReplaceKey(const ByteString& oldkey, const ByteString& newkey); 96 97 const_iterator begin() const { return m_Map.begin(); } 98 const_iterator end() const { return m_Map.end(); } 99 100 WeakPtr<ByteStringPool> GetByteStringPool() const { return m_pPool; } 101 102 protected: 103 ByteString MaybeIntern(const ByteString& str); 104 std::unique_ptr<CPDF_Object> CloneNonCyclic( 105 bool bDirect, 106 std::set<const CPDF_Object*>* visited) const override; 107 108 WeakPtr<ByteStringPool> m_pPool; 109 std::map<ByteString, std::unique_ptr<CPDF_Object>> m_Map; 110}; 111 112inline CPDF_Dictionary* ToDictionary(CPDF_Object* obj) { 113 return obj ? obj->AsDictionary() : nullptr; 114} 115 116inline const CPDF_Dictionary* ToDictionary(const CPDF_Object* obj) { 117 return obj ? obj->AsDictionary() : nullptr; 118} 119 120inline std::unique_ptr<CPDF_Dictionary> ToDictionary( 121 std::unique_ptr<CPDF_Object> obj) { 122 CPDF_Dictionary* pDict = ToDictionary(obj.get()); 123 if (!pDict) 124 return nullptr; 125 obj.release(); 126 return std::unique_ptr<CPDF_Dictionary>(pDict); 127} 128 129#endif // CORE_FPDFAPI_PARSER_CPDF_DICTIONARY_H_ 130