diff options
author | Tom Sepez <tsepez@chromium.org> | 2018-08-21 20:30:39 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-08-21 20:30:39 +0000 |
commit | 2696aa066d9bd10641e4d88cb3c020af7b8086b3 (patch) | |
tree | b1dbf073215a8a4e63eef4047f36a50bf902db6b | |
parent | dc2bb9ad21a10550fb451d7c842c63cbce98045b (diff) | |
download | pdfium-2696aa066d9bd10641e4d88cb3c020af7b8086b3.tar.xz |
Allow creation of a MaybeOwned<T> from UnownedPtr<T>.
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 <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
-rw-r--r-- | core/fxcrt/maybe_owned.h | 5 | ||||
-rw-r--r-- | core/fxcrt/maybe_owned_unittest.cpp | 16 |
2 files changed, 21 insertions, 0 deletions
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<T>& ptr) : m_pObj(ptr.Get()) {} explicit MaybeOwned(std::unique_ptr<T, D> 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<T>& ptr) { + Reset(ptr.Get()); + return *this; + } MaybeOwned& operator=(std::unique_ptr<T, D> 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 <utility> #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<PseudoDeletable> unowned1(&thing1); + UnownedPtr<PseudoDeletable> unowned2(&thing2); + { + MaybeOwned<PseudoDeletable> ptr1(unowned1); + MaybeOwned<PseudoDeletable> ptr2(unowned2); + ptr2 = unowned1; + ptr1 = unowned2; + } + EXPECT_EQ(0, delete_count); +} + TEST(MaybeOwned, Owned) { int delete_count = 0; { |