callback.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. // This file was GENERATED by command:
  2. // pump.py callback.h.pump
  3. // DO NOT EDIT BY HAND!!!
  4. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  5. // Use of this source code is governed by a BSD-style license that can be
  6. // found in the LICENSE file.
  7. #ifndef BASE_CALLBACK_H_
  8. #define BASE_CALLBACK_H_
  9. #include "base/callback_forward.h"
  10. #include "base/callback_internal.h"
  11. #include "base/type_traits.h"
  12. // NOTE: Header files that do not require the full definition of Callback or
  13. // Closure should #include "base/callback_forward.h" instead of this file.
  14. // -----------------------------------------------------------------------------
  15. // Introduction
  16. // -----------------------------------------------------------------------------
  17. //
  18. // The templated Callback class is a generalized function object. Together
  19. // with the Bind() function in bind.h, they provide a type-safe method for
  20. // performing partial application of functions.
  21. //
  22. // Partial application (or "currying") is the process of binding a subset of
  23. // a function's arguments to produce another function that takes fewer
  24. // arguments. This can be used to pass around a unit of delayed execution,
  25. // much like lexical closures are used in other languages. For example, it
  26. // is used in Chromium code to schedule tasks on different MessageLoops.
  27. //
  28. // A callback with no unbound input parameters (base::Callback<void(void)>)
  29. // is called a base::Closure. Note that this is NOT the same as what other
  30. // languages refer to as a closure -- it does not retain a reference to its
  31. // enclosing environment.
  32. //
  33. // MEMORY MANAGEMENT AND PASSING
  34. //
  35. // The Callback objects themselves should be passed by const-reference, and
  36. // stored by copy. They internally store their state via a refcounted class
  37. // and thus do not need to be deleted.
  38. //
  39. // The reason to pass via a const-reference is to avoid unnecessary
  40. // AddRef/Release pairs to the internal state.
  41. //
  42. //
  43. // -----------------------------------------------------------------------------
  44. // Quick reference for basic stuff
  45. // -----------------------------------------------------------------------------
  46. //
  47. // BINDING A BARE FUNCTION
  48. //
  49. // int Return5() { return 5; }
  50. // base::Callback<int(void)> func_cb = base::Bind(&Return5);
  51. // LOG(INFO) << func_cb.Run(); // Prints 5.
  52. //
  53. // BINDING A CLASS METHOD
  54. //
  55. // The first argument to bind is the member function to call, the second is
  56. // the object on which to call it.
  57. //
  58. // class Ref : public base::RefCountedThreadSafe<Ref> {
  59. // public:
  60. // int Foo() { return 3; }
  61. // void PrintBye() { LOG(INFO) << "bye."; }
  62. // };
  63. // scoped_refptr<Ref> ref = new Ref();
  64. // base::Callback<void(void)> ref_cb = base::Bind(&Ref::Foo, ref);
  65. // LOG(INFO) << ref_cb.Run(); // Prints out 3.
  66. //
  67. // By default the object must support RefCounted or you will get a compiler
  68. // error. If you're passing between threads, be sure it's
  69. // RefCountedThreadSafe! See "Advanced binding of member functions" below if
  70. // you don't want to use reference counting.
  71. //
  72. // RUNNING A CALLBACK
  73. //
  74. // Callbacks can be run with their "Run" method, which has the same
  75. // signature as the template argument to the callback.
  76. //
  77. // void DoSomething(const base::Callback<void(int, std::string)>& callback) {
  78. // callback.Run(5, "hello");
  79. // }
  80. //
  81. // Callbacks can be run more than once (they don't get deleted or marked when
  82. // run). However, this precludes using base::Passed (see below).
  83. //
  84. // void DoSomething(const base::Callback<double(double)>& callback) {
  85. // double myresult = callback.Run(3.14159);
  86. // myresult += callback.Run(2.71828);
  87. // }
  88. //
  89. // PASSING UNBOUND INPUT PARAMETERS
  90. //
  91. // Unbound parameters are specified at the time a callback is Run(). They are
  92. // specified in the Callback template type:
  93. //
  94. // void MyFunc(int i, const std::string& str) {}
  95. // base::Callback<void(int, const std::string&)> cb = base::Bind(&MyFunc);
  96. // cb.Run(23, "hello, world");
  97. //
  98. // PASSING BOUND INPUT PARAMETERS
  99. //
  100. // Bound parameters are specified when you create thee callback as arguments
  101. // to Bind(). They will be passed to the function and the Run()ner of the
  102. // callback doesn't see those values or even know that the function it's
  103. // calling.
  104. //
  105. // void MyFunc(int i, const std::string& str) {}
  106. // base::Callback<void(void)> cb = base::Bind(&MyFunc, 23, "hello world");
  107. // cb.Run();
  108. //
  109. // A callback with no unbound input parameters (base::Callback<void(void)>)
  110. // is called a base::Closure. So we could have also written:
  111. //
  112. // base::Closure cb = base::Bind(&MyFunc, 23, "hello world");
  113. //
  114. // When calling member functions, bound parameters just go after the object
  115. // pointer.
  116. //
  117. // base::Closure cb = base::Bind(&MyClass::MyFunc, this, 23, "hello world");
  118. //
  119. // PARTIAL BINDING OF PARAMETERS
  120. //
  121. // You can specify some parameters when you create the callback, and specify
  122. // the rest when you execute the callback.
  123. //
  124. // void MyFunc(int i, const std::string& str) {}
  125. // base::Callback<void(const std::string&)> cb = base::Bind(&MyFunc, 23);
  126. // cb.Run("hello world");
  127. //
  128. // When calling a function bound parameters are first, followed by unbound
  129. // parameters.
  130. //
  131. //
  132. // -----------------------------------------------------------------------------
  133. // Quick reference for advanced binding
  134. // -----------------------------------------------------------------------------
  135. //
  136. // BINDING A CLASS METHOD WITH WEAK POINTERS
  137. //
  138. // base::Bind(&MyClass::Foo, GetWeakPtr());
  139. //
  140. // The callback will not be run if the object has already been destroyed.
  141. // DANGER: weak pointers are not threadsafe, so don't use this
  142. // when passing between threads!
  143. //
  144. // BINDING A CLASS METHOD WITH MANUAL LIFETIME MANAGEMENT
  145. //
  146. // base::Bind(&MyClass::Foo, base::Unretained(this));
  147. //
  148. // This disables all lifetime management on the object. You're responsible
  149. // for making sure the object is alive at the time of the call. You break it,
  150. // you own it!
  151. //
  152. // BINDING A CLASS METHOD AND HAVING THE CALLBACK OWN THE CLASS
  153. //
  154. // MyClass* myclass = new MyClass;
  155. // base::Bind(&MyClass::Foo, base::Owned(myclass));
  156. //
  157. // The object will be deleted when the callback is destroyed, even if it's
  158. // not run (like if you post a task during shutdown). Potentially useful for
  159. // "fire and forget" cases.
  160. //
  161. // IGNORING RETURN VALUES
  162. //
  163. // Sometimes you want to call a function that returns a value in a callback
  164. // that doesn't expect a return value.
  165. //
  166. // int DoSomething(int arg) { cout << arg << endl; }
  167. // base::Callback<void<int>) cb =
  168. // base::Bind(base::IgnoreResult(&DoSomething));
  169. //
  170. //
  171. // -----------------------------------------------------------------------------
  172. // Quick reference for binding parameters to Bind()
  173. // -----------------------------------------------------------------------------
  174. //
  175. // Bound parameters are specified as arguments to Bind() and are passed to the
  176. // function. A callback with no parameters or no unbound parameters is called a
  177. // Closure (base::Callback<void(void)> and base::Closure are the same thing).
  178. //
  179. // PASSING PARAMETERS OWNED BY THE CALLBACK
  180. //
  181. // void Foo(int* arg) { cout << *arg << endl; }
  182. // int* pn = new int(1);
  183. // base::Closure foo_callback = base::Bind(&foo, base::Owned(pn));
  184. //
  185. // The parameter will be deleted when the callback is destroyed, even if it's
  186. // not run (like if you post a task during shutdown).
  187. //
  188. // PASSING PARAMETERS AS A scoped_ptr
  189. //
  190. // void TakesOwnership(scoped_ptr<Foo> arg) {}
  191. // scoped_ptr<Foo> f(new Foo);
  192. // // f becomes null during the following call.
  193. // base::Closure cb = base::Bind(&TakesOwnership, base::Passed(&f));
  194. //
  195. // Ownership of the parameter will be with the callback until the it is run,
  196. // when ownership is passed to the callback function. This means the callback
  197. // can only be run once. If the callback is never run, it will delete the
  198. // object when it's destroyed.
  199. //
  200. // PASSING PARAMETERS AS A scoped_refptr
  201. //
  202. // void TakesOneRef(scoped_refptr<Foo> arg) {}
  203. // scoped_refptr<Foo> f(new Foo)
  204. // base::Closure cb = base::Bind(&TakesOneRef, f);
  205. //
  206. // This should "just work." The closure will take a reference as long as it
  207. // is alive, and another reference will be taken for the called function.
  208. //
  209. // PASSING PARAMETERS BY REFERENCE
  210. //
  211. // Const references are *copied* unless ConstRef is used. Example:
  212. //
  213. // void foo(const int& arg) { printf("%d %p\n", arg, &arg); }
  214. // int n = 1;
  215. // base::Closure has_copy = base::Bind(&foo, n);
  216. // base::Closure has_ref = base::Bind(&foo, base::ConstRef(n));
  217. // n = 2;
  218. // foo(n); // Prints "2 0xaaaaaaaaaaaa"
  219. // has_copy.Run(); // Prints "1 0xbbbbbbbbbbbb"
  220. // has_ref.Run(); // Prints "2 0xaaaaaaaaaaaa"
  221. //
  222. // Normally parameters are copied in the closure. DANGER: ConstRef stores a
  223. // const reference instead, referencing the original parameter. This means
  224. // that you must ensure the object outlives the callback!
  225. //
  226. //
  227. // -----------------------------------------------------------------------------
  228. // Implementation notes
  229. // -----------------------------------------------------------------------------
  230. //
  231. // WHERE IS THIS DESIGN FROM:
  232. //
  233. // The design Callback and Bind is heavily influenced by C++'s
  234. // tr1::function/tr1::bind, and by the "Google Callback" system used inside
  235. // Google.
  236. //
  237. //
  238. // HOW THE IMPLEMENTATION WORKS:
  239. //
  240. // There are three main components to the system:
  241. // 1) The Callback classes.
  242. // 2) The Bind() functions.
  243. // 3) The arguments wrappers (e.g., Unretained() and ConstRef()).
  244. //
  245. // The Callback classes represent a generic function pointer. Internally,
  246. // it stores a refcounted piece of state that represents the target function
  247. // and all its bound parameters. Each Callback specialization has a templated
  248. // constructor that takes an BindState<>*. In the context of the constructor,
  249. // the static type of this BindState<> pointer uniquely identifies the
  250. // function it is representing, all its bound parameters, and a Run() method
  251. // that is capable of invoking the target.
  252. //
  253. // Callback's constructor takes the BindState<>* that has the full static type
  254. // and erases the target function type as well as the types of the bound
  255. // parameters. It does this by storing a pointer to the specific Run()
  256. // function, and upcasting the state of BindState<>* to a
  257. // BindStateBase*. This is safe as long as this BindStateBase pointer
  258. // is only used with the stored Run() pointer.
  259. //
  260. // To BindState<> objects are created inside the Bind() functions.
  261. // These functions, along with a set of internal templates, are responsible for
  262. //
  263. // - Unwrapping the function signature into return type, and parameters
  264. // - Determining the number of parameters that are bound
  265. // - Creating the BindState storing the bound parameters
  266. // - Performing compile-time asserts to avoid error-prone behavior
  267. // - Returning an Callback<> with an arity matching the number of unbound
  268. // parameters and that knows the correct refcounting semantics for the
  269. // target object if we are binding a method.
  270. //
  271. // The Bind functions do the above using type-inference, and template
  272. // specializations.
  273. //
  274. // By default Bind() will store copies of all bound parameters, and attempt
  275. // to refcount a target object if the function being bound is a class method.
  276. // These copies are created even if the function takes parameters as const
  277. // references. (Binding to non-const references is forbidden, see bind.h.)
  278. //
  279. // To change this behavior, we introduce a set of argument wrappers
  280. // (e.g., Unretained(), and ConstRef()). These are simple container templates
  281. // that are passed by value, and wrap a pointer to argument. See the
  282. // file-level comment in base/bind_helpers.h for more info.
  283. //
  284. // These types are passed to the Unwrap() functions, and the MaybeRefcount()
  285. // functions respectively to modify the behavior of Bind(). The Unwrap()
  286. // and MaybeRefcount() functions change behavior by doing partial
  287. // specialization based on whether or not a parameter is a wrapper type.
  288. //
  289. // ConstRef() is similar to tr1::cref. Unretained() is specific to Chromium.
  290. //
  291. //
  292. // WHY NOT TR1 FUNCTION/BIND?
  293. //
  294. // Direct use of tr1::function and tr1::bind was considered, but ultimately
  295. // rejected because of the number of copy constructors invocations involved
  296. // in the binding of arguments during construction, and the forwarding of
  297. // arguments during invocation. These copies will no longer be an issue in
  298. // C++0x because C++0x will support rvalue reference allowing for the compiler
  299. // to avoid these copies. However, waiting for C++0x is not an option.
  300. //
  301. // Measured with valgrind on gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5), the
  302. // tr1::bind call itself will invoke a non-trivial copy constructor three times
  303. // for each bound parameter. Also, each when passing a tr1::function, each
  304. // bound argument will be copied again.
  305. //
  306. // In addition to the copies taken at binding and invocation, copying a
  307. // tr1::function causes a copy to be made of all the bound parameters and
  308. // state.
  309. //
  310. // Furthermore, in Chromium, it is desirable for the Callback to take a
  311. // reference on a target object when representing a class method call. This
  312. // is not supported by tr1.
  313. //
  314. // Lastly, tr1::function and tr1::bind has a more general and flexible API.
  315. // This includes things like argument reordering by use of
  316. // tr1::bind::placeholder, support for non-const reference parameters, and some
  317. // limited amount of subtyping of the tr1::function object (e.g.,
  318. // tr1::function<int(int)> is convertible to tr1::function<void(int)>).
  319. //
  320. // These are not features that are required in Chromium. Some of them, such as
  321. // allowing for reference parameters, and subtyping of functions, may actually
  322. // become a source of errors. Removing support for these features actually
  323. // allows for a simpler implementation, and a terser Currying API.
  324. //
  325. //
  326. // WHY NOT GOOGLE CALLBACKS?
  327. //
  328. // The Google callback system also does not support refcounting. Furthermore,
  329. // its implementation has a number of strange edge cases with respect to type
  330. // conversion of its arguments. In particular, the argument's constness must
  331. // at times match exactly the function signature, or the type-inference might
  332. // break. Given the above, writing a custom solution was easier.
  333. //
  334. //
  335. // MISSING FUNCTIONALITY
  336. // - Invoking the return of Bind. Bind(&foo).Run() does not work;
  337. // - Binding arrays to functions that take a non-const pointer.
  338. // Example:
  339. // void Foo(const char* ptr);
  340. // void Bar(char* ptr);
  341. // Bind(&Foo, "test");
  342. // Bind(&Bar, "test"); // This fails because ptr is not const.
  343. namespace base {
  344. // First, we forward declare the Callback class template. This informs the
  345. // compiler that the template only has 1 type parameter which is the function
  346. // signature that the Callback is representing.
  347. //
  348. // After this, create template specializations for 0-7 parameters. Note that
  349. // even though the template typelist grows, the specialization still
  350. // only has one type: the function signature.
  351. //
  352. // If you are thinking of forward declaring Callback in your own header file,
  353. // please include "base/callback_forward.h" instead.
  354. template <typename Sig>
  355. class Callback;
  356. namespace internal {
  357. template <typename Runnable, typename RunType, typename BoundArgsType>
  358. struct BindState;
  359. } // namespace internal
  360. template <typename R>
  361. class Callback<R(void)> : public internal::CallbackBase {
  362. public:
  363. typedef R(RunType)();
  364. Callback() : CallbackBase(NULL) { }
  365. // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
  366. // return the exact Callback<> type. See base/bind.h for details.
  367. template <typename Runnable, typename BindRunType, typename BoundArgsType>
  368. Callback(internal::BindState<Runnable, BindRunType,
  369. BoundArgsType>* bind_state)
  370. : CallbackBase(bind_state) {
  371. // Force the assignment to a local variable of PolymorphicInvoke
  372. // so the compiler will typecheck that the passed in Run() method has
  373. // the correct type.
  374. PolymorphicInvoke invoke_func =
  375. &internal::BindState<Runnable, BindRunType, BoundArgsType>
  376. ::InvokerType::Run;
  377. polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
  378. }
  379. bool Equals(const Callback& other) const {
  380. return CallbackBase::Equals(other);
  381. }
  382. R Run() const {
  383. PolymorphicInvoke f =
  384. reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
  385. return f(bind_state_.get());
  386. }
  387. private:
  388. typedef R(*PolymorphicInvoke)(
  389. internal::BindStateBase*);
  390. };
  391. template <typename R, typename A1>
  392. class Callback<R(A1)> : public internal::CallbackBase {
  393. public:
  394. typedef R(RunType)(A1);
  395. Callback() : CallbackBase(NULL) { }
  396. // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
  397. // return the exact Callback<> type. See base/bind.h for details.
  398. template <typename Runnable, typename BindRunType, typename BoundArgsType>
  399. Callback(internal::BindState<Runnable, BindRunType,
  400. BoundArgsType>* bind_state)
  401. : CallbackBase(bind_state) {
  402. // Force the assignment to a local variable of PolymorphicInvoke
  403. // so the compiler will typecheck that the passed in Run() method has
  404. // the correct type.
  405. PolymorphicInvoke invoke_func =
  406. &internal::BindState<Runnable, BindRunType, BoundArgsType>
  407. ::InvokerType::Run;
  408. polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
  409. }
  410. bool Equals(const Callback& other) const {
  411. return CallbackBase::Equals(other);
  412. }
  413. R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1) const {
  414. PolymorphicInvoke f =
  415. reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
  416. return f(bind_state_.get(), internal::CallbackForward(a1));
  417. }
  418. private:
  419. typedef R(*PolymorphicInvoke)(
  420. internal::BindStateBase*,
  421. typename internal::CallbackParamTraits<A1>::ForwardType);
  422. };
  423. template <typename R, typename A1, typename A2>
  424. class Callback<R(A1, A2)> : public internal::CallbackBase {
  425. public:
  426. typedef R(RunType)(A1, A2);
  427. Callback() : CallbackBase(NULL) { }
  428. // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
  429. // return the exact Callback<> type. See base/bind.h for details.
  430. template <typename Runnable, typename BindRunType, typename BoundArgsType>
  431. Callback(internal::BindState<Runnable, BindRunType,
  432. BoundArgsType>* bind_state)
  433. : CallbackBase(bind_state) {
  434. // Force the assignment to a local variable of PolymorphicInvoke
  435. // so the compiler will typecheck that the passed in Run() method has
  436. // the correct type.
  437. PolymorphicInvoke invoke_func =
  438. &internal::BindState<Runnable, BindRunType, BoundArgsType>
  439. ::InvokerType::Run;
  440. polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
  441. }
  442. bool Equals(const Callback& other) const {
  443. return CallbackBase::Equals(other);
  444. }
  445. R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
  446. typename internal::CallbackParamTraits<A2>::ForwardType a2) const {
  447. PolymorphicInvoke f =
  448. reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
  449. return f(bind_state_.get(), internal::CallbackForward(a1),
  450. internal::CallbackForward(a2));
  451. }
  452. private:
  453. typedef R(*PolymorphicInvoke)(
  454. internal::BindStateBase*,
  455. typename internal::CallbackParamTraits<A1>::ForwardType,
  456. typename internal::CallbackParamTraits<A2>::ForwardType);
  457. };
  458. template <typename R, typename A1, typename A2, typename A3>
  459. class Callback<R(A1, A2, A3)> : public internal::CallbackBase {
  460. public:
  461. typedef R(RunType)(A1, A2, A3);
  462. Callback() : CallbackBase(NULL) { }
  463. // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
  464. // return the exact Callback<> type. See base/bind.h for details.
  465. template <typename Runnable, typename BindRunType, typename BoundArgsType>
  466. Callback(internal::BindState<Runnable, BindRunType,
  467. BoundArgsType>* bind_state)
  468. : CallbackBase(bind_state) {
  469. // Force the assignment to a local variable of PolymorphicInvoke
  470. // so the compiler will typecheck that the passed in Run() method has
  471. // the correct type.
  472. PolymorphicInvoke invoke_func =
  473. &internal::BindState<Runnable, BindRunType, BoundArgsType>
  474. ::InvokerType::Run;
  475. polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
  476. }
  477. bool Equals(const Callback& other) const {
  478. return CallbackBase::Equals(other);
  479. }
  480. R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
  481. typename internal::CallbackParamTraits<A2>::ForwardType a2,
  482. typename internal::CallbackParamTraits<A3>::ForwardType a3) const {
  483. PolymorphicInvoke f =
  484. reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
  485. return f(bind_state_.get(), internal::CallbackForward(a1),
  486. internal::CallbackForward(a2),
  487. internal::CallbackForward(a3));
  488. }
  489. private:
  490. typedef R(*PolymorphicInvoke)(
  491. internal::BindStateBase*,
  492. typename internal::CallbackParamTraits<A1>::ForwardType,
  493. typename internal::CallbackParamTraits<A2>::ForwardType,
  494. typename internal::CallbackParamTraits<A3>::ForwardType);
  495. };
  496. template <typename R, typename A1, typename A2, typename A3, typename A4>
  497. class Callback<R(A1, A2, A3, A4)> : public internal::CallbackBase {
  498. public:
  499. typedef R(RunType)(A1, A2, A3, A4);
  500. Callback() : CallbackBase(NULL) { }
  501. // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
  502. // return the exact Callback<> type. See base/bind.h for details.
  503. template <typename Runnable, typename BindRunType, typename BoundArgsType>
  504. Callback(internal::BindState<Runnable, BindRunType,
  505. BoundArgsType>* bind_state)
  506. : CallbackBase(bind_state) {
  507. // Force the assignment to a local variable of PolymorphicInvoke
  508. // so the compiler will typecheck that the passed in Run() method has
  509. // the correct type.
  510. PolymorphicInvoke invoke_func =
  511. &internal::BindState<Runnable, BindRunType, BoundArgsType>
  512. ::InvokerType::Run;
  513. polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
  514. }
  515. bool Equals(const Callback& other) const {
  516. return CallbackBase::Equals(other);
  517. }
  518. R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
  519. typename internal::CallbackParamTraits<A2>::ForwardType a2,
  520. typename internal::CallbackParamTraits<A3>::ForwardType a3,
  521. typename internal::CallbackParamTraits<A4>::ForwardType a4) const {
  522. PolymorphicInvoke f =
  523. reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
  524. return f(bind_state_.get(), internal::CallbackForward(a1),
  525. internal::CallbackForward(a2),
  526. internal::CallbackForward(a3),
  527. internal::CallbackForward(a4));
  528. }
  529. private:
  530. typedef R(*PolymorphicInvoke)(
  531. internal::BindStateBase*,
  532. typename internal::CallbackParamTraits<A1>::ForwardType,
  533. typename internal::CallbackParamTraits<A2>::ForwardType,
  534. typename internal::CallbackParamTraits<A3>::ForwardType,
  535. typename internal::CallbackParamTraits<A4>::ForwardType);
  536. };
  537. template <typename R, typename A1, typename A2, typename A3, typename A4,
  538. typename A5>
  539. class Callback<R(A1, A2, A3, A4, A5)> : public internal::CallbackBase {
  540. public:
  541. typedef R(RunType)(A1, A2, A3, A4, A5);
  542. Callback() : CallbackBase(NULL) { }
  543. // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
  544. // return the exact Callback<> type. See base/bind.h for details.
  545. template <typename Runnable, typename BindRunType, typename BoundArgsType>
  546. Callback(internal::BindState<Runnable, BindRunType,
  547. BoundArgsType>* bind_state)
  548. : CallbackBase(bind_state) {
  549. // Force the assignment to a local variable of PolymorphicInvoke
  550. // so the compiler will typecheck that the passed in Run() method has
  551. // the correct type.
  552. PolymorphicInvoke invoke_func =
  553. &internal::BindState<Runnable, BindRunType, BoundArgsType>
  554. ::InvokerType::Run;
  555. polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
  556. }
  557. bool Equals(const Callback& other) const {
  558. return CallbackBase::Equals(other);
  559. }
  560. R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
  561. typename internal::CallbackParamTraits<A2>::ForwardType a2,
  562. typename internal::CallbackParamTraits<A3>::ForwardType a3,
  563. typename internal::CallbackParamTraits<A4>::ForwardType a4,
  564. typename internal::CallbackParamTraits<A5>::ForwardType a5) const {
  565. PolymorphicInvoke f =
  566. reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
  567. return f(bind_state_.get(), internal::CallbackForward(a1),
  568. internal::CallbackForward(a2),
  569. internal::CallbackForward(a3),
  570. internal::CallbackForward(a4),
  571. internal::CallbackForward(a5));
  572. }
  573. private:
  574. typedef R(*PolymorphicInvoke)(
  575. internal::BindStateBase*,
  576. typename internal::CallbackParamTraits<A1>::ForwardType,
  577. typename internal::CallbackParamTraits<A2>::ForwardType,
  578. typename internal::CallbackParamTraits<A3>::ForwardType,
  579. typename internal::CallbackParamTraits<A4>::ForwardType,
  580. typename internal::CallbackParamTraits<A5>::ForwardType);
  581. };
  582. template <typename R, typename A1, typename A2, typename A3, typename A4,
  583. typename A5, typename A6>
  584. class Callback<R(A1, A2, A3, A4, A5, A6)> : public internal::CallbackBase {
  585. public:
  586. typedef R(RunType)(A1, A2, A3, A4, A5, A6);
  587. Callback() : CallbackBase(NULL) { }
  588. // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
  589. // return the exact Callback<> type. See base/bind.h for details.
  590. template <typename Runnable, typename BindRunType, typename BoundArgsType>
  591. Callback(internal::BindState<Runnable, BindRunType,
  592. BoundArgsType>* bind_state)
  593. : CallbackBase(bind_state) {
  594. // Force the assignment to a local variable of PolymorphicInvoke
  595. // so the compiler will typecheck that the passed in Run() method has
  596. // the correct type.
  597. PolymorphicInvoke invoke_func =
  598. &internal::BindState<Runnable, BindRunType, BoundArgsType>
  599. ::InvokerType::Run;
  600. polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
  601. }
  602. bool Equals(const Callback& other) const {
  603. return CallbackBase::Equals(other);
  604. }
  605. R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
  606. typename internal::CallbackParamTraits<A2>::ForwardType a2,
  607. typename internal::CallbackParamTraits<A3>::ForwardType a3,
  608. typename internal::CallbackParamTraits<A4>::ForwardType a4,
  609. typename internal::CallbackParamTraits<A5>::ForwardType a5,
  610. typename internal::CallbackParamTraits<A6>::ForwardType a6) const {
  611. PolymorphicInvoke f =
  612. reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
  613. return f(bind_state_.get(), internal::CallbackForward(a1),
  614. internal::CallbackForward(a2),
  615. internal::CallbackForward(a3),
  616. internal::CallbackForward(a4),
  617. internal::CallbackForward(a5),
  618. internal::CallbackForward(a6));
  619. }
  620. private:
  621. typedef R(*PolymorphicInvoke)(
  622. internal::BindStateBase*,
  623. typename internal::CallbackParamTraits<A1>::ForwardType,
  624. typename internal::CallbackParamTraits<A2>::ForwardType,
  625. typename internal::CallbackParamTraits<A3>::ForwardType,
  626. typename internal::CallbackParamTraits<A4>::ForwardType,
  627. typename internal::CallbackParamTraits<A5>::ForwardType,
  628. typename internal::CallbackParamTraits<A6>::ForwardType);
  629. };
  630. template <typename R, typename A1, typename A2, typename A3, typename A4,
  631. typename A5, typename A6, typename A7>
  632. class Callback<R(A1, A2, A3, A4, A5, A6, A7)> : public internal::CallbackBase {
  633. public:
  634. typedef R(RunType)(A1, A2, A3, A4, A5, A6, A7);
  635. Callback() : CallbackBase(NULL) { }
  636. // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
  637. // return the exact Callback<> type. See base/bind.h for details.
  638. template <typename Runnable, typename BindRunType, typename BoundArgsType>
  639. Callback(internal::BindState<Runnable, BindRunType,
  640. BoundArgsType>* bind_state)
  641. : CallbackBase(bind_state) {
  642. // Force the assignment to a local variable of PolymorphicInvoke
  643. // so the compiler will typecheck that the passed in Run() method has
  644. // the correct type.
  645. PolymorphicInvoke invoke_func =
  646. &internal::BindState<Runnable, BindRunType, BoundArgsType>
  647. ::InvokerType::Run;
  648. polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
  649. }
  650. bool Equals(const Callback& other) const {
  651. return CallbackBase::Equals(other);
  652. }
  653. R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
  654. typename internal::CallbackParamTraits<A2>::ForwardType a2,
  655. typename internal::CallbackParamTraits<A3>::ForwardType a3,
  656. typename internal::CallbackParamTraits<A4>::ForwardType a4,
  657. typename internal::CallbackParamTraits<A5>::ForwardType a5,
  658. typename internal::CallbackParamTraits<A6>::ForwardType a6,
  659. typename internal::CallbackParamTraits<A7>::ForwardType a7) const {
  660. PolymorphicInvoke f =
  661. reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
  662. return f(bind_state_.get(), internal::CallbackForward(a1),
  663. internal::CallbackForward(a2),
  664. internal::CallbackForward(a3),
  665. internal::CallbackForward(a4),
  666. internal::CallbackForward(a5),
  667. internal::CallbackForward(a6),
  668. internal::CallbackForward(a7));
  669. }
  670. private:
  671. typedef R(*PolymorphicInvoke)(
  672. internal::BindStateBase*,
  673. typename internal::CallbackParamTraits<A1>::ForwardType,
  674. typename internal::CallbackParamTraits<A2>::ForwardType,
  675. typename internal::CallbackParamTraits<A3>::ForwardType,
  676. typename internal::CallbackParamTraits<A4>::ForwardType,
  677. typename internal::CallbackParamTraits<A5>::ForwardType,
  678. typename internal::CallbackParamTraits<A6>::ForwardType,
  679. typename internal::CallbackParamTraits<A7>::ForwardType);
  680. };
  681. // Syntactic sugar to make Callbacks<void(void)> easier to declare since it
  682. // will be used in a lot of APIs with delayed execution.
  683. typedef Callback<void(void)> Closure;
  684. } // namespace base
  685. #endif // BASE_CALLBACK_H