Omnibox: Pass Input Box Type to AutocompleteClassifier
[chromium-blink-merge.git] / base / callback.h
bloba962957437c1500d199839befb2a20d5d467be43
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 // Const references are *copied* unless ConstRef is used. Example:
219 // void foo(const int& arg) { printf("%d %p\n", arg, &arg); }
220 // int n = 1;
221 // base::Closure has_copy = base::Bind(&foo, n);
222 // base::Closure has_ref = base::Bind(&foo, base::ConstRef(n));
223 // n = 2;
224 // foo(n); // Prints "2 0xaaaaaaaaaaaa"
225 // has_copy.Run(); // Prints "1 0xbbbbbbbbbbbb"
226 // has_ref.Run(); // Prints "2 0xaaaaaaaaaaaa"
228 // Normally parameters are copied in the closure. DANGER: ConstRef stores a
229 // const reference instead, referencing the original parameter. This means
230 // that you must ensure the object outlives the callback!
233 // -----------------------------------------------------------------------------
234 // Implementation notes
235 // -----------------------------------------------------------------------------
237 // WHERE IS THIS DESIGN FROM:
239 // The design Callback and Bind is heavily influenced by C++'s
240 // tr1::function/tr1::bind, and by the "Google Callback" system used inside
241 // Google.
244 // HOW THE IMPLEMENTATION WORKS:
246 // There are three main components to the system:
247 // 1) The Callback classes.
248 // 2) The Bind() functions.
249 // 3) The arguments wrappers (e.g., Unretained() and ConstRef()).
251 // The Callback classes represent a generic function pointer. Internally,
252 // it stores a refcounted piece of state that represents the target function
253 // and all its bound parameters. Each Callback specialization has a templated
254 // constructor that takes an BindState<>*. In the context of the constructor,
255 // the static type of this BindState<> pointer uniquely identifies the
256 // function it is representing, all its bound parameters, and a Run() method
257 // that is capable of invoking the target.
259 // Callback's constructor takes the BindState<>* that has the full static type
260 // and erases the target function type as well as the types of the bound
261 // parameters. It does this by storing a pointer to the specific Run()
262 // function, and upcasting the state of BindState<>* to a
263 // BindStateBase*. This is safe as long as this BindStateBase pointer
264 // is only used with the stored Run() pointer.
266 // To BindState<> objects are created inside the Bind() functions.
267 // These functions, along with a set of internal templates, are responsible for
269 // - Unwrapping the function signature into return type, and parameters
270 // - Determining the number of parameters that are bound
271 // - Creating the BindState storing the bound parameters
272 // - Performing compile-time asserts to avoid error-prone behavior
273 // - Returning an Callback<> with an arity matching the number of unbound
274 // parameters and that knows the correct refcounting semantics for the
275 // target object if we are binding a method.
277 // The Bind functions do the above using type-inference, and template
278 // specializations.
280 // By default Bind() will store copies of all bound parameters, and attempt
281 // to refcount a target object if the function being bound is a class method.
282 // These copies are created even if the function takes parameters as const
283 // references. (Binding to non-const references is forbidden, see bind.h.)
285 // To change this behavior, we introduce a set of argument wrappers
286 // (e.g., Unretained(), and ConstRef()). These are simple container templates
287 // that are passed by value, and wrap a pointer to argument. See the
288 // file-level comment in base/bind_helpers.h for more info.
290 // These types are passed to the Unwrap() functions, and the MaybeRefcount()
291 // functions respectively to modify the behavior of Bind(). The Unwrap()
292 // and MaybeRefcount() functions change behavior by doing partial
293 // specialization based on whether or not a parameter is a wrapper type.
295 // ConstRef() is similar to tr1::cref. Unretained() is specific to Chromium.
298 // WHY NOT TR1 FUNCTION/BIND?
300 // Direct use of tr1::function and tr1::bind was considered, but ultimately
301 // rejected because of the number of copy constructors invocations involved
302 // in the binding of arguments during construction, and the forwarding of
303 // arguments during invocation. These copies will no longer be an issue in
304 // C++0x because C++0x will support rvalue reference allowing for the compiler
305 // to avoid these copies. However, waiting for C++0x is not an option.
307 // Measured with valgrind on gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5), the
308 // tr1::bind call itself will invoke a non-trivial copy constructor three times
309 // for each bound parameter. Also, each when passing a tr1::function, each
310 // bound argument will be copied again.
312 // In addition to the copies taken at binding and invocation, copying a
313 // tr1::function causes a copy to be made of all the bound parameters and
314 // state.
316 // Furthermore, in Chromium, it is desirable for the Callback to take a
317 // reference on a target object when representing a class method call. This
318 // is not supported by tr1.
320 // Lastly, tr1::function and tr1::bind has a more general and flexible API.
321 // This includes things like argument reordering by use of
322 // tr1::bind::placeholder, support for non-const reference parameters, and some
323 // limited amount of subtyping of the tr1::function object (e.g.,
324 // tr1::function<int(int)> is convertible to tr1::function<void(int)>).
326 // These are not features that are required in Chromium. Some of them, such as
327 // allowing for reference parameters, and subtyping of functions, may actually
328 // become a source of errors. Removing support for these features actually
329 // allows for a simpler implementation, and a terser Currying API.
332 // WHY NOT GOOGLE CALLBACKS?
334 // The Google callback system also does not support refcounting. Furthermore,
335 // its implementation has a number of strange edge cases with respect to type
336 // conversion of its arguments. In particular, the argument's constness must
337 // at times match exactly the function signature, or the type-inference might
338 // break. Given the above, writing a custom solution was easier.
341 // MISSING FUNCTIONALITY
342 // - Invoking the return of Bind. Bind(&foo).Run() does not work;
343 // - Binding arrays to functions that take a non-const pointer.
344 // Example:
345 // void Foo(const char* ptr);
346 // void Bar(char* ptr);
347 // Bind(&Foo, "test");
348 // Bind(&Bar, "test"); // This fails because ptr is not const.
350 namespace base {
352 // First, we forward declare the Callback class template. This informs the
353 // compiler that the template only has 1 type parameter which is the function
354 // signature that the Callback is representing.
356 // After this, create template specializations for 0-7 parameters. Note that
357 // even though the template typelist grows, the specialization still
358 // only has one type: the function signature.
360 // If you are thinking of forward declaring Callback in your own header file,
361 // please include "base/callback_forward.h" instead.
362 template <typename Sig>
363 class Callback;
365 namespace internal {
366 template <typename Runnable, typename RunType, typename BoundArgsType>
367 struct BindState;
368 } // namespace internal
370 template <typename R>
371 class Callback<R(void)> : public internal::CallbackBase {
372 public:
373 typedef R(RunType)();
375 Callback() : CallbackBase(NULL) { }
377 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
378 // return the exact Callback<> type. See base/bind.h for details.
379 template <typename Runnable, typename BindRunType, typename BoundArgsType>
380 Callback(internal::BindState<Runnable, BindRunType,
381 BoundArgsType>* bind_state)
382 : CallbackBase(bind_state) {
384 // Force the assignment to a local variable of PolymorphicInvoke
385 // so the compiler will typecheck that the passed in Run() method has
386 // the correct type.
387 PolymorphicInvoke invoke_func =
388 &internal::BindState<Runnable, BindRunType, BoundArgsType>
389 ::InvokerType::Run;
390 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
393 bool Equals(const Callback& other) const {
394 return CallbackBase::Equals(other);
397 R Run() const {
398 PolymorphicInvoke f =
399 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
401 return f(bind_state_.get());
404 private:
405 typedef R(*PolymorphicInvoke)(
406 internal::BindStateBase*);
410 template <typename R, typename A1>
411 class Callback<R(A1)> : public internal::CallbackBase {
412 public:
413 typedef R(RunType)(A1);
415 Callback() : CallbackBase(NULL) { }
417 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
418 // return the exact Callback<> type. See base/bind.h for details.
419 template <typename Runnable, typename BindRunType, typename BoundArgsType>
420 Callback(internal::BindState<Runnable, BindRunType,
421 BoundArgsType>* bind_state)
422 : CallbackBase(bind_state) {
424 // Force the assignment to a local variable of PolymorphicInvoke
425 // so the compiler will typecheck that the passed in Run() method has
426 // the correct type.
427 PolymorphicInvoke invoke_func =
428 &internal::BindState<Runnable, BindRunType, BoundArgsType>
429 ::InvokerType::Run;
430 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
433 bool Equals(const Callback& other) const {
434 return CallbackBase::Equals(other);
437 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1) const {
438 PolymorphicInvoke f =
439 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
441 return f(bind_state_.get(), internal::CallbackForward(a1));
444 private:
445 typedef R(*PolymorphicInvoke)(
446 internal::BindStateBase*,
447 typename internal::CallbackParamTraits<A1>::ForwardType);
451 template <typename R, typename A1, typename A2>
452 class Callback<R(A1, A2)> : public internal::CallbackBase {
453 public:
454 typedef R(RunType)(A1, A2);
456 Callback() : CallbackBase(NULL) { }
458 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
459 // return the exact Callback<> type. See base/bind.h for details.
460 template <typename Runnable, typename BindRunType, typename BoundArgsType>
461 Callback(internal::BindState<Runnable, BindRunType,
462 BoundArgsType>* bind_state)
463 : CallbackBase(bind_state) {
465 // Force the assignment to a local variable of PolymorphicInvoke
466 // so the compiler will typecheck that the passed in Run() method has
467 // the correct type.
468 PolymorphicInvoke invoke_func =
469 &internal::BindState<Runnable, BindRunType, BoundArgsType>
470 ::InvokerType::Run;
471 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
474 bool Equals(const Callback& other) const {
475 return CallbackBase::Equals(other);
478 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
479 typename internal::CallbackParamTraits<A2>::ForwardType a2) const {
480 PolymorphicInvoke f =
481 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
483 return f(bind_state_.get(), internal::CallbackForward(a1),
484 internal::CallbackForward(a2));
487 private:
488 typedef R(*PolymorphicInvoke)(
489 internal::BindStateBase*,
490 typename internal::CallbackParamTraits<A1>::ForwardType,
491 typename internal::CallbackParamTraits<A2>::ForwardType);
495 template <typename R, typename A1, typename A2, typename A3>
496 class Callback<R(A1, A2, A3)> : public internal::CallbackBase {
497 public:
498 typedef R(RunType)(A1, A2, A3);
500 Callback() : CallbackBase(NULL) { }
502 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
503 // return the exact Callback<> type. See base/bind.h for details.
504 template <typename Runnable, typename BindRunType, typename BoundArgsType>
505 Callback(internal::BindState<Runnable, BindRunType,
506 BoundArgsType>* bind_state)
507 : CallbackBase(bind_state) {
509 // Force the assignment to a local variable of PolymorphicInvoke
510 // so the compiler will typecheck that the passed in Run() method has
511 // the correct type.
512 PolymorphicInvoke invoke_func =
513 &internal::BindState<Runnable, BindRunType, BoundArgsType>
514 ::InvokerType::Run;
515 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
518 bool Equals(const Callback& other) const {
519 return CallbackBase::Equals(other);
522 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
523 typename internal::CallbackParamTraits<A2>::ForwardType a2,
524 typename internal::CallbackParamTraits<A3>::ForwardType a3) const {
525 PolymorphicInvoke f =
526 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
528 return f(bind_state_.get(), internal::CallbackForward(a1),
529 internal::CallbackForward(a2),
530 internal::CallbackForward(a3));
533 private:
534 typedef R(*PolymorphicInvoke)(
535 internal::BindStateBase*,
536 typename internal::CallbackParamTraits<A1>::ForwardType,
537 typename internal::CallbackParamTraits<A2>::ForwardType,
538 typename internal::CallbackParamTraits<A3>::ForwardType);
542 template <typename R, typename A1, typename A2, typename A3, typename A4>
543 class Callback<R(A1, A2, A3, A4)> : public internal::CallbackBase {
544 public:
545 typedef R(RunType)(A1, A2, A3, A4);
547 Callback() : CallbackBase(NULL) { }
549 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
550 // return the exact Callback<> type. See base/bind.h for details.
551 template <typename Runnable, typename BindRunType, typename BoundArgsType>
552 Callback(internal::BindState<Runnable, BindRunType,
553 BoundArgsType>* bind_state)
554 : CallbackBase(bind_state) {
556 // Force the assignment to a local variable of PolymorphicInvoke
557 // so the compiler will typecheck that the passed in Run() method has
558 // the correct type.
559 PolymorphicInvoke invoke_func =
560 &internal::BindState<Runnable, BindRunType, BoundArgsType>
561 ::InvokerType::Run;
562 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
565 bool Equals(const Callback& other) const {
566 return CallbackBase::Equals(other);
569 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
570 typename internal::CallbackParamTraits<A2>::ForwardType a2,
571 typename internal::CallbackParamTraits<A3>::ForwardType a3,
572 typename internal::CallbackParamTraits<A4>::ForwardType a4) const {
573 PolymorphicInvoke f =
574 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
576 return f(bind_state_.get(), internal::CallbackForward(a1),
577 internal::CallbackForward(a2),
578 internal::CallbackForward(a3),
579 internal::CallbackForward(a4));
582 private:
583 typedef R(*PolymorphicInvoke)(
584 internal::BindStateBase*,
585 typename internal::CallbackParamTraits<A1>::ForwardType,
586 typename internal::CallbackParamTraits<A2>::ForwardType,
587 typename internal::CallbackParamTraits<A3>::ForwardType,
588 typename internal::CallbackParamTraits<A4>::ForwardType);
592 template <typename R, typename A1, typename A2, typename A3, typename A4,
593 typename A5>
594 class Callback<R(A1, A2, A3, A4, A5)> : public internal::CallbackBase {
595 public:
596 typedef R(RunType)(A1, A2, A3, A4, A5);
598 Callback() : CallbackBase(NULL) { }
600 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
601 // return the exact Callback<> type. See base/bind.h for details.
602 template <typename Runnable, typename BindRunType, typename BoundArgsType>
603 Callback(internal::BindState<Runnable, BindRunType,
604 BoundArgsType>* bind_state)
605 : CallbackBase(bind_state) {
607 // Force the assignment to a local variable of PolymorphicInvoke
608 // so the compiler will typecheck that the passed in Run() method has
609 // the correct type.
610 PolymorphicInvoke invoke_func =
611 &internal::BindState<Runnable, BindRunType, BoundArgsType>
612 ::InvokerType::Run;
613 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
616 bool Equals(const Callback& other) const {
617 return CallbackBase::Equals(other);
620 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
621 typename internal::CallbackParamTraits<A2>::ForwardType a2,
622 typename internal::CallbackParamTraits<A3>::ForwardType a3,
623 typename internal::CallbackParamTraits<A4>::ForwardType a4,
624 typename internal::CallbackParamTraits<A5>::ForwardType a5) const {
625 PolymorphicInvoke f =
626 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
628 return f(bind_state_.get(), internal::CallbackForward(a1),
629 internal::CallbackForward(a2),
630 internal::CallbackForward(a3),
631 internal::CallbackForward(a4),
632 internal::CallbackForward(a5));
635 private:
636 typedef R(*PolymorphicInvoke)(
637 internal::BindStateBase*,
638 typename internal::CallbackParamTraits<A1>::ForwardType,
639 typename internal::CallbackParamTraits<A2>::ForwardType,
640 typename internal::CallbackParamTraits<A3>::ForwardType,
641 typename internal::CallbackParamTraits<A4>::ForwardType,
642 typename internal::CallbackParamTraits<A5>::ForwardType);
646 template <typename R, typename A1, typename A2, typename A3, typename A4,
647 typename A5, typename A6>
648 class Callback<R(A1, A2, A3, A4, A5, A6)> : public internal::CallbackBase {
649 public:
650 typedef R(RunType)(A1, A2, A3, A4, A5, A6);
652 Callback() : CallbackBase(NULL) { }
654 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
655 // return the exact Callback<> type. See base/bind.h for details.
656 template <typename Runnable, typename BindRunType, typename BoundArgsType>
657 Callback(internal::BindState<Runnable, BindRunType,
658 BoundArgsType>* bind_state)
659 : CallbackBase(bind_state) {
661 // Force the assignment to a local variable of PolymorphicInvoke
662 // so the compiler will typecheck that the passed in Run() method has
663 // the correct type.
664 PolymorphicInvoke invoke_func =
665 &internal::BindState<Runnable, BindRunType, BoundArgsType>
666 ::InvokerType::Run;
667 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
670 bool Equals(const Callback& other) const {
671 return CallbackBase::Equals(other);
674 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
675 typename internal::CallbackParamTraits<A2>::ForwardType a2,
676 typename internal::CallbackParamTraits<A3>::ForwardType a3,
677 typename internal::CallbackParamTraits<A4>::ForwardType a4,
678 typename internal::CallbackParamTraits<A5>::ForwardType a5,
679 typename internal::CallbackParamTraits<A6>::ForwardType a6) const {
680 PolymorphicInvoke f =
681 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
683 return f(bind_state_.get(), internal::CallbackForward(a1),
684 internal::CallbackForward(a2),
685 internal::CallbackForward(a3),
686 internal::CallbackForward(a4),
687 internal::CallbackForward(a5),
688 internal::CallbackForward(a6));
691 private:
692 typedef R(*PolymorphicInvoke)(
693 internal::BindStateBase*,
694 typename internal::CallbackParamTraits<A1>::ForwardType,
695 typename internal::CallbackParamTraits<A2>::ForwardType,
696 typename internal::CallbackParamTraits<A3>::ForwardType,
697 typename internal::CallbackParamTraits<A4>::ForwardType,
698 typename internal::CallbackParamTraits<A5>::ForwardType,
699 typename internal::CallbackParamTraits<A6>::ForwardType);
703 template <typename R, typename A1, typename A2, typename A3, typename A4,
704 typename A5, typename A6, typename A7>
705 class Callback<R(A1, A2, A3, A4, A5, A6, A7)> : public internal::CallbackBase {
706 public:
707 typedef R(RunType)(A1, A2, A3, A4, A5, A6, A7);
709 Callback() : CallbackBase(NULL) { }
711 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
712 // return the exact Callback<> type. See base/bind.h for details.
713 template <typename Runnable, typename BindRunType, typename BoundArgsType>
714 Callback(internal::BindState<Runnable, BindRunType,
715 BoundArgsType>* bind_state)
716 : CallbackBase(bind_state) {
718 // Force the assignment to a local variable of PolymorphicInvoke
719 // so the compiler will typecheck that the passed in Run() method has
720 // the correct type.
721 PolymorphicInvoke invoke_func =
722 &internal::BindState<Runnable, BindRunType, BoundArgsType>
723 ::InvokerType::Run;
724 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
727 bool Equals(const Callback& other) const {
728 return CallbackBase::Equals(other);
731 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
732 typename internal::CallbackParamTraits<A2>::ForwardType a2,
733 typename internal::CallbackParamTraits<A3>::ForwardType a3,
734 typename internal::CallbackParamTraits<A4>::ForwardType a4,
735 typename internal::CallbackParamTraits<A5>::ForwardType a5,
736 typename internal::CallbackParamTraits<A6>::ForwardType a6,
737 typename internal::CallbackParamTraits<A7>::ForwardType a7) const {
738 PolymorphicInvoke f =
739 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
741 return f(bind_state_.get(), internal::CallbackForward(a1),
742 internal::CallbackForward(a2),
743 internal::CallbackForward(a3),
744 internal::CallbackForward(a4),
745 internal::CallbackForward(a5),
746 internal::CallbackForward(a6),
747 internal::CallbackForward(a7));
750 private:
751 typedef R(*PolymorphicInvoke)(
752 internal::BindStateBase*,
753 typename internal::CallbackParamTraits<A1>::ForwardType,
754 typename internal::CallbackParamTraits<A2>::ForwardType,
755 typename internal::CallbackParamTraits<A3>::ForwardType,
756 typename internal::CallbackParamTraits<A4>::ForwardType,
757 typename internal::CallbackParamTraits<A5>::ForwardType,
758 typename internal::CallbackParamTraits<A6>::ForwardType,
759 typename internal::CallbackParamTraits<A7>::ForwardType);
764 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it
765 // will be used in a lot of APIs with delayed execution.
766 typedef Callback<void(void)> Closure;
768 } // namespace base
770 #endif // BASE_CALLBACK_H