summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2017-05-19 12:39:12 -0700
committerChromium commit bot <commit-bot@chromium.org>2017-05-19 21:18:28 +0000
commit7ec8eaa59308c39c8256300a488a618a3354a5bd (patch)
treee699d36ca013881018e2cc014a13f6c7ebdc5e36
parent92c13a7d495c54d86ac8faf8a56a017da2ad39ff (diff)
downloadpdfium-7ec8eaa59308c39c8256300a488a618a3354a5bd.tar.xz
Back-fill some functionality for CFX_UnownedPtr
Add Release() method, type-convertible compares and assigns, and right hand vs. left hand comparisons. Change-Id: I96b1112e328802143d314aa6c92948f26583fa90 Reviewed-on: https://pdfium-review.googlesource.com/5731 Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Tom Sepez <tsepez@chromium.org>
-rw-r--r--core/fxcrt/cfx_unowned_ptr.h43
-rw-r--r--core/fxcrt/cfx_unowned_ptr_unittest.cpp28
2 files changed, 59 insertions, 12 deletions
diff --git a/core/fxcrt/cfx_unowned_ptr.h b/core/fxcrt/cfx_unowned_ptr.h
index d91833abd4..da1875c7ab 100644
--- a/core/fxcrt/cfx_unowned_ptr.h
+++ b/core/fxcrt/cfx_unowned_ptr.h
@@ -10,8 +10,6 @@
#include <type_traits>
#include <utility>
-#include "core/fxcrt/fx_memory.h"
-
template <class T>
class CFX_UnownedPtr {
public:
@@ -25,21 +23,16 @@ class CFX_UnownedPtr {
// NOLINTNEXTLINE(runtime/explicit)
CFX_UnownedPtr(std::nullptr_t ptr) {}
-#if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
~CFX_UnownedPtr() { Probe(); }
-#endif
CFX_UnownedPtr& operator=(T* that) {
-#if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
Probe();
-#endif
m_pObj = that;
return *this;
}
+
CFX_UnownedPtr& operator=(const CFX_UnownedPtr& that) {
-#if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
Probe();
-#endif
if (*this != that)
m_pObj = that.Get();
return *this;
@@ -48,27 +41,53 @@ class CFX_UnownedPtr {
bool operator==(const CFX_UnownedPtr& that) const {
return Get() == that.Get();
}
- bool operator==(const T* that) const { return Get() == that; }
bool operator!=(const CFX_UnownedPtr& that) const { return !(*this == that); }
- bool operator!=(const T* that) const { return !(*this == that); }
bool operator<(const CFX_UnownedPtr& that) const {
return std::less<T*>()(Get(), that.Get());
}
+ template <typename U>
+ bool operator==(const U* that) const {
+ return Get() == that;
+ }
+
+ template <typename U>
+ bool operator!=(const U* that) const {
+ return !(*this == that);
+ }
+
T* Get() const { return m_pObj; }
+
+ T* Release() {
+ Probe();
+ T* pTemp = nullptr;
+ std::swap(pTemp, m_pObj);
+ return pTemp;
+ }
+
explicit operator bool() const { return !!m_pObj; }
T& operator*() const { return *m_pObj; }
T* operator->() const { return m_pObj; }
private:
+ inline void Probe() {
#if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
- void Probe() {
if (m_pObj)
reinterpret_cast<const volatile uint8_t*>(m_pObj)[0];
- }
#endif
+ }
T* m_pObj = nullptr;
};
+template <typename T, typename U>
+inline bool operator==(const U* lhs, const CFX_UnownedPtr<T>& rhs) {
+ return rhs == lhs;
+}
+
+template <typename T, typename U>
+inline bool operator!=(const U* lhs, const CFX_UnownedPtr<T>& rhs) {
+ return rhs != lhs;
+}
+
#endif // CORE_FXCRT_CFX_UNOWNED_PTR_H_
diff --git a/core/fxcrt/cfx_unowned_ptr_unittest.cpp b/core/fxcrt/cfx_unowned_ptr_unittest.cpp
index 586c4bbb2e..6218679fa2 100644
--- a/core/fxcrt/cfx_unowned_ptr_unittest.cpp
+++ b/core/fxcrt/cfx_unowned_ptr_unittest.cpp
@@ -34,6 +34,15 @@ void AssignDangling() {
delete ptr2;
}
+void ReleaseDangling() {
+ Clink* ptr1 = new Clink();
+ Clink* ptr2 = new Clink();
+ ptr2->next_ = ptr1;
+ delete ptr1;
+ ptr2->next_.Release();
+ delete ptr2;
+}
+
} // namespace
TEST(fxcrt, UnownedPtrOk) {
@@ -69,6 +78,23 @@ TEST(fxcrt, UnownedAssignNotOk) {
#endif
}
+TEST(fxcrt, UnownedReleaseOk) {
+ Clink* ptr1 = new Clink();
+ Clink* ptr2 = new Clink();
+ ptr2->next_ = ptr1;
+ ptr2->next_.Release();
+ delete ptr1;
+ delete ptr2;
+}
+
+TEST(fxcrt, UnownedReleaseNotOk) {
+#if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
+ EXPECT_DEATH(ReleaseDangling(), "");
+#else
+ ReleaseDangling();
+#endif
+}
+
TEST(fxcrt, UnownedOperatorEQ) {
int foo;
CFX_UnownedPtr<int> ptr1;
@@ -78,6 +104,7 @@ TEST(fxcrt, UnownedOperatorEQ) {
EXPECT_TRUE(ptr1 == ptr2);
CFX_UnownedPtr<int> ptr3(&foo);
+ EXPECT_TRUE(&foo == ptr3);
EXPECT_TRUE(ptr3 == &foo);
EXPECT_FALSE(ptr1 == ptr3);
@@ -94,6 +121,7 @@ TEST(fxcrt, UnownedOperatorNE) {
EXPECT_FALSE(ptr1 != ptr2);
CFX_UnownedPtr<int> ptr3(&foo);
+ EXPECT_FALSE(&foo != ptr3);
EXPECT_FALSE(ptr3 != &foo);
EXPECT_TRUE(ptr1 != ptr3);