OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_OBSERVER_LIST_H__ | 5 #ifndef BASE_OBSERVER_LIST_H__ |
6 #define BASE_OBSERVER_LIST_H__ | 6 #define BASE_OBSERVER_LIST_H__ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <limits> | 10 #include <limits> |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 private: | 104 private: |
105 ObserverListBase<ObserverType>& list_; | 105 ObserverListBase<ObserverType>& list_; |
106 size_t index_; | 106 size_t index_; |
107 size_t max_index_; | 107 size_t max_index_; |
108 }; | 108 }; |
109 | 109 |
110 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} | 110 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} |
111 explicit ObserverListBase(NotificationType type) | 111 explicit ObserverListBase(NotificationType type) |
112 : notify_depth_(0), type_(type) {} | 112 : notify_depth_(0), type_(type) {} |
113 | 113 |
114 // Add an observer to the list. | 114 // Add an observer to the list. An observer should not be added to |
| 115 // the same list more than once. |
115 void AddObserver(ObserverType* obs) { | 116 void AddObserver(ObserverType* obs) { |
116 DCHECK(find(observers_.begin(), observers_.end(), obs) == observers_.end()) | 117 if (std::find(observers_.begin(), observers_.end(), obs) |
117 << "Observers can only be added once!"; | 118 != observers_.end()) { |
| 119 NOTREACHED() << "Observers can only be added once!"; |
| 120 return; |
| 121 } |
118 observers_.push_back(obs); | 122 observers_.push_back(obs); |
119 } | 123 } |
120 | 124 |
121 // Remove an observer from the list. | 125 // Remove an observer from the list if it is in the list. |
122 void RemoveObserver(ObserverType* obs) { | 126 void RemoveObserver(ObserverType* obs) { |
123 typename ListType::iterator it = | 127 typename ListType::iterator it = |
124 std::find(observers_.begin(), observers_.end(), obs); | 128 std::find(observers_.begin(), observers_.end(), obs); |
125 if (it != observers_.end()) { | 129 if (it != observers_.end()) { |
126 if (notify_depth_) { | 130 if (notify_depth_) { |
127 *it = 0; | 131 *it = 0; |
128 } else { | 132 } else { |
129 observers_.erase(it); | 133 observers_.erase(it); |
130 } | 134 } |
131 } | 135 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 203 |
200 #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \ | 204 #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \ |
201 do { \ | 205 do { \ |
202 ObserverListBase<ObserverType>::Iterator it(observer_list); \ | 206 ObserverListBase<ObserverType>::Iterator it(observer_list); \ |
203 ObserverType* obs; \ | 207 ObserverType* obs; \ |
204 while ((obs = it.GetNext()) != NULL) \ | 208 while ((obs = it.GetNext()) != NULL) \ |
205 obs->func; \ | 209 obs->func; \ |
206 } while (0) | 210 } while (0) |
207 | 211 |
208 #endif // BASE_OBSERVER_LIST_H__ | 212 #endif // BASE_OBSERVER_LIST_H__ |
OLD | NEW |