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 bool empty() { return callbacks_.empty(); } | |
dcheng
2014/05/08 21:28:05
Super duper minor driveby nit: I think this can be
| |
128 | |
119 protected: | 129 protected: |
120 // 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. |
121 class Iterator { | 131 class Iterator { |
122 public: | 132 public: |
123 explicit Iterator(CallbackListBase<CallbackType>* list) | 133 explicit Iterator(CallbackListBase<CallbackType>* list) |
124 : list_(list), | 134 : list_(list), |
125 list_iter_(list_->callbacks_.begin()) { | 135 list_iter_(list_->callbacks_.begin()) { |
126 ++list_->active_iterator_count_; | 136 ++list_->active_iterator_count_; |
127 } | 137 } |
128 | 138 |
(...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 | 175 // Returns an instance of a CallbackListBase::Iterator which can be used |
166 // to run callbacks. | 176 // to run callbacks. |
167 Iterator GetIterator() { | 177 Iterator GetIterator() { |
168 return Iterator(this); | 178 return Iterator(this); |
169 } | 179 } |
170 | 180 |
171 // Compact the list: remove any entries which were NULLed out during | 181 // Compact the list: remove any entries which were NULLed out during |
172 // iteration. | 182 // iteration. |
173 void Compact() { | 183 void Compact() { |
174 typename std::list<CallbackType>::iterator it = callbacks_.begin(); | 184 typename std::list<CallbackType>::iterator it = callbacks_.begin(); |
185 bool updated = false; | |
175 while (it != callbacks_.end()) { | 186 while (it != callbacks_.end()) { |
176 if ((*it).is_null()) | 187 if ((*it).is_null()) { |
188 updated = true; | |
177 it = callbacks_.erase(it); | 189 it = callbacks_.erase(it); |
178 else | 190 } else { |
179 ++it; | 191 ++it; |
192 } | |
193 | |
194 if (updated && !removal_callback_.is_null()) | |
195 removal_callback_.Run(); | |
180 } | 196 } |
181 } | 197 } |
182 | 198 |
183 private: | 199 private: |
184 std::list<CallbackType> callbacks_; | 200 std::list<CallbackType> callbacks_; |
185 int active_iterator_count_; | 201 int active_iterator_count_; |
202 Closure removal_callback_; | |
186 | 203 |
187 DISALLOW_COPY_AND_ASSIGN(CallbackListBase); | 204 DISALLOW_COPY_AND_ASSIGN(CallbackListBase); |
188 }; | 205 }; |
189 | 206 |
190 } // namespace internal | 207 } // namespace internal |
191 | 208 |
192 template <typename Sig> class CallbackList; | 209 template <typename Sig> class CallbackList; |
193 | 210 |
194 | 211 |
195 $range ARITY 0..MAX_ARITY | 212 $range ARITY 0..MAX_ARITY |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 | 255 |
239 private: | 256 private: |
240 DISALLOW_COPY_AND_ASSIGN(CallbackList); | 257 DISALLOW_COPY_AND_ASSIGN(CallbackList); |
241 }; | 258 }; |
242 | 259 |
243 | 260 |
244 ]] $$ for ARITY | 261 ]] $$ for ARITY |
245 } // namespace base | 262 } // namespace base |
246 | 263 |
247 #endif // BASE_CALLBACK_LIST_H_ | 264 #endif // BASE_CALLBACK_LIST_H_ |
OLD | NEW |