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

Tidy base::Scoped*Observation classes.

This addresses nits from https://crrev.com/c/2518028, and generally
tides up ScopedObservation and ScopedMultiSourceObservation.

Bug: 1145565
Change-Id: I6aa515b25eede775b17a21b34f688e3daf8410a7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2533899Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Commit-Queue: Sigurður Ásgeirsson <siggi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826833}
parent 902cdf5c
...@@ -15,33 +15,36 @@ ...@@ -15,33 +15,36 @@
namespace base { namespace base {
// ScopedMultiSourceObservation is used to keep track of the set of sources an // ScopedMultiSourceObservation is used to keep track of plural observation,
// object has attached itself to as an observer. // e.g. where an observer observes more than a single source.
// //
// For objects that observe only a single source, use base::ScopedObservation // Use base::ScopedObservation for objects that observe only a single source.
// rather than this class. This class and base::ScopedObservation replace // This class and base::ScopedObservation replace ScopedObserver.
// ScopedObserver.
// //
// When ScopedMultiSourceObservation is destroyed it removes the object as an // When ScopedMultiSourceObservation is destroyed, it removes the object as an
// observer from all sources it has been added to. // observer from all sources it has been added to.
// Basic example (as a member variable): // Basic example (as a member variable):
// //
// class MyFooObserver : public FooObserver { // class MyFooObserver : public FooObserver {
// ... // ...
// private: // private:
// ScopedMultiSourceObservation<Foo, FooObserver> observed_foo_{this}; // ScopedMultiSourceObservation<Foo, FooObserver> foo_observations_{this};
// }; // };
// //
// MyFooObserver::OnFooCreated(Foo* foo) {
// foo_observations_.AddObservation(foo);
// }
//
// For cases with methods not named AddObserver/RemoveObserver: // For cases with methods not named AddObserver/RemoveObserver:
// //
// class MyFooStateObserver : public FooStateObserver { // class MyFooStateObserver : public FooStateObserver {
// ... // ...
// private: // private:
// ScopedMultiSourceObservation<Foo, // ScopedMultiSourceObservation<Foo,
// FooStateObserver, // FooStateObserver,
// &Foo::AddStateObserver, // &Foo::AddStateObserver,
// &Foo::RemoveStateObserver> // &Foo::RemoveStateObserver>
// observed_foo_{this}; // foo_observations_{this};
// }; // };
template <class Source, template <class Source,
class Observer, class Observer,
...@@ -70,22 +73,27 @@ class ScopedMultiSourceObservation { ...@@ -70,22 +73,27 @@ class ScopedMultiSourceObservation {
(source->*RemoveObsFn)(observer_); (source->*RemoveObsFn)(observer_);
} }
// Remove the object passed to the constructor as an observer from all sources
// it's observing.
void RemoveAllObservations() { void RemoveAllObservations() {
for (Source* source : sources_) for (Source* source : sources_)
(source->*RemoveObsFn)(observer_); (source->*RemoveObsFn)(observer_);
sources_.clear(); sources_.clear();
} }
// Returns true if any source is being observed.
bool IsObservingAnySource() const { return !sources_.empty(); }
// Returns true if |source| is being observed.
bool IsObservingSource(Source* source) const { bool IsObservingSource(Source* source) const {
return base::Contains(sources_, source); return base::Contains(sources_, source);
} }
bool IsObservingAnySource() const { return !sources_.empty(); } // Returns the number of sources being observed.
size_t GetSourcesCount() const { return sources_.size(); } size_t GetSourcesCount() const { return sources_.size(); }
private: private:
Observer* observer_; Observer* const observer_;
std::vector<Source*> sources_; std::vector<Source*> sources_;
}; };
......
...@@ -11,7 +11,13 @@ ...@@ -11,7 +11,13 @@
namespace base { namespace base {
// ScopedObservation is used to keep track of a single observation. // ScopedObservation is used to keep track of singular observation, e.g.
// where an observer observes a single source only.
//
// Use base::ScopedMultiSourceObservation for objects that observe multiple
// sources. This class and base::ScopedMultiSourceObservation replace
// ScopedObserver.
//
// When ScopedObservation is destroyed, it removes the registered observation, // When ScopedObservation is destroyed, it removes the registered observation,
// if any. Basic example (as a member variable): // if any. Basic example (as a member variable):
// //
...@@ -21,19 +27,21 @@ namespace base { ...@@ -21,19 +27,21 @@ namespace base {
// ScopedObservation<Foo, FooObserver> foo_observation_{this}; // ScopedObservation<Foo, FooObserver> foo_observation_{this};
// }; // };
// //
// MyFooObserver::MyFooObserver(Foo* foo) {
// foo_observation_.Observe(foo);
// }
//
// For cases with methods not named AddObserver/RemoveObserver: // For cases with methods not named AddObserver/RemoveObserver:
// //
// class MyFooStateObserver : public FooStateObserver { // class MyFooStateObserver : public FooStateObserver {
// ... // ...
// private: // private:
// ScopedObservation<Foo, // ScopedObservation<Foo,
// FooStateObserver, // FooStateObserver,
// &Foo::AddStateObserver, // &Foo::AddStateObserver,
// &Foo::RemoveStateObserver> // &Foo::RemoveStateObserver>
// observed_foo_{this}; // foo_observation_{this};
// }; // };
//
// See also base::ScopedObserver to manage observations from multiple sources.
template <class Source, template <class Source,
class Observer, class Observer,
void (Source::*AddObsFn)(Observer*) = &Source::AddObserver, void (Source::*AddObsFn)(Observer*) = &Source::AddObserver,
......
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