Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / base / callback.h
1 // This file was GENERATED by command:
2 //     pump.py callback.h.pump
3 // DO NOT EDIT BY HAND!!!
4
5
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.
9
10 #ifndef BASE_CALLBACK_H_
11 #define BASE_CALLBACK_H_
12
13 #include "base/callback_forward.h"
14 #include "base/callback_internal.h"
15 #include "base/template_util.h"
16
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.
19
20 // -----------------------------------------------------------------------------
21 // Introduction
22 // -----------------------------------------------------------------------------
23 //
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.
27 //
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.
33 //
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.
38 //
39 // MEMORY MANAGEMENT AND PASSING
40 //
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.
44 //
45 // The reason to pass via a const-reference is to avoid unnecessary
46 // AddRef/Release pairs to the internal state.
47 //
48 //
49 // -----------------------------------------------------------------------------
50 // Quick reference for basic stuff
51 // -----------------------------------------------------------------------------
52 //
53 // BINDING A BARE FUNCTION
54 //
55 //   int Return5() { return 5; }
56 //   base::Callback<int(void)> func_cb = base::Bind(&Return5);
57 //   LOG(INFO) << func_cb.Run();  // Prints 5.
58 //
59 // BINDING A CLASS METHOD
60 //
61 //   The first argument to bind is the member function to call, the second is
62 //   the object on which to call it.
63 //
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.
72 //
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.
77 //
78 // RUNNING A CALLBACK
79 //
80 //   Callbacks can be run with their "Run" method, which has the same
81 //   signature as the template argument to the callback.
82 //
83 //   void DoSomething(const base::Callback<void(int, std::string)>& callback) {
84 //     callback.Run(5, "hello");
85 //   }
86 //
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).
89 //
90 //   void DoSomething(const base::Callback<double(double)>& callback) {
91 //     double myresult = callback.Run(3.14159);
92 //     myresult += callback.Run(2.71828);
93 //   }
94 //
95 // PASSING UNBOUND INPUT PARAMETERS
96 //
97 //   Unbound parameters are specified at the time a callback is Run(). They are
98 //   specified in the Callback template type:
99 //
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");
103 //
104 // PASSING BOUND INPUT PARAMETERS
105 //
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.
110 //
111 //   void MyFunc(int i, const std::string& str) {}
112 //   base::Callback<void(void)> cb = base::Bind(&MyFunc, 23, "hello world");
113 //   cb.Run();
114 //
115 //   A callback with no unbound input parameters (base::Callback<void(void)>)
116 //   is called a base::Closure. So we could have also written:
117 //
118 //   base::Closure cb = base::Bind(&MyFunc, 23, "hello world");
119 //
120 //   When calling member functions, bound parameters just go after the object
121 //   pointer.
122 //
123 //   base::Closure cb = base::Bind(&MyClass::MyFunc, this, 23, "hello world");
124 //
125 // PARTIAL BINDING OF PARAMETERS
126 //
127 //   You can specify some parameters when you create the callback, and specify
128 //   the rest when you execute the callback.
129 //
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");
133 //
134 //   When calling a function bound parameters are first, followed by unbound
135 //   parameters.
136 //
137 //
138 // -----------------------------------------------------------------------------
139 // Quick reference for advanced binding
140 // -----------------------------------------------------------------------------
141 //
142 // BINDING A CLASS METHOD WITH WEAK POINTERS
143 //
144 //   base::Bind(&MyClass::Foo, GetWeakPtr());
145 //
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!
149 //
150 // BINDING A CLASS METHOD WITH MANUAL LIFETIME MANAGEMENT
151 //
152 //   base::Bind(&MyClass::Foo, base::Unretained(this));
153 //
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!
157 //
158 // BINDING A CLASS METHOD AND HAVING THE CALLBACK OWN THE CLASS
159 //
160 //   MyClass* myclass = new MyClass;
161 //   base::Bind(&MyClass::Foo, base::Owned(myclass));
162 //
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.
166 //
167 // IGNORING RETURN VALUES
168 //
169 //   Sometimes you want to call a function that returns a value in a callback
170 //   that doesn't expect a return value.
171 //
172 //   int DoSomething(int arg) { cout << arg << endl; }
173 //   base::Callback<void<int>) cb =
174 //       base::Bind(base::IgnoreResult(&DoSomething));
175 //
176 //
177 // -----------------------------------------------------------------------------
178 // Quick reference for binding parameters to Bind()
179 // -----------------------------------------------------------------------------
180 //
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).
184 //
185 // PASSING PARAMETERS OWNED BY THE CALLBACK
186 //
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));
190 //
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).
193 //
194 // PASSING PARAMETERS AS A scoped_ptr
195 //
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));
200 //
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.
205 //
206 // PASSING PARAMETERS AS A scoped_refptr
207 //
208 //   void TakesOneRef(scoped_refptr<Foo> arg) {}
209 //   scoped_refptr<Foo> f(new Foo)
210 //   base::Closure cb = base::Bind(&TakesOneRef, f);
211 //
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.
214 //
215 // PASSING PARAMETERS BY REFERENCE
216 //
217 //   Const references are *copied* unless ConstRef is used. Example:
218 //
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"
227 //
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!
231 //
232 //
233 // -----------------------------------------------------------------------------
234 // Implementation notes
235 // -----------------------------------------------------------------------------
236 //
237 // WHERE IS THIS DESIGN FROM:
238 //
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.
242 //
243 //
244 // HOW THE IMPLEMENTATION WORKS:
245 //
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()).
250 //
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.
258 //
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.
265 //
266 // To BindState<> objects are created inside the Bind() functions.
267 // These functions, along with a set of internal templates, are responsible for
268 //
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.
276 //
277 // The Bind functions do the above using type-inference, and template
278 // specializations.
279 //
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.)
284 //
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.
289 //
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.
294 //
295 // ConstRef() is similar to tr1::cref.  Unretained() is specific to Chromium.
296 //
297 //
298 // WHY NOT TR1 FUNCTION/BIND?
299 //
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.
306 //
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.
311 //
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.
315 //
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.
319 //
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)>).
325 //
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.
330 //
331 //
332 // WHY NOT GOOGLE CALLBACKS?
333 //
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.
339 //
340 //
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.
349
350 namespace base {
351
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.
355 //
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.
359 //
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;
364
365 namespace internal {
366 template <typename Runnable, typename RunType, typename BoundArgsType>
367 struct BindState;
368 }  // namespace internal
369
370 template <typename R>
371 class Callback<R(void)> : public internal::CallbackBase {
372  public:
373   typedef R(RunType)();
374
375   Callback() : CallbackBase(NULL) { }
376
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) {
383
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);
391   }
392
393   bool Equals(const Callback& other) const {
394     return CallbackBase::Equals(other);
395   }
396
397   R Run() const {
398     PolymorphicInvoke f =
399         reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
400
401     return f(bind_state_.get());
402   }
403
404  private:
405   typedef R(*PolymorphicInvoke)(
406       internal::BindStateBase*);
407
408 };
409
410 template <typename R, typename A1>
411 class Callback<R(A1)> : public internal::CallbackBase {
412  public:
413   typedef R(RunType)(A1);
414
415   Callback() : CallbackBase(NULL) { }
416
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) {
423
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);
431   }
432
433   bool Equals(const Callback& other) const {
434     return CallbackBase::Equals(other);
435   }
436
437   R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1) const {
438     PolymorphicInvoke f =
439         reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
440
441     return f(bind_state_.get(), internal::CallbackForward(a1));
442   }
443
444  private:
445   typedef R(*PolymorphicInvoke)(
446       internal::BindStateBase*,
447           typename internal::CallbackParamTraits<A1>::ForwardType);
448
449 };
450
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);
455
456   Callback() : CallbackBase(NULL) { }
457
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) {
464
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);
472   }
473
474   bool Equals(const Callback& other) const {
475     return CallbackBase::Equals(other);
476   }
477
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_);
482
483     return f(bind_state_.get(), internal::CallbackForward(a1),
484              internal::CallbackForward(a2));
485   }
486
487  private:
488   typedef R(*PolymorphicInvoke)(
489       internal::BindStateBase*,
490           typename internal::CallbackParamTraits<A1>::ForwardType,
491           typename internal::CallbackParamTraits<A2>::ForwardType);
492
493 };
494
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);
499
500   Callback() : CallbackBase(NULL) { }
501
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) {
508
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);
516   }
517
518   bool Equals(const Callback& other) const {
519     return CallbackBase::Equals(other);
520   }
521
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_);
527
528     return f(bind_state_.get(), internal::CallbackForward(a1),
529              internal::CallbackForward(a2),
530              internal::CallbackForward(a3));
531   }
532
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);
539
540 };
541
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);
546
547   Callback() : CallbackBase(NULL) { }
548
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) {
555
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);
563   }
564
565   bool Equals(const Callback& other) const {
566     return CallbackBase::Equals(other);
567   }
568
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_);
575
576     return f(bind_state_.get(), internal::CallbackForward(a1),
577              internal::CallbackForward(a2),
578              internal::CallbackForward(a3),
579              internal::CallbackForward(a4));
580   }
581
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);
589
590 };
591
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);
597
598   Callback() : CallbackBase(NULL) { }
599
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) {
606
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);
614   }
615
616   bool Equals(const Callback& other) const {
617     return CallbackBase::Equals(other);
618   }
619
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_);
627
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));
633   }
634
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);
643
644 };
645
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);
651
652   Callback() : CallbackBase(NULL) { }
653
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) {
660
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);
668   }
669
670   bool Equals(const Callback& other) const {
671     return CallbackBase::Equals(other);
672   }
673
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_);
682
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));
689   }
690
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);
700
701 };
702
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);
708
709   Callback() : CallbackBase(NULL) { }
710
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) {
717
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);
725   }
726
727   bool Equals(const Callback& other) const {
728     return CallbackBase::Equals(other);
729   }
730
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_);
740
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));
748   }
749
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);
760
761 };
762
763
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;
767
768 }  // namespace base
769
770 #endif  // BASE_CALLBACK_H