summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortsepez <tsepez@chromium.org>2016-12-28 11:16:15 -0800
committerCommit bot <commit-bot@chromium.org>2016-12-28 11:16:15 -0800
commitd0bbccdd32113baf95ae16565c0314166d989638 (patch)
tree08d7c16a7a29919efc8b6c91fbde3baef835e533
parent04a407093c135cdf08644a03c210ae2182869254 (diff)
downloadpdfium-chromium/2966.tar.xz
Make CFX_Observable copy constructable.chromium/2969chromium/2968chromium/2967chromium/2966
Fix some naming to make things clearer. Review-Url: https://codereview.chromium.org/2587233002
-rw-r--r--core/fxcrt/cfx_observable.h47
-rw-r--r--core/fxcrt/cfx_observable_unittest.cpp72
2 files changed, 97 insertions, 22 deletions
diff --git a/core/fxcrt/cfx_observable.h b/core/fxcrt/cfx_observable.h
index dc869b1766..8dc0907cae 100644
--- a/core/fxcrt/cfx_observable.h
+++ b/core/fxcrt/cfx_observable.h
@@ -15,39 +15,42 @@ class CFX_Observable {
public:
class ObservedPtr {
public:
- ObservedPtr() : m_pObservedPtr(nullptr) {}
- explicit ObservedPtr(T* pObservedPtr) : m_pObservedPtr(pObservedPtr) {
- if (m_pObservedPtr)
- m_pObservedPtr->AddObservedPtr(this);
+ ObservedPtr() : m_pObservable(nullptr) {}
+ explicit ObservedPtr(T* pObservable) : m_pObservable(pObservable) {
+ if (m_pObservable)
+ m_pObservable->AddObservedPtr(this);
}
- ObservedPtr(const ObservedPtr& that) = delete;
+ ObservedPtr(const ObservedPtr& that) : ObservedPtr(that.Get()) {}
~ObservedPtr() {
- if (m_pObservedPtr)
- m_pObservedPtr->RemoveObservedPtr(this);
+ if (m_pObservable)
+ m_pObservable->RemoveObservedPtr(this);
}
- void Reset(T* pObservedPtr = nullptr) {
- if (m_pObservedPtr)
- m_pObservedPtr->RemoveObservedPtr(this);
- m_pObservedPtr = pObservedPtr;
- if (m_pObservedPtr)
- m_pObservedPtr->AddObservedPtr(this);
+ void Reset(T* pObservable = nullptr) {
+ if (m_pObservable)
+ m_pObservable->RemoveObservedPtr(this);
+ m_pObservable = pObservable;
+ if (m_pObservable)
+ m_pObservable->AddObservedPtr(this);
}
void OnDestroy() {
- ASSERT(m_pObservedPtr);
- m_pObservedPtr = nullptr;
+ ASSERT(m_pObservable);
+ m_pObservable = nullptr;
+ }
+ ObservedPtr& operator=(const ObservedPtr& that) {
+ Reset(that.Get());
+ return *this;
}
- ObservedPtr& operator=(const ObservedPtr& that) = delete;
bool operator==(const ObservedPtr& that) const {
- return m_pObservedPtr == that.m_pObservedPtr;
+ return m_pObservable == that.m_pObservable;
}
bool operator!=(const ObservedPtr& that) const { return !(*this == that); }
- explicit operator bool() const { return !!m_pObservedPtr; }
- T* Get() const { return m_pObservedPtr; }
- T& operator*() const { return *m_pObservedPtr; }
- T* operator->() const { return m_pObservedPtr; }
+ explicit operator bool() const { return !!m_pObservable; }
+ T* Get() const { return m_pObservable; }
+ T& operator*() const { return *m_pObservable; }
+ T* operator->() const { return m_pObservable; }
private:
- T* m_pObservedPtr;
+ T* m_pObservable;
};
CFX_Observable() {}
diff --git a/core/fxcrt/cfx_observable_unittest.cpp b/core/fxcrt/cfx_observable_unittest.cpp
index 15c76495cf..3c0fabb13e 100644
--- a/core/fxcrt/cfx_observable_unittest.cpp
+++ b/core/fxcrt/cfx_observable_unittest.cpp
@@ -5,6 +5,7 @@
#include "core/fxcrt/cfx_observable.h"
#include <utility>
+#include <vector>
#include "testing/fx_string_testhelpers.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -44,6 +45,77 @@ TEST(fxcrt, ObservePtrLivesShorter) {
EXPECT_EQ(0u, obs.ActiveObservedPtrs());
}
+TEST(fxcrt, ObserveCopyConstruct) {
+ PseudoObservable obs;
+ {
+ PseudoObservable::ObservedPtr ptr(&obs);
+ EXPECT_NE(nullptr, ptr.Get());
+ EXPECT_EQ(1u, obs.ActiveObservedPtrs());
+ {
+ PseudoObservable::ObservedPtr ptr2(ptr);
+ EXPECT_NE(nullptr, ptr2.Get());
+ EXPECT_EQ(2u, obs.ActiveObservedPtrs());
+ }
+ EXPECT_EQ(1u, obs.ActiveObservedPtrs());
+ }
+ EXPECT_EQ(0u, obs.ActiveObservedPtrs());
+}
+
+TEST(fxcrt, ObserveCopyAssign) {
+ PseudoObservable obs;
+ {
+ PseudoObservable::ObservedPtr ptr(&obs);
+ EXPECT_NE(nullptr, ptr.Get());
+ EXPECT_EQ(1u, obs.ActiveObservedPtrs());
+ {
+ PseudoObservable::ObservedPtr ptr2;
+ ptr2 = ptr;
+ EXPECT_NE(nullptr, ptr2.Get());
+ EXPECT_EQ(2u, obs.ActiveObservedPtrs());
+ }
+ EXPECT_EQ(1u, obs.ActiveObservedPtrs());
+ }
+ EXPECT_EQ(0u, obs.ActiveObservedPtrs());
+}
+
+TEST(fxcrt, ObserveVector) {
+ PseudoObservable obs;
+ {
+ std::vector<PseudoObservable::ObservedPtr> vec1;
+ std::vector<PseudoObservable::ObservedPtr> vec2;
+ vec1.emplace_back(&obs);
+ vec1.emplace_back(&obs);
+ EXPECT_NE(nullptr, vec1[0].Get());
+ EXPECT_NE(nullptr, vec1[1].Get());
+ EXPECT_EQ(2u, obs.ActiveObservedPtrs());
+ vec2 = vec1;
+ EXPECT_NE(nullptr, vec2[0].Get());
+ EXPECT_NE(nullptr, vec2[1].Get());
+ EXPECT_EQ(4u, obs.ActiveObservedPtrs());
+ vec1.clear();
+ EXPECT_EQ(2u, obs.ActiveObservedPtrs());
+ vec2.resize(10000);
+ EXPECT_EQ(2u, obs.ActiveObservedPtrs());
+ vec2.resize(0);
+ EXPECT_EQ(0u, obs.ActiveObservedPtrs());
+ }
+ EXPECT_EQ(0u, obs.ActiveObservedPtrs());
+}
+
+TEST(fxcrt, ObserveVectorAutoClear) {
+ std::vector<PseudoObservable::ObservedPtr> vec1;
+ {
+ PseudoObservable obs;
+ vec1.emplace_back(&obs);
+ vec1.emplace_back(&obs);
+ EXPECT_NE(nullptr, vec1[0].Get());
+ EXPECT_NE(nullptr, vec1[1].Get());
+ EXPECT_EQ(2u, obs.ActiveObservedPtrs());
+ }
+ EXPECT_EQ(nullptr, vec1[0].Get());
+ EXPECT_EQ(nullptr, vec1[1].Get());
+}
+
TEST(fxcrt, ObservePtrResetNull) {
PseudoObservable obs;
PseudoObservable::ObservedPtr ptr(&obs);