ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 1 | // This file was GENERATED by command: |
| 2 | // pump.py callback.h.pump |
| 3 | // DO NOT EDIT BY HAND!!! |
| 4 | |
| 5 | |
ajwong@chromium.org | 22a8a0d | 2012-01-04 09:57:39 +0900 | [diff] [blame] | 6 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 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 | |
erikwright@chromium.org | ac2a188 | 2011-12-08 06:44:06 +0900 | [diff] [blame] | 13 | #include "base/callback_forward.h" |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 14 | #include "base/callback_internal.h" |
ajwong@chromium.org | ec1750a | 2011-06-27 01:22:50 +0900 | [diff] [blame] | 15 | #include "base/template_util.h" |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 16 | |
erikwright@chromium.org | ac2a188 | 2011-12-08 06:44:06 +0900 | [diff] [blame] | 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 | |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 20 | // ----------------------------------------------------------------------------- |
| 21 | // Introduction |
| 22 | // ----------------------------------------------------------------------------- |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 23 | // |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 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 |
dubroy@chromium.org | 43d9ac3 | 2012-12-07 04:00:50 +0900 | [diff] [blame] | 26 | // performing partial application of functions. |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 27 | // |
dubroy@chromium.org | 43d9ac3 | 2012-12-07 04:00:50 +0900 | [diff] [blame] | 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. |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 33 | // |
dubroy@chromium.org | 43d9ac3 | 2012-12-07 04:00:50 +0900 | [diff] [blame] | 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. |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 38 | // |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 39 | // MEMORY MANAGEMENT AND PASSING |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 40 | // |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 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. |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 44 | // |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 45 | // The reason to pass via a const-reference is to avoid unnecessary |
| 46 | // AddRef/Release pairs to the internal state. |
| 47 | // |
| 48 | // |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 49 | // ----------------------------------------------------------------------------- |
| 50 | // Quick reference for basic stuff |
| 51 | // ----------------------------------------------------------------------------- |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 52 | // |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 53 | // BINDING A BARE FUNCTION |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 54 | // |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 55 | // int Return5() { return 5; } |
| 56 | // base::Callback<int(void)> func_cb = base::Bind(&Return5); |
| 57 | // LOG(INFO) << func_cb.Run(); // Prints 5. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 58 | // |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 59 | // BINDING A CLASS METHOD |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 60 | // |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 61 | // The first argument to bind is the member function to call, the second is |
| 62 | // the object on which to call it. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 63 | // |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 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. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 72 | // |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 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. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 77 | // |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 78 | // RUNNING A CALLBACK |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 79 | // |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 80 | // Callbacks can be run with their "Run" method, which has the same |
| 81 | // signature as the template argument to the callback. |
ajwong@chromium.org | 97d22e3 | 2011-05-19 05:21:12 +0900 | [diff] [blame] | 82 | // |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 83 | // void DoSomething(const base::Callback<void(int, std::string)>& callback) { |
| 84 | // callback.Run(5, "hello"); |
| 85 | // } |
ajwong@chromium.org | 97d22e3 | 2011-05-19 05:21:12 +0900 | [diff] [blame] | 86 | // |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 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) {} |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 131 | // base::Callback<void(const std::string&)> cb = base::Bind(&MyFunc, 23); |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 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 | // |
brandonsalmon@chromium.org | acaaae7 | 2014-06-26 15:07:45 +0900 | [diff] [blame] | 146 | // The callback will not be run if the object has already been destroyed. |
| 147 | // DANGER: weak pointers are not threadsafe, so don't use this |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 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 | // |
scheib@chromium.org | a2f2a9b | 2013-11-28 14:18:53 +0900 | [diff] [blame] | 217 | // Const references are *copied* unless ConstRef is used. Example: |
| 218 | // |
| 219 | // void foo(const int& arg) { printf("%d %p\n", arg, &arg); } |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 220 | // int n = 1; |
scheib@chromium.org | a2f2a9b | 2013-11-28 14:18:53 +0900 | [diff] [blame] | 221 | // base::Closure has_copy = base::Bind(&foo, n); |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 222 | // base::Closure has_ref = base::Bind(&foo, base::ConstRef(n)); |
| 223 | // n = 2; |
scheib@chromium.org | a2f2a9b | 2013-11-28 14:18:53 +0900 | [diff] [blame] | 224 | // foo(n); // Prints "2 0xaaaaaaaaaaaa" |
| 225 | // has_copy.Run(); // Prints "1 0xbbbbbbbbbbbb" |
| 226 | // has_ref.Run(); // Prints "2 0xaaaaaaaaaaaa" |
brettw@chromium.org | 11e3bfd | 2012-07-13 05:06:40 +0900 | [diff] [blame] | 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 | // ----------------------------------------------------------------------------- |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 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. |
jhawkins@chromium.org | bb87d2a | 2011-12-13 09:06:28 +0900 | [diff] [blame] | 249 | // 3) The arguments wrappers (e.g., Unretained() and ConstRef()). |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 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 |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 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. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 258 | // |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 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. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 265 | // |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 266 | // To BindState<> objects are created inside the Bind() functions. |
| 267 | // These functions, along with a set of internal templates, are responsible for |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 268 | // |
| 269 | // - Unwrapping the function signature into return type, and parameters |
| 270 | // - Determining the number of parameters that are bound |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 271 | // - Creating the BindState storing the bound parameters |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 272 | // - Performing compile-time asserts to avoid error-prone behavior |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 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. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 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. |
groby@chromium.org | 34d50fa | 2012-10-11 12:54:33 +0900 | [diff] [blame] | 282 | // These copies are created even if the function takes parameters as const |
dubroy@chromium.org | 43d9ac3 | 2012-12-07 04:00:50 +0900 | [diff] [blame] | 283 | // references. (Binding to non-const references is forbidden, see bind.h.) |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 284 | // |
| 285 | // To change this behavior, we introduce a set of argument wrappers |
jhawkins@chromium.org | bb87d2a | 2011-12-13 09:06:28 +0900 | [diff] [blame] | 286 | // (e.g., Unretained(), and ConstRef()). These are simple container templates |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 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 |
jhawkins@chromium.org | bb87d2a | 2011-12-13 09:06:28 +0900 | [diff] [blame] | 323 | // limited amount of subtyping of the tr1::function object (e.g., |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 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 |
thakis@chromium.org | 21cbe22 | 2011-04-20 03:22:00 +0900 | [diff] [blame] | 328 | // become a source of errors. Removing support for these features actually |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 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. |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 349 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 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 | // |
ajwong@chromium.org | 6f015bd | 2011-11-29 07:13:54 +0900 | [diff] [blame] | 356 | // After this, create template specializations for 0-7 parameters. Note that |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 357 | // even though the template typelist grows, the specialization still |
| 358 | // only has one type: the function signature. |
erikwright@chromium.org | ac2a188 | 2011-12-08 06:44:06 +0900 | [diff] [blame] | 359 | // |
| 360 | // If you are thinking of forward declaring Callback in your own header file, |
| 361 | // please include "base/callback_forward.h" instead. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 362 | template <typename Sig> |
| 363 | class Callback; |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 364 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 365 | namespace internal { |
| 366 | template <typename Runnable, typename RunType, typename BoundArgsType> |
| 367 | struct BindState; |
| 368 | } // namespace internal |
| 369 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 370 | template <typename R> |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 371 | class Callback<R(void)> : public internal::CallbackBase { |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 372 | public: |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 373 | typedef R(RunType)(); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 374 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 375 | Callback() : CallbackBase(NULL) { } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 376 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 377 | // Note that this constructor CANNOT be explicit, and that Bind() CANNOT |
| 378 | // return the exact Callback<> type. See base/bind.h for details. |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 379 | template <typename Runnable, typename BindRunType, typename BoundArgsType> |
| 380 | Callback(internal::BindState<Runnable, BindRunType, |
| 381 | BoundArgsType>* bind_state) |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 382 | : CallbackBase(bind_state) { |
| 383 | |
| 384 | // Force the assignment to a local variable of PolymorphicInvoke |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 385 | // so the compiler will typecheck that the passed in Run() method has |
| 386 | // the correct type. |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 387 | PolymorphicInvoke invoke_func = |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 388 | &internal::BindState<Runnable, BindRunType, BoundArgsType> |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 389 | ::InvokerType::Run; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 390 | polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 391 | } |
| 392 | |
ajwong@chromium.org | 3bc50ce | 2011-09-10 12:14:35 +0900 | [diff] [blame] | 393 | bool Equals(const Callback& other) const { |
| 394 | return CallbackBase::Equals(other); |
| 395 | } |
| 396 | |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 397 | R Run() const { |
| 398 | PolymorphicInvoke f = |
| 399 | reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 400 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 401 | return f(bind_state_.get()); |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 402 | } |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 403 | |
| 404 | private: |
| 405 | typedef R(*PolymorphicInvoke)( |
| 406 | internal::BindStateBase*); |
| 407 | |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 408 | }; |
| 409 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 410 | template <typename R, typename A1> |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 411 | class Callback<R(A1)> : public internal::CallbackBase { |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 412 | public: |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 413 | typedef R(RunType)(A1); |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 414 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 415 | Callback() : CallbackBase(NULL) { } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 416 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 417 | // Note that this constructor CANNOT be explicit, and that Bind() CANNOT |
| 418 | // return the exact Callback<> type. See base/bind.h for details. |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 419 | template <typename Runnable, typename BindRunType, typename BoundArgsType> |
| 420 | Callback(internal::BindState<Runnable, BindRunType, |
| 421 | BoundArgsType>* bind_state) |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 422 | : CallbackBase(bind_state) { |
| 423 | |
| 424 | // Force the assignment to a local variable of PolymorphicInvoke |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 425 | // so the compiler will typecheck that the passed in Run() method has |
| 426 | // the correct type. |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 427 | PolymorphicInvoke invoke_func = |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 428 | &internal::BindState<Runnable, BindRunType, BoundArgsType> |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 429 | ::InvokerType::Run; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 430 | polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 431 | } |
| 432 | |
ajwong@chromium.org | 3bc50ce | 2011-09-10 12:14:35 +0900 | [diff] [blame] | 433 | bool Equals(const Callback& other) const { |
| 434 | return CallbackBase::Equals(other); |
| 435 | } |
| 436 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 437 | R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1) const { |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 438 | PolymorphicInvoke f = |
| 439 | reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 440 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 441 | return f(bind_state_.get(), internal::CallbackForward(a1)); |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 442 | } |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 443 | |
| 444 | private: |
| 445 | typedef R(*PolymorphicInvoke)( |
| 446 | internal::BindStateBase*, |
| 447 | typename internal::CallbackParamTraits<A1>::ForwardType); |
| 448 | |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 449 | }; |
| 450 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 451 | template <typename R, typename A1, typename A2> |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 452 | class Callback<R(A1, A2)> : public internal::CallbackBase { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 453 | public: |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 454 | typedef R(RunType)(A1, A2); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 455 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 456 | Callback() : CallbackBase(NULL) { } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 457 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 458 | // Note that this constructor CANNOT be explicit, and that Bind() CANNOT |
| 459 | // return the exact Callback<> type. See base/bind.h for details. |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 460 | template <typename Runnable, typename BindRunType, typename BoundArgsType> |
| 461 | Callback(internal::BindState<Runnable, BindRunType, |
| 462 | BoundArgsType>* bind_state) |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 463 | : CallbackBase(bind_state) { |
| 464 | |
| 465 | // Force the assignment to a local variable of PolymorphicInvoke |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 466 | // so the compiler will typecheck that the passed in Run() method has |
| 467 | // the correct type. |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 468 | PolymorphicInvoke invoke_func = |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 469 | &internal::BindState<Runnable, BindRunType, BoundArgsType> |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 470 | ::InvokerType::Run; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 471 | polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 472 | } |
| 473 | |
ajwong@chromium.org | 3bc50ce | 2011-09-10 12:14:35 +0900 | [diff] [blame] | 474 | bool Equals(const Callback& other) const { |
| 475 | return CallbackBase::Equals(other); |
| 476 | } |
| 477 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 478 | R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1, |
| 479 | typename internal::CallbackParamTraits<A2>::ForwardType a2) const { |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 480 | PolymorphicInvoke f = |
| 481 | reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 482 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 483 | return f(bind_state_.get(), internal::CallbackForward(a1), |
| 484 | internal::CallbackForward(a2)); |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 485 | } |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 486 | |
| 487 | private: |
| 488 | typedef R(*PolymorphicInvoke)( |
| 489 | internal::BindStateBase*, |
| 490 | typename internal::CallbackParamTraits<A1>::ForwardType, |
| 491 | typename internal::CallbackParamTraits<A2>::ForwardType); |
| 492 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 493 | }; |
| 494 | |
| 495 | template <typename R, typename A1, typename A2, typename A3> |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 496 | class Callback<R(A1, A2, A3)> : public internal::CallbackBase { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 497 | public: |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 498 | typedef R(RunType)(A1, A2, A3); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 499 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 500 | Callback() : CallbackBase(NULL) { } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 501 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 502 | // Note that this constructor CANNOT be explicit, and that Bind() CANNOT |
| 503 | // return the exact Callback<> type. See base/bind.h for details. |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 504 | template <typename Runnable, typename BindRunType, typename BoundArgsType> |
| 505 | Callback(internal::BindState<Runnable, BindRunType, |
| 506 | BoundArgsType>* bind_state) |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 507 | : CallbackBase(bind_state) { |
| 508 | |
| 509 | // Force the assignment to a local variable of PolymorphicInvoke |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 510 | // so the compiler will typecheck that the passed in Run() method has |
| 511 | // the correct type. |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 512 | PolymorphicInvoke invoke_func = |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 513 | &internal::BindState<Runnable, BindRunType, BoundArgsType> |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 514 | ::InvokerType::Run; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 515 | polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 516 | } |
| 517 | |
ajwong@chromium.org | 3bc50ce | 2011-09-10 12:14:35 +0900 | [diff] [blame] | 518 | bool Equals(const Callback& other) const { |
| 519 | return CallbackBase::Equals(other); |
| 520 | } |
| 521 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 522 | R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1, |
| 523 | typename internal::CallbackParamTraits<A2>::ForwardType a2, |
| 524 | typename internal::CallbackParamTraits<A3>::ForwardType a3) const { |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 525 | PolymorphicInvoke f = |
| 526 | reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 527 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 528 | return f(bind_state_.get(), internal::CallbackForward(a1), |
| 529 | internal::CallbackForward(a2), |
| 530 | internal::CallbackForward(a3)); |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 531 | } |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 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 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | template <typename R, typename A1, typename A2, typename A3, typename A4> |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 543 | class Callback<R(A1, A2, A3, A4)> : public internal::CallbackBase { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 544 | public: |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 545 | typedef R(RunType)(A1, A2, A3, A4); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 546 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 547 | Callback() : CallbackBase(NULL) { } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 548 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 549 | // Note that this constructor CANNOT be explicit, and that Bind() CANNOT |
| 550 | // return the exact Callback<> type. See base/bind.h for details. |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 551 | template <typename Runnable, typename BindRunType, typename BoundArgsType> |
| 552 | Callback(internal::BindState<Runnable, BindRunType, |
| 553 | BoundArgsType>* bind_state) |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 554 | : CallbackBase(bind_state) { |
| 555 | |
| 556 | // Force the assignment to a local variable of PolymorphicInvoke |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 557 | // so the compiler will typecheck that the passed in Run() method has |
| 558 | // the correct type. |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 559 | PolymorphicInvoke invoke_func = |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 560 | &internal::BindState<Runnable, BindRunType, BoundArgsType> |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 561 | ::InvokerType::Run; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 562 | polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 563 | } |
| 564 | |
ajwong@chromium.org | 3bc50ce | 2011-09-10 12:14:35 +0900 | [diff] [blame] | 565 | bool Equals(const Callback& other) const { |
| 566 | return CallbackBase::Equals(other); |
| 567 | } |
| 568 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 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 { |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 573 | PolymorphicInvoke f = |
| 574 | reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 575 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 576 | return f(bind_state_.get(), internal::CallbackForward(a1), |
| 577 | internal::CallbackForward(a2), |
| 578 | internal::CallbackForward(a3), |
| 579 | internal::CallbackForward(a4)); |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 580 | } |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 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 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | template <typename R, typename A1, typename A2, typename A3, typename A4, |
| 593 | typename A5> |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 594 | class Callback<R(A1, A2, A3, A4, A5)> : public internal::CallbackBase { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 595 | public: |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 596 | typedef R(RunType)(A1, A2, A3, A4, A5); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 597 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 598 | Callback() : CallbackBase(NULL) { } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 599 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 600 | // Note that this constructor CANNOT be explicit, and that Bind() CANNOT |
| 601 | // return the exact Callback<> type. See base/bind.h for details. |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 602 | template <typename Runnable, typename BindRunType, typename BoundArgsType> |
| 603 | Callback(internal::BindState<Runnable, BindRunType, |
| 604 | BoundArgsType>* bind_state) |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 605 | : CallbackBase(bind_state) { |
| 606 | |
| 607 | // Force the assignment to a local variable of PolymorphicInvoke |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 608 | // so the compiler will typecheck that the passed in Run() method has |
| 609 | // the correct type. |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 610 | PolymorphicInvoke invoke_func = |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 611 | &internal::BindState<Runnable, BindRunType, BoundArgsType> |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 612 | ::InvokerType::Run; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 613 | polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 614 | } |
| 615 | |
ajwong@chromium.org | 3bc50ce | 2011-09-10 12:14:35 +0900 | [diff] [blame] | 616 | bool Equals(const Callback& other) const { |
| 617 | return CallbackBase::Equals(other); |
| 618 | } |
| 619 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 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 { |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 625 | PolymorphicInvoke f = |
| 626 | reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 627 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 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)); |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 633 | } |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 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 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 644 | }; |
| 645 | |
| 646 | template <typename R, typename A1, typename A2, typename A3, typename A4, |
| 647 | typename A5, typename A6> |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 648 | class Callback<R(A1, A2, A3, A4, A5, A6)> : public internal::CallbackBase { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 649 | public: |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 650 | typedef R(RunType)(A1, A2, A3, A4, A5, A6); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 651 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 652 | Callback() : CallbackBase(NULL) { } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 653 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 654 | // Note that this constructor CANNOT be explicit, and that Bind() CANNOT |
| 655 | // return the exact Callback<> type. See base/bind.h for details. |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 656 | template <typename Runnable, typename BindRunType, typename BoundArgsType> |
| 657 | Callback(internal::BindState<Runnable, BindRunType, |
| 658 | BoundArgsType>* bind_state) |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 659 | : CallbackBase(bind_state) { |
| 660 | |
| 661 | // Force the assignment to a local variable of PolymorphicInvoke |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 662 | // so the compiler will typecheck that the passed in Run() method has |
| 663 | // the correct type. |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 664 | PolymorphicInvoke invoke_func = |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 665 | &internal::BindState<Runnable, BindRunType, BoundArgsType> |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 666 | ::InvokerType::Run; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 667 | polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 668 | } |
| 669 | |
ajwong@chromium.org | 3bc50ce | 2011-09-10 12:14:35 +0900 | [diff] [blame] | 670 | bool Equals(const Callback& other) const { |
| 671 | return CallbackBase::Equals(other); |
| 672 | } |
| 673 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 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 { |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 680 | PolymorphicInvoke f = |
| 681 | reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 682 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 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)); |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 689 | } |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 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 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 701 | }; |
| 702 | |
ajwong@chromium.org | 6f015bd | 2011-11-29 07:13:54 +0900 | [diff] [blame] | 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 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 709 | Callback() : CallbackBase(NULL) { } |
ajwong@chromium.org | 6f015bd | 2011-11-29 07:13:54 +0900 | [diff] [blame] | 710 | |
ajwong@chromium.org | 6f015bd | 2011-11-29 07:13:54 +0900 | [diff] [blame] | 711 | // Note that this constructor CANNOT be explicit, and that Bind() CANNOT |
| 712 | // return the exact Callback<> type. See base/bind.h for details. |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 713 | template <typename Runnable, typename BindRunType, typename BoundArgsType> |
| 714 | Callback(internal::BindState<Runnable, BindRunType, |
| 715 | BoundArgsType>* bind_state) |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 716 | : CallbackBase(bind_state) { |
| 717 | |
| 718 | // Force the assignment to a local variable of PolymorphicInvoke |
ajwong@chromium.org | 6f015bd | 2011-11-29 07:13:54 +0900 | [diff] [blame] | 719 | // so the compiler will typecheck that the passed in Run() method has |
| 720 | // the correct type. |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 721 | PolymorphicInvoke invoke_func = |
xiaomings@google.com | dcfad55 | 2012-08-14 08:11:50 +0900 | [diff] [blame] | 722 | &internal::BindState<Runnable, BindRunType, BoundArgsType> |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 723 | ::InvokerType::Run; |
ajwong@chromium.org | 6f015bd | 2011-11-29 07:13:54 +0900 | [diff] [blame] | 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 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 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)); |
ajwong@chromium.org | 6f015bd | 2011-11-29 07:13:54 +0900 | [diff] [blame] | 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 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 763 | |
wtc@chromium.org | 98eec04 | 2014-07-03 15:10:13 +0900 | [diff] [blame] | 764 | // Syntactic sugar to make Callback<void(void)> easier to declare since it |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 765 | // will be used in a lot of APIs with delayed execution. |
| 766 | typedef Callback<void(void)> Closure; |
| 767 | |
| 768 | } // namespace base |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 769 | |
| 770 | #endif // BASE_CALLBACK_H |