| 163 | protected: |
| 164 | // An iterator class that can be used to access the list of callbacks. |
| 165 | class Iterator { |
| 166 | public: |
| 167 | explicit Iterator(CallbackListBase<CallbackType>* list) |
| 168 | : list_(list), list_iter_(list_->callbacks_.begin()) { |
| 169 | ++list_->active_iterator_count_; |
| 170 | } |
| 171 | |
| 172 | Iterator(const Iterator& iter) |
| 173 | : list_(iter.list_), list_iter_(iter.list_iter_) { |
| 174 | ++list_->active_iterator_count_; |
| 175 | } |
| 176 | |
| 177 | ~Iterator() { |
| 178 | if (list_ && --list_->active_iterator_count_ == 0) { |
| 179 | list_->Compact(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | CallbackType* GetNext() { |
| 184 | while ((list_iter_ != list_->callbacks_.end()) && list_iter_->is_null()) |
| 185 | ++list_iter_; |
| 186 | |
| 187 | CallbackType* cb = NULL; |
| 188 | if (list_iter_ != list_->callbacks_.end()) { |
| 189 | cb = &(*list_iter_); |
| 190 | ++list_iter_; |
| 191 | } |
| 192 | return cb; |
| 193 | } |
| 194 | |
| 195 | private: |
| 196 | CallbackListBase<CallbackType>* list_; |
| 197 | typename std::list<CallbackType>::iterator list_iter_; |
| 198 | }; |
| 199 | |
| 200 | CallbackListBase() : active_iterator_count_(0) {} |
| 201 | |