OLD | NEW |
1 // This file was GENERATED by command: | 1 // This file was GENERATED by command: |
2 // pump.py callback_list.h.pump | 2 // pump.py callback_list.h.pump |
3 // DO NOT EDIT BY HAND!!! | 3 // DO NOT EDIT BY HAND!!! |
4 | 4 |
5 | 5 |
6 // Copyright 2013 The Chromium Authors. All rights reserved. | 6 // Copyright 2013 The Chromium Authors. All rights reserved. |
7 // Use of this source code is governed by a BSD-style license that can be | 7 // Use of this source code is governed by a BSD-style license that can be |
8 // found in the LICENSE file. | 8 // found in the LICENSE file. |
9 | 9 |
10 #ifndef BASE_CALLBACK_LIST_H_ | 10 #ifndef BASE_CALLBACK_LIST_H_ |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 public: | 82 public: |
83 class Subscription { | 83 class Subscription { |
84 public: | 84 public: |
85 Subscription(CallbackListBase<CallbackType>* list, | 85 Subscription(CallbackListBase<CallbackType>* list, |
86 typename std::list<CallbackType>::iterator iter) | 86 typename std::list<CallbackType>::iterator iter) |
87 : list_(list), | 87 : list_(list), |
88 iter_(iter) { | 88 iter_(iter) { |
89 } | 89 } |
90 | 90 |
91 ~Subscription() { | 91 ~Subscription() { |
92 if (list_->active_iterator_count_) | 92 if (list_->active_iterator_count_) { |
93 iter_->Reset(); | 93 iter_->Reset(); |
94 else | 94 } else { |
95 list_->callbacks_.erase(iter_); | 95 list_->callbacks_.erase(iter_); |
| 96 if (!list_->removal_callback_.is_null()) |
| 97 list_->removal_callback_.Run(); |
| 98 } |
96 } | 99 } |
97 | 100 |
98 private: | 101 private: |
99 CallbackListBase<CallbackType>* list_; | 102 CallbackListBase<CallbackType>* list_; |
100 typename std::list<CallbackType>::iterator iter_; | 103 typename std::list<CallbackType>::iterator iter_; |
101 | 104 |
102 DISALLOW_COPY_AND_ASSIGN(Subscription); | 105 DISALLOW_COPY_AND_ASSIGN(Subscription); |
103 }; | 106 }; |
104 | 107 |
105 // Add a callback to the list. The callback will remain registered until the | 108 // Add a callback to the list. The callback will remain registered until the |
106 // returned Subscription is destroyed, which must occur before the | 109 // returned Subscription is destroyed, which must occur before the |
107 // CallbackList is destroyed. | 110 // CallbackList is destroyed. |
108 scoped_ptr<Subscription> Add(const CallbackType& cb) WARN_UNUSED_RESULT { | 111 scoped_ptr<Subscription> Add(const CallbackType& cb) WARN_UNUSED_RESULT { |
109 DCHECK(!cb.is_null()); | 112 DCHECK(!cb.is_null()); |
110 return scoped_ptr<Subscription>( | 113 return scoped_ptr<Subscription>( |
111 new Subscription(this, callbacks_.insert(callbacks_.end(), cb))); | 114 new Subscription(this, callbacks_.insert(callbacks_.end(), cb))); |
112 } | 115 } |
113 | 116 |
| 117 // Sets a callback which will be run when a subscription list is changed. |
| 118 void set_removal_callback(const Closure& callback) { |
| 119 removal_callback_ = callback; |
| 120 } |
| 121 |
| 122 bool empty() { return callbacks_.empty(); } |
| 123 |
114 protected: | 124 protected: |
115 // An iterator class that can be used to access the list of callbacks. | 125 // An iterator class that can be used to access the list of callbacks. |
116 class Iterator { | 126 class Iterator { |
117 public: | 127 public: |
118 explicit Iterator(CallbackListBase<CallbackType>* list) | 128 explicit Iterator(CallbackListBase<CallbackType>* list) |
119 : list_(list), | 129 : list_(list), |
120 list_iter_(list_->callbacks_.begin()) { | 130 list_iter_(list_->callbacks_.begin()) { |
121 ++list_->active_iterator_count_; | 131 ++list_->active_iterator_count_; |
122 } | 132 } |
123 | 133 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 // Returns an instance of a CallbackListBase::Iterator which can be used | 170 // Returns an instance of a CallbackListBase::Iterator which can be used |
161 // to run callbacks. | 171 // to run callbacks. |
162 Iterator GetIterator() { | 172 Iterator GetIterator() { |
163 return Iterator(this); | 173 return Iterator(this); |
164 } | 174 } |
165 | 175 |
166 // Compact the list: remove any entries which were NULLed out during | 176 // Compact the list: remove any entries which were NULLed out during |
167 // iteration. | 177 // iteration. |
168 void Compact() { | 178 void Compact() { |
169 typename std::list<CallbackType>::iterator it = callbacks_.begin(); | 179 typename std::list<CallbackType>::iterator it = callbacks_.begin(); |
| 180 bool updated = false; |
170 while (it != callbacks_.end()) { | 181 while (it != callbacks_.end()) { |
171 if ((*it).is_null()) | 182 if ((*it).is_null()) { |
| 183 updated = true; |
172 it = callbacks_.erase(it); | 184 it = callbacks_.erase(it); |
173 else | 185 } else { |
174 ++it; | 186 ++it; |
| 187 } |
| 188 |
| 189 if (updated && !removal_callback_.is_null()) |
| 190 removal_callback_.Run(); |
175 } | 191 } |
176 } | 192 } |
177 | 193 |
178 private: | 194 private: |
179 std::list<CallbackType> callbacks_; | 195 std::list<CallbackType> callbacks_; |
180 int active_iterator_count_; | 196 int active_iterator_count_; |
| 197 Closure removal_callback_; |
181 | 198 |
182 DISALLOW_COPY_AND_ASSIGN(CallbackListBase); | 199 DISALLOW_COPY_AND_ASSIGN(CallbackListBase); |
183 }; | 200 }; |
184 | 201 |
185 } // namespace internal | 202 } // namespace internal |
186 | 203 |
187 template <typename Sig> class CallbackList; | 204 template <typename Sig> class CallbackList; |
188 | 205 |
189 template <> | 206 template <> |
190 class CallbackList<void(void)> | 207 class CallbackList<void(void)> |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 } | 392 } |
376 } | 393 } |
377 | 394 |
378 private: | 395 private: |
379 DISALLOW_COPY_AND_ASSIGN(CallbackList); | 396 DISALLOW_COPY_AND_ASSIGN(CallbackList); |
380 }; | 397 }; |
381 | 398 |
382 } // namespace base | 399 } // namespace base |
383 | 400 |
384 #endif // BASE_CALLBACK_LIST_H_ | 401 #endif // BASE_CALLBACK_LIST_H_ |
OLD | NEW |