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 {
explicit ScopedObservation(Observer* observer) : observer_(observer) {}
ScopedObservation(const ScopedObservation&) = delete;
ScopedObservation& operator=(const ScopedObservation&) = delete;
~ScopedObservation() {
if (IsObserving())
RemoveObservation();
}
~ScopedObservation() { Reset(); }
// Adds the object passed to the constructor as an observer on |source|.
// IsObserving() must be false.
......@@ -64,11 +61,11 @@ class ScopedObservation {
(source_->*AddObsFn)(observer_);
}
// Remove the object passed to the constructor as an observer from |source|.
void RemoveObservation() {
DCHECK_NE(source_, nullptr);
(source_->*RemoveObsFn)(observer_);
source_ = nullptr;
// Remove the object passed to the constructor as an observer from |source_|
// if currently observing. Does nothing otherwise.
void Reset() {
if (IsObserving())
RemoveObservation();
}
// Returns true if any source is being observed.
......@@ -80,6 +77,14 @@ class ScopedObservation {
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:
Observer* const observer_;
......
......@@ -77,6 +77,25 @@ TEST(ScopedObservationTest, RemoveObservation) {
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) {
TestSource s1;
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