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 28 matching lines...) Expand all Loading... | |
39 // | 39 // |
40 // void AddObserver(Observer* obs) { | 40 // void AddObserver(Observer* obs) { |
41 // observer_list_.AddObserver(obs); | 41 // observer_list_.AddObserver(obs); |
42 // } | 42 // } |
43 // | 43 // |
44 // void RemoveObserver(Observer* obs) { | 44 // void RemoveObserver(Observer* obs) { |
45 // observer_list_.RemoveObserver(obs); | 45 // observer_list_.RemoveObserver(obs); |
46 // } | 46 // } |
47 // | 47 // |
48 // void NotifyFoo() { | 48 // void NotifyFoo() { |
49 // for (auto& observer : observer_list_) | |
50 // observer.OnFoo(this); | |
51 // } | |
52 // | |
53 // void NotifyBar(int x, int y) { | |
54 // for (FooList::iterator i = observer_list.begin(), | |
55 // e = observer_list.end(); i != e; ++i) | |
56 // i->OnBar(this, x, y); | |
57 // } | |
58 // | |
59 // void NotifyFooDeprecated() { | |
49 // FOR_EACH_OBSERVER(Observer, observer_list_, OnFoo(this)); | 60 // FOR_EACH_OBSERVER(Observer, observer_list_, OnFoo(this)); |
50 // } | 61 // } |
51 // | 62 // |
52 // void NotifyBar(int x, int y) { | 63 // void NotifyBarDeprecated(int x, int y) { |
53 // FOR_EACH_OBSERVER(Observer, observer_list_, OnBar(this, x, y)); | 64 // FOR_EACH_OBSERVER(Observer, observer_list_, OnBar(this, x, y)); |
54 // } | 65 // } |
55 // | 66 // |
56 // private: | 67 // private: |
57 // base::ObserverList<Observer> observer_list_; | 68 // base::ObserverList<Observer> observer_list_; |
58 // }; | 69 // }; |
59 // | 70 // |
60 // | 71 // |
61 /////////////////////////////////////////////////////////////////////////////// | 72 /////////////////////////////////////////////////////////////////////////////// |
62 | 73 |
(...skipping 10 matching lines...) Expand all Loading... | |
73 enum NotificationType { | 84 enum NotificationType { |
74 // Specifies that any observers added during notification are notified. | 85 // Specifies that any observers added during notification are notified. |
75 // This is the default type if non type is provided to the constructor. | 86 // This is the default type if non type is provided to the constructor. |
76 NOTIFY_ALL, | 87 NOTIFY_ALL, |
77 | 88 |
78 // Specifies that observers added while sending out notification are not | 89 // Specifies that observers added while sending out notification are not |
79 // notified. | 90 // notified. |
80 NOTIFY_EXISTING_ONLY | 91 NOTIFY_EXISTING_ONLY |
81 }; | 92 }; |
82 | 93 |
83 // An iterator class that can be used to access the list of observers. See | 94 // An iterator class that can be used to access the list of observers. |
84 // also the FOR_EACH_OBSERVER macro defined below. | 95 template <class ContainerType> |
85 class Iterator { | 96 class Iter { |
86 public: | 97 public: |
87 explicit Iterator(ObserverListBase<ObserverType>* list); | 98 Iter(); |
88 ~Iterator(); | 99 explicit Iter(ContainerType* list); |
100 ~Iter(); | |
101 | |
89 ObserverType* GetNext(); | 102 ObserverType* GetNext(); |
90 | 103 |
104 ObserverType* GetCurrent() const; | |
105 void Advance(); | |
106 | |
107 // A workaround for C2244. MSVC requires fully qualified type name for | |
108 // return type on a function definition to match a function declaration. | |
109 using ThisType = | |
110 typename ObserverListBase<ObserverType>::template Iter<ContainerType>; | |
111 | |
112 bool operator!=(const Iter& other) const; | |
113 ThisType& operator++(); | |
114 ThisType operator++(int); | |
dcheng
2016/09/16 21:44:17
Do we need to provide both prefix and postfix vers
| |
115 ObserverType* operator->() const; | |
116 ObserverType& operator*() const; | |
117 | |
91 private: | 118 private: |
92 WeakPtr<ObserverListBase<ObserverType>> list_; | 119 size_t GetNonNullIndex() const; |
120 | |
121 size_t checked_max_index() const { | |
122 return std::min(max_index_, list_->observers_.size()); | |
123 } | |
124 WeakPtr<ContainerType> list_; | |
93 size_t index_; | 125 size_t index_; |
94 size_t max_index_; | 126 size_t max_index_; |
95 }; | 127 }; |
96 | 128 |
129 using Iterator = Iter<ObserverListBase<ObserverType>>; | |
130 | |
131 using iterator = Iter<ObserverListBase<ObserverType>>; | |
132 iterator begin() { return iterator(this); } | |
133 iterator end() { return iterator(); } | |
134 | |
135 using const_iterator = Iter<const ObserverListBase<ObserverType>>; | |
136 const_iterator begin() const { return const_iterator(this); } | |
137 const_iterator end() const { return const_iterator(); } | |
138 | |
97 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} | 139 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} |
98 explicit ObserverListBase(NotificationType type) | 140 explicit ObserverListBase(NotificationType type) |
99 : notify_depth_(0), type_(type) {} | 141 : notify_depth_(0), type_(type) {} |
100 | 142 |
101 // Add an observer to the list. An observer should not be added to | 143 // Add an observer to the list. An observer should not be added to |
102 // the same list more than once. | 144 // the same list more than once. |
103 void AddObserver(ObserverType* obs); | 145 void AddObserver(ObserverType* obs); |
104 | 146 |
105 // Remove an observer from the list if it is in the list. | 147 // Remove an observer from the list if it is in the list. |
106 void RemoveObserver(ObserverType* obs); | 148 void RemoveObserver(ObserverType* obs); |
107 | 149 |
108 // Determine whether a particular observer is in the list. | 150 // Determine whether a particular observer is in the list. |
109 bool HasObserver(const ObserverType* observer) const; | 151 bool HasObserver(const ObserverType* observer) const; |
110 | 152 |
111 void Clear(); | 153 void Clear(); |
112 | 154 |
113 protected: | 155 protected: |
114 size_t size() const { return observers_.size(); } | 156 size_t size() const { return observers_.size(); } |
115 | 157 |
116 void Compact(); | 158 // Defragments the list despite const. |
159 void Compact() const; | |
117 | 160 |
118 private: | 161 private: |
119 friend class ObserverListThreadSafe<ObserverType>; | 162 friend class ObserverListThreadSafe<ObserverType>; |
120 | 163 |
121 typedef std::vector<ObserverType*> ListType; | 164 typedef std::vector<ObserverType*> ListType; |
122 | 165 |
123 ListType observers_; | 166 ListType observers_; |
124 int notify_depth_; | 167 mutable int notify_depth_; |
125 NotificationType type_; | 168 NotificationType type_; |
126 | 169 |
127 friend class ObserverListBase::Iterator; | 170 template <class ContainerType> |
171 friend class Iter; | |
128 | 172 |
129 DISALLOW_COPY_AND_ASSIGN(ObserverListBase); | 173 DISALLOW_COPY_AND_ASSIGN(ObserverListBase); |
130 }; | 174 }; |
131 | 175 |
132 template <class ObserverType> | 176 template <class ObserverType> |
133 ObserverListBase<ObserverType>::Iterator::Iterator( | 177 template <class ContainerType> |
134 ObserverListBase<ObserverType>* list) | 178 ObserverListBase<ObserverType>::Iter<ContainerType>::Iter() |
135 : list_(list->AsWeakPtr()), | 179 : index_(), max_index_() {} |
180 | |
181 template <class ObserverType> | |
182 template <class ContainerType> | |
183 ObserverListBase<ObserverType>::Iter<ContainerType>::Iter(ContainerType* list) | |
184 : list_(const_cast<ObserverListBase<ObserverType>*>(list)->AsWeakPtr()), | |
136 index_(0), | 185 index_(0), |
137 max_index_(list->type_ == NOTIFY_ALL ? std::numeric_limits<size_t>::max() | 186 max_index_(list->type_ == NOTIFY_ALL ? std::numeric_limits<size_t>::max() |
138 : list->observers_.size()) { | 187 : list->observers_.size()) { |
139 ++list_->notify_depth_; | 188 ++list_->notify_depth_; |
140 } | 189 } |
141 | 190 |
142 template <class ObserverType> | 191 template <class ObserverType> |
143 ObserverListBase<ObserverType>::Iterator::~Iterator() { | 192 template <class ContainerType> |
193 ObserverListBase<ObserverType>::Iter<ContainerType>::~Iter() { | |
144 if (list_.get() && --list_->notify_depth_ == 0) | 194 if (list_.get() && --list_->notify_depth_ == 0) |
145 list_->Compact(); | 195 list_->Compact(); |
146 } | 196 } |
147 | 197 |
148 template <class ObserverType> | 198 template <class ObserverType> |
149 ObserverType* ObserverListBase<ObserverType>::Iterator::GetNext() { | 199 template <class ContainerType> |
150 if (!list_.get()) | 200 bool ObserverListBase<ObserverType>::Iter<ContainerType>::operator!=( |
151 return nullptr; | 201 const Iter& other) const { |
152 ListType& observers = list_->observers_; | 202 return GetCurrent() != other.GetCurrent(); |
153 // Advance if the current element is null | |
154 size_t max_index = std::min(max_index_, observers.size()); | |
155 while (index_ < max_index && !observers[index_]) | |
156 ++index_; | |
157 return index_ < max_index ? observers[index_++] : nullptr; | |
158 } | 203 } |
159 | 204 |
160 template <class ObserverType> | 205 template <class ObserverType> |
206 template <class ContainerType> | |
207 typename ObserverListBase<ObserverType>::template Iter<ContainerType>& | |
208 ObserverListBase<ObserverType>::Iter<ContainerType>::operator++() { | |
209 Advance(); | |
210 return *this; | |
211 } | |
212 | |
213 template <class ObserverType> | |
214 template <class ContainerType> | |
215 typename ObserverListBase<ObserverType>::template Iter<ContainerType> | |
216 ObserverListBase<ObserverType>::Iter<ContainerType>::operator++(int) { | |
217 Iter previous(*this); | |
218 Advance(); | |
219 return previous; | |
220 } | |
221 | |
222 template <class ObserverType> | |
223 template <class ContainerType> | |
224 ObserverType* ObserverListBase<ObserverType>::Iter<ContainerType>::operator->() | |
225 const { | |
226 return GetCurrent(); | |
227 } | |
228 | |
229 template <class ObserverType> | |
230 template <class ContainerType> | |
231 ObserverType& ObserverListBase<ObserverType>::Iter<ContainerType>::operator*() | |
232 const { | |
233 ObserverType* current = GetCurrent(); | |
234 DCHECK(current); | |
235 return *current; | |
236 } | |
237 | |
238 template <class ObserverType> | |
239 template <class ContainerType> | |
240 ObserverType* ObserverListBase<ObserverType>::Iter<ContainerType>::GetCurrent() | |
241 const { | |
242 if (!list_) | |
243 return nullptr; | |
244 size_t index = GetNonNullIndex(); | |
245 return index < checked_max_index() ? list_->observers_[index] : nullptr; | |
246 } | |
247 | |
248 template <class ObserverType> | |
249 template <class ContainerType> | |
250 size_t ObserverListBase<ObserverType>::Iter<ContainerType>::GetNonNullIndex() | |
251 const { | |
252 size_t index = index_; | |
253 size_t max_index = checked_max_index(); | |
254 while (index < max_index && !list_->observers_[index]) | |
255 ++index; | |
256 return index; | |
257 } | |
258 | |
259 template <class ObserverType> | |
260 template <class ContainerType> | |
261 void ObserverListBase<ObserverType>::Iter<ContainerType>::Advance() { | |
262 if (!list_) | |
263 return; | |
264 | |
265 ++index_; | |
266 index_ = GetNonNullIndex(); | |
267 } | |
268 | |
269 template <class ObserverType> | |
270 template <class ContainerType> | |
271 ObserverType* ObserverListBase<ObserverType>::Iter<ContainerType>::GetNext() { | |
272 if (!list_) | |
273 return nullptr; | |
274 | |
275 ObserverType* current = GetCurrent(); | |
276 | |
277 index_ = GetNonNullIndex(); | |
278 ++index_; | |
279 | |
280 return current; | |
281 } | |
282 | |
283 template <class ObserverType> | |
161 void ObserverListBase<ObserverType>::AddObserver(ObserverType* obs) { | 284 void ObserverListBase<ObserverType>::AddObserver(ObserverType* obs) { |
162 DCHECK(obs); | 285 DCHECK(obs); |
163 if (ContainsValue(observers_, obs)) { | 286 if (ContainsValue(observers_, obs)) { |
164 NOTREACHED() << "Observers can only be added once!"; | 287 NOTREACHED() << "Observers can only be added once!"; |
165 return; | 288 return; |
166 } | 289 } |
167 observers_.push_back(obs); | 290 observers_.push_back(obs); |
168 } | 291 } |
169 | 292 |
170 template <class ObserverType> | 293 template <class ObserverType> |
(...skipping 26 matching lines...) Expand all Loading... | |
197 for (typename ListType::iterator it = observers_.begin(); | 320 for (typename ListType::iterator it = observers_.begin(); |
198 it != observers_.end(); ++it) { | 321 it != observers_.end(); ++it) { |
199 *it = nullptr; | 322 *it = nullptr; |
200 } | 323 } |
201 } else { | 324 } else { |
202 observers_.clear(); | 325 observers_.clear(); |
203 } | 326 } |
204 } | 327 } |
205 | 328 |
206 template <class ObserverType> | 329 template <class ObserverType> |
207 void ObserverListBase<ObserverType>::Compact() { | 330 void ObserverListBase<ObserverType>::Compact() const { |
208 observers_.erase( | 331 auto& observers = const_cast<ListType&>(observers_); |
209 std::remove(observers_.begin(), observers_.end(), nullptr), | 332 observers.erase(std::remove(observers.begin(), observers.end(), nullptr), |
210 observers_.end()); | 333 observers.end()); |
211 } | 334 } |
212 | 335 |
213 template <class ObserverType, bool check_empty = false> | 336 template <class ObserverType, bool check_empty = false> |
214 class ObserverList : public ObserverListBase<ObserverType> { | 337 class ObserverList : public ObserverListBase<ObserverType> { |
215 public: | 338 public: |
216 typedef typename ObserverListBase<ObserverType>::NotificationType | 339 typedef typename ObserverListBase<ObserverType>::NotificationType |
217 NotificationType; | 340 NotificationType; |
218 | 341 |
219 ObserverList() {} | 342 ObserverList() {} |
220 explicit ObserverList(NotificationType type) | 343 explicit ObserverList(NotificationType type) |
(...skipping 19 matching lines...) Expand all Loading... | |
240 it_inside_observer_macro(&observer_list); \ | 363 it_inside_observer_macro(&observer_list); \ |
241 ObserverType* obs; \ | 364 ObserverType* obs; \ |
242 while ((obs = it_inside_observer_macro.GetNext()) != nullptr) \ | 365 while ((obs = it_inside_observer_macro.GetNext()) != nullptr) \ |
243 obs->func; \ | 366 obs->func; \ |
244 } \ | 367 } \ |
245 } while (0) | 368 } while (0) |
246 | 369 |
247 } // namespace base | 370 } // namespace base |
248 | 371 |
249 #endif // BASE_OBSERVER_LIST_H_ | 372 #endif // BASE_OBSERVER_LIST_H_ |
OLD | NEW |