diff options
-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; { |