1 // This file was GENERATED by command:
2 // pump.py callback_registry.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_REGISTRY_H_
11 #define BASE_CALLBACK_REGISTRY_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::CallbackRegistry<void(const Foo&)>::Subscription>
38 // RegisterCallback(const OnFooCallback& cb) {
39 // return callback_registry_.Add(cb);
43 // void NotifyFoo(const Foo& foo) {
44 // callback_registry_.Notify(foo);
47 // base::CallbackRegistry<void(const Foo&)> callback_registry_;
51 // class MyWidgetListener {
53 // MyWidgetListener::MyWidgetListener() {
54 // foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback(
55 // base::Bind(&MyWidgetListener::OnFoo, this)));
58 // MyWidgetListener::~MyWidgetListener() {
59 // // Subscription gets deleted automatically and will deregister
60 // // the callback in the process.
64 // void OnFoo(const Foo& foo) {
68 // scoped_ptr<base::CallbackRegistry<void(const Foo&)>::Subscription>
76 template <typename CallbackType
>
77 class CallbackRegistryBase
{
81 Subscription(CallbackRegistryBase
<CallbackType
>* list
,
82 typename
std::list
<CallbackType
>::iterator iter
)
87 if (list_
->active_iterator_count_
)
90 list_
->callbacks_
.erase(iter_
);
94 CallbackRegistryBase
<CallbackType
>* list_
;
95 typename
std::list
<CallbackType
>::iterator iter_
;
97 DISALLOW_COPY_AND_ASSIGN(Subscription
);
100 // Add a callback to the list. The callback will remain registered until the
101 // returned Subscription is destroyed, which must occur before the
102 // CallbackRegistry is destroyed.
103 scoped_ptr
<Subscription
> Add(const CallbackType
& cb
) {
104 DCHECK(!cb
.is_null());
105 return scoped_ptr
<Subscription
>(
106 new Subscription(this, callbacks_
.insert(callbacks_
.end(), cb
)));
110 // An iterator class that can be used to access the list of callbacks.
113 explicit Iterator(CallbackRegistryBase
<CallbackType
>* list
)
115 list_iter_(list_
->callbacks_
.begin()) {
116 ++list_
->active_iterator_count_
;
119 Iterator(const Iterator
& iter
)
121 list_iter_(iter
.list_iter_
) {
122 ++list_
->active_iterator_count_
;
126 if (list_
&& --list_
->active_iterator_count_
== 0) {
131 CallbackType
* GetNext() {
132 while ((list_iter_
!= list_
->callbacks_
.end()) && list_iter_
->is_null())
135 CallbackType
* cb
= NULL
;
136 if (list_iter_
!= list_
->callbacks_
.end()) {
144 CallbackRegistryBase
<CallbackType
>* list_
;
145 typename
std::list
<CallbackType
>::iterator list_iter_
;
148 CallbackRegistryBase()
149 : active_iterator_count_(0) {}
151 ~CallbackRegistryBase() {
152 DCHECK_EQ(0, active_iterator_count_
);
153 DCHECK_EQ(0U, callbacks_
.size());
156 // Returns an instance of a CallbackRegistryBase::Iterator which can be used
158 Iterator
GetIterator() {
159 return Iterator(this);
162 // Compact the list: remove any entries which were NULLed out during
165 typename
std::list
<CallbackType
>::iterator it
= callbacks_
.begin();
166 while (it
!= callbacks_
.end()) {
168 it
= callbacks_
.erase(it
);
175 std::list
<CallbackType
> callbacks_
;
176 int active_iterator_count_
;
178 DISALLOW_COPY_AND_ASSIGN(CallbackRegistryBase
);
181 } // namespace internal
183 template <typename Sig
> class CallbackRegistry
;
186 class CallbackRegistry
<void(void)>
187 : public internal::CallbackRegistryBase
<Callback
<void(void)> > {
189 typedef Callback
<void(void)> CallbackType
;
191 CallbackRegistry() {}
194 internal::CallbackRegistryBase
<CallbackType
>::Iterator it
=
197 while((cb
= it
.GetNext()) != NULL
) {
203 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry
);
206 template <typename A1
>
207 class CallbackRegistry
<void(A1
)>
208 : public internal::CallbackRegistryBase
<
209 Callback
<void(A1
)> > {
211 typedef Callback
<void(A1
)> CallbackType
;
213 CallbackRegistry() {}
215 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
) {
216 typename
internal::CallbackRegistryBase
<CallbackType
>::Iterator it
=
219 while((cb
= it
.GetNext()) != NULL
) {
225 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry
);
228 template <typename A1
, typename A2
>
229 class CallbackRegistry
<void(A1
, A2
)>
230 : public internal::CallbackRegistryBase
<
231 Callback
<void(A1
, A2
)> > {
233 typedef Callback
<void(A1
, A2
)> CallbackType
;
235 CallbackRegistry() {}
237 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
,
238 typename
internal::CallbackParamTraits
<A2
>::ForwardType a2
) {
239 typename
internal::CallbackRegistryBase
<CallbackType
>::Iterator it
=
242 while((cb
= it
.GetNext()) != NULL
) {
248 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry
);
251 template <typename A1
, typename A2
, typename A3
>
252 class CallbackRegistry
<void(A1
, A2
, A3
)>
253 : public internal::CallbackRegistryBase
<
254 Callback
<void(A1
, A2
, A3
)> > {
256 typedef Callback
<void(A1
, A2
, A3
)> CallbackType
;
258 CallbackRegistry() {}
260 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
,
261 typename
internal::CallbackParamTraits
<A2
>::ForwardType a2
,
262 typename
internal::CallbackParamTraits
<A3
>::ForwardType a3
) {
263 typename
internal::CallbackRegistryBase
<CallbackType
>::Iterator it
=
266 while((cb
= it
.GetNext()) != NULL
) {
272 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry
);
275 template <typename A1
, typename A2
, typename A3
, typename A4
>
276 class CallbackRegistry
<void(A1
, A2
, A3
, A4
)>
277 : public internal::CallbackRegistryBase
<
278 Callback
<void(A1
, A2
, A3
, A4
)> > {
280 typedef Callback
<void(A1
, A2
, A3
, A4
)> CallbackType
;
282 CallbackRegistry() {}
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::CallbackRegistryBase
<CallbackType
>::Iterator it
=
291 while((cb
= it
.GetNext()) != NULL
) {
292 cb
->Run(a1
, a2
, a3
, a4
);
297 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry
);
300 template <typename A1
, typename A2
, typename A3
, typename A4
, typename A5
>
301 class CallbackRegistry
<void(A1
, A2
, A3
, A4
, A5
)>
302 : public internal::CallbackRegistryBase
<
303 Callback
<void(A1
, A2
, A3
, A4
, A5
)> > {
305 typedef Callback
<void(A1
, A2
, A3
, A4
, A5
)> CallbackType
;
307 CallbackRegistry() {}
309 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
,
310 typename
internal::CallbackParamTraits
<A2
>::ForwardType a2
,
311 typename
internal::CallbackParamTraits
<A3
>::ForwardType a3
,
312 typename
internal::CallbackParamTraits
<A4
>::ForwardType a4
,
313 typename
internal::CallbackParamTraits
<A5
>::ForwardType a5
) {
314 typename
internal::CallbackRegistryBase
<CallbackType
>::Iterator it
=
317 while((cb
= it
.GetNext()) != NULL
) {
318 cb
->Run(a1
, a2
, a3
, a4
, a5
);
323 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry
);
326 template <typename A1
, typename A2
, typename A3
, typename A4
, typename A5
,
328 class CallbackRegistry
<void(A1
, A2
, A3
, A4
, A5
, A6
)>
329 : public internal::CallbackRegistryBase
<
330 Callback
<void(A1
, A2
, A3
, A4
, A5
, A6
)> > {
332 typedef Callback
<void(A1
, A2
, A3
, A4
, A5
, A6
)> CallbackType
;
334 CallbackRegistry() {}
336 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
,
337 typename
internal::CallbackParamTraits
<A2
>::ForwardType a2
,
338 typename
internal::CallbackParamTraits
<A3
>::ForwardType a3
,
339 typename
internal::CallbackParamTraits
<A4
>::ForwardType a4
,
340 typename
internal::CallbackParamTraits
<A5
>::ForwardType a5
,
341 typename
internal::CallbackParamTraits
<A6
>::ForwardType a6
) {
342 typename
internal::CallbackRegistryBase
<CallbackType
>::Iterator it
=
345 while((cb
= it
.GetNext()) != NULL
) {
346 cb
->Run(a1
, a2
, a3
, a4
, a5
, a6
);
351 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry
);
354 template <typename A1
, typename A2
, typename A3
, typename A4
, typename A5
,
355 typename A6
, typename A7
>
356 class CallbackRegistry
<void(A1
, A2
, A3
, A4
, A5
, A6
, A7
)>
357 : public internal::CallbackRegistryBase
<
358 Callback
<void(A1
, A2
, A3
, A4
, A5
, A6
, A7
)> > {
360 typedef Callback
<void(A1
, A2
, A3
, A4
, A5
, A6
, A7
)> CallbackType
;
362 CallbackRegistry() {}
364 void Notify(typename
internal::CallbackParamTraits
<A1
>::ForwardType a1
,
365 typename
internal::CallbackParamTraits
<A2
>::ForwardType a2
,
366 typename
internal::CallbackParamTraits
<A3
>::ForwardType a3
,
367 typename
internal::CallbackParamTraits
<A4
>::ForwardType a4
,
368 typename
internal::CallbackParamTraits
<A5
>::ForwardType a5
,
369 typename
internal::CallbackParamTraits
<A6
>::ForwardType a6
,
370 typename
internal::CallbackParamTraits
<A7
>::ForwardType a7
) {
371 typename
internal::CallbackRegistryBase
<CallbackType
>::Iterator it
=
374 while((cb
= it
.GetNext()) != NULL
) {
375 cb
->Run(a1
, a2
, a3
, a4
, a5
, a6
, a7
);
380 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry
);
385 #endif // BASE_CALLBACK_REGISTRY_H