Commit c7a6ac2d authored by Sigurdur Asgeirsson's avatar Sigurdur Asgeirsson Committed by Commit Bot

Replace ScopedObservation::RemoveObservation with Reset().

This makes the class simpler to use in the common case.

Bug: 1145565
Change-Id: Ib6aab62eebf4e402e364687d4224d0f8d7077e6a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2549534Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Commit-Queue: Sigurður Ásgeirsson <siggi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#830231}
parent 287418da
...@@ -51,10 +51,7 @@ class ScopedObservation { ...@@ -51,10 +51,7 @@ class ScopedObservation {
explicit ScopedObservation(Observer* observer) : observer_(observer) {} explicit ScopedObservation(Observer* observer) : observer_(observer) {}
ScopedObservation(const ScopedObservation&) = delete; ScopedObservation(const ScopedObservation&) = delete;
ScopedObservation& operator=(const ScopedObservation&) = delete; ScopedObservation& operator=(const ScopedObservation&) = delete;
~ScopedObservation() { ~ScopedObservation() { Reset(); }
if (IsObserving())
RemoveObservation();
}
// Adds the object passed to the constructor as an observer on |source|. // Adds the object passed to the constructor as an observer on |source|.
// IsObserving() must be false. // IsObserving() must be false.
...@@ -64,11 +61,11 @@ class ScopedObservation { ...@@ -64,11 +61,11 @@ class ScopedObservation {
(source_->*AddObsFn)(observer_); (source_->*AddObsFn)(observer_);
} }
// Remove the object passed to the constructor as an observer from |source|. // Remove the object passed to the constructor as an observer from |source_|
void RemoveObservation() { // if currently observing. Does nothing otherwise.
DCHECK_NE(source_, nullptr); void Reset() {
(source_->*RemoveObsFn)(observer_); if (IsObserving())
source_ = nullptr; RemoveObservation();
} }
// Returns true if any source is being observed. // Returns true if any source is being observed.
...@@ -80,6 +77,14 @@ class ScopedObservation { ...@@ -80,6 +77,14 @@ class ScopedObservation {
return source_ == source; return source_ == source;
} }
// Remove the object passed to the constructor as an observer from |source_|.
// This method is DEPRECATED, please use Reset().
void RemoveObservation() {
DCHECK_NE(source_, nullptr);
(source_->*RemoveObsFn)(observer_);
source_ = nullptr;
}
private: private:
Observer* const observer_; Observer* const observer_;
......
...@@ -77,6 +77,25 @@ TEST(ScopedObservationTest, RemoveObservation) { ...@@ -77,6 +77,25 @@ TEST(ScopedObservationTest, RemoveObservation) {
EXPECT_EQ(0u, s1.num_observers()); EXPECT_EQ(0u, s1.num_observers());
} }
TEST(ScopedObservationTest, Reset) {
TestSource s1;
TestSourceObserver o1;
TestScopedObservation obs(&o1);
EXPECT_EQ(0u, s1.num_observers());
obs.Reset();
obs.Observe(&s1);
EXPECT_EQ(1u, s1.num_observers());
EXPECT_TRUE(s1.HasObserver(&o1));
obs.Reset();
EXPECT_EQ(0u, s1.num_observers());
// Safe to call with no observation.
obs.Reset();
EXPECT_EQ(0u, s1.num_observers());
}
TEST(ScopedObservationTest, IsObserving) { TEST(ScopedObservationTest, IsObserving) {
TestSource s1; TestSource s1;
TestSourceObserver o1; TestSourceObserver o1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment