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

Side by Side Diff: base/observer_list.h

Issue 2340583005: Base ObserverList: Add basic support for standard C++ iterators. (Closed)
Patch Set: Erase postfix ++ version. Add comments on deprecation. Created 4 years, 3 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 | base/observer_list_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 <stddef.h> 8 #include <stddef.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 28 matching lines...) Expand all
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_EACH_OBSERVER(Observer, observer_list_, OnFoo(this)); 49 // for (auto& observer : observer_list_)
50 // observer.OnFoo(this);
50 // } 51 // }
51 // 52 //
52 // void NotifyBar(int x, int y) { 53 // void NotifyBar(int x, int y) {
53 // FOR_EACH_OBSERVER(Observer, observer_list_, OnBar(this, x, y)); 54 // for (FooList::iterator i = observer_list.begin(),
55 // e = observer_list.end(); i != e; ++i)
56 // i->OnBar(this, x, y);
54 // } 57 // }
55 // 58 //
56 // private: 59 // private:
57 // base::ObserverList<Observer> observer_list_; 60 // base::ObserverList<Observer> observer_list_;
58 // }; 61 // };
59 // 62 //
60 // 63 //
61 /////////////////////////////////////////////////////////////////////////////// 64 ///////////////////////////////////////////////////////////////////////////////
62 65
63 namespace base { 66 namespace base {
64 67
65 template <typename ObserverType> 68 template <typename ObserverType>
66 class ObserverListThreadSafe; 69 class ObserverListThreadSafe;
67 70
68 template <class ObserverType> 71 template <class ObserverType>
69 class ObserverListBase 72 class ObserverListBase
70 : public SupportsWeakPtr<ObserverListBase<ObserverType>> { 73 : public SupportsWeakPtr<ObserverListBase<ObserverType>> {
71 public: 74 public:
72 // Enumeration of which observers are notified. 75 // Enumeration of which observers are notified.
73 enum NotificationType { 76 enum NotificationType {
74 // Specifies that any observers added during notification are notified. 77 // Specifies that any observers added during notification are notified.
75 // This is the default type if non type is provided to the constructor. 78 // This is the default type if non type is provided to the constructor.
76 NOTIFY_ALL, 79 NOTIFY_ALL,
77 80
78 // Specifies that observers added while sending out notification are not 81 // Specifies that observers added while sending out notification are not
79 // notified. 82 // notified.
80 NOTIFY_EXISTING_ONLY 83 NOTIFY_EXISTING_ONLY
81 }; 84 };
82 85
83 // An iterator class that can be used to access the list of observers. See 86 // An iterator class that can be used to access the list of observers.
84 // also the FOR_EACH_OBSERVER macro defined below. 87 template <class ContainerType>
85 class Iterator { 88 class Iter {
86 public: 89 public:
87 explicit Iterator(ObserverListBase<ObserverType>* list); 90 Iter();
88 ~Iterator(); 91 explicit Iter(ContainerType* list);
92 ~Iter();
93
94 // Deprecated.
89 ObserverType* GetNext(); 95 ObserverType* GetNext();
90 96
97 // A workaround for C2244. MSVC requires fully qualified type name for
98 // return type on a function definition to match a function declaration.
99 using ThisType =
100 typename ObserverListBase<ObserverType>::template Iter<ContainerType>;
101
102 bool operator!=(const Iter& other) const;
103 ThisType& operator++();
104 ObserverType* operator->() const;
105 ObserverType& operator*() const;
106
91 private: 107 private:
92 WeakPtr<ObserverListBase<ObserverType>> list_; 108 ObserverType* GetCurrent() const;
109
110 size_t GetNonNullIndex() const;
111
112 size_t checked_max_index() const {
dcheng 2016/09/23 07:39:01 Nit: checked here makes it sound like it will CHEC
loyso (OOO) 2016/09/26 04:26:24 Done.
113 return std::min(max_index_, list_->observers_.size());
114 }
115 WeakPtr<ContainerType> list_;
93 size_t index_; 116 size_t index_;
94 size_t max_index_; 117 size_t max_index_;
95 }; 118 };
96 119
120 using Iterator = Iter<ObserverListBase<ObserverType>>;
121
122 using iterator = Iter<ObserverListBase<ObserverType>>;
123 iterator begin() { return iterator(this); }
124 iterator end() { return iterator(); }
125
126 using const_iterator = Iter<const ObserverListBase<ObserverType>>;
127 const_iterator begin() const { return const_iterator(this); }
128 const_iterator end() const { return const_iterator(); }
129
97 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} 130 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {}
98 explicit ObserverListBase(NotificationType type) 131 explicit ObserverListBase(NotificationType type)
99 : notify_depth_(0), type_(type) {} 132 : notify_depth_(0), type_(type) {}
100 133
101 // Add an observer to the list. An observer should not be added to 134 // Add an observer to the list. An observer should not be added to
102 // the same list more than once. 135 // the same list more than once.
103 void AddObserver(ObserverType* obs); 136 void AddObserver(ObserverType* obs);
104 137
105 // Remove an observer from the list if it is in the list. 138 // Remove an observer from the list if it is in the list.
106 void RemoveObserver(ObserverType* obs); 139 void RemoveObserver(ObserverType* obs);
107 140
108 // Determine whether a particular observer is in the list. 141 // Determine whether a particular observer is in the list.
109 bool HasObserver(const ObserverType* observer) const; 142 bool HasObserver(const ObserverType* observer) const;
110 143
111 void Clear(); 144 void Clear();
112 145
113 protected: 146 protected:
114 size_t size() const { return observers_.size(); } 147 size_t size() const { return observers_.size(); }
115 148
116 void Compact(); 149 // Defragments the list despite const.
150 void Compact() const;
117 151
118 private: 152 private:
119 friend class ObserverListThreadSafe<ObserverType>; 153 friend class ObserverListThreadSafe<ObserverType>;
120 154
121 typedef std::vector<ObserverType*> ListType; 155 typedef std::vector<ObserverType*> ListType;
122 156
123 ListType observers_; 157 ListType observers_;
124 int notify_depth_; 158 mutable int notify_depth_;
125 NotificationType type_; 159 NotificationType type_;
126 160
127 friend class ObserverListBase::Iterator; 161 template <class ContainerType>
162 friend class Iter;
128 163
129 DISALLOW_COPY_AND_ASSIGN(ObserverListBase); 164 DISALLOW_COPY_AND_ASSIGN(ObserverListBase);
130 }; 165 };
131 166
132 template <class ObserverType> 167 template <class ObserverType>
133 ObserverListBase<ObserverType>::Iterator::Iterator( 168 template <class ContainerType>
134 ObserverListBase<ObserverType>* list) 169 ObserverListBase<ObserverType>::Iter<ContainerType>::Iter()
135 : list_(list->AsWeakPtr()), 170 : index_(), max_index_() {}
171
172 template <class ObserverType>
173 template <class ContainerType>
174 ObserverListBase<ObserverType>::Iter<ContainerType>::Iter(ContainerType* list)
175 : list_(const_cast<ObserverListBase<ObserverType>*>(list)->AsWeakPtr()),
136 index_(0), 176 index_(0),
137 max_index_(list->type_ == NOTIFY_ALL ? std::numeric_limits<size_t>::max() 177 max_index_(list->type_ == NOTIFY_ALL ? std::numeric_limits<size_t>::max()
138 : list->observers_.size()) { 178 : list->observers_.size()) {
139 ++list_->notify_depth_; 179 ++list_->notify_depth_;
140 } 180 }
141 181
142 template <class ObserverType> 182 template <class ObserverType>
143 ObserverListBase<ObserverType>::Iterator::~Iterator() { 183 template <class ContainerType>
184 ObserverListBase<ObserverType>::Iter<ContainerType>::~Iter() {
144 if (list_.get() && --list_->notify_depth_ == 0) 185 if (list_.get() && --list_->notify_depth_ == 0)
145 list_->Compact(); 186 list_->Compact();
146 } 187 }
147 188
148 template <class ObserverType> 189 template <class ObserverType>
149 ObserverType* ObserverListBase<ObserverType>::Iterator::GetNext() { 190 template <class ContainerType>
150 if (!list_.get()) 191 bool ObserverListBase<ObserverType>::Iter<ContainerType>::operator!=(
151 return nullptr; 192 const Iter& other) const {
152 ListType& observers = list_->observers_; 193 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 } 194 }
159 195
160 template <class ObserverType> 196 template <class ObserverType>
197 template <class ContainerType>
198 typename ObserverListBase<ObserverType>::template Iter<ContainerType>&
199 ObserverListBase<ObserverType>::Iter<ContainerType>::operator++() {
200 if (list_) {
201 ++index_;
202 index_ = GetNonNullIndex();
203 }
204 return *this;
205 }
206
207 template <class ObserverType>
208 template <class ContainerType>
209 ObserverType* ObserverListBase<ObserverType>::Iter<ContainerType>::operator->()
210 const {
211 return GetCurrent();
dcheng 2016/09/23 07:39:01 If operator* DCHECKs() this, operator-> probably s
loyso (OOO) 2016/09/26 04:26:24 Done.
212 }
213
214 template <class ObserverType>
215 template <class ContainerType>
216 ObserverType& ObserverListBase<ObserverType>::Iter<ContainerType>::operator*()
217 const {
218 ObserverType* current = GetCurrent();
219 DCHECK(current);
220 return *current;
221 }
222
223 template <class ObserverType>
224 template <class ContainerType>
225 ObserverType* ObserverListBase<ObserverType>::Iter<ContainerType>::GetCurrent()
226 const {
227 if (!list_)
228 return nullptr;
229 size_t index = GetNonNullIndex();
230 return index < checked_max_index() ? list_->observers_[index] : nullptr;
231 }
232
233 template <class ObserverType>
234 template <class ContainerType>
235 size_t ObserverListBase<ObserverType>::Iter<ContainerType>::GetNonNullIndex()
236 const {
237 size_t index = index_;
238 size_t max_index = checked_max_index();
239 while (index < max_index && !list_->observers_[index])
240 ++index;
241 return index;
242 }
243
244 template <class ObserverType>
245 template <class ContainerType>
246 ObserverType* ObserverListBase<ObserverType>::Iter<ContainerType>::GetNext() {
247 if (!list_)
248 return nullptr;
249
250 ObserverType* current = GetCurrent();
251
252 index_ = GetNonNullIndex();
253 ++index_;
254
255 return current;
256 }
257
258 template <class ObserverType>
161 void ObserverListBase<ObserverType>::AddObserver(ObserverType* obs) { 259 void ObserverListBase<ObserverType>::AddObserver(ObserverType* obs) {
162 DCHECK(obs); 260 DCHECK(obs);
163 if (ContainsValue(observers_, obs)) { 261 if (ContainsValue(observers_, obs)) {
164 NOTREACHED() << "Observers can only be added once!"; 262 NOTREACHED() << "Observers can only be added once!";
165 return; 263 return;
166 } 264 }
167 observers_.push_back(obs); 265 observers_.push_back(obs);
168 } 266 }
169 267
170 template <class ObserverType> 268 template <class ObserverType>
(...skipping 26 matching lines...) Expand all
197 for (typename ListType::iterator it = observers_.begin(); 295 for (typename ListType::iterator it = observers_.begin();
198 it != observers_.end(); ++it) { 296 it != observers_.end(); ++it) {
199 *it = nullptr; 297 *it = nullptr;
200 } 298 }
201 } else { 299 } else {
202 observers_.clear(); 300 observers_.clear();
203 } 301 }
204 } 302 }
205 303
206 template <class ObserverType> 304 template <class ObserverType>
207 void ObserverListBase<ObserverType>::Compact() { 305 void ObserverListBase<ObserverType>::Compact() const {
208 observers_.erase( 306 auto& observers = const_cast<ListType&>(observers_);
209 std::remove(observers_.begin(), observers_.end(), nullptr), 307 observers.erase(std::remove(observers.begin(), observers.end(), nullptr),
210 observers_.end()); 308 observers.end());
211 } 309 }
212 310
213 template <class ObserverType, bool check_empty = false> 311 template <class ObserverType, bool check_empty = false>
214 class ObserverList : public ObserverListBase<ObserverType> { 312 class ObserverList : public ObserverListBase<ObserverType> {
215 public: 313 public:
216 typedef typename ObserverListBase<ObserverType>::NotificationType 314 typedef typename ObserverListBase<ObserverType>::NotificationType
217 NotificationType; 315 NotificationType;
218 316
219 ObserverList() {} 317 ObserverList() {}
220 explicit ObserverList(NotificationType type) 318 explicit ObserverList(NotificationType type)
221 : ObserverListBase<ObserverType>(type) {} 319 : ObserverListBase<ObserverType>(type) {}
222 320
223 ~ObserverList() { 321 ~ObserverList() {
224 // When check_empty is true, assert that the list is empty on destruction. 322 // When check_empty is true, assert that the list is empty on destruction.
225 if (check_empty) { 323 if (check_empty) {
226 ObserverListBase<ObserverType>::Compact(); 324 ObserverListBase<ObserverType>::Compact();
227 DCHECK_EQ(ObserverListBase<ObserverType>::size(), 0U); 325 DCHECK_EQ(ObserverListBase<ObserverType>::size(), 0U);
228 } 326 }
229 } 327 }
230 328
231 bool might_have_observers() const { 329 bool might_have_observers() const {
232 return ObserverListBase<ObserverType>::size() != 0; 330 return ObserverListBase<ObserverType>::size() != 0;
233 } 331 }
234 }; 332 };
235 333
334 // Deprecated. Use the range-based for loop instead.
236 #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \ 335 #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \
237 do { \ 336 do { \
238 if ((observer_list).might_have_observers()) { \ 337 if ((observer_list).might_have_observers()) { \
239 typename base::ObserverListBase<ObserverType>::Iterator \ 338 typename base::ObserverListBase<ObserverType>::Iterator \
240 it_inside_observer_macro(&observer_list); \ 339 it_inside_observer_macro(&observer_list); \
241 ObserverType* obs; \ 340 ObserverType* obs; \
242 while ((obs = it_inside_observer_macro.GetNext()) != nullptr) \ 341 while ((obs = it_inside_observer_macro.GetNext()) != nullptr) \
243 obs->func; \ 342 obs->func; \
244 } \ 343 } \
245 } while (0) 344 } while (0)
246 345
247 } // namespace base 346 } // namespace base
248 347
249 #endif // BASE_OBSERVER_LIST_H_ 348 #endif // BASE_OBSERVER_LIST_H_
OLDNEW
« no previous file with comments | « no previous file | base/observer_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698