From 2696aa066d9bd10641e4d88cb3c020af7b8086b3 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 21 Aug 2018 20:30:39 +0000 Subject: Allow creation of a MaybeOwned from UnownedPtr. Small test to show these forms compile and do not transfer ownership. Change-Id: I1787a57d4d0bf70da7b6101c1f958cfe9a8f9dda Reviewed-on: https://pdfium-review.googlesource.com/40830 Reviewed-by: Lei Zhang Commit-Queue: Tom Sepez --- core/fxcrt/maybe_owned.h | 5 +++++ core/fxcrt/maybe_owned_unittest.cpp | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) (limited to 'core') diff --git a/core/fxcrt/maybe_owned.h b/core/fxcrt/maybe_owned.h index d920ea7a91..ac3a81d47e 100644 --- a/core/fxcrt/maybe_owned.h +++ b/core/fxcrt/maybe_owned.h @@ -24,6 +24,7 @@ class MaybeOwned { public: MaybeOwned() : m_pObj(nullptr) {} explicit MaybeOwned(T* ptr) : m_pObj(ptr) {} + explicit MaybeOwned(const UnownedPtr& ptr) : m_pObj(ptr.Get()) {} explicit MaybeOwned(std::unique_ptr ptr) : m_pOwnedObj(std::move(ptr)), m_pObj(m_pOwnedObj.get()) {} @@ -74,6 +75,10 @@ class MaybeOwned { Reset(ptr); return *this; } + MaybeOwned& operator=(const UnownedPtr& ptr) { + Reset(ptr.Get()); + return *this; + } MaybeOwned& operator=(std::unique_ptr ptr) { Reset(std::move(ptr)); return *this; diff --git a/core/fxcrt/maybe_owned_unittest.cpp b/core/fxcrt/maybe_owned_unittest.cpp index 7182647207..e1637c59b3 100644 --- a/core/fxcrt/maybe_owned_unittest.cpp +++ b/core/fxcrt/maybe_owned_unittest.cpp @@ -8,6 +8,7 @@ #include #include "core/fxcrt/fx_memory.h" +#include "core/fxcrt/unowned_ptr.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/base/ptr_util.h" @@ -80,6 +81,21 @@ TEST(MaybeOwned, NotOwned) { EXPECT_EQ(1, owned_delete_count); } +TEST(MaybeOwned, UnownedPtr) { + int delete_count = 0; + PseudoDeletable thing1(100, &delete_count); + PseudoDeletable thing2(200, &delete_count); + UnownedPtr unowned1(&thing1); + UnownedPtr unowned2(&thing2); + { + MaybeOwned ptr1(unowned1); + MaybeOwned ptr2(unowned2); + ptr2 = unowned1; + ptr1 = unowned2; + } + EXPECT_EQ(0, delete_count); +} + TEST(MaybeOwned, Owned) { int delete_count = 0; { -- cgit v1.2.3