[email protected] | 5924a0b | 2012-04-27 17:02:28 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be | ||||
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 28f57b3 | 2012-06-22 21:47:30 | [diff] [blame] | 5 | #ifndef BASE_SCOPED_OBSERVER_H_ |
6 | #define BASE_SCOPED_OBSERVER_H_ | ||||
[email protected] | 5924a0b | 2012-04-27 17:02:28 | [diff] [blame] | 7 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
9 | |||||
[email protected] | 5924a0b | 2012-04-27 17:02:28 | [diff] [blame] | 10 | #include <algorithm> |
11 | #include <vector> | ||||
12 | |||||
scheib | cc12a6a | 2014-10-27 22:17:32 | [diff] [blame] | 13 | #include "base/logging.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 14 | #include "base/macros.h" |
thestig | 6c335d4 | 2015-12-07 18:25:34 | [diff] [blame] | 15 | #include "base/stl_util.h" |
[email protected] | 5924a0b | 2012-04-27 17:02:28 | [diff] [blame] | 16 | |
[email protected] | 5924a0b | 2012-04-27 17:02:28 | [diff] [blame] | 17 | // ScopedObserver is used to keep track of the set of sources an object has |
18 | // attached itself to as an observer. When ScopedObserver is destroyed it | ||||
19 | // removes the object as an observer from all sources it has been added to. | ||||
20 | template <class Source, class Observer> | ||||
21 | class ScopedObserver { | ||||
22 | public: | ||||
23 | explicit ScopedObserver(Observer* observer) : observer_(observer) {} | ||||
24 | |||||
25 | ~ScopedObserver() { | ||||
[email protected] | 50703fc | 2014-04-08 04:01:06 | [diff] [blame] | 26 | RemoveAll(); |
[email protected] | 5924a0b | 2012-04-27 17:02:28 | [diff] [blame] | 27 | } |
28 | |||||
29 | // Adds the object passed to the constructor as an observer on |source|. | ||||
30 | void Add(Source* source) { | ||||
31 | sources_.push_back(source); | ||||
32 | source->AddObserver(observer_); | ||||
33 | } | ||||
34 | |||||
[email protected] | d725b2c | 2012-10-05 16:01:57 | [diff] [blame] | 35 | // Remove the object passed to the constructor as an observer from |source|. |
[email protected] | 5924a0b | 2012-04-27 17:02:28 | [diff] [blame] | 36 | void Remove(Source* source) { |
scheib | cc12a6a | 2014-10-27 22:17:32 | [diff] [blame] | 37 | auto it = std::find(sources_.begin(), sources_.end(), source); |
38 | DCHECK(it != sources_.end()); | ||||
39 | sources_.erase(it); | ||||
[email protected] | 5924a0b | 2012-04-27 17:02:28 | [diff] [blame] | 40 | source->RemoveObserver(observer_); |
41 | } | ||||
42 | |||||
[email protected] | 50703fc | 2014-04-08 04:01:06 | [diff] [blame] | 43 | void RemoveAll() { |
44 | for (size_t i = 0; i < sources_.size(); ++i) | ||||
45 | sources_[i]->RemoveObserver(observer_); | ||||
46 | sources_.clear(); | ||||
47 | } | ||||
48 | |||||
[email protected] | d725b2c | 2012-10-05 16:01:57 | [diff] [blame] | 49 | bool IsObserving(Source* source) const { |
skyostil | 52f72dda | 2016-08-12 19:44:49 | [diff] [blame] | 50 | return base::ContainsValue(sources_, source); |
[email protected] | d725b2c | 2012-10-05 16:01:57 | [diff] [blame] | 51 | } |
52 | |||||
hanxi | 9bd85fa | 2015-05-05 19:55:00 | [diff] [blame] | 53 | bool IsObservingSources() const { return !sources_.empty(); } |
54 | |||||
[email protected] | 5924a0b | 2012-04-27 17:02:28 | [diff] [blame] | 55 | private: |
56 | Observer* observer_; | ||||
57 | |||||
58 | std::vector<Source*> sources_; | ||||
59 | |||||
60 | DISALLOW_COPY_AND_ASSIGN(ScopedObserver); | ||||
61 | }; | ||||
62 | |||||
[email protected] | 28f57b3 | 2012-06-22 21:47:30 | [diff] [blame] | 63 | #endif // BASE_SCOPED_OBSERVER_H_ |