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 // Returns true if there are no subscriptions. This is only valid to call when |
| 123 // not looping through the list. |
| 124 bool empty() { |
| 125 DCHECK_EQ(0, active_iterator_count_); |
| 126 return callbacks_.empty(); |
| 127 } |
| 128 |
114 protected: | 129 protected: |
115 // An iterator class that can be used to access the list of callbacks. | 130 // An iterator class that can be used to access the list of callbacks. |
116 class Iterator { | 131 class Iterator { |
117 public: | 132 public: |
118 explicit Iterator(CallbackListBase<CallbackType>* list) | 133 explicit Iterator(CallbackListBase<CallbackType>* list) |
119 : list_(list), | 134 : list_(list), |
120 list_iter_(list_->callbacks_.begin()) { | 135 list_iter_(list_->callbacks_.begin()) { |
121 ++list_->active_iterator_count_; | 136 ++list_->active_iterator_count_; |
122 } | 137 } |
123 | 138 |
(...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 | 175 // Returns an instance of a CallbackListBase::Iterator which can be used |
161 // to run callbacks. | 176 // to run callbacks. |
162 Iterator GetIterator() { | 177 Iterator GetIterator() { |
163 return Iterator(this); | 178 return Iterator(this); |
164 } | 179 } |
165 | 180 |
166 // Compact the list: remove any entries which were NULLed out during | 181 // Compact the list: remove any entries which were NULLed out during |
167 // iteration. | 182 // iteration. |
168 void Compact() { | 183 void Compact() { |
169 typename std::list<CallbackType>::iterator it = callbacks_.begin(); | 184 typename std::list<CallbackType>::iterator it = callbacks_.begin(); |
| 185 bool updated = false; |
170 while (it != callbacks_.end()) { | 186 while (it != callbacks_.end()) { |
171 if ((*it).is_null()) | 187 if ((*it).is_null()) { |
| 188 updated = true; |
172 it = callbacks_.erase(it); | 189 it = callbacks_.erase(it); |
173 else | 190 } else { |
174 ++it; | 191 ++it; |
| 192 } |
| 193 |
| 194 if (updated && !removal_callback_.is_null()) |
| 195 removal_callback_.Run(); |
175 } | 196 } |
176 } | 197 } |
177 | 198 |
178 private: | 199 private: |
179 std::list<CallbackType> callbacks_; | 200 std::list<CallbackType> callbacks_; |
180 int active_iterator_count_; | 201 int active_iterator_count_; |
| 202 Closure removal_callback_; |
181 | 203 |
182 DISALLOW_COPY_AND_ASSIGN(CallbackListBase); | 204 DISALLOW_COPY_AND_ASSIGN(CallbackListBase); |
183 }; | 205 }; |
184 | 206 |
185 } // namespace internal | 207 } // namespace internal |
186 | 208 |
187 template <typename Sig> class CallbackList; | 209 template <typename Sig> class CallbackList; |
188 | 210 |
189 template <> | 211 template <> |
190 class CallbackList<void(void)> | 212 class CallbackList<void(void)> |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 } | 397 } |
376 } | 398 } |
377 | 399 |
378 private: | 400 private: |
379 DISALLOW_COPY_AND_ASSIGN(CallbackList); | 401 DISALLOW_COPY_AND_ASSIGN(CallbackList); |
380 }; | 402 }; |
381 | 403 |
382 } // namespace base | 404 } // namespace base |
383 | 405 |
384 #endif // BASE_CALLBACK_LIST_H_ | 406 #endif // BASE_CALLBACK_LIST_H_ |
OLD | NEW |