Simple Cache: a few tests for rare corner cases with CRC check missing.
[chromium-blink-merge.git] / base / callback.h
blobade3f8c1fc000ab7267fecbd69a3f531bb7ba83b
1 // This file was GENERATED by command:
2 // pump.py callback.h.pump
3 // DO NOT EDIT BY HAND!!!
6 // Copyright (c) 2012 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_H_
11 #define BASE_CALLBACK_H_
13 #include "base/callback_forward.h"
14 #include "base/callback_internal.h"
15 #include "base/template_util.h"
17 // NOTE: Header files that do not require the full definition of Callback or
18 // Closure should #include "base/callback_forward.h" instead of this file.
20 // -----------------------------------------------------------------------------
21 // Introduction
22 // -----------------------------------------------------------------------------
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 partial application of functions.
28 // Partial application (or "currying") is the process of binding a subset of
29 // a function's arguments to produce another function that takes fewer
30 // arguments. This can be used to pass around a unit of delayed execution,
31 // much like lexical closures are used in other languages. For example, it
32 // is used in Chromium code to schedule tasks on different MessageLoops.
34 // A callback with no unbound input parameters (base::Callback<void(void)>)
35 // is called a base::Closure. Note that this is NOT the same as what other
36 // languages refer to as a closure -- it does not retain a reference to its
37 // enclosing environment.
39 // MEMORY MANAGEMENT AND PASSING
41 // The Callback objects themselves should be passed by const-reference, and
42 // stored by copy. They internally store their state via a refcounted class
43 // and thus do not need to be deleted.
45 // The reason to pass via a const-reference is to avoid unnecessary
46 // AddRef/Release pairs to the internal state.
49 // -----------------------------------------------------------------------------
50 // Quick reference for basic stuff
51 // -----------------------------------------------------------------------------
53 // BINDING A BARE FUNCTION
55 // int Return5() { return 5; }
56 // base::Callback<int(void)> func_cb = base::Bind(&Return5);
57 // LOG(INFO) << func_cb.Run(); // Prints 5.
59 // BINDING A CLASS METHOD
61 // The first argument to bind is the member function to call, the second is
62 // the object on which to call it.
64 // class Ref : public base::RefCountedThreadSafe<Ref> {
65 // public:
66 // int Foo() { return 3; }
67 // void PrintBye() { LOG(INFO) << "bye."; }
68 // };
69 // scoped_refptr<Ref> ref = new Ref();
70 // base::Callback<void(void)> ref_cb = base::Bind(&Ref::Foo, ref);
71 // LOG(INFO) << ref_cb.Run(); // Prints out 3.
73 // By default the object must support RefCounted or you will get a compiler
74 // error. If you're passing between threads, be sure it's
75 // RefCountedThreadSafe! See "Advanced binding of member functions" below if
76 // you don't want to use reference counting.
78 // RUNNING A CALLBACK
80 // Callbacks can be run with their "Run" method, which has the same
81 // signature as the template argument to the callback.
83 // void DoSomething(const base::Callback<void(int, std::string)>& callback) {
84 // callback.Run(5, "hello");
85 // }
87 // Callbacks can be run more than once (they don't get deleted or marked when
88 // run). However, this precludes using base::Passed (see below).
90 // void DoSomething(const base::Callback<double(double)>& callback) {
91 // double myresult = callback.Run(3.14159);
92 // myresult += callback.Run(2.71828);
93 // }
95 // PASSING UNBOUND INPUT PARAMETERS
97 // Unbound parameters are specified at the time a callback is Run(). They are
98 // specified in the Callback template type:
100 // void MyFunc(int i, const std::string& str) {}
101 // base::Callback<void(int, const std::string&)> cb = base::Bind(&MyFunc);
102 // cb.Run(23, "hello, world");
104 // PASSING BOUND INPUT PARAMETERS
106 // Bound parameters are specified when you create thee callback as arguments
107 // to Bind(). They will be passed to the function and the Run()ner of the
108 // callback doesn't see those values or even know that the function it's
109 // calling.
111 // void MyFunc(int i, const std::string& str) {}
112 // base::Callback<void(void)> cb = base::Bind(&MyFunc, 23, "hello world");
113 // cb.Run();
115 // A callback with no unbound input parameters (base::Callback<void(void)>)
116 // is called a base::Closure. So we could have also written:
118 // base::Closure cb = base::Bind(&MyFunc, 23, "hello world");
120 // When calling member functions, bound parameters just go after the object
121 // pointer.
123 // base::Closure cb = base::Bind(&MyClass::MyFunc, this, 23, "hello world");
125 // PARTIAL BINDING OF PARAMETERS
127 // You can specify some parameters when you create the callback, and specify
128 // the rest when you execute the callback.
130 // void MyFunc(int i, const std::string& str) {}
131 // base::Callback<void(const std::string&)> cb = base::Bind(&MyFunc, 23);
132 // cb.Run("hello world");
134 // When calling a function bound parameters are first, followed by unbound
135 // parameters.
138 // -----------------------------------------------------------------------------
139 // Quick reference for advanced binding
140 // -----------------------------------------------------------------------------
142 // BINDING A CLASS METHOD WITH WEAK POINTERS
144 // base::Bind(&MyClass::Foo, GetWeakPtr());
146 // The callback will not be issued if the object is destroyed at the time
147 // it's issued. DANGER: weak pointers are not threadsafe, so don't use this
148 // when passing between threads!
150 // BINDING A CLASS METHOD WITH MANUAL LIFETIME MANAGEMENT
152 // base::Bind(&MyClass::Foo, base::Unretained(this));
154 // This disables all lifetime management on the object. You're responsible
155 // for making sure the object is alive at the time of the call. You break it,
156 // you own it!
158 // BINDING A CLASS METHOD AND HAVING THE CALLBACK OWN THE CLASS
160 // MyClass* myclass = new MyClass;
161 // base::Bind(&MyClass::Foo, base::Owned(myclass));
163 // The object will be deleted when the callback is destroyed, even if it's
164 // not run (like if you post a task during shutdown). Potentially useful for
165 // "fire and forget" cases.
167 // IGNORING RETURN VALUES
169 // Sometimes you want to call a function that returns a value in a callback
170 // that doesn't expect a return value.
172 // int DoSomething(int arg) { cout << arg << endl; }
173 // base::Callback<void<int>) cb =
174 // base::Bind(base::IgnoreResult(&DoSomething));
177 // -----------------------------------------------------------------------------
178 // Quick reference for binding parameters to Bind()
179 // -----------------------------------------------------------------------------
181 // Bound parameters are specified as arguments to Bind() and are passed to the
182 // function. A callback with no parameters or no unbound parameters is called a
183 // Closure (base::Callback<void(void)> and base::Closure are the same thing).
185 // PASSING PARAMETERS OWNED BY THE CALLBACK
187 // void Foo(int* arg) { cout << *arg << endl; }
188 // int* pn = new int(1);
189 // base::Closure foo_callback = base::Bind(&foo, base::Owned(pn));
191 // The parameter will be deleted when the callback is destroyed, even if it's
192 // not run (like if you post a task during shutdown).
194 // PASSING PARAMETERS AS A scoped_ptr
196 // void TakesOwnership(scoped_ptr<Foo> arg) {}
197 // scoped_ptr<Foo> f(new Foo);
198 // // f becomes null during the following call.
199 // base::Closure cb = base::Bind(&TakesOwnership, base::Passed(&f));
201 // Ownership of the parameter will be with the callback until the it is run,
202 // when ownership is passed to the callback function. This means the callback
203 // can only be run once. If the callback is never run, it will delete the
204 // object when it's destroyed.
206 // PASSING PARAMETERS AS A scoped_refptr
208 // void TakesOneRef(scoped_refptr<Foo> arg) {}
209 // scoped_refptr<Foo> f(new Foo)
210 // base::Closure cb = base::Bind(&TakesOneRef, f);
212 // This should "just work." The closure will take a reference as long as it
213 // is alive, and another reference will be taken for the called function.
215 // PASSING PARAMETERS BY REFERENCE
217 // void foo(int arg) { cout << arg << endl }
218 // int n = 1;
219 // base::Closure has_ref = base::Bind(&foo, base::ConstRef(n));
220 // n = 2;
221 // has_ref.Run(); // Prints "2"
223 // Normally parameters are copied in the closure. DANGER: ConstRef stores a
224 // const reference instead, referencing the original parameter. This means
225 // that you must ensure the object outlives the callback!
228 // -----------------------------------------------------------------------------
229 // Implementation notes
230 // -----------------------------------------------------------------------------
232 // WHERE IS THIS DESIGN FROM:
234 // The design Callback and Bind is heavily influenced by C++'s
235 // tr1::function/tr1::bind, and by the "Google Callback" system used inside
236 // Google.
239 // HOW THE IMPLEMENTATION WORKS:
241 // There are three main components to the system:
242 // 1) The Callback classes.
243 // 2) The Bind() functions.
244 // 3) The arguments wrappers (e.g., Unretained() and ConstRef()).
246 // The Callback classes represent a generic function pointer. Internally,
247 // it stores a refcounted piece of state that represents the target function
248 // and all its bound parameters. Each Callback specialization has a templated
249 // constructor that takes an BindState<>*. In the context of the constructor,
250 // the static type of this BindState<> pointer uniquely identifies the
251 // function it is representing, all its bound parameters, and a Run() method
252 // that is capable of invoking the target.
254 // Callback's constructor takes the BindState<>* that has the full static type
255 // and erases the target function type as well as the types of the bound
256 // parameters. It does this by storing a pointer to the specific Run()
257 // function, and upcasting the state of BindState<>* to a
258 // BindStateBase*. This is safe as long as this BindStateBase pointer
259 // is only used with the stored Run() pointer.
261 // To BindState<> objects are created inside the Bind() functions.
262 // These functions, along with a set of internal templates, are responsible for
264 // - Unwrapping the function signature into return type, and parameters
265 // - Determining the number of parameters that are bound
266 // - Creating the BindState storing the bound parameters
267 // - Performing compile-time asserts to avoid error-prone behavior
268 // - Returning an Callback<> with an arity matching the number of unbound
269 // parameters and that knows the correct refcounting semantics for the
270 // target object if we are binding a method.
272 // The Bind functions do the above using type-inference, and template
273 // specializations.
275 // By default Bind() will store copies of all bound parameters, and attempt
276 // to refcount a target object if the function being bound is a class method.
277 // These copies are created even if the function takes parameters as const
278 // references. (Binding to non-const references is forbidden, see bind.h.)
280 // To change this behavior, we introduce a set of argument wrappers
281 // (e.g., Unretained(), and ConstRef()). These are simple container templates
282 // that are passed by value, and wrap a pointer to argument. See the
283 // file-level comment in base/bind_helpers.h for more info.
285 // These types are passed to the Unwrap() functions, and the MaybeRefcount()
286 // functions respectively to modify the behavior of Bind(). The Unwrap()
287 // and MaybeRefcount() functions change behavior by doing partial
288 // specialization based on whether or not a parameter is a wrapper type.
290 // ConstRef() is similar to tr1::cref. Unretained() is specific to Chromium.
293 // WHY NOT TR1 FUNCTION/BIND?
295 // Direct use of tr1::function and tr1::bind was considered, but ultimately
296 // rejected because of the number of copy constructors invocations involved
297 // in the binding of arguments during construction, and the forwarding of
298 // arguments during invocation. These copies will no longer be an issue in
299 // C++0x because C++0x will support rvalue reference allowing for the compiler
300 // to avoid these copies. However, waiting for C++0x is not an option.
302 // Measured with valgrind on gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5), the
303 // tr1::bind call itself will invoke a non-trivial copy constructor three times
304 // for each bound parameter. Also, each when passing a tr1::function, each
305 // bound argument will be copied again.
307 // In addition to the copies taken at binding and invocation, copying a
308 // tr1::function causes a copy to be made of all the bound parameters and
309 // state.
311 // Furthermore, in Chromium, it is desirable for the Callback to take a
312 // reference on a target object when representing a class method call. This
313 // is not supported by tr1.
315 // Lastly, tr1::function and tr1::bind has a more general and flexible API.
316 // This includes things like argument reordering by use of
317 // tr1::bind::placeholder, support for non-const reference parameters, and some
318 // limited amount of subtyping of the tr1::function object (e.g.,
319 // tr1::function<int(int)> is convertible to tr1::function<void(int)>).
321 // These are not features that are required in Chromium. Some of them, such as
322 // allowing for reference parameters, and subtyping of functions, may actually
323 // become a source of errors. Removing support for these features actually
324 // allows for a simpler implementation, and a terser Currying API.
327 // WHY NOT GOOGLE CALLBACKS?
329 // The Google callback system also does not support refcounting. Furthermore,
330 // its implementation has a number of strange edge cases with respect to type
331 // conversion of its arguments. In particular, the argument's constness must
332 // at times match exactly the function signature, or the type-inference might
333 // break. Given the above, writing a custom solution was easier.
336 // MISSING FUNCTIONALITY
337 // - Invoking the return of Bind. Bind(&foo).Run() does not work;
338 // - Binding arrays to functions that take a non-const pointer.
339 // Example:
340 // void Foo(const char* ptr);
341 // void Bar(char* ptr);
342 // Bind(&Foo, "test");
343 // Bind(&Bar, "test"); // This fails because ptr is not const.
345 namespace base {
347 // First, we forward declare the Callback class template. This informs the
348 // compiler that the template only has 1 type parameter which is the function
349 // signature that the Callback is representing.
351 // After this, create template specializations for 0-7 parameters. Note that
352 // even though the template typelist grows, the specialization still
353 // only has one type: the function signature.
355 // If you are thinking of forward declaring Callback in your own header file,
356 // please include "base/callback_forward.h" instead.
357 template <typename Sig>
358 class Callback;
360 namespace internal {
361 template <typename Runnable, typename RunType, typename BoundArgsType>
362 struct BindState;
363 } // namespace internal
365 template <typename R>
366 class Callback<R(void)> : public internal::CallbackBase {
367 public:
368 typedef R(RunType)();
370 Callback() : CallbackBase(NULL) { }
372 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
373 // return the exact Callback<> type. See base/bind.h for details.
374 template <typename Runnable, typename BindRunType, typename BoundArgsType>
375 Callback(internal::BindState<Runnable, BindRunType,
376 BoundArgsType>* bind_state)
377 : CallbackBase(bind_state) {
379 // Force the assignment to a local variable of PolymorphicInvoke
380 // so the compiler will typecheck that the passed in Run() method has
381 // the correct type.
382 PolymorphicInvoke invoke_func =
383 &internal::BindState<Runnable, BindRunType, BoundArgsType>
384 ::InvokerType::Run;
385 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
388 bool Equals(const Callback& other) const {
389 return CallbackBase::Equals(other);
392 R Run() const {
393 PolymorphicInvoke f =
394 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
396 return f(bind_state_.get());
399 private:
400 typedef R(*PolymorphicInvoke)(
401 internal::BindStateBase*);
405 template <typename R, typename A1>
406 class Callback<R(A1)> : public internal::CallbackBase {
407 public:
408 typedef R(RunType)(A1);
410 Callback() : CallbackBase(NULL) { }
412 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
413 // return the exact Callback<> type. See base/bind.h for details.
414 template <typename Runnable, typename BindRunType, typename BoundArgsType>
415 Callback(internal::BindState<Runnable, BindRunType,
416 BoundArgsType>* bind_state)
417 : CallbackBase(bind_state) {
419 // Force the assignment to a local variable of PolymorphicInvoke
420 // so the compiler will typecheck that the passed in Run() method has
421 // the correct type.
422 PolymorphicInvoke invoke_func =
423 &internal::BindState<Runnable, BindRunType, BoundArgsType>
424 ::InvokerType::Run;
425 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
428 bool Equals(const Callback& other) const {
429 return CallbackBase::Equals(other);
432 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1) const {
433 PolymorphicInvoke f =
434 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
436 return f(bind_state_.get(), internal::CallbackForward(a1));
439 private:
440 typedef R(*PolymorphicInvoke)(
441 internal::BindStateBase*,
442 typename internal::CallbackParamTraits<A1>::ForwardType);
446 template <typename R, typename A1, typename A2>
447 class Callback<R(A1, A2)> : public internal::CallbackBase {
448 public:
449 typedef R(RunType)(A1, A2);
451 Callback() : CallbackBase(NULL) { }
453 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
454 // return the exact Callback<> type. See base/bind.h for details.
455 template <typename Runnable, typename BindRunType, typename BoundArgsType>
456 Callback(internal::BindState<Runnable, BindRunType,
457 BoundArgsType>* bind_state)
458 : CallbackBase(bind_state) {
460 // Force the assignment to a local variable of PolymorphicInvoke
461 // so the compiler will typecheck that the passed in Run() method has
462 // the correct type.
463 PolymorphicInvoke invoke_func =
464 &internal::BindState<Runnable, BindRunType, BoundArgsType>
465 ::InvokerType::Run;
466 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
469 bool Equals(const Callback& other) const {
470 return CallbackBase::Equals(other);
473 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
474 typename internal::CallbackParamTraits<A2>::ForwardType a2) const {
475 PolymorphicInvoke f =
476 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
478 return f(bind_state_.get(), internal::CallbackForward(a1),
479 internal::CallbackForward(a2));
482 private:
483 typedef R(*PolymorphicInvoke)(
484 internal::BindStateBase*,
485 typename internal::CallbackParamTraits<A1>::ForwardType,
486 typename internal::CallbackParamTraits<A2>::ForwardType);
490 template <typename R, typename A1, typename A2, typename A3>
491 class Callback<R(A1, A2, A3)> : public internal::CallbackBase {
492 public:
493 typedef R(RunType)(A1, A2, A3);
495 Callback() : CallbackBase(NULL) { }
497 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
498 // return the exact Callback<> type. See base/bind.h for details.
499 template <typename Runnable, typename BindRunType, typename BoundArgsType>
500 Callback(internal::BindState<Runnable, BindRunType,
501 BoundArgsType>* bind_state)
502 : CallbackBase(bind_state) {
504 // Force the assignment to a local variable of PolymorphicInvoke
505 // so the compiler will typecheck that the passed in Run() method has
506 // the correct type.
507 PolymorphicInvoke invoke_func =
508 &internal::BindState<Runnable, BindRunType, BoundArgsType>
509 ::InvokerType::Run;
510 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
513 bool Equals(const Callback& other) const {
514 return CallbackBase::Equals(other);
517 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
518 typename internal::CallbackParamTraits<A2>::ForwardType a2,
519 typename internal::CallbackParamTraits<A3>::ForwardType a3) const {
520 PolymorphicInvoke f =
521 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
523 return f(bind_state_.get(), internal::CallbackForward(a1),
524 internal::CallbackForward(a2),
525 internal::CallbackForward(a3));
528 private:
529 typedef R(*PolymorphicInvoke)(
530 internal::BindStateBase*,
531 typename internal::CallbackParamTraits<A1>::ForwardType,
532 typename internal::CallbackParamTraits<A2>::ForwardType,
533 typename internal::CallbackParamTraits<A3>::ForwardType);
537 template <typename R, typename A1, typename A2, typename A3, typename A4>
538 class Callback<R(A1, A2, A3, A4)> : public internal::CallbackBase {
539 public:
540 typedef R(RunType)(A1, A2, A3, A4);
542 Callback() : CallbackBase(NULL) { }
544 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
545 // return the exact Callback<> type. See base/bind.h for details.
546 template <typename Runnable, typename BindRunType, typename BoundArgsType>
547 Callback(internal::BindState<Runnable, BindRunType,
548 BoundArgsType>* bind_state)
549 : CallbackBase(bind_state) {
551 // Force the assignment to a local variable of PolymorphicInvoke
552 // so the compiler will typecheck that the passed in Run() method has
553 // the correct type.
554 PolymorphicInvoke invoke_func =
555 &internal::BindState<Runnable, BindRunType, BoundArgsType>
556 ::InvokerType::Run;
557 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
560 bool Equals(const Callback& other) const {
561 return CallbackBase::Equals(other);
564 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
565 typename internal::CallbackParamTraits<A2>::ForwardType a2,
566 typename internal::CallbackParamTraits<A3>::ForwardType a3,
567 typename internal::CallbackParamTraits<A4>::ForwardType a4) const {
568 PolymorphicInvoke f =
569 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
571 return f(bind_state_.get(), internal::CallbackForward(a1),
572 internal::CallbackForward(a2),
573 internal::CallbackForward(a3),
574 internal::CallbackForward(a4));
577 private:
578 typedef R(*PolymorphicInvoke)(
579 internal::BindStateBase*,
580 typename internal::CallbackParamTraits<A1>::ForwardType,
581 typename internal::CallbackParamTraits<A2>::ForwardType,
582 typename internal::CallbackParamTraits<A3>::ForwardType,
583 typename internal::CallbackParamTraits<A4>::ForwardType);
587 template <typename R, typename A1, typename A2, typename A3, typename A4,
588 typename A5>
589 class Callback<R(A1, A2, A3, A4, A5)> : public internal::CallbackBase {
590 public:
591 typedef R(RunType)(A1, A2, A3, A4, A5);
593 Callback() : CallbackBase(NULL) { }
595 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
596 // return the exact Callback<> type. See base/bind.h for details.
597 template <typename Runnable, typename BindRunType, typename BoundArgsType>
598 Callback(internal::BindState<Runnable, BindRunType,
599 BoundArgsType>* bind_state)
600 : CallbackBase(bind_state) {
602 // Force the assignment to a local variable of PolymorphicInvoke
603 // so the compiler will typecheck that the passed in Run() method has
604 // the correct type.
605 PolymorphicInvoke invoke_func =
606 &internal::BindState<Runnable, BindRunType, BoundArgsType>
607 ::InvokerType::Run;
608 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
611 bool Equals(const Callback& other) const {
612 return CallbackBase::Equals(other);
615 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
616 typename internal::CallbackParamTraits<A2>::ForwardType a2,
617 typename internal::CallbackParamTraits<A3>::ForwardType a3,
618 typename internal::CallbackParamTraits<A4>::ForwardType a4,
619 typename internal::CallbackParamTraits<A5>::ForwardType a5) const {
620 PolymorphicInvoke f =
621 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
623 return f(bind_state_.get(), internal::CallbackForward(a1),
624 internal::CallbackForward(a2),
625 internal::CallbackForward(a3),
626 internal::CallbackForward(a4),
627 internal::CallbackForward(a5));
630 private:
631 typedef R(*PolymorphicInvoke)(
632 internal::BindStateBase*,
633 typename internal::CallbackParamTraits<A1>::ForwardType,
634 typename internal::CallbackParamTraits<A2>::ForwardType,
635 typename internal::CallbackParamTraits<A3>::ForwardType,
636 typename internal::CallbackParamTraits<A4>::ForwardType,
637 typename internal::CallbackParamTraits<A5>::ForwardType);
641 template <typename R, typename A1, typename A2, typename A3, typename A4,
642 typename A5, typename A6>
643 class Callback<R(A1, A2, A3, A4, A5, A6)> : public internal::CallbackBase {
644 public:
645 typedef R(RunType)(A1, A2, A3, A4, A5, A6);
647 Callback() : CallbackBase(NULL) { }
649 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
650 // return the exact Callback<> type. See base/bind.h for details.
651 template <typename Runnable, typename BindRunType, typename BoundArgsType>
652 Callback(internal::BindState<Runnable, BindRunType,
653 BoundArgsType>* bind_state)
654 : CallbackBase(bind_state) {
656 // Force the assignment to a local variable of PolymorphicInvoke
657 // so the compiler will typecheck that the passed in Run() method has
658 // the correct type.
659 PolymorphicInvoke invoke_func =
660 &internal::BindState<Runnable, BindRunType, BoundArgsType>
661 ::InvokerType::Run;
662 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
665 bool Equals(const Callback& other) const {
666 return CallbackBase::Equals(other);
669 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
670 typename internal::CallbackParamTraits<A2>::ForwardType a2,
671 typename internal::CallbackParamTraits<A3>::ForwardType a3,
672 typename internal::CallbackParamTraits<A4>::ForwardType a4,
673 typename internal::CallbackParamTraits<A5>::ForwardType a5,
674 typename internal::CallbackParamTraits<A6>::ForwardType a6) const {
675 PolymorphicInvoke f =
676 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
678 return f(bind_state_.get(), internal::CallbackForward(a1),
679 internal::CallbackForward(a2),
680 internal::CallbackForward(a3),
681 internal::CallbackForward(a4),
682 internal::CallbackForward(a5),
683 internal::CallbackForward(a6));
686 private:
687 typedef R(*PolymorphicInvoke)(
688 internal::BindStateBase*,
689 typename internal::CallbackParamTraits<A1>::ForwardType,
690 typename internal::CallbackParamTraits<A2>::ForwardType,
691 typename internal::CallbackParamTraits<A3>::ForwardType,
692 typename internal::CallbackParamTraits<A4>::ForwardType,
693 typename internal::CallbackParamTraits<A5>::ForwardType,
694 typename internal::CallbackParamTraits<A6>::ForwardType);
698 template <typename R, typename A1, typename A2, typename A3, typename A4,
699 typename A5, typename A6, typename A7>
700 class Callback<R(A1, A2, A3, A4, A5, A6, A7)> : public internal::CallbackBase {
701 public:
702 typedef R(RunType)(A1, A2, A3, A4, A5, A6, A7);
704 Callback() : CallbackBase(NULL) { }
706 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
707 // return the exact Callback<> type. See base/bind.h for details.
708 template <typename Runnable, typename BindRunType, typename BoundArgsType>
709 Callback(internal::BindState<Runnable, BindRunType,
710 BoundArgsType>* bind_state)
711 : CallbackBase(bind_state) {
713 // Force the assignment to a local variable of PolymorphicInvoke
714 // so the compiler will typecheck that the passed in Run() method has
715 // the correct type.
716 PolymorphicInvoke invoke_func =
717 &internal::BindState<Runnable, BindRunType, BoundArgsType>
718 ::InvokerType::Run;
719 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
722 bool Equals(const Callback& other) const {
723 return CallbackBase::Equals(other);
726 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
727 typename internal::CallbackParamTraits<A2>::ForwardType a2,
728 typename internal::CallbackParamTraits<A3>::ForwardType a3,
729 typename internal::CallbackParamTraits<A4>::ForwardType a4,
730 typename internal::CallbackParamTraits<A5>::ForwardType a5,
731 typename internal::CallbackParamTraits<A6>::ForwardType a6,
732 typename internal::CallbackParamTraits<A7>::ForwardType a7) const {
733 PolymorphicInvoke f =
734 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
736 return f(bind_state_.get(), internal::CallbackForward(a1),
737 internal::CallbackForward(a2),
738 internal::CallbackForward(a3),
739 internal::CallbackForward(a4),
740 internal::CallbackForward(a5),
741 internal::CallbackForward(a6),
742 internal::CallbackForward(a7));
745 private:
746 typedef R(*PolymorphicInvoke)(
747 internal::BindStateBase*,
748 typename internal::CallbackParamTraits<A1>::ForwardType,
749 typename internal::CallbackParamTraits<A2>::ForwardType,
750 typename internal::CallbackParamTraits<A3>::ForwardType,
751 typename internal::CallbackParamTraits<A4>::ForwardType,
752 typename internal::CallbackParamTraits<A5>::ForwardType,
753 typename internal::CallbackParamTraits<A6>::ForwardType,
754 typename internal::CallbackParamTraits<A7>::ForwardType);
759 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it
760 // will be used in a lot of APIs with delayed execution.
761 typedef Callback<void(void)> Closure;
763 } // namespace base
765 #endif // BASE_CALLBACK_H