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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 72 |
73 // Specifies that observers added while sending out notification are not | 73 // Specifies that observers added while sending out notification are not |
74 // notified. | 74 // notified. |
75 NOTIFY_EXISTING_ONLY | 75 NOTIFY_EXISTING_ONLY |
76 }; | 76 }; |
77 | 77 |
78 // An iterator class that can be used to access the list of observers. See | 78 // An iterator class that can be used to access the list of observers. See |
79 // also the FOR_EACH_OBSERVER macro defined below. | 79 // also the FOR_EACH_OBSERVER macro defined below. |
80 class Iterator { | 80 class Iterator { |
81 public: | 81 public: |
82 Iterator(ObserverListBase<ObserverType>& list); | 82 explicit Iterator(ObserverListBase<ObserverType>* list); |
83 ~Iterator(); | 83 ~Iterator(); |
84 ObserverType* GetNext(); | 84 ObserverType* GetNext(); |
85 | 85 |
86 private: | 86 private: |
87 base::WeakPtr<ObserverListBase<ObserverType> > list_; | 87 base::WeakPtr<ObserverListBase<ObserverType> > list_; |
88 size_t index_; | 88 size_t index_; |
89 size_t max_index_; | 89 size_t max_index_; |
90 }; | 90 }; |
91 | 91 |
92 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} | 92 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} |
(...skipping 26 matching lines...) Expand all Loading... |
119 int notify_depth_; | 119 int notify_depth_; |
120 NotificationType type_; | 120 NotificationType type_; |
121 | 121 |
122 friend class ObserverListBase::Iterator; | 122 friend class ObserverListBase::Iterator; |
123 | 123 |
124 DISALLOW_COPY_AND_ASSIGN(ObserverListBase); | 124 DISALLOW_COPY_AND_ASSIGN(ObserverListBase); |
125 }; | 125 }; |
126 | 126 |
127 template <class ObserverType> | 127 template <class ObserverType> |
128 ObserverListBase<ObserverType>::Iterator::Iterator( | 128 ObserverListBase<ObserverType>::Iterator::Iterator( |
129 ObserverListBase<ObserverType>& list) | 129 ObserverListBase<ObserverType>* list) |
130 : list_(list.AsWeakPtr()), | 130 : list_(list->AsWeakPtr()), |
131 index_(0), | 131 index_(0), |
132 max_index_(list.type_ == NOTIFY_ALL ? | 132 max_index_(list->type_ == NOTIFY_ALL ? std::numeric_limits<size_t>::max() |
133 std::numeric_limits<size_t>::max() : | 133 : list->observers_.size()) { |
134 list.observers_.size()) { | |
135 ++list_->notify_depth_; | 134 ++list_->notify_depth_; |
136 } | 135 } |
137 | 136 |
138 template <class ObserverType> | 137 template <class ObserverType> |
139 ObserverListBase<ObserverType>::Iterator::~Iterator() { | 138 ObserverListBase<ObserverType>::Iterator::~Iterator() { |
140 if (list_.get() && --list_->notify_depth_ == 0) | 139 if (list_.get() && --list_->notify_depth_ == 0) |
141 list_->Compact(); | 140 list_->Compact(); |
142 } | 141 } |
143 | 142 |
144 template <class ObserverType> | 143 template <class ObserverType> |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 ObserverListBase<ObserverType>::Compact(); | 220 ObserverListBase<ObserverType>::Compact(); |
222 DCHECK_EQ(ObserverListBase<ObserverType>::size(), 0U); | 221 DCHECK_EQ(ObserverListBase<ObserverType>::size(), 0U); |
223 } | 222 } |
224 } | 223 } |
225 | 224 |
226 bool might_have_observers() const { | 225 bool might_have_observers() const { |
227 return ObserverListBase<ObserverType>::size() != 0; | 226 return ObserverListBase<ObserverType>::size() != 0; |
228 } | 227 } |
229 }; | 228 }; |
230 | 229 |
231 #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \ | 230 #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \ |
232 do { \ | 231 do { \ |
233 if ((observer_list).might_have_observers()) { \ | 232 if ((observer_list).might_have_observers()) { \ |
234 ObserverListBase<ObserverType>::Iterator \ | 233 ObserverListBase<ObserverType>::Iterator it_inside_observer_macro( \ |
235 it_inside_observer_macro(observer_list); \ | 234 &observer_list); \ |
236 ObserverType* obs; \ | 235 ObserverType* obs; \ |
237 while ((obs = it_inside_observer_macro.GetNext()) != NULL) \ | 236 while ((obs = it_inside_observer_macro.GetNext()) != NULL) \ |
238 obs->func; \ | 237 obs->func; \ |
239 } \ | 238 } \ |
240 } while (0) | 239 } while (0) |
241 | 240 |
242 #endif // BASE_OBSERVER_LIST_H__ | 241 #endif // BASE_OBSERVER_LIST_H__ |
OLD | NEW |