OLD | NEW |
1 $$ This is a pump file for generating file templates. Pump is a python | 1 $$ This is a pump file for generating file templates. Pump is a python |
2 $$ script that is part of the Google Test suite of utilities. Description | 2 $$ script that is part of the Google Test suite of utilities. Description |
3 $$ can be found here: | 3 $$ can be found here: |
4 $$ | 4 $$ |
5 $$ http://code.google.com/p/googletest/wiki/PumpManual | 5 $$ http://code.google.com/p/googletest/wiki/PumpManual |
6 $$ | 6 $$ |
7 | 7 |
8 $$ See comment for MAX_ARITY in base/bind.h.pump. | 8 $$ See comment for MAX_ARITY in base/bind.h.pump. |
9 $var MAX_ARITY = 7 | 9 $var MAX_ARITY = 7 |
10 | 10 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 public: | 87 public: |
88 class Subscription { | 88 class Subscription { |
89 public: | 89 public: |
90 Subscription(CallbackListBase<CallbackType>* list, | 90 Subscription(CallbackListBase<CallbackType>* list, |
91 typename std::list<CallbackType>::iterator iter) | 91 typename std::list<CallbackType>::iterator iter) |
92 : list_(list), | 92 : list_(list), |
93 iter_(iter) { | 93 iter_(iter) { |
94 } | 94 } |
95 | 95 |
96 ~Subscription() { | 96 ~Subscription() { |
97 if (list_->active_iterator_count_) | 97 if (list_->active_iterator_count_) { |
98 iter_->Reset(); | 98 iter_->Reset(); |
99 else | 99 } else { |
100 list_->callbacks_.erase(iter_); | 100 list_->callbacks_.erase(iter_); |
| 101 if (!list_->removal_callback_.is_null()) |
| 102 list_->removal_callback_.Run(); |
| 103 } |
101 } | 104 } |
102 | 105 |
103 private: | 106 private: |
104 CallbackListBase<CallbackType>* list_; | 107 CallbackListBase<CallbackType>* list_; |
105 typename std::list<CallbackType>::iterator iter_; | 108 typename std::list<CallbackType>::iterator iter_; |
106 | 109 |
107 DISALLOW_COPY_AND_ASSIGN(Subscription); | 110 DISALLOW_COPY_AND_ASSIGN(Subscription); |
108 }; | 111 }; |
109 | 112 |
110 // Add a callback to the list. The callback will remain registered until the | 113 // Add a callback to the list. The callback will remain registered until the |
111 // returned Subscription is destroyed, which must occur before the | 114 // returned Subscription is destroyed, which must occur before the |
112 // CallbackList is destroyed. | 115 // CallbackList is destroyed. |
113 scoped_ptr<Subscription> Add(const CallbackType& cb) WARN_UNUSED_RESULT { | 116 scoped_ptr<Subscription> Add(const CallbackType& cb) WARN_UNUSED_RESULT { |
114 DCHECK(!cb.is_null()); | 117 DCHECK(!cb.is_null()); |
115 return scoped_ptr<Subscription>( | 118 return scoped_ptr<Subscription>( |
116 new Subscription(this, callbacks_.insert(callbacks_.end(), cb))); | 119 new Subscription(this, callbacks_.insert(callbacks_.end(), cb))); |
117 } | 120 } |
118 | 121 |
| 122 // Sets a callback which will be run when a subscription list is changed. |
| 123 void set_removal_callback(const Closure& callback) { |
| 124 removal_callback_ = callback; |
| 125 } |
| 126 |
| 127 // Returns true if there are no subscriptions. This is only valid to call when |
| 128 // not looping through the list. |
| 129 bool empty() { |
| 130 DCHECK_EQ(0, active_iterator_count_); |
| 131 return callbacks_.empty(); |
| 132 } |
| 133 |
119 protected: | 134 protected: |
120 // An iterator class that can be used to access the list of callbacks. | 135 // An iterator class that can be used to access the list of callbacks. |
121 class Iterator { | 136 class Iterator { |
122 public: | 137 public: |
123 explicit Iterator(CallbackListBase<CallbackType>* list) | 138 explicit Iterator(CallbackListBase<CallbackType>* list) |
124 : list_(list), | 139 : list_(list), |
125 list_iter_(list_->callbacks_.begin()) { | 140 list_iter_(list_->callbacks_.begin()) { |
126 ++list_->active_iterator_count_; | 141 ++list_->active_iterator_count_; |
127 } | 142 } |
128 | 143 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 // Returns an instance of a CallbackListBase::Iterator which can be used | 180 // Returns an instance of a CallbackListBase::Iterator which can be used |
166 // to run callbacks. | 181 // to run callbacks. |
167 Iterator GetIterator() { | 182 Iterator GetIterator() { |
168 return Iterator(this); | 183 return Iterator(this); |
169 } | 184 } |
170 | 185 |
171 // Compact the list: remove any entries which were NULLed out during | 186 // Compact the list: remove any entries which were NULLed out during |
172 // iteration. | 187 // iteration. |
173 void Compact() { | 188 void Compact() { |
174 typename std::list<CallbackType>::iterator it = callbacks_.begin(); | 189 typename std::list<CallbackType>::iterator it = callbacks_.begin(); |
| 190 bool updated = false; |
175 while (it != callbacks_.end()) { | 191 while (it != callbacks_.end()) { |
176 if ((*it).is_null()) | 192 if ((*it).is_null()) { |
| 193 updated = true; |
177 it = callbacks_.erase(it); | 194 it = callbacks_.erase(it); |
178 else | 195 } else { |
179 ++it; | 196 ++it; |
| 197 } |
| 198 |
| 199 if (updated && !removal_callback_.is_null()) |
| 200 removal_callback_.Run(); |
180 } | 201 } |
181 } | 202 } |
182 | 203 |
183 private: | 204 private: |
184 std::list<CallbackType> callbacks_; | 205 std::list<CallbackType> callbacks_; |
185 int active_iterator_count_; | 206 int active_iterator_count_; |
| 207 Closure removal_callback_; |
186 | 208 |
187 DISALLOW_COPY_AND_ASSIGN(CallbackListBase); | 209 DISALLOW_COPY_AND_ASSIGN(CallbackListBase); |
188 }; | 210 }; |
189 | 211 |
190 } // namespace internal | 212 } // namespace internal |
191 | 213 |
192 template <typename Sig> class CallbackList; | 214 template <typename Sig> class CallbackList; |
193 | 215 |
194 | 216 |
195 $range ARITY 0..MAX_ARITY | 217 $range ARITY 0..MAX_ARITY |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 | 260 |
239 private: | 261 private: |
240 DISALLOW_COPY_AND_ASSIGN(CallbackList); | 262 DISALLOW_COPY_AND_ASSIGN(CallbackList); |
241 }; | 263 }; |
242 | 264 |
243 | 265 |
244 ]] $$ for ARITY | 266 ]] $$ for ARITY |
245 } // namespace base | 267 } // namespace base |
246 | 268 |
247 #endif // BASE_CALLBACK_LIST_H_ | 269 #endif // BASE_CALLBACK_LIST_H_ |
OLD | NEW |