OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 ASH_SHELF_SCOPED_OBSERVER_WITH_DUPLICATED_SOURCES_H_ | 5 #ifndef ASH_SHELF_SCOPED_OBSERVER_WITH_DUPLICATED_SOURCES_H_ |
6 #define ASH_SHELF_SCOPED_OBSERVER_WITH_DUPLICATED_SOURCES_H_ | 6 #define ASH_SHELF_SCOPED_OBSERVER_WITH_DUPLICATED_SOURCES_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 // |source|. | 35 // |source|. |
36 void Add(Source* source) { | 36 void Add(Source* source) { |
37 if (counted_sources_.find(source) == counted_sources_.end()) | 37 if (counted_sources_.find(source) == counted_sources_.end()) |
38 source->AddObserver(observer_); | 38 source->AddObserver(observer_); |
39 counted_sources_[source]++; | 39 counted_sources_[source]++; |
40 } | 40 } |
41 | 41 |
42 // Only remove the object passed to the constructor as an observer from | 42 // Only remove the object passed to the constructor as an observer from |
43 // |source| when Remove() is invoked the same number of times as Add(). | 43 // |source| when Remove() is invoked the same number of times as Add(). |
44 void Remove(Source* source) { | 44 void Remove(Source* source) { |
45 typename SourceToAddCountMap::iterator iter = | 45 typename SourceToAddCountMap::iterator iter = counted_sources_.find(source); |
46 counted_sources_.find(source); | |
47 DCHECK(iter != counted_sources_.end() && iter->second > 0); | 46 DCHECK(iter != counted_sources_.end() && iter->second > 0); |
48 | 47 |
49 if (--iter->second == 0) { | 48 if (--iter->second == 0) { |
50 counted_sources_.erase(source); | 49 counted_sources_.erase(source); |
51 source->RemoveObserver(observer_); | 50 source->RemoveObserver(observer_); |
52 } | 51 } |
53 } | 52 } |
54 | 53 |
55 bool IsObserving(Source* source) const { | 54 bool IsObserving(Source* source) const { |
56 return counted_sources_.find(source) != counted_sources_.end(); | 55 return counted_sources_.find(source) != counted_sources_.end(); |
57 } | 56 } |
58 | 57 |
59 private: | 58 private: |
60 typedef std::map<Source*, int> SourceToAddCountMap; | 59 typedef std::map<Source*, int> SourceToAddCountMap; |
61 | 60 |
62 Observer* observer_; | 61 Observer* observer_; |
63 | 62 |
64 // Map Source and its adding count. | 63 // Map Source and its adding count. |
65 std::map<Source*, int> counted_sources_; | 64 std::map<Source*, int> counted_sources_; |
66 | 65 |
67 DISALLOW_COPY_AND_ASSIGN(ScopedObserverWithDuplicatedSources); | 66 DISALLOW_COPY_AND_ASSIGN(ScopedObserverWithDuplicatedSources); |
68 }; | 67 }; |
69 | 68 |
70 #endif // ASH_SHELF_SCOPED_OBSERVER_WITH_DUPLICATED_SOURCES_H_ | 69 #endif // ASH_SHELF_SCOPED_OBSERVER_WITH_DUPLICATED_SOURCES_H_ |
OLD | NEW |