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>
diff --git a/core/fxcrt/maybe_owned.h b/core/fxcrt/maybe_owned.h
index d920ea7..ac3a81d 100644
--- a/core/fxcrt/maybe_owned.h
+++ b/core/fxcrt/maybe_owned.h
@@ -24,6 +24,7 @@
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 @@
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 7182647..e1637c5 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 @@
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;
{