From b872a93e5cd940dc28ad960b13b0cf01a6db3400 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 21 Sep 2017 17:05:15 -0400 Subject: Move CFX_WeakPtr to WeakPtr This CL renames CFX_WeakPtr to WeakPtr and moves into the fxcrt namespace. Bug: pdfium:898 Change-Id: Ide50a8afeb1e987c48c8fbd103898745c9199d6a Reviewed-on: https://pdfium-review.googlesource.com/14621 Commit-Queue: dsinclair Commit-Queue: Tom Sepez Reviewed-by: Tom Sepez --- BUILD.gn | 4 +- core/fpdfapi/font/cpdf_fontencoding.cpp | 2 +- core/fpdfapi/font/cpdf_fontencoding.h | 4 +- core/fpdfapi/page/cpdf_streamparser.cpp | 2 +- core/fpdfapi/page/cpdf_streamparser.h | 6 +- core/fpdfapi/parser/cpdf_array.cpp | 3 +- core/fpdfapi/parser/cpdf_array.h | 4 +- core/fpdfapi/parser/cpdf_dictionary.cpp | 4 +- core/fpdfapi/parser/cpdf_dictionary.h | 8 +- core/fpdfapi/parser/cpdf_indirect_object_holder.h | 6 +- core/fpdfapi/parser/cpdf_name.cpp | 2 +- core/fpdfapi/parser/cpdf_name.h | 4 +- core/fpdfapi/parser/cpdf_string.cpp | 5 +- core/fpdfapi/parser/cpdf_string.h | 8 +- core/fpdfapi/parser/cpdf_syntax_parser.cpp | 4 +- core/fpdfapi/parser/cpdf_syntax_parser.h | 6 +- core/fpdfdoc/cpdf_filespec.h | 2 +- core/fxcrt/cfx_weak_ptr.h | 86 ----------- core/fxcrt/cfx_weak_ptr_unittest.cpp | 173 --------------------- core/fxcrt/weak_ptr.h | 92 +++++++++++ core/fxcrt/weak_ptr_unittest.cpp | 176 ++++++++++++++++++++++ 21 files changed, 303 insertions(+), 298 deletions(-) delete mode 100644 core/fxcrt/cfx_weak_ptr.h delete mode 100644 core/fxcrt/cfx_weak_ptr_unittest.cpp create mode 100644 core/fxcrt/weak_ptr.h create mode 100644 core/fxcrt/weak_ptr_unittest.cpp diff --git a/BUILD.gn b/BUILD.gn index 7526fa0dac..3a95895f25 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -823,7 +823,6 @@ static_library("fxcrt") { "core/fxcrt/cfx_memorystream.h", "core/fxcrt/cfx_utf8decoder.cpp", "core/fxcrt/cfx_utf8decoder.h", - "core/fxcrt/cfx_weak_ptr.h", "core/fxcrt/cfx_widetextbuf.cpp", "core/fxcrt/cfx_widetextbuf.h", "core/fxcrt/fx_bidi.cpp", @@ -858,6 +857,7 @@ static_library("fxcrt") { "core/fxcrt/string_pool_template.h", "core/fxcrt/string_view_template.h", "core/fxcrt/unowned_ptr.h", + "core/fxcrt/weak_ptr.h", "core/fxcrt/widestring.cpp", "core/fxcrt/widestring.h", "core/fxcrt/xml/cxml_attritem.cpp", @@ -1920,7 +1920,6 @@ test("pdfium_unittests") { "core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp", "core/fxcodec/jbig2/JBig2_Image_unittest.cpp", "core/fxcrt/bytestring_unittest.cpp", - "core/fxcrt/cfx_weak_ptr_unittest.cpp", "core/fxcrt/fx_bidi_unittest.cpp", "core/fxcrt/fx_coordinates_unittest.cpp", "core/fxcrt/fx_extension_unittest.cpp", @@ -1934,6 +1933,7 @@ test("pdfium_unittests") { "core/fxcrt/shared_copy_on_write_unittest.cpp", "core/fxcrt/string_pool_template_unittest.cpp", "core/fxcrt/unowned_ptr_unittest.cpp", + "core/fxcrt/weak_ptr_unittest.cpp", "core/fxcrt/widestring_unittest.cpp", "core/fxge/dib/cstretchengine_unittest.cpp", "fpdfsdk/fpdfdoc_unittest.cpp", diff --git a/core/fpdfapi/font/cpdf_fontencoding.cpp b/core/fpdfapi/font/cpdf_fontencoding.cpp index 6290f20f5e..921d5842b8 100644 --- a/core/fpdfapi/font/cpdf_fontencoding.cpp +++ b/core/fpdfapi/font/cpdf_fontencoding.cpp @@ -1672,7 +1672,7 @@ bool CPDF_FontEncoding::IsIdentical(CPDF_FontEncoding* pAnother) const { } std::unique_ptr CPDF_FontEncoding::Realize( - CFX_WeakPtr pPool) { + WeakPtr pPool) { int predefined = 0; for (int cs = PDFFONT_ENCODING_WINANSI; cs < PDFFONT_ENCODING_ZAPFDINGBATS; cs++) { diff --git a/core/fpdfapi/font/cpdf_fontencoding.h b/core/fpdfapi/font/cpdf_fontencoding.h index d96f787da1..cb99975b98 100644 --- a/core/fpdfapi/font/cpdf_fontencoding.h +++ b/core/fpdfapi/font/cpdf_fontencoding.h @@ -9,9 +9,9 @@ #include -#include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/string_pool_template.h" +#include "core/fxcrt/weak_ptr.h" #define PDFFONT_ENCODING_BUILTIN 0 #define PDFFONT_ENCODING_WINANSI 1 @@ -53,7 +53,7 @@ class CPDF_FontEncoding { m_Unicodes[charcode] = unicode; } - std::unique_ptr Realize(CFX_WeakPtr pPool); + std::unique_ptr Realize(WeakPtr pPool); public: wchar_t m_Unicodes[256]; diff --git a/core/fpdfapi/page/cpdf_streamparser.cpp b/core/fpdfapi/page/cpdf_streamparser.cpp index ffd76005e0..e9320cbc6f 100644 --- a/core/fpdfapi/page/cpdf_streamparser.cpp +++ b/core/fpdfapi/page/cpdf_streamparser.cpp @@ -107,7 +107,7 @@ CPDF_StreamParser::CPDF_StreamParser(const uint8_t* pData, uint32_t dwSize) CPDF_StreamParser::CPDF_StreamParser(const uint8_t* pData, uint32_t dwSize, - const CFX_WeakPtr& pPool) + const WeakPtr& pPool) : m_pBuf(pData), m_Size(dwSize), m_Pos(0), m_pPool(pPool) {} CPDF_StreamParser::~CPDF_StreamParser() {} diff --git a/core/fpdfapi/page/cpdf_streamparser.h b/core/fpdfapi/page/cpdf_streamparser.h index 53d5dfd135..158726b026 100644 --- a/core/fpdfapi/page/cpdf_streamparser.h +++ b/core/fpdfapi/page/cpdf_streamparser.h @@ -14,8 +14,8 @@ #include "core/fpdfapi/parser/cpdf_document.h" #include "core/fpdfapi/parser/cpdf_object.h" #include "core/fpdfapi/parser/cpdf_stream.h" -#include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/string_pool_template.h" +#include "core/fxcrt/weak_ptr.h" class CPDF_StreamParser { public: @@ -24,7 +24,7 @@ class CPDF_StreamParser { CPDF_StreamParser(const uint8_t* pData, uint32_t dwSize); CPDF_StreamParser(const uint8_t* pData, uint32_t dwSize, - const CFX_WeakPtr& pPool); + const WeakPtr& pPool); ~CPDF_StreamParser(); SyntaxType ParseNextElement(); @@ -56,7 +56,7 @@ class CPDF_StreamParser { uint8_t m_WordBuffer[256]; uint32_t m_WordSize; std::unique_ptr m_pLastObj; - CFX_WeakPtr m_pPool; + WeakPtr m_pPool; }; #endif // CORE_FPDFAPI_PAGE_CPDF_STREAMPARSER_H_ diff --git a/core/fpdfapi/parser/cpdf_array.cpp b/core/fpdfapi/parser/cpdf_array.cpp index f4907ffdf1..5a5cdc9be0 100644 --- a/core/fpdfapi/parser/cpdf_array.cpp +++ b/core/fpdfapi/parser/cpdf_array.cpp @@ -20,8 +20,7 @@ CPDF_Array::CPDF_Array() {} -CPDF_Array::CPDF_Array(const CFX_WeakPtr& pPool) - : m_pPool(pPool) {} +CPDF_Array::CPDF_Array(const WeakPtr& pPool) : m_pPool(pPool) {} CPDF_Array::~CPDF_Array() { // Break cycles for cyclic references. diff --git a/core/fpdfapi/parser/cpdf_array.h b/core/fpdfapi/parser/cpdf_array.h index 3b18025b2d..bf8cb426c4 100644 --- a/core/fpdfapi/parser/cpdf_array.h +++ b/core/fpdfapi/parser/cpdf_array.h @@ -24,7 +24,7 @@ class CPDF_Array : public CPDF_Object { std::vector>::const_iterator; CPDF_Array(); - explicit CPDF_Array(const CFX_WeakPtr& pPool); + explicit CPDF_Array(const WeakPtr& pPool); ~CPDF_Array() override; // CPDF_Object: @@ -112,7 +112,7 @@ class CPDF_Array : public CPDF_Object { std::set* pVisited) const override; std::vector> m_Objects; - CFX_WeakPtr m_pPool; + WeakPtr m_pPool; }; inline CPDF_Array* ToArray(CPDF_Object* obj) { diff --git a/core/fpdfapi/parser/cpdf_dictionary.cpp b/core/fpdfapi/parser/cpdf_dictionary.cpp index 570c57e557..5cdcf34f81 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.cpp +++ b/core/fpdfapi/parser/cpdf_dictionary.cpp @@ -22,9 +22,9 @@ #include "third_party/base/stl_util.h" CPDF_Dictionary::CPDF_Dictionary() - : CPDF_Dictionary(CFX_WeakPtr()) {} + : CPDF_Dictionary(WeakPtr()) {} -CPDF_Dictionary::CPDF_Dictionary(const CFX_WeakPtr& pPool) +CPDF_Dictionary::CPDF_Dictionary(const WeakPtr& pPool) : m_pPool(pPool) {} CPDF_Dictionary::~CPDF_Dictionary() { diff --git a/core/fpdfapi/parser/cpdf_dictionary.h b/core/fpdfapi/parser/cpdf_dictionary.h index a9ff353fbe..688e88e510 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.h +++ b/core/fpdfapi/parser/cpdf_dictionary.h @@ -13,10 +13,10 @@ #include #include "core/fpdfapi/parser/cpdf_object.h" -#include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/fx_coordinates.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/string_pool_template.h" +#include "core/fxcrt/weak_ptr.h" #include "third_party/base/ptr_util.h" class CPDF_IndirectObjectHolder; @@ -27,7 +27,7 @@ class CPDF_Dictionary : public CPDF_Object { std::map>::const_iterator; CPDF_Dictionary(); - explicit CPDF_Dictionary(const CFX_WeakPtr& pPool); + explicit CPDF_Dictionary(const WeakPtr& pPool); ~CPDF_Dictionary() override; // CPDF_Object: @@ -97,7 +97,7 @@ class CPDF_Dictionary : public CPDF_Object { const_iterator begin() const { return m_Map.begin(); } const_iterator end() const { return m_Map.end(); } - CFX_WeakPtr GetByteStringPool() const { return m_pPool; } + WeakPtr GetByteStringPool() const { return m_pPool; } protected: ByteString MaybeIntern(const ByteString& str); @@ -105,7 +105,7 @@ class CPDF_Dictionary : public CPDF_Object { bool bDirect, std::set* visited) const override; - CFX_WeakPtr m_pPool; + WeakPtr m_pPool; std::map> m_Map; }; diff --git a/core/fpdfapi/parser/cpdf_indirect_object_holder.h b/core/fpdfapi/parser/cpdf_indirect_object_holder.h index ea70c4a351..57374b9cc2 100644 --- a/core/fpdfapi/parser/cpdf_indirect_object_holder.h +++ b/core/fpdfapi/parser/cpdf_indirect_object_holder.h @@ -14,9 +14,9 @@ #include #include "core/fpdfapi/parser/cpdf_object.h" -#include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/fx_system.h" #include "core/fxcrt/string_pool_template.h" +#include "core/fxcrt/weak_ptr.h" #include "third_party/base/ptr_util.h" class CPDF_IndirectObjectHolder { @@ -58,7 +58,7 @@ class CPDF_IndirectObjectHolder { uint32_t GetLastObjNum() const { return m_LastObjNum; } void SetLastObjNum(uint32_t objnum) { m_LastObjNum = objnum; } - CFX_WeakPtr GetByteStringPool() const { + WeakPtr GetByteStringPool() const { return m_pByteStringPool; } @@ -72,7 +72,7 @@ class CPDF_IndirectObjectHolder { uint32_t m_LastObjNum; std::map> m_IndirectObjs; std::vector> m_OrphanObjs; - CFX_WeakPtr m_pByteStringPool; + WeakPtr m_pByteStringPool; }; #endif // CORE_FPDFAPI_PARSER_CPDF_INDIRECT_OBJECT_HOLDER_H_ diff --git a/core/fpdfapi/parser/cpdf_name.cpp b/core/fpdfapi/parser/cpdf_name.cpp index 0dd9890702..0607a30342 100644 --- a/core/fpdfapi/parser/cpdf_name.cpp +++ b/core/fpdfapi/parser/cpdf_name.cpp @@ -10,7 +10,7 @@ #include "core/fxcrt/fx_stream.h" #include "third_party/base/ptr_util.h" -CPDF_Name::CPDF_Name(CFX_WeakPtr pPool, const ByteString& str) +CPDF_Name::CPDF_Name(WeakPtr pPool, const ByteString& str) : m_Name(str) { if (pPool) m_Name = pPool->Intern(m_Name); diff --git a/core/fpdfapi/parser/cpdf_name.h b/core/fpdfapi/parser/cpdf_name.h index 8a1cf2b63b..880c10aedb 100644 --- a/core/fpdfapi/parser/cpdf_name.h +++ b/core/fpdfapi/parser/cpdf_name.h @@ -10,12 +10,12 @@ #include #include "core/fpdfapi/parser/cpdf_object.h" -#include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/string_pool_template.h" +#include "core/fxcrt/weak_ptr.h" class CPDF_Name : public CPDF_Object { public: - CPDF_Name(CFX_WeakPtr pPool, const ByteString& str); + CPDF_Name(WeakPtr pPool, const ByteString& str); ~CPDF_Name() override; // CPDF_Object: diff --git a/core/fpdfapi/parser/cpdf_string.cpp b/core/fpdfapi/parser/cpdf_string.cpp index 1539861622..851442e8b4 100644 --- a/core/fpdfapi/parser/cpdf_string.cpp +++ b/core/fpdfapi/parser/cpdf_string.cpp @@ -14,7 +14,7 @@ CPDF_String::CPDF_String() : m_bHex(false) {} -CPDF_String::CPDF_String(CFX_WeakPtr pPool, +CPDF_String::CPDF_String(WeakPtr pPool, const ByteString& str, bool bHex) : m_String(str), m_bHex(bHex) { @@ -22,8 +22,7 @@ CPDF_String::CPDF_String(CFX_WeakPtr pPool, m_String = pPool->Intern(m_String); } -CPDF_String::CPDF_String(CFX_WeakPtr pPool, - const WideString& str) +CPDF_String::CPDF_String(WeakPtr pPool, const WideString& str) : m_String(PDF_EncodeText(str)), m_bHex(false) { if (pPool) m_String = pPool->Intern(m_String); diff --git a/core/fpdfapi/parser/cpdf_string.h b/core/fpdfapi/parser/cpdf_string.h index 6f762ae27b..b6aaacb043 100644 --- a/core/fpdfapi/parser/cpdf_string.h +++ b/core/fpdfapi/parser/cpdf_string.h @@ -10,18 +10,16 @@ #include #include "core/fpdfapi/parser/cpdf_object.h" -#include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/fx_system.h" #include "core/fxcrt/string_pool_template.h" +#include "core/fxcrt/weak_ptr.h" class CPDF_String : public CPDF_Object { public: CPDF_String(); - CPDF_String(CFX_WeakPtr pPool, - const ByteString& str, - bool bHex); - CPDF_String(CFX_WeakPtr pPool, const WideString& str); + CPDF_String(WeakPtr pPool, const ByteString& str, bool bHex); + CPDF_String(WeakPtr pPool, const WideString& str); ~CPDF_String() override; // CPDF_Object: diff --git a/core/fpdfapi/parser/cpdf_syntax_parser.cpp b/core/fpdfapi/parser/cpdf_syntax_parser.cpp index 7bea4c45fc..bf6d90ca9a 100644 --- a/core/fpdfapi/parser/cpdf_syntax_parser.cpp +++ b/core/fpdfapi/parser/cpdf_syntax_parser.cpp @@ -41,9 +41,9 @@ enum class ReadStatus { Normal, Backslash, Octal, FinishOctal, CarriageReturn }; int CPDF_SyntaxParser::s_CurrentRecursionDepth = 0; CPDF_SyntaxParser::CPDF_SyntaxParser() - : CPDF_SyntaxParser(CFX_WeakPtr()) {} + : CPDF_SyntaxParser(WeakPtr()) {} -CPDF_SyntaxParser::CPDF_SyntaxParser(const CFX_WeakPtr& pPool) +CPDF_SyntaxParser::CPDF_SyntaxParser(const WeakPtr& pPool) : m_MetadataObjnum(0), m_pFileAccess(nullptr), m_pFileBuf(nullptr), diff --git a/core/fpdfapi/parser/cpdf_syntax_parser.h b/core/fpdfapi/parser/cpdf_syntax_parser.h index cb266837dc..2b642b5963 100644 --- a/core/fpdfapi/parser/cpdf_syntax_parser.h +++ b/core/fpdfapi/parser/cpdf_syntax_parser.h @@ -10,8 +10,8 @@ #include #include -#include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/string_pool_template.h" +#include "core/fxcrt/weak_ptr.h" class CPDF_CryptoHandler; class CPDF_Dictionary; @@ -26,7 +26,7 @@ class CPDF_SyntaxParser { enum class ParseType { kStrict, kLoose }; CPDF_SyntaxParser(); - explicit CPDF_SyntaxParser(const CFX_WeakPtr& pPool); + explicit CPDF_SyntaxParser(const WeakPtr& pPool); ~CPDF_SyntaxParser(); void InitParser(const RetainPtr& pFileAccess, @@ -121,7 +121,7 @@ class CPDF_SyntaxParser { RetainPtr m_pCryptoHandler; uint8_t m_WordBuffer[257]; uint32_t m_WordSize; - CFX_WeakPtr m_pPool; + WeakPtr m_pPool; }; #endif // CORE_FPDFAPI_PARSER_CPDF_SYNTAX_PARSER_H_ diff --git a/core/fpdfdoc/cpdf_filespec.h b/core/fpdfdoc/cpdf_filespec.h index b32207d273..deeccb9619 100644 --- a/core/fpdfdoc/cpdf_filespec.h +++ b/core/fpdfdoc/cpdf_filespec.h @@ -7,10 +7,10 @@ #ifndef CORE_FPDFDOC_CPDF_FILESPEC_H_ #define CORE_FPDFDOC_CPDF_FILESPEC_H_ -#include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/string_pool_template.h" #include "core/fxcrt/unowned_ptr.h" +#include "core/fxcrt/weak_ptr.h" class CPDF_Dictionary; class CPDF_Object; diff --git a/core/fxcrt/cfx_weak_ptr.h b/core/fxcrt/cfx_weak_ptr.h deleted file mode 100644 index da96000a1b..0000000000 --- a/core/fxcrt/cfx_weak_ptr.h +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright 2016 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_FXCRT_CFX_WEAK_PTR_H_ -#define CORE_FXCRT_CFX_WEAK_PTR_H_ - -#include -#include -#include - -#include "core/fxcrt/fx_system.h" -#include "core/fxcrt/retain_ptr.h" - -template > -class CFX_WeakPtr { - public: - CFX_WeakPtr() {} - CFX_WeakPtr(const CFX_WeakPtr& that) : m_pHandle(that.m_pHandle) {} - CFX_WeakPtr(CFX_WeakPtr&& that) noexcept { Swap(that); } - explicit CFX_WeakPtr(std::unique_ptr pObj) - : m_pHandle(new Handle(std::move(pObj))) {} - - // Deliberately implicit to allow passing nullptr. - // NOLINTNEXTLINE(runtime/explicit) - CFX_WeakPtr(std::nullptr_t arg) {} - - explicit operator bool() const { return m_pHandle && !!m_pHandle->Get(); } - bool HasOneRef() const { return m_pHandle && m_pHandle->HasOneRef(); } - T* operator->() { return m_pHandle->Get(); } - const T* operator->() const { return m_pHandle->Get(); } - CFX_WeakPtr& operator=(const CFX_WeakPtr& that) { - m_pHandle = that.m_pHandle; - return *this; - } - bool operator==(const CFX_WeakPtr& that) const { - return m_pHandle == that.m_pHandle; - } - bool operator!=(const CFX_WeakPtr& that) const { return !(*this == that); } - - T* Get() const { return m_pHandle ? m_pHandle->Get() : nullptr; } - void DeleteObject() { - if (m_pHandle) { - m_pHandle->Clear(); - m_pHandle.Reset(); - } - } - void Reset() { m_pHandle.Reset(); } - void Reset(std::unique_ptr pObj) { - m_pHandle.Reset(new Handle(std::move(pObj))); - } - void Swap(CFX_WeakPtr& that) { m_pHandle.Swap(that.m_pHandle); } - - private: - class Handle { - public: - explicit Handle(std::unique_ptr ptr) - : m_nCount(0), m_pObj(std::move(ptr)) {} - void Reset(std::unique_ptr ptr) { m_pObj = std::move(ptr); } - void Clear() { // Now you're all weak ptrs ... - m_pObj.reset(); // unique_ptr nulls first before invoking delete. - } - T* Get() const { return m_pObj.get(); } - T* Retain() { - ++m_nCount; - return m_pObj.get(); - } - void Release() { - if (--m_nCount == 0) - delete this; - } - bool HasOneRef() const { return m_nCount == 1; } - - private: - ~Handle() {} - - intptr_t m_nCount; - std::unique_ptr m_pObj; - }; - - RetainPtr m_pHandle; -}; - -#endif // CORE_FXCRT_CFX_WEAK_PTR_H_ diff --git a/core/fxcrt/cfx_weak_ptr_unittest.cpp b/core/fxcrt/cfx_weak_ptr_unittest.cpp deleted file mode 100644 index aafcf16b3d..0000000000 --- a/core/fxcrt/cfx_weak_ptr_unittest.cpp +++ /dev/null @@ -1,173 +0,0 @@ -// Copyright 2016 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "core/fxcrt/cfx_weak_ptr.h" - -#include -#include - -#include "core/fxcrt/fx_memory.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace { - -class PseudoDeletable; -using WeakPtr = CFX_WeakPtr>; -using UniquePtr = - std::unique_ptr>; - -class PseudoDeletable { - public: - PseudoDeletable() : delete_count_(0) {} - void Release() { - ++delete_count_; - next_.Reset(); - } - void SetNext(const WeakPtr& next) { next_ = next; } - int delete_count() const { return delete_count_; } - - private: - int delete_count_; - WeakPtr next_; -}; - -} // namespace - -TEST(fxcrt, WeakPtrNull) { - WeakPtr ptr1; - EXPECT_FALSE(ptr1); - - WeakPtr ptr2; - EXPECT_TRUE(ptr1 == ptr2); - EXPECT_FALSE(ptr1 != ptr2); - - WeakPtr ptr3(ptr1); - EXPECT_TRUE(ptr1 == ptr3); - EXPECT_FALSE(ptr1 != ptr3); - - WeakPtr ptr4 = ptr1; - EXPECT_TRUE(ptr1 == ptr4); - EXPECT_FALSE(ptr1 != ptr4); -} - -TEST(fxcrt, WeakPtrNonNull) { - PseudoDeletable thing; - EXPECT_EQ(0, thing.delete_count()); - { - UniquePtr unique(&thing); - WeakPtr ptr1(std::move(unique)); - EXPECT_TRUE(ptr1); - EXPECT_EQ(&thing, ptr1.Get()); - - WeakPtr ptr2; - EXPECT_FALSE(ptr1 == ptr2); - EXPECT_TRUE(ptr1 != ptr2); - { - WeakPtr ptr3(ptr1); - EXPECT_TRUE(ptr1 == ptr3); - EXPECT_FALSE(ptr1 != ptr3); - EXPECT_EQ(&thing, ptr3.Get()); - { - WeakPtr ptr4 = ptr1; - EXPECT_TRUE(ptr1 == ptr4); - EXPECT_FALSE(ptr1 != ptr4); - EXPECT_EQ(&thing, ptr4.Get()); - } - } - EXPECT_EQ(0, thing.delete_count()); - } - EXPECT_EQ(1, thing.delete_count()); -} - -TEST(fxcrt, WeakPtrResetNull) { - PseudoDeletable thing; - { - UniquePtr unique(&thing); - WeakPtr ptr1(std::move(unique)); - WeakPtr ptr2 = ptr1; - ptr1.Reset(); - EXPECT_FALSE(ptr1); - EXPECT_EQ(nullptr, ptr1.Get()); - EXPECT_TRUE(ptr2); - EXPECT_EQ(&thing, ptr2.Get()); - EXPECT_FALSE(ptr1 == ptr2); - EXPECT_TRUE(ptr1 != ptr2); - EXPECT_EQ(0, thing.delete_count()); - } - EXPECT_EQ(1, thing.delete_count()); -} - -TEST(fxcrt, WeakPtrResetNonNull) { - PseudoDeletable thing1; - PseudoDeletable thing2; - { - UniquePtr unique1(&thing1); - WeakPtr ptr1(std::move(unique1)); - WeakPtr ptr2 = ptr1; - UniquePtr unique2(&thing2); - ptr2.Reset(std::move(unique2)); - EXPECT_TRUE(ptr1); - EXPECT_EQ(&thing1, ptr1.Get()); - EXPECT_TRUE(ptr2); - EXPECT_EQ(&thing2, ptr2.Get()); - EXPECT_FALSE(ptr1 == ptr2); - EXPECT_TRUE(ptr1 != ptr2); - EXPECT_EQ(0, thing1.delete_count()); - EXPECT_EQ(0, thing2.delete_count()); - } - EXPECT_EQ(1, thing1.delete_count()); - EXPECT_EQ(1, thing2.delete_count()); -} - -TEST(fxcrt, WeakPtrDeleteObject) { - PseudoDeletable thing; - { - UniquePtr unique(&thing); - WeakPtr ptr1(std::move(unique)); - WeakPtr ptr2 = ptr1; - ptr1.DeleteObject(); - EXPECT_FALSE(ptr1); - EXPECT_EQ(nullptr, ptr1.Get()); - EXPECT_FALSE(ptr2); - EXPECT_EQ(nullptr, ptr2.Get()); - EXPECT_FALSE(ptr1 == ptr2); - EXPECT_TRUE(ptr1 != ptr2); - EXPECT_EQ(1, thing.delete_count()); - } - EXPECT_EQ(1, thing.delete_count()); -} - -TEST(fxcrt, WeakPtrCyclic) { - PseudoDeletable thing1; - PseudoDeletable thing2; - { - UniquePtr unique1(&thing1); - UniquePtr unique2(&thing2); - WeakPtr ptr1(std::move(unique1)); - WeakPtr ptr2(std::move(unique2)); - ptr1->SetNext(ptr2); - ptr2->SetNext(ptr1); - } - // Leaks without explicit clear. - EXPECT_EQ(0, thing1.delete_count()); - EXPECT_EQ(0, thing2.delete_count()); -} - -TEST(fxcrt, WeakPtrCyclicDeleteObject) { - PseudoDeletable thing1; - PseudoDeletable thing2; - { - UniquePtr unique1(&thing1); - UniquePtr unique2(&thing2); - WeakPtr ptr1(std::move(unique1)); - WeakPtr ptr2(std::move(unique2)); - ptr1->SetNext(ptr2); - ptr2->SetNext(ptr1); - ptr1.DeleteObject(); - EXPECT_EQ(1, thing1.delete_count()); - EXPECT_EQ(0, thing2.delete_count()); - } - EXPECT_EQ(1, thing1.delete_count()); - EXPECT_EQ(1, thing2.delete_count()); -} diff --git a/core/fxcrt/weak_ptr.h b/core/fxcrt/weak_ptr.h new file mode 100644 index 0000000000..eb8523bd3d --- /dev/null +++ b/core/fxcrt/weak_ptr.h @@ -0,0 +1,92 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXCRT_WEAK_PTR_H_ +#define CORE_FXCRT_WEAK_PTR_H_ + +#include +#include +#include + +#include "core/fxcrt/fx_system.h" +#include "core/fxcrt/retain_ptr.h" + +namespace fxcrt { + +template > +class WeakPtr { + public: + WeakPtr() {} + WeakPtr(const WeakPtr& that) : m_pHandle(that.m_pHandle) {} + WeakPtr(WeakPtr&& that) noexcept { Swap(that); } + explicit WeakPtr(std::unique_ptr pObj) + : m_pHandle(new Handle(std::move(pObj))) {} + + // Deliberately implicit to allow passing nullptr. + // NOLINTNEXTLINE(runtime/explicit) + WeakPtr(std::nullptr_t arg) {} + + explicit operator bool() const { return m_pHandle && !!m_pHandle->Get(); } + bool HasOneRef() const { return m_pHandle && m_pHandle->HasOneRef(); } + T* operator->() { return m_pHandle->Get(); } + const T* operator->() const { return m_pHandle->Get(); } + WeakPtr& operator=(const WeakPtr& that) { + m_pHandle = that.m_pHandle; + return *this; + } + bool operator==(const WeakPtr& that) const { + return m_pHandle == that.m_pHandle; + } + bool operator!=(const WeakPtr& that) const { return !(*this == that); } + + T* Get() const { return m_pHandle ? m_pHandle->Get() : nullptr; } + void DeleteObject() { + if (m_pHandle) { + m_pHandle->Clear(); + m_pHandle.Reset(); + } + } + void Reset() { m_pHandle.Reset(); } + void Reset(std::unique_ptr pObj) { + m_pHandle.Reset(new Handle(std::move(pObj))); + } + void Swap(WeakPtr& that) { m_pHandle.Swap(that.m_pHandle); } + + private: + class Handle { + public: + explicit Handle(std::unique_ptr ptr) + : m_nCount(0), m_pObj(std::move(ptr)) {} + void Reset(std::unique_ptr ptr) { m_pObj = std::move(ptr); } + void Clear() { // Now you're all weak ptrs ... + m_pObj.reset(); // unique_ptr nulls first before invoking delete. + } + T* Get() const { return m_pObj.get(); } + T* Retain() { + ++m_nCount; + return m_pObj.get(); + } + void Release() { + if (--m_nCount == 0) + delete this; + } + bool HasOneRef() const { return m_nCount == 1; } + + private: + ~Handle() {} + + intptr_t m_nCount; + std::unique_ptr m_pObj; + }; + + RetainPtr m_pHandle; +}; + +} // namespace fxcrt + +using fxcrt::WeakPtr; + +#endif // CORE_FXCRT_WEAK_PTR_H_ diff --git a/core/fxcrt/weak_ptr_unittest.cpp b/core/fxcrt/weak_ptr_unittest.cpp new file mode 100644 index 0000000000..22a723043b --- /dev/null +++ b/core/fxcrt/weak_ptr_unittest.cpp @@ -0,0 +1,176 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "core/fxcrt/weak_ptr.h" + +#include +#include + +#include "core/fxcrt/fx_memory.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace fxcrt { +namespace { + +class PseudoDeletable; +using WeakTestPtr = WeakPtr>; +using UniqueTestPtr = + std::unique_ptr>; + +class PseudoDeletable { + public: + PseudoDeletable() : delete_count_(0) {} + void Release() { + ++delete_count_; + next_.Reset(); + } + void SetNext(const WeakTestPtr& next) { next_ = next; } + int delete_count() const { return delete_count_; } + + private: + int delete_count_; + WeakTestPtr next_; +}; + +} // namespace + +TEST(WeakPtr, Null) { + WeakTestPtr ptr1; + EXPECT_FALSE(ptr1); + + WeakTestPtr ptr2; + EXPECT_TRUE(ptr1 == ptr2); + EXPECT_FALSE(ptr1 != ptr2); + + WeakTestPtr ptr3(ptr1); + EXPECT_TRUE(ptr1 == ptr3); + EXPECT_FALSE(ptr1 != ptr3); + + WeakTestPtr ptr4 = ptr1; + EXPECT_TRUE(ptr1 == ptr4); + EXPECT_FALSE(ptr1 != ptr4); +} + +TEST(WeakPtr, NonNull) { + PseudoDeletable thing; + EXPECT_EQ(0, thing.delete_count()); + { + UniqueTestPtr unique(&thing); + WeakTestPtr ptr1(std::move(unique)); + EXPECT_TRUE(ptr1); + EXPECT_EQ(&thing, ptr1.Get()); + + WeakTestPtr ptr2; + EXPECT_FALSE(ptr1 == ptr2); + EXPECT_TRUE(ptr1 != ptr2); + { + WeakTestPtr ptr3(ptr1); + EXPECT_TRUE(ptr1 == ptr3); + EXPECT_FALSE(ptr1 != ptr3); + EXPECT_EQ(&thing, ptr3.Get()); + { + WeakTestPtr ptr4 = ptr1; + EXPECT_TRUE(ptr1 == ptr4); + EXPECT_FALSE(ptr1 != ptr4); + EXPECT_EQ(&thing, ptr4.Get()); + } + } + EXPECT_EQ(0, thing.delete_count()); + } + EXPECT_EQ(1, thing.delete_count()); +} + +TEST(WeakPtr, ResetNull) { + PseudoDeletable thing; + { + UniqueTestPtr unique(&thing); + WeakTestPtr ptr1(std::move(unique)); + WeakTestPtr ptr2 = ptr1; + ptr1.Reset(); + EXPECT_FALSE(ptr1); + EXPECT_EQ(nullptr, ptr1.Get()); + EXPECT_TRUE(ptr2); + EXPECT_EQ(&thing, ptr2.Get()); + EXPECT_FALSE(ptr1 == ptr2); + EXPECT_TRUE(ptr1 != ptr2); + EXPECT_EQ(0, thing.delete_count()); + } + EXPECT_EQ(1, thing.delete_count()); +} + +TEST(WeakPtr, ResetNonNull) { + PseudoDeletable thing1; + PseudoDeletable thing2; + { + UniqueTestPtr unique1(&thing1); + WeakTestPtr ptr1(std::move(unique1)); + WeakTestPtr ptr2 = ptr1; + UniqueTestPtr unique2(&thing2); + ptr2.Reset(std::move(unique2)); + EXPECT_TRUE(ptr1); + EXPECT_EQ(&thing1, ptr1.Get()); + EXPECT_TRUE(ptr2); + EXPECT_EQ(&thing2, ptr2.Get()); + EXPECT_FALSE(ptr1 == ptr2); + EXPECT_TRUE(ptr1 != ptr2); + EXPECT_EQ(0, thing1.delete_count()); + EXPECT_EQ(0, thing2.delete_count()); + } + EXPECT_EQ(1, thing1.delete_count()); + EXPECT_EQ(1, thing2.delete_count()); +} + +TEST(WeakPtr, DeleteObject) { + PseudoDeletable thing; + { + UniqueTestPtr unique(&thing); + WeakTestPtr ptr1(std::move(unique)); + WeakTestPtr ptr2 = ptr1; + ptr1.DeleteObject(); + EXPECT_FALSE(ptr1); + EXPECT_EQ(nullptr, ptr1.Get()); + EXPECT_FALSE(ptr2); + EXPECT_EQ(nullptr, ptr2.Get()); + EXPECT_FALSE(ptr1 == ptr2); + EXPECT_TRUE(ptr1 != ptr2); + EXPECT_EQ(1, thing.delete_count()); + } + EXPECT_EQ(1, thing.delete_count()); +} + +TEST(WeakPtr, Cyclic) { + PseudoDeletable thing1; + PseudoDeletable thing2; + { + UniqueTestPtr unique1(&thing1); + UniqueTestPtr unique2(&thing2); + WeakTestPtr ptr1(std::move(unique1)); + WeakTestPtr ptr2(std::move(unique2)); + ptr1->SetNext(ptr2); + ptr2->SetNext(ptr1); + } + // Leaks without explicit clear. + EXPECT_EQ(0, thing1.delete_count()); + EXPECT_EQ(0, thing2.delete_count()); +} + +TEST(WeakPtr, CyclicDeleteObject) { + PseudoDeletable thing1; + PseudoDeletable thing2; + { + UniqueTestPtr unique1(&thing1); + UniqueTestPtr unique2(&thing2); + WeakTestPtr ptr1(std::move(unique1)); + WeakTestPtr ptr2(std::move(unique2)); + ptr1->SetNext(ptr2); + ptr2->SetNext(ptr1); + ptr1.DeleteObject(); + EXPECT_EQ(1, thing1.delete_count()); + EXPECT_EQ(0, thing2.delete_count()); + } + EXPECT_EQ(1, thing1.delete_count()); + EXPECT_EQ(1, thing2.delete_count()); +} + +} // namespace fxcrt -- cgit v1.2.3