1 // This file was GENERATED by command:
2 // pump.py callback.h.pump
3 // DO NOT EDIT BY HAND!!!
7 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
8 // Use of this source code is governed by a BSD-style license that can be
9 // found in the LICENSE file.
11 #ifndef BASE_CALLBACK_H_
12 #define BASE_CALLBACK_H_
15 #include "base/callback_internal.h"
16 #include "base/template_util.h"
18 // New, super-duper, unified Callback system. This will eventually replace
19 // NewRunnableMethod, NewRunnableFunction, CreateFunctor, and CreateCallback
20 // systems currently in the Chromium code base.
24 // The templated Callback class is a generalized function object. Together
25 // with the Bind() function in bind.h, they provide a type-safe method for
26 // performing currying of arguments, and creating a "closure."
28 // In programming languages, a closure is a first-class function where all its
29 // parameters have been bound (usually via currying). Closures are well
30 // suited for representing, and passing around a unit of delayed execution.
31 // They are used in Chromium code to schedule tasks on different MessageLoops.
34 // MEMORY MANAGEMENT AND PASSING
36 // The Callback objects themselves should be passed by const-reference, and
37 // stored by copy. They internally store their state via a refcounted class
38 // and thus do not need to be deleted.
40 // The reason to pass via a const-reference is to avoid unnecessary
41 // AddRef/Release pairs to the internal state.
46 // /* Binding a normal function. */
47 // int Return5() { return 5; }
48 // base::Callback<int(void)> func_cb = base::Bind(&Return5);
49 // LOG(INFO) << func_cb.Run(); // Prints 5.
51 // void PrintHi() { LOG(INFO) << "hi."; }
52 // base::Closure void_func_cb = base::Bind(&PrintHi);
53 // void_func_cb.Run(); // Prints: hi.
55 // /* Binding a class method. */
56 // class Ref : public RefCountedThreadSafe<Ref> {
58 // int Foo() { return 3; }
59 // void PrintBye() { LOG(INFO) << "bye."; }
61 // scoped_refptr<Ref> ref = new Ref();
62 // base::Callback<int(void)> ref_cb = base::Bind(&Ref::Foo, ref.get());
63 // LOG(INFO) << ref_cb.Run(); // Prints out 3.
65 // base::Closure void_ref_cb = base::Bind(&Ref::PrintBye, ref.get());
66 // void_ref_cb.Run(); // Prints: bye.
68 // /* Binding a class method in a non-refcounted class.
70 // * WARNING: You must be sure the referee outlives the callback!
71 // * This is particularly important if you post a closure to a
72 // * MessageLoop because then it becomes hard to know what the
73 // * lifetime of the referee needs to be.
77 // int Foo() { return 4; }
78 // void PrintWhy() { LOG(INFO) << "why???"; }
81 // base::Callback<int(void)> base::no_ref_cb =
82 // base::Bind(&NoRef::Foo, base::Unretained(&no_ref));
83 // LOG(INFO) << ref_cb.Run(); // Prints out 4.
85 // base::Closure void_no_ref_cb =
86 // base::Bind(&NoRef::PrintWhy, base::Unretained(no_ref));
87 // void_no_ref_cb.Run(); // Prints: why???
89 // /* Binding a reference. */
90 // int Identity(int n) { return n; }
92 // base::Callback<int(void)> bound_copy_cb = base::Bind(&Identity, value);
93 // base::Callback<int(void)> bound_ref_cb =
94 // base::Bind(&Identity, base::ConstRef(value));
95 // LOG(INFO) << bound_copy_cb.Run(); // Prints 1.
96 // LOG(INFO) << bound_ref_cb.Run(); // Prints 1.
98 // LOG(INFO) << bound_copy_cb.Run(); // Prints 1.
99 // LOG(INFO) << bound_ref_cb.Run(); // Prints 2.
101 // /* Currying parameters. This also works for methods. */
102 // int Sum(int a, int b, int c) {
105 // base::Callback<int(int, int)> sum3_cb = base::Bind(&Sum, 3);
106 // LOG(INFO) << sum3_cb.Run(4, 5); // Prints 12.
108 // base::Callback<int(int)> sum7_cb = base::Bind(&Sum, 3, 4);
109 // LOG(INFO) << sum7_cb.Run(10); // Prints 17.
112 // WHERE IS THIS DESIGN FROM:
114 // The design Callback and Bind is heavily influenced by C++'s
115 // tr1::function/tr1::bind, and by the "Google Callback" system used inside
119 // HOW THE IMPLEMENTATION WORKS:
121 // There are three main components to the system:
122 // 1) The Callback classes.
123 // 2) The Bind() functions.
124 // 3) The arguments wrappers (eg., Unretained() and ConstRef()).
126 // The Callback classes represent a generic function pointer. Internally,
127 // it stores a refcounted piece of state that represents the target function
128 // and all its bound parameters. Each Callback specialization has a templated
129 // constructor that takes an InvokerStorageHolder<> object. In the context of
130 // the constructor, the static type of this InvokerStorageHolder<> object
131 // uniquely identifies the function it is representing, all its bound
132 // parameters, and a DoInvoke() that is capable of invoking the target.
134 // Callback's constructor is takes the InvokerStorageHolder<> that has the
135 // full static type and erases the target function type, and the bound
136 // parameters. It does this by storing a pointer to the specific DoInvoke()
137 // function, and upcasting the state of InvokerStorageHolder<> to a
138 // InvokerStorageBase. This is safe as long as this InvokerStorageBase pointer
139 // is only used with the stored DoInvoke() pointer.
141 // To create InvokerStorageHolder<> objects, we use the Bind() functions.
142 // These functions, along with a set of internal templates, are reponsible for
144 // - Unwrapping the function signature into return type, and parameters
145 // - Determining the number of parameters that are bound
146 // - Creating the storage for the bound parameters
147 // - Performing compile-time asserts to avoid error-prone behavior
148 // - Returning an InvokerStorageHolder<> with an DoInvoke() that has an arity
149 // matching the number of unbound parameters, and knows the correct
150 // refcounting semantics for the target object if we are binding a class
153 // The Bind functions do the above using type-inference, and template
156 // By default Bind() will store copies of all bound parameters, and attempt
157 // to refcount a target object if the function being bound is a class method.
159 // To change this behavior, we introduce a set of argument wrappers
160 // (eg. Unretained(), and ConstRef()). These are simple container templates
161 // that are passed by value, and wrap a pointer to argument. See the
162 // file-level comment in base/bind_helpers.h for more info.
164 // These types are passed to the Unwrap() functions, and the MaybeRefcount()
165 // functions respectively to modify the behavior of Bind(). The Unwrap()
166 // and MaybeRefcount() functions change behavior by doing partial
167 // specialization based on whether or not a parameter is a wrapper type.
169 // ConstRef() is similar to tr1::cref. Unretained() is specific to Chromium.
172 // WHY NOT TR1 FUNCTION/BIND?
174 // Direct use of tr1::function and tr1::bind was considered, but ultimately
175 // rejected because of the number of copy constructors invocations involved
176 // in the binding of arguments during construction, and the forwarding of
177 // arguments during invocation. These copies will no longer be an issue in
178 // C++0x because C++0x will support rvalue reference allowing for the compiler
179 // to avoid these copies. However, waiting for C++0x is not an option.
181 // Measured with valgrind on gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5), the
182 // tr1::bind call itself will invoke a non-trivial copy constructor three times
183 // for each bound parameter. Also, each when passing a tr1::function, each
184 // bound argument will be copied again.
186 // In addition to the copies taken at binding and invocation, copying a
187 // tr1::function causes a copy to be made of all the bound parameters and
190 // Furthermore, in Chromium, it is desirable for the Callback to take a
191 // reference on a target object when representing a class method call. This
192 // is not supported by tr1.
194 // Lastly, tr1::function and tr1::bind has a more general and flexible API.
195 // This includes things like argument reordering by use of
196 // tr1::bind::placeholder, support for non-const reference parameters, and some
197 // limited amount of subtyping of the tr1::function object (eg.,
198 // tr1::function<int(int)> is convertible to tr1::function<void(int)>).
200 // These are not features that are required in Chromium. Some of them, such as
201 // allowing for reference parameters, and subtyping of functions, may actually
202 // become a source of errors. Removing support for these features actually
203 // allows for a simpler implementation, and a terser Currying API.
206 // WHY NOT GOOGLE CALLBACKS?
208 // The Google callback system also does not support refcounting. Furthermore,
209 // its implementation has a number of strange edge cases with respect to type
210 // conversion of its arguments. In particular, the argument's constness must
211 // at times match exactly the function signature, or the type-inference might
212 // break. Given the above, writing a custom solution was easier.
215 // MISSING FUNCTIONALITY
216 // - Invoking the return of Bind. Bind(&foo).Run() does not work;
217 // - Binding arrays to functions that take a non-const pointer.
219 // void Foo(const char* ptr);
220 // void Bar(char* ptr);
221 // Bind(&Foo, "test");
222 // Bind(&Bar, "test"); // This fails because ptr is not const.
226 // First, we forward declare the Callback class template. This informs the
227 // compiler that the template only has 1 type parameter which is the function
228 // signature that the Callback is representing.
230 // After this, create template specializations for 0-6 parameters. Note that
231 // even though the template typelist grows, the specialization still
232 // only has one type: the function signature.
233 template <typename Sig
>
236 template <typename R
>
237 class Callback
<R(void)> : public internal::CallbackBase
{
239 typedef R(*PolymorphicInvoke
)(
240 internal::InvokerStorageBase
*);
242 Callback() : CallbackBase(NULL
, NULL
) { }
244 // We pass InvokerStorageHolder by const ref to avoid incurring an
245 // unnecessary AddRef/Unref pair even though we will modify the object.
246 // We cannot use a normal reference because the compiler will warn
247 // since this is often used on a return value, which is a temporary.
249 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
250 // return the exact Callback<> type. See base/bind.h for details.
251 template <typename T
>
252 Callback(const internal::InvokerStorageHolder
<T
>& invoker_holder
)
254 reinterpret_cast<InvokeFuncStorage
>(&T::Invoker::DoInvoke
),
255 &invoker_holder
.invoker_storage_
) {
256 COMPILE_ASSERT((is_same
<PolymorphicInvoke
,
257 typename
T::Invoker::DoInvokeType
>::value
),
258 callback_type_does_not_match_bind_result
);
261 bool Equals(const Callback
& other
) const {
262 return CallbackBase::Equals(other
);
266 PolymorphicInvoke f
=
267 reinterpret_cast<PolymorphicInvoke
>(polymorphic_invoke_
);
269 return f(invoker_storage_
.get());
273 template <typename R
, typename A1
>
274 class Callback
<R(A1
)> : public internal::CallbackBase
{
276 typedef R(*PolymorphicInvoke
)(
277 internal::InvokerStorageBase
*,
278 typename
internal::ParamTraits
<A1
>::ForwardType
);
280 Callback() : CallbackBase(NULL
, NULL
) { }
282 // We pass InvokerStorageHolder by const ref to avoid incurring an
283 // unnecessary AddRef/Unref pair even though we will modify the object.
284 // We cannot use a normal reference because the compiler will warn
285 // since this is often used on a return value, which is a temporary.
287 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
288 // return the exact Callback<> type. See base/bind.h for details.
289 template <typename T
>
290 Callback(const internal::InvokerStorageHolder
<T
>& invoker_holder
)
292 reinterpret_cast<InvokeFuncStorage
>(&T::Invoker::DoInvoke
),
293 &invoker_holder
.invoker_storage_
) {
294 COMPILE_ASSERT((is_same
<PolymorphicInvoke
,
295 typename
T::Invoker::DoInvokeType
>::value
),
296 callback_type_does_not_match_bind_result
);
299 bool Equals(const Callback
& other
) const {
300 return CallbackBase::Equals(other
);
303 R
Run(typename
internal::ParamTraits
<A1
>::ForwardType a1
) const {
304 PolymorphicInvoke f
=
305 reinterpret_cast<PolymorphicInvoke
>(polymorphic_invoke_
);
307 return f(invoker_storage_
.get(), a1
);
311 template <typename R
, typename A1
, typename A2
>
312 class Callback
<R(A1
, A2
)> : public internal::CallbackBase
{
314 typedef R(*PolymorphicInvoke
)(
315 internal::InvokerStorageBase
*,
316 typename
internal::ParamTraits
<A1
>::ForwardType
,
317 typename
internal::ParamTraits
<A2
>::ForwardType
);
319 Callback() : CallbackBase(NULL
, NULL
) { }
321 // We pass InvokerStorageHolder by const ref to avoid incurring an
322 // unnecessary AddRef/Unref pair even though we will modify the object.
323 // We cannot use a normal reference because the compiler will warn
324 // since this is often used on a return value, which is a temporary.
326 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
327 // return the exact Callback<> type. See base/bind.h for details.
328 template <typename T
>
329 Callback(const internal::InvokerStorageHolder
<T
>& invoker_holder
)
331 reinterpret_cast<InvokeFuncStorage
>(&T::Invoker::DoInvoke
),
332 &invoker_holder
.invoker_storage_
) {
333 COMPILE_ASSERT((is_same
<PolymorphicInvoke
,
334 typename
T::Invoker::DoInvokeType
>::value
),
335 callback_type_does_not_match_bind_result
);
338 bool Equals(const Callback
& other
) const {
339 return CallbackBase::Equals(other
);
342 R
Run(typename
internal::ParamTraits
<A1
>::ForwardType a1
,
343 typename
internal::ParamTraits
<A2
>::ForwardType a2
) const {
344 PolymorphicInvoke f
=
345 reinterpret_cast<PolymorphicInvoke
>(polymorphic_invoke_
);
347 return f(invoker_storage_
.get(), a1
,
352 template <typename R
, typename A1
, typename A2
, typename A3
>
353 class Callback
<R(A1
, A2
, A3
)> : public internal::CallbackBase
{
355 typedef R(*PolymorphicInvoke
)(
356 internal::InvokerStorageBase
*,
357 typename
internal::ParamTraits
<A1
>::ForwardType
,
358 typename
internal::ParamTraits
<A2
>::ForwardType
,
359 typename
internal::ParamTraits
<A3
>::ForwardType
);
361 Callback() : CallbackBase(NULL
, NULL
) { }
363 // We pass InvokerStorageHolder by const ref to avoid incurring an
364 // unnecessary AddRef/Unref pair even though we will modify the object.
365 // We cannot use a normal reference because the compiler will warn
366 // since this is often used on a return value, which is a temporary.
368 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
369 // return the exact Callback<> type. See base/bind.h for details.
370 template <typename T
>
371 Callback(const internal::InvokerStorageHolder
<T
>& invoker_holder
)
373 reinterpret_cast<InvokeFuncStorage
>(&T::Invoker::DoInvoke
),
374 &invoker_holder
.invoker_storage_
) {
375 COMPILE_ASSERT((is_same
<PolymorphicInvoke
,
376 typename
T::Invoker::DoInvokeType
>::value
),
377 callback_type_does_not_match_bind_result
);
380 bool Equals(const Callback
& other
) const {
381 return CallbackBase::Equals(other
);
384 R
Run(typename
internal::ParamTraits
<A1
>::ForwardType a1
,
385 typename
internal::ParamTraits
<A2
>::ForwardType a2
,
386 typename
internal::ParamTraits
<A3
>::ForwardType a3
) const {
387 PolymorphicInvoke f
=
388 reinterpret_cast<PolymorphicInvoke
>(polymorphic_invoke_
);
390 return f(invoker_storage_
.get(), a1
,
396 template <typename R
, typename A1
, typename A2
, typename A3
, typename A4
>
397 class Callback
<R(A1
, A2
, A3
, A4
)> : public internal::CallbackBase
{
399 typedef R(*PolymorphicInvoke
)(
400 internal::InvokerStorageBase
*,
401 typename
internal::ParamTraits
<A1
>::ForwardType
,
402 typename
internal::ParamTraits
<A2
>::ForwardType
,
403 typename
internal::ParamTraits
<A3
>::ForwardType
,
404 typename
internal::ParamTraits
<A4
>::ForwardType
);
406 Callback() : CallbackBase(NULL
, NULL
) { }
408 // We pass InvokerStorageHolder by const ref to avoid incurring an
409 // unnecessary AddRef/Unref pair even though we will modify the object.
410 // We cannot use a normal reference because the compiler will warn
411 // since this is often used on a return value, which is a temporary.
413 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
414 // return the exact Callback<> type. See base/bind.h for details.
415 template <typename T
>
416 Callback(const internal::InvokerStorageHolder
<T
>& invoker_holder
)
418 reinterpret_cast<InvokeFuncStorage
>(&T::Invoker::DoInvoke
),
419 &invoker_holder
.invoker_storage_
) {
420 COMPILE_ASSERT((is_same
<PolymorphicInvoke
,
421 typename
T::Invoker::DoInvokeType
>::value
),
422 callback_type_does_not_match_bind_result
);
425 bool Equals(const Callback
& other
) const {
426 return CallbackBase::Equals(other
);
429 R
Run(typename
internal::ParamTraits
<A1
>::ForwardType a1
,
430 typename
internal::ParamTraits
<A2
>::ForwardType a2
,
431 typename
internal::ParamTraits
<A3
>::ForwardType a3
,
432 typename
internal::ParamTraits
<A4
>::ForwardType a4
) const {
433 PolymorphicInvoke f
=
434 reinterpret_cast<PolymorphicInvoke
>(polymorphic_invoke_
);
436 return f(invoker_storage_
.get(), a1
,
443 template <typename R
, typename A1
, typename A2
, typename A3
, typename A4
,
445 class Callback
<R(A1
, A2
, A3
, A4
, A5
)> : public internal::CallbackBase
{
447 typedef R(*PolymorphicInvoke
)(
448 internal::InvokerStorageBase
*,
449 typename
internal::ParamTraits
<A1
>::ForwardType
,
450 typename
internal::ParamTraits
<A2
>::ForwardType
,
451 typename
internal::ParamTraits
<A3
>::ForwardType
,
452 typename
internal::ParamTraits
<A4
>::ForwardType
,
453 typename
internal::ParamTraits
<A5
>::ForwardType
);
455 Callback() : CallbackBase(NULL
, NULL
) { }
457 // We pass InvokerStorageHolder by const ref to avoid incurring an
458 // unnecessary AddRef/Unref pair even though we will modify the object.
459 // We cannot use a normal reference because the compiler will warn
460 // since this is often used on a return value, which is a temporary.
462 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
463 // return the exact Callback<> type. See base/bind.h for details.
464 template <typename T
>
465 Callback(const internal::InvokerStorageHolder
<T
>& invoker_holder
)
467 reinterpret_cast<InvokeFuncStorage
>(&T::Invoker::DoInvoke
),
468 &invoker_holder
.invoker_storage_
) {
469 COMPILE_ASSERT((is_same
<PolymorphicInvoke
,
470 typename
T::Invoker::DoInvokeType
>::value
),
471 callback_type_does_not_match_bind_result
);
474 bool Equals(const Callback
& other
) const {
475 return CallbackBase::Equals(other
);
478 R
Run(typename
internal::ParamTraits
<A1
>::ForwardType a1
,
479 typename
internal::ParamTraits
<A2
>::ForwardType a2
,
480 typename
internal::ParamTraits
<A3
>::ForwardType a3
,
481 typename
internal::ParamTraits
<A4
>::ForwardType a4
,
482 typename
internal::ParamTraits
<A5
>::ForwardType a5
) const {
483 PolymorphicInvoke f
=
484 reinterpret_cast<PolymorphicInvoke
>(polymorphic_invoke_
);
486 return f(invoker_storage_
.get(), a1
,
494 template <typename R
, typename A1
, typename A2
, typename A3
, typename A4
,
495 typename A5
, typename A6
>
496 class Callback
<R(A1
, A2
, A3
, A4
, A5
, A6
)> : public internal::CallbackBase
{
498 typedef R(*PolymorphicInvoke
)(
499 internal::InvokerStorageBase
*,
500 typename
internal::ParamTraits
<A1
>::ForwardType
,
501 typename
internal::ParamTraits
<A2
>::ForwardType
,
502 typename
internal::ParamTraits
<A3
>::ForwardType
,
503 typename
internal::ParamTraits
<A4
>::ForwardType
,
504 typename
internal::ParamTraits
<A5
>::ForwardType
,
505 typename
internal::ParamTraits
<A6
>::ForwardType
);
507 Callback() : CallbackBase(NULL
, NULL
) { }
509 // We pass InvokerStorageHolder by const ref to avoid incurring an
510 // unnecessary AddRef/Unref pair even though we will modify the object.
511 // We cannot use a normal reference because the compiler will warn
512 // since this is often used on a return value, which is a temporary.
514 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
515 // return the exact Callback<> type. See base/bind.h for details.
516 template <typename T
>
517 Callback(const internal::InvokerStorageHolder
<T
>& invoker_holder
)
519 reinterpret_cast<InvokeFuncStorage
>(&T::Invoker::DoInvoke
),
520 &invoker_holder
.invoker_storage_
) {
521 COMPILE_ASSERT((is_same
<PolymorphicInvoke
,
522 typename
T::Invoker::DoInvokeType
>::value
),
523 callback_type_does_not_match_bind_result
);
526 bool Equals(const Callback
& other
) const {
527 return CallbackBase::Equals(other
);
530 R
Run(typename
internal::ParamTraits
<A1
>::ForwardType a1
,
531 typename
internal::ParamTraits
<A2
>::ForwardType a2
,
532 typename
internal::ParamTraits
<A3
>::ForwardType a3
,
533 typename
internal::ParamTraits
<A4
>::ForwardType a4
,
534 typename
internal::ParamTraits
<A5
>::ForwardType a5
,
535 typename
internal::ParamTraits
<A6
>::ForwardType a6
) const {
536 PolymorphicInvoke f
=
537 reinterpret_cast<PolymorphicInvoke
>(polymorphic_invoke_
);
539 return f(invoker_storage_
.get(), a1
,
549 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it
550 // will be used in a lot of APIs with delayed execution.
551 typedef Callback
<void(void)> Closure
;
555 #endif // BASE_CALLBACK_H