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
5 $$ http://code.google.com/p/googletest/wiki/PumpManual
8 $$ See comment for MAX_ARITY in base/bind.h.pump.
11 // Copyright 2013 The Chromium Authors. All rights reserved.
12 // Use of this source code is governed by a BSD-style license that can be
13 // found in the LICENSE file.
15 #ifndef BASE_CALLBACK_LIST_H_
16 #define BASE_CALLBACK_LIST_H_
20 #include "base/basictypes.h"
21 #include "base/callback.h"
22 #include "base/callback_internal.h"
23 #include "base/compiler_specific.h"
24 #include "base/logging.h"
25 #include "base/memory/scoped_ptr.h"
29 // A container for a list of callbacks. Unlike a normal STL vector or list,
30 // this container can be modified during iteration without invalidating the
31 // iterator. It safely handles the case of a callback removing itself
32 // or another callback from the list while callbacks are being run.
40 // typedef base::Callback<void(const Foo&)> OnFooCallback;
42 // scoped_ptr<base::CallbackList<void(const Foo&)>::Subscription>
43 // RegisterCallback(const OnFooCallback& cb) {
44 // return callback_list_.Add(cb);
48 // void NotifyFoo(const Foo& foo) {
49 // callback_list_.Notify(foo);
52 // base::CallbackList<void(const Foo&)> callback_list_;
54 // DISALLOW_COPY_AND_ASSIGN(MyWidget);
58 // class MyWidgetListener {
60 // MyWidgetListener::MyWidgetListener() {
61 // foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback(
62 // base::Bind(&MyWidgetListener::OnFoo, this)));
65 // MyWidgetListener::~MyWidgetListener() {
66 // // Subscription gets deleted automatically and will deregister
67 // // the callback in the process.
71 // void OnFoo(const Foo& foo) {
75 // scoped_ptr<base::CallbackList<void(const Foo&)>::Subscription>
78 // DISALLOW_COPY_AND_ASSIGN(MyWidgetListener);
85 template <typename CallbackType>
86 class CallbackListBase {
90 Subscription(CallbackListBase<CallbackType>* list,
91 typename std::list<CallbackType>::iterator iter)
97 if (list_->active_iterator_count_) {
100 list_->callbacks_.erase(iter_);
101 if (!list_->removal_callback_.is_null())
102 list_->removal_callback_.Run();
107 CallbackListBase<CallbackType>* list_;
108 typename std::list<CallbackType>::iterator iter_;
110 DISALLOW_COPY_AND_ASSIGN(Subscription);
113 // Add a callback to the list. The callback will remain registered until the
114 // returned Subscription is destroyed, which must occur before the
115 // CallbackList is destroyed.
116 scoped_ptr<Subscription> Add(const CallbackType& cb) WARN_UNUSED_RESULT {
117 DCHECK(!cb.is_null());
118 return scoped_ptr<Subscription>(
119 new Subscription(this, callbacks_.insert(callbacks_.end(), cb)));
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;
127 // Returns true if there are no subscriptions. This is only valid to call when
128 // not looping through the list.
130 DCHECK_EQ(0, active_iterator_count_);
131 return callbacks_.empty();
135 // An iterator class that can be used to access the list of callbacks.
138 explicit Iterator(CallbackListBase<CallbackType>* list)
140 list_iter_(list_->callbacks_.begin()) {
141 ++list_->active_iterator_count_;
144 Iterator(const Iterator& iter)
146 list_iter_(iter.list_iter_) {
147 ++list_->active_iterator_count_;
151 if (list_ && --list_->active_iterator_count_ == 0) {
156 CallbackType* GetNext() {
157 while ((list_iter_ != list_->callbacks_.end()) && list_iter_->is_null())
160 CallbackType* cb = NULL;
161 if (list_iter_ != list_->callbacks_.end()) {
169 CallbackListBase<CallbackType>* list_;
170 typename std::list<CallbackType>::iterator list_iter_;
173 CallbackListBase() : active_iterator_count_(0) {}
175 ~CallbackListBase() {
176 DCHECK_EQ(0, active_iterator_count_);
177 DCHECK_EQ(0U, callbacks_.size());
180 // Returns an instance of a CallbackListBase::Iterator which can be used
182 Iterator GetIterator() {
183 return Iterator(this);
186 // Compact the list: remove any entries which were NULLed out during
189 typename std::list<CallbackType>::iterator it = callbacks_.begin();
190 bool updated = false;
191 while (it != callbacks_.end()) {
192 if ((*it).is_null()) {
194 it = callbacks_.erase(it);
199 if (updated && !removal_callback_.is_null())
200 removal_callback_.Run();
205 std::list<CallbackType> callbacks_;
206 int active_iterator_count_;
207 Closure removal_callback_;
209 DISALLOW_COPY_AND_ASSIGN(CallbackListBase);
212 } // namespace internal
214 template <typename Sig> class CallbackList;
217 $range ARITY 0..MAX_ARITY
223 class CallbackList<void(void)>
224 : public internal::CallbackListBase<Callback<void(void)> > {
226 template <$for ARG , [[typename A$(ARG)]]>
227 class CallbackList<void($for ARG , [[A$(ARG)]])>
228 : public internal::CallbackListBase<Callback<void($for ARG , [[A$(ARG)]])> > {
234 typedef Callback<void(void)> CallbackType;
237 typedef Callback<void($for ARG , [[A$(ARG)]])> CallbackType;
243 void Notify($for ARG ,
244 [[typename internal::CallbackParamTraits<A$(ARG)>::ForwardType a$(ARG)]]) {
247 internal::CallbackListBase<CallbackType>::Iterator it =
251 typename internal::CallbackListBase<CallbackType>::Iterator it =
256 while ((cb = it.GetNext()) != NULL) {
257 cb->Run($for ARG , [[a$(ARG)]]);
262 DISALLOW_COPY_AND_ASSIGN(CallbackList);
269 #endif // BASE_CALLBACK_LIST_H_