1 // This file was GENERATED by command:
2 // pump.py callback_list.h.pump
3 // DO NOT EDIT BY HAND!!!
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
8 // found in the LICENSE file.
10 #ifndef BASE_CALLBACK_LIST_H_
11 #define BASE_CALLBACK_LIST_H_
15 #include "base/basictypes.h"
16 #include "base/callback.h"
17 #include "base/callback_internal.h"
18 #include "base/compiler_specific.h"
19 #include "base/logging.h"
20 #include "base/memory/scoped_ptr.h"
24 // A container for a list of callbacks. Unlike a normal STL vector or list,
25 // this container can be modified during iteration without invalidating the
26 // iterator. It safely handles the case of a callback removing itself
27 // or another callback from the list while callbacks are being run.
35 // typedef base::Callback<void(const Foo&)> OnFooCallback;
37 // scoped_ptr<base::CallbackList<void(const Foo&)>::Subscription>
38 // RegisterCallback(const OnFooCallback& cb) {
39 // return callback_list_.Add(cb);
43 // void NotifyFoo(const Foo& foo) {
44 // callback_list_.Notify(foo);
47 // base::CallbackList<void(const Foo&)> callback_list_;
49 // DISALLOW_COPY_AND_ASSIGN(MyWidget);
53 // class MyWidgetListener {
55 // MyWidgetListener::MyWidgetListener() {
56 // foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback(
57 // base::Bind(&MyWidgetListener::OnFoo, this)));
60 // MyWidgetListener::~MyWidgetListener() {
61 // // Subscription gets deleted automatically and will deregister
62 // // the callback in the process.
66 // void OnFoo(const Foo& foo) {
70 // scoped_ptr<base::CallbackList<void(const Foo&)>::Subscription>
73 // DISALLOW_COPY_AND_ASSIGN(MyWidgetListener);
80 template <typename CallbackType
>
81 class CallbackListBase
{
85 Subscription(CallbackListBase
<CallbackType
>* list
,
86 typename
std::list
<CallbackType
>::iterator iter
)
92 if (list_
->active_iterator_count_
)
95 list_
->callbacks_
.erase(iter_
);
99 CallbackListBase
<CallbackType
>* list_
;
100 typename
std::list
<CallbackType
>::iterator iter_
;
102 DISALLOW_COPY_AND_ASSIGN(Subscription
);
105 // Add a callback to the list. The callback will remain registered until the
106 // returned Subscription is destroyed, which must occur before the
107 // CallbackList is destroyed.
108 scoped_ptr
<Subscription
> Add(const CallbackType
& cb
) {
109 DCHECK(!cb
.is_null());
110 return scoped_ptr
<Subscription
>(
111 new Subscription(this, callbacks_
.insert(callbacks_
.end(), cb
)));
115 // An iterator class that can be used to access the list of callbacks.
118 explicit Iterator(CallbackListBase
<CallbackType
>* list
)
120 list_iter_(list_
->callbacks_
.begin()) {
121 ++list_
->active_iterator_count_
;
124 Iterator(const Iterator
& iter
)
126 list_iter_(iter
.list_iter_
) {
127 ++list_
->active_iterator_count_
;
131 if (list_
&& --list_
->active_iterator_count_
== 0) {
136 CallbackType
* GetNext() {
137 while ((list_iter_
!= list_
->callbacks_
.end()) && list_iter_
->is_null())
140 CallbackType
* cb
= NULL
;
141 if (list_iter_
!= list_
->callbacks_
.end()) {
149 CallbackListBase
<CallbackType
>* list_
;
150 typename
std::list
<CallbackType
>::iterator list_iter_
;
153 CallbackListBase() : active_iterator_count_(0) {}
155 ~CallbackListBase() {
156 DCHECK_EQ(0, active_iterator_count_
);
157 DCHECK_EQ(0U, callbacks_
.size());
160 // Returns an instance of a CallbackListBase::Iterator which can be used
162 Iterator
GetIterator() {
163 return Iterator(this);
166 // Compact the list: remove any entries which were NULLed out during
169 typename
std::list
<CallbackType
>::iterator it
= callbacks_
.begin();
170 while (it
!= callbacks_
.end()) {
172 it
= callbacks_
.erase(it
);
179 std::list
<CallbackType
> callbacks_
;
180 int active_iterator_count_
;
182 DISALLOW_COPY_AND_ASSIGN(CallbackListBase
);
185 } // namespace internal
187 template <typename Sig
> class CallbackList
;
190 class CallbackList
<void(void)>
191 : public internal::CallbackListBase
<Callback
<void(void)> > {
193 typedef Callback
<void(void)> CallbackType
;
198 internal::CallbackListBase
<CallbackType
>::Iterator it
=
201 while ((cb
= it
.GetNext()) != NULL
) {
207 DISALLOW_COPY_AND_ASSIGN(CallbackList
);
210 template <typename A1
>
211 class CallbackList
<void(A1
)>
212 : public internal::CallbackListBase
<Callback
<void(A1
)> > {
214 typedef Callback
<void(A1
)> CallbackType
;
218 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
) {
219 typename
internal::CallbackListBase
<CallbackType
>::Iterator it
=
222 while ((cb
= it
.GetNext()) != NULL
) {
228 DISALLOW_COPY_AND_ASSIGN(CallbackList
);
231 template <typename A1
, typename A2
>
232 class CallbackList
<void(A1
, A2
)>
233 : public internal::CallbackListBase
<Callback
<void(A1
, A2
)> > {
235 typedef Callback
<void(A1
, A2
)> CallbackType
;
239 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
,
240 typename
internal::CallbackParamTraits
<A2
>::ForwardType a2
) {
241 typename
internal::CallbackListBase
<CallbackType
>::Iterator it
=
244 while ((cb
= it
.GetNext()) != NULL
) {
250 DISALLOW_COPY_AND_ASSIGN(CallbackList
);
253 template <typename A1
, typename A2
, typename A3
>
254 class CallbackList
<void(A1
, A2
, A3
)>
255 : public internal::CallbackListBase
<Callback
<void(A1
, A2
, A3
)> > {
257 typedef Callback
<void(A1
, A2
, A3
)> CallbackType
;
261 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
,
262 typename
internal::CallbackParamTraits
<A2
>::ForwardType a2
,
263 typename
internal::CallbackParamTraits
<A3
>::ForwardType a3
) {
264 typename
internal::CallbackListBase
<CallbackType
>::Iterator it
=
267 while ((cb
= it
.GetNext()) != NULL
) {
273 DISALLOW_COPY_AND_ASSIGN(CallbackList
);
276 template <typename A1
, typename A2
, typename A3
, typename A4
>
277 class CallbackList
<void(A1
, A2
, A3
, A4
)>
278 : public internal::CallbackListBase
<Callback
<void(A1
, A2
, A3
, A4
)> > {
280 typedef Callback
<void(A1
, A2
, A3
, A4
)> CallbackType
;
284 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
,
285 typename
internal::CallbackParamTraits
<A2
>::ForwardType a2
,
286 typename
internal::CallbackParamTraits
<A3
>::ForwardType a3
,
287 typename
internal::CallbackParamTraits
<A4
>::ForwardType a4
) {
288 typename
internal::CallbackListBase
<CallbackType
>::Iterator it
=
291 while ((cb
= it
.GetNext()) != NULL
) {
292 cb
->Run(a1
, a2
, a3
, a4
);
297 DISALLOW_COPY_AND_ASSIGN(CallbackList
);
300 template <typename A1
, typename A2
, typename A3
, typename A4
, typename A5
>
301 class CallbackList
<void(A1
, A2
, A3
, A4
, A5
)>
302 : public internal::CallbackListBase
<Callback
<void(A1
, A2
, A3
, A4
, A5
)> > {
304 typedef Callback
<void(A1
, A2
, A3
, A4
, A5
)> CallbackType
;
308 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
,
309 typename
internal::CallbackParamTraits
<A2
>::ForwardType a2
,
310 typename
internal::CallbackParamTraits
<A3
>::ForwardType a3
,
311 typename
internal::CallbackParamTraits
<A4
>::ForwardType a4
,
312 typename
internal::CallbackParamTraits
<A5
>::ForwardType a5
) {
313 typename
internal::CallbackListBase
<CallbackType
>::Iterator it
=
316 while ((cb
= it
.GetNext()) != NULL
) {
317 cb
->Run(a1
, a2
, a3
, a4
, a5
);
322 DISALLOW_COPY_AND_ASSIGN(CallbackList
);
325 template <typename A1
, typename A2
, typename A3
, typename A4
, typename A5
,
327 class CallbackList
<void(A1
, A2
, A3
, A4
, A5
, A6
)>
328 : public internal::CallbackListBase
<Callback
<void(A1
, A2
, A3
, A4
, A5
,
331 typedef Callback
<void(A1
, A2
, A3
, A4
, A5
, A6
)> CallbackType
;
335 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
,
336 typename
internal::CallbackParamTraits
<A2
>::ForwardType a2
,
337 typename
internal::CallbackParamTraits
<A3
>::ForwardType a3
,
338 typename
internal::CallbackParamTraits
<A4
>::ForwardType a4
,
339 typename
internal::CallbackParamTraits
<A5
>::ForwardType a5
,
340 typename
internal::CallbackParamTraits
<A6
>::ForwardType a6
) {
341 typename
internal::CallbackListBase
<CallbackType
>::Iterator it
=
344 while ((cb
= it
.GetNext()) != NULL
) {
345 cb
->Run(a1
, a2
, a3
, a4
, a5
, a6
);
350 DISALLOW_COPY_AND_ASSIGN(CallbackList
);
353 template <typename A1
, typename A2
, typename A3
, typename A4
, typename A5
,
354 typename A6
, typename A7
>
355 class CallbackList
<void(A1
, A2
, A3
, A4
, A5
, A6
, A7
)>
356 : public internal::CallbackListBase
<Callback
<void(A1
, A2
, A3
, A4
, A5
, A6
,
359 typedef Callback
<void(A1
, A2
, A3
, A4
, A5
, A6
, A7
)> CallbackType
;
363 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
,
364 typename
internal::CallbackParamTraits
<A2
>::ForwardType a2
,
365 typename
internal::CallbackParamTraits
<A3
>::ForwardType a3
,
366 typename
internal::CallbackParamTraits
<A4
>::ForwardType a4
,
367 typename
internal::CallbackParamTraits
<A5
>::ForwardType a5
,
368 typename
internal::CallbackParamTraits
<A6
>::ForwardType a6
,
369 typename
internal::CallbackParamTraits
<A7
>::ForwardType a7
) {
370 typename
internal::CallbackListBase
<CallbackType
>::Iterator it
=
373 while ((cb
= it
.GetNext()) != NULL
) {
374 cb
->Run(a1
, a2
, a3
, a4
, a5
, a6
, a7
);
379 DISALLOW_COPY_AND_ASSIGN(CallbackList
);
384 #endif // BASE_CALLBACK_LIST_H_