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 <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 }; | 85 }; |
86 | 86 |
87 // An iterator class that can be used to access the list of observers. | 87 // An iterator class that can be used to access the list of observers. |
88 template <class ContainerType> | 88 template <class ContainerType> |
89 class Iter { | 89 class Iter { |
90 public: | 90 public: |
91 Iter(); | 91 Iter(); |
92 explicit Iter(ContainerType* list); | 92 explicit Iter(ContainerType* list); |
93 ~Iter(); | 93 ~Iter(); |
94 | 94 |
95 // Deprecated. | |
96 ObserverType* GetNext(); | |
97 | |
98 // A workaround for C2244. MSVC requires fully qualified type name for | 95 // A workaround for C2244. MSVC requires fully qualified type name for |
99 // return type on a function definition to match a function declaration. | 96 // return type on a function definition to match a function declaration. |
100 using ThisType = | 97 using ThisType = |
101 typename ObserverListBase<ObserverType>::template Iter<ContainerType>; | 98 typename ObserverListBase<ObserverType>::template Iter<ContainerType>; |
102 | 99 |
103 bool operator==(const Iter& other) const; | 100 bool operator==(const Iter& other) const; |
104 bool operator!=(const Iter& other) const; | 101 bool operator!=(const Iter& other) const; |
105 ThisType& operator++(); | 102 ThisType& operator++(); |
106 ObserverType* operator->() const; | 103 ObserverType* operator->() const; |
107 ObserverType& operator*() const; | 104 ObserverType& operator*() const; |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 void ObserverListBase<ObserverType>::Iter<ContainerType>::EnsureValidIndex() { | 263 void ObserverListBase<ObserverType>::Iter<ContainerType>::EnsureValidIndex() { |
267 if (!list_) | 264 if (!list_) |
268 return; | 265 return; |
269 | 266 |
270 size_t max_index = clamped_max_index(); | 267 size_t max_index = clamped_max_index(); |
271 while (index_ < max_index && !list_->observers_[index_]) | 268 while (index_ < max_index && !list_->observers_[index_]) |
272 ++index_; | 269 ++index_; |
273 } | 270 } |
274 | 271 |
275 template <class ObserverType> | 272 template <class ObserverType> |
276 template <class ContainerType> | |
277 ObserverType* ObserverListBase<ObserverType>::Iter<ContainerType>::GetNext() { | |
278 EnsureValidIndex(); | |
279 ObserverType* current = GetCurrent(); | |
280 operator++(); | |
281 return current; | |
282 } | |
283 | |
284 template <class ObserverType> | |
285 void ObserverListBase<ObserverType>::AddObserver(ObserverType* obs) { | 273 void ObserverListBase<ObserverType>::AddObserver(ObserverType* obs) { |
286 DCHECK(obs); | 274 DCHECK(obs); |
287 if (ContainsValue(observers_, obs)) { | 275 if (ContainsValue(observers_, obs)) { |
288 NOTREACHED() << "Observers can only be added once!"; | 276 NOTREACHED() << "Observers can only be added once!"; |
289 return; | 277 return; |
290 } | 278 } |
291 observers_.push_back(obs); | 279 observers_.push_back(obs); |
292 } | 280 } |
293 | 281 |
294 template <class ObserverType> | 282 template <class ObserverType> |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 // Deprecated. Use the range-based for loop instead. | 347 // Deprecated. Use the range-based for loop instead. |
360 #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \ | 348 #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \ |
361 do { \ | 349 do { \ |
362 for (ObserverType & o : observer_list) \ | 350 for (ObserverType & o : observer_list) \ |
363 o.func; \ | 351 o.func; \ |
364 } while (0) | 352 } while (0) |
365 | 353 |
366 } // namespace base | 354 } // namespace base |
367 | 355 |
368 #endif // BASE_OBSERVER_LIST_H_ | 356 #endif // BASE_OBSERVER_LIST_H_ |
OLD | NEW |