summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortsepez <tsepez@chromium.org>2017-01-04 11:31:47 -0800
committerCommit bot <commit-bot@chromium.org>2017-01-04 11:31:47 -0800
commitdc39e377e1a3923e63569020beb29f9662c1a3ee (patch)
treea6cd7345b12610eb0cf840932cb72f2a0e2856af
parent6745f96fab41e2b46f57a3717b034a4064c0de02 (diff)
downloadpdfium-dc39e377e1a3923e63569020beb29f9662c1a3ee.tar.xz
Add missing operator<() to CFX_RetainPtr.
Use std::less<>() rather than a direct ptr1 < ptr2 comparison to be strictly correct in face of unspecified behaviour when ptr1 and ptr2 don't point within the same "object" (e.g. segment of memory on a brain-dead segmented architecture). This will allow their use as keys in maps. Review-Url: https://codereview.chromium.org/2616683002
-rw-r--r--core/fxcrt/cfx_retain_ptr.h7
-rw-r--r--core/fxcrt/cfx_retain_ptr_unittest.cpp8
2 files changed, 14 insertions, 1 deletions
diff --git a/core/fxcrt/cfx_retain_ptr.h b/core/fxcrt/cfx_retain_ptr.h
index 1b137d4974..f70faf1464 100644
--- a/core/fxcrt/cfx_retain_ptr.h
+++ b/core/fxcrt/cfx_retain_ptr.h
@@ -5,11 +5,13 @@
#ifndef CORE_FXCRT_CFX_RETAIN_PTR_H_
#define CORE_FXCRT_CFX_RETAIN_PTR_H_
+#include <functional>
#include <memory>
#include <utility>
#include "core/fxcrt/fx_memory.h"
+// Analogous to base's scoped_refptr.
template <class T>
class CFX_RetainPtr {
public:
@@ -50,9 +52,12 @@ class CFX_RetainPtr {
bool operator==(const CFX_RetainPtr& that) const {
return Get() == that.Get();
}
-
bool operator!=(const CFX_RetainPtr& that) const { return !(*this == that); }
+ bool operator<(const CFX_RetainPtr& that) const {
+ return std::less<T*>()(Get(), that.Get());
+ }
+
explicit operator bool() const { return !!m_pObj; }
T& operator*() const { return *m_pObj.get(); }
T* operator->() const { return m_pObj.get(); }
diff --git a/core/fxcrt/cfx_retain_ptr_unittest.cpp b/core/fxcrt/cfx_retain_ptr_unittest.cpp
index f9a4ecb74f..3168b5a4c7 100644
--- a/core/fxcrt/cfx_retain_ptr_unittest.cpp
+++ b/core/fxcrt/cfx_retain_ptr_unittest.cpp
@@ -225,6 +225,14 @@ TEST(fxcrt, RetainPtrNotEquals) {
EXPECT_TRUE(obj1_ptr1 != obj2_ptr1);
}
+TEST(fxcrt, RetainPtrLessThan) {
+ PseudoRetainable objs[2];
+ CFX_RetainPtr<PseudoRetainable> obj1_ptr(&objs[0]);
+ CFX_RetainPtr<PseudoRetainable> obj2_ptr(&objs[1]);
+ EXPECT_TRUE(obj1_ptr < obj2_ptr);
+ EXPECT_FALSE(obj2_ptr < obj1_ptr);
+}
+
TEST(fxcrt, RetainPtrBool) {
PseudoRetainable obj1;
CFX_RetainPtr<PseudoRetainable> null_ptr;