OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <limits> | 9 #include <limits> |
10 #include <vector> | 10 #include <vector> |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 ListType& observers = list_->observers_; | 147 ListType& observers = list_->observers_; |
148 // Advance if the current element is null | 148 // Advance if the current element is null |
149 size_t max_index = std::min(max_index_, observers.size()); | 149 size_t max_index = std::min(max_index_, observers.size()); |
150 while (index_ < max_index && !observers[index_]) | 150 while (index_ < max_index && !observers[index_]) |
151 ++index_; | 151 ++index_; |
152 return index_ < max_index ? observers[index_++] : NULL; | 152 return index_ < max_index ? observers[index_++] : NULL; |
153 } | 153 } |
154 | 154 |
155 template <class ObserverType> | 155 template <class ObserverType> |
156 void ObserverListBase<ObserverType>::AddObserver(ObserverType* obs) { | 156 void ObserverListBase<ObserverType>::AddObserver(ObserverType* obs) { |
157 DCHECK(obs); | |
oshima
2015/04/03 23:09:28
This will most likely lead to crash, so maybe we w
danakj
2015/04/06 17:18:47
DCHECK is right. You can CHECK with a TODO and bug
| |
157 if (std::find(observers_.begin(), observers_.end(), obs) | 158 if (std::find(observers_.begin(), observers_.end(), obs) |
158 != observers_.end()) { | 159 != observers_.end()) { |
159 NOTREACHED() << "Observers can only be added once!"; | 160 NOTREACHED() << "Observers can only be added once!"; |
160 return; | 161 return; |
161 } | 162 } |
162 observers_.push_back(obs); | 163 observers_.push_back(obs); |
163 } | 164 } |
164 | 165 |
165 template <class ObserverType> | 166 template <class ObserverType> |
166 void ObserverListBase<ObserverType>::RemoveObserver(ObserverType* obs) { | 167 void ObserverListBase<ObserverType>::RemoveObserver(ObserverType* obs) { |
168 DCHECK(obs); | |
danakj
2015/04/06 17:18:47
do we care about removing null? we don't seem to c
| |
167 typename ListType::iterator it = | 169 typename ListType::iterator it = |
168 std::find(observers_.begin(), observers_.end(), obs); | 170 std::find(observers_.begin(), observers_.end(), obs); |
169 if (it != observers_.end()) { | 171 if (it != observers_.end()) { |
170 if (notify_depth_) { | 172 if (notify_depth_) { |
171 *it = 0; | 173 *it = 0; |
172 } else { | 174 } else { |
173 observers_.erase(it); | 175 observers_.erase(it); |
174 } | 176 } |
175 } | 177 } |
176 } | 178 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 if ((observer_list).might_have_observers()) { \ | 234 if ((observer_list).might_have_observers()) { \ |
233 ObserverListBase<ObserverType>::Iterator it_inside_observer_macro( \ | 235 ObserverListBase<ObserverType>::Iterator it_inside_observer_macro( \ |
234 &observer_list); \ | 236 &observer_list); \ |
235 ObserverType* obs; \ | 237 ObserverType* obs; \ |
236 while ((obs = it_inside_observer_macro.GetNext()) != NULL) \ | 238 while ((obs = it_inside_observer_macro.GetNext()) != NULL) \ |
237 obs->func; \ | 239 obs->func; \ |
238 } \ | 240 } \ |
239 } while (0) | 241 } while (0) |
240 | 242 |
241 #endif // BASE_OBSERVER_LIST_H__ | 243 #endif // BASE_OBSERVER_LIST_H__ |
OLD | NEW |