From 0e606b5ecd6e45f74391f110cc1fe0cce0e80c64 Mon Sep 17 00:00:00 2001 From: tsepez Date: Fri, 18 Nov 2016 16:22:41 -0800 Subject: Make CPDF_Dictionary use unique pointers. Some changes were required to match underlying ctors as invoked by the templated methods. Many release() calls go away, a few WrapUniques() are introduced to avoid going deeper into other code. Review-Url: https://codereview.chromium.org/2510223002 --- core/fpdfapi/parser/cpdf_dictionary.h | 41 ++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'core/fpdfapi/parser/cpdf_dictionary.h') diff --git a/core/fpdfapi/parser/cpdf_dictionary.h b/core/fpdfapi/parser/cpdf_dictionary.h index ebfd7e92ee..5d333808b9 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.h +++ b/core/fpdfapi/parser/cpdf_dictionary.h @@ -8,6 +8,7 @@ #define CORE_FPDFAPI_PARSER_CPDF_DICTIONARY_H_ #include +#include #include #include "core/fpdfapi/parser/cpdf_object.h" @@ -21,8 +22,8 @@ class CPDF_IndirectObjectHolder; class CPDF_Dictionary : public CPDF_Object { public: - using iterator = std::map::iterator; - using const_iterator = std::map::const_iterator; + using const_iterator = + std::map>::const_iterator; CPDF_Dictionary(); explicit CPDF_Dictionary(const CFX_WeakPtr& pPool); @@ -60,19 +61,27 @@ class CPDF_Dictionary : public CPDF_Object { bool IsSignatureDict() const; // Set* functions invalidate iterators for the element with the key |key|. - void SetFor(const CFX_ByteString& key, CPDF_Object* pObj); - void SetBooleanFor(const CFX_ByteString& key, bool bValue); - void SetNameFor(const CFX_ByteString& key, const CFX_ByteString& name); - void SetStringFor(const CFX_ByteString& key, const CFX_ByteString& str); - void SetIntegerFor(const CFX_ByteString& key, int i); - void SetNumberFor(const CFX_ByteString& key, FX_FLOAT f); - void SetReferenceFor(const CFX_ByteString& key, - CPDF_IndirectObjectHolder* pDoc, - uint32_t objnum); - void SetReferenceFor(const CFX_ByteString& key, - CPDF_IndirectObjectHolder* pDoc, - CPDF_Object* pObj); + // Takes ownership of |pObj|, returns an unowned pointer to it. + CPDF_Object* SetFor(const CFX_ByteString& key, + std::unique_ptr pObj); + + // Creates a new object owned by the dictionary and returns an unowned + // pointer to it. + template + typename std::enable_if::value, T*>::type SetNewFor( + const CFX_ByteString& key, + Args... args) { + return static_cast(SetFor(key, pdfium::MakeUnique(args...))); + } + template + typename std::enable_if::value, T*>::type SetNewFor( + const CFX_ByteString& key, + Args... args) { + return static_cast( + SetFor(key, pdfium::MakeUnique(m_pPool, args...))); + } + // Convenience functions to convert native objects to array form. void SetRectFor(const CFX_ByteString& key, const CFX_FloatRect& rect); void SetMatrixFor(const CFX_ByteString& key, const CFX_Matrix& matrix); @@ -85,8 +94,6 @@ class CPDF_Dictionary : public CPDF_Object { // Invalidates iterators for the element with the key |oldkey|. void ReplaceKey(const CFX_ByteString& oldkey, const CFX_ByteString& newkey); - iterator begin() { return m_Map.begin(); } - iterator end() { return m_Map.end(); } const_iterator begin() const { return m_Map.begin(); } const_iterator end() const { return m_Map.end(); } @@ -99,7 +106,7 @@ class CPDF_Dictionary : public CPDF_Object { std::set* visited) const override; CFX_WeakPtr m_pPool; - std::map m_Map; + std::map> m_Map; }; inline CPDF_Dictionary* ToDictionary(CPDF_Object* obj) { -- cgit v1.2.3