Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: base/observer_list.h

Issue 1059383003: Make sure observers are not nullptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ui/gfx/display_change_notifier_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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);
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);
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
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__
OLDNEW
« no previous file with comments | « no previous file | ui/gfx/display_change_notifier_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698