OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_SCOPED_OBSERVER_H_ | 5 #ifndef BASE_SCOPED_OBSERVER_H_ |
6 #define BASE_SCOPED_OBSERVER_H_ | 6 #define BASE_SCOPED_OBSERVER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 | 13 |
14 // ScopedObserver is used to keep track of the set of sources an object has | 14 // ScopedObserver is used to keep track of the set of sources an object has |
15 // attached itself to as an observer. When ScopedObserver is destroyed it | 15 // attached itself to as an observer. When ScopedObserver is destroyed it |
16 // removes the object as an observer from all sources it has been added to. | 16 // removes the object as an observer from all sources it has been added to. |
17 template <class Source, class Observer> | 17 template <class Source, class Observer> |
18 class ScopedObserver { | 18 class ScopedObserver { |
19 public: | 19 public: |
20 explicit ScopedObserver(Observer* observer) : observer_(observer) {} | 20 explicit ScopedObserver(Observer* observer) : observer_(observer) {} |
21 | 21 |
| 22 // Creates with an initial |source|. |
| 23 ScopedObserver(Observer* observer, Source* source) : observer_(observer) { |
| 24 Add(source); |
| 25 } |
| 26 |
22 ~ScopedObserver() { | 27 ~ScopedObserver() { |
23 for (size_t i = 0; i < sources_.size(); ++i) | 28 for (size_t i = 0; i < sources_.size(); ++i) |
24 sources_[i]->RemoveObserver(observer_); | 29 sources_[i]->RemoveObserver(observer_); |
25 } | 30 } |
26 | 31 |
27 // Adds the object passed to the constructor as an observer on |source|. | 32 // Adds the object passed to the constructor as an observer on |source|. |
28 void Add(Source* source) { | 33 void Add(Source* source) { |
29 sources_.push_back(source); | 34 sources_.push_back(source); |
30 source->AddObserver(observer_); | 35 source->AddObserver(observer_); |
31 } | 36 } |
32 | 37 |
33 // Removse the object passed to the constructor as an observer from |source|. | 38 // Removes the object passed to the constructor as an observer from |source|. |
34 void Remove(Source* source) { | 39 void Remove(Source* source) { |
35 sources_.erase(std::find(sources_.begin(), sources_.end(), source)); | 40 sources_.erase(std::find(sources_.begin(), sources_.end(), source)); |
36 source->RemoveObserver(observer_); | 41 source->RemoveObserver(observer_); |
37 } | 42 } |
38 | 43 |
39 private: | 44 private: |
40 Observer* observer_; | 45 Observer* observer_; |
41 | 46 |
42 std::vector<Source*> sources_; | 47 std::vector<Source*> sources_; |
43 | 48 |
44 DISALLOW_COPY_AND_ASSIGN(ScopedObserver); | 49 DISALLOW_COPY_AND_ASSIGN(ScopedObserver); |
45 }; | 50 }; |
46 | 51 |
47 #endif // BASE_SCOPED_OBSERVER_H_ | 52 #endif // BASE_SCOPED_OBSERVER_H_ |
OLD | NEW |