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 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... | |
22 for (size_t i = 0; i < sources_.size(); ++i) | 22 for (size_t i = 0; i < sources_.size(); ++i) |
23 sources_[i]->RemoveObserver(observer_); | 23 sources_[i]->RemoveObserver(observer_); |
24 } | 24 } |
25 | 25 |
26 // Adds the object passed to the constructor as an observer on |source|. | 26 // Adds the object passed to the constructor as an observer on |source|. |
27 void Add(Source* source) { | 27 void Add(Source* source) { |
28 sources_.push_back(source); | 28 sources_.push_back(source); |
29 source->AddObserver(observer_); | 29 source->AddObserver(observer_); |
30 } | 30 } |
31 | 31 |
32 // Removse the object passed to the constructor as an observer from |source|. | 32 // Removse the object passed to the constructor as an observer from |source|. |
jar (doing other things)
2012/10/05 16:00:31
nit: typo: removse --> removes
jam
2012/10/05 16:02:46
Done.
| |
33 void Remove(Source* source) { | 33 void Remove(Source* source) { |
34 sources_.erase(std::find(sources_.begin(), sources_.end(), source)); | 34 sources_.erase(std::find(sources_.begin(), sources_.end(), source)); |
35 source->RemoveObserver(observer_); | 35 source->RemoveObserver(observer_); |
36 } | 36 } |
37 | 37 |
38 bool IsObserving(Source* source) const { | |
39 for (size_t i = 0; i < sources_.size(); ++i) { | |
40 if (sources_[i] == source) | |
41 return true; | |
42 } | |
43 return false; | |
44 } | |
45 | |
38 private: | 46 private: |
39 Observer* observer_; | 47 Observer* observer_; |
40 | 48 |
41 std::vector<Source*> sources_; | 49 std::vector<Source*> sources_; |
42 | 50 |
43 DISALLOW_COPY_AND_ASSIGN(ScopedObserver); | 51 DISALLOW_COPY_AND_ASSIGN(ScopedObserver); |
44 }; | 52 }; |
45 | 53 |
46 #endif // BASE_SCOPED_OBSERVER_H_ | 54 #endif // BASE_SCOPED_OBSERVER_H_ |
OLD | NEW |