| OLD | NEW |
| 1 # base::Callback<> and base::Bind() |
| 1 | 2 |
| 2 # Introduction | 3 ## Introduction |
| 3 | 4 |
| 4 The templated Callback class is a generalized function object. Together | 5 The templated `Callback<>` class is a generalized function object. Together with |
| 5 with the `Bind()` function in bind.h, they provide a type-safe method for | 6 the `Bind()` function in base/bind.h, they provide a type-safe method for |
| 6 performing partial application of functions. | 7 performing partial application of functions. |
| 7 | 8 |
| 8 Partial application (or "currying") is the process of binding a subset of | 9 Partial application (or "currying") is the process of binding a subset of a |
| 9 a function's arguments to produce another function that takes fewer | 10 function's arguments to produce another function that takes fewer arguments. |
| 10 arguments. This can be used to pass around a unit of delayed execution, | 11 This can be used to pass around a unit of delayed execution, much like lexical |
| 11 much like lexical closures are used in other languages. For example, it | 12 closures are used in other languages. For example, it is used in Chromium code |
| 12 is used in Chromium code to schedule tasks on different MessageLoops. | 13 to schedule tasks on different MessageLoops. |
| 13 | 14 |
| 14 A callback with no unbound input parameters (`base::Callback<void()>`) | 15 A callback with no unbound input parameters (`Callback<void()>`) is called a |
| 15 is called a `base::Closure`. Note that this is NOT the same as what other | 16 `Closure`. Note that this is NOT the same as what other languages refer to as a |
| 16 languages refer to as a closure -- it does not retain a reference to its | 17 closure -- it does not retain a reference to its enclosing environment. |
| 17 enclosing environment. | |
| 18 | 18 |
| 19 ## MEMORY MANAGEMENT AND PASSING | 19 ### Memory Management And Passing |
| 20 | 20 |
| 21 The Callback objects themselves should be passed by const-reference, and | 21 The Callback objects themselves should be passed by const-reference, and stored |
| 22 stored by copy. They internally store their state via a refcounted class | 22 by copy. They internally store their state via a refcounted class and thus do |
| 23 and thus do not need to be deleted. | 23 not need to be deleted. |
| 24 | 24 |
| 25 The reason to pass via a const-reference is to avoid unnecessary | 25 The reason to pass via a const-reference is to avoid unnecessary AddRef/Release |
| 26 AddRef/Release pairs to the internal state. | 26 pairs to the internal state. |
| 27 | 27 |
| 28 ## Quick reference for basic stuff |
| 28 | 29 |
| 29 # Quick reference for basic stuff | 30 ### Binding A Bare Function |
| 30 | |
| 31 ## BINDING A BARE FUNCTION | |
| 32 | 31 |
| 33 ```cpp | 32 ```cpp |
| 34 int Return5() { return 5; } | 33 int Return5() { return 5; } |
| 35 base::Callback<int()> func_cb = base::Bind(&Return5); | 34 base::Callback<int()> func_cb = base::Bind(&Return5); |
| 36 LOG(INFO) << func_cb.Run(); // Prints 5. | 35 LOG(INFO) << func_cb.Run(); // Prints 5. |
| 37 ``` | 36 ``` |
| 38 | 37 |
| 39 ## BINDING A CLASS METHOD | 38 ### Binding A Class Method |
| 40 | 39 |
| 41 The first argument to bind is the member function to call, the second is | 40 The first argument to bind is the member function to call, the second is the |
| 42 the object on which to call it. | 41 object on which to call it. |
| 43 | 42 |
| 44 ```cpp | 43 ```cpp |
| 45 class Ref : public base::RefCountedThreadSafe<Ref> { | 44 class Ref : public base::RefCountedThreadSafe<Ref> { |
| 46 public: | 45 public: |
| 47 int Foo() { return 3; } | 46 int Foo() { return 3; } |
| 48 void PrintBye() { LOG(INFO) << "bye."; } | 47 void PrintBye() { LOG(INFO) << "bye."; } |
| 49 }; | 48 }; |
| 50 scoped_refptr<Ref> ref = new Ref(); | 49 scoped_refptr<Ref> ref = new Ref(); |
| 51 base::Callback<void()> ref_cb = base::Bind(&Ref::Foo, ref); | 50 base::Callback<void()> ref_cb = base::Bind(&Ref::Foo, ref); |
| 52 LOG(INFO) << ref_cb.Run(); // Prints out 3. | 51 LOG(INFO) << ref_cb.Run(); // Prints out 3. |
| 53 ``` | 52 ``` |
| 54 | 53 |
| 55 By default the object must support RefCounted or you will get a compiler | 54 By default the object must support RefCounted or you will get a compiler |
| 56 error. If you're passing between threads, be sure it's | 55 error. If you're passing between threads, be sure it's |
| 57 RefCountedThreadSafe! See "Advanced binding of member functions" below if | 56 RefCountedThreadSafe! See "Advanced binding of member functions" below if |
| 58 you don't want to use reference counting. | 57 you don't want to use reference counting. |
| 59 | 58 |
| 60 ## RUNNING A CALLBACK | 59 ### Running A Callback |
| 61 | 60 |
| 62 Callbacks can be run with their `Run` method, which has the same | 61 Callbacks can be run with their `Run` method, which has the same |
| 63 signature as the template argument to the callback. | 62 signature as the template argument to the callback. |
| 64 | 63 |
| 65 ```cpp | 64 ```cpp |
| 66 void DoSomething(const base::Callback<void(int, std::string)>& callback) { | 65 void DoSomething(const base::Callback<void(int, std::string)>& callback) { |
| 67 callback.Run(5, "hello"); | 66 callback.Run(5, "hello"); |
| 68 } | 67 } |
| 69 ``` | 68 ``` |
| 70 | 69 |
| 71 Callbacks can be run more than once (they don't get deleted or marked when | 70 Callbacks can be run more than once (they don't get deleted or marked when |
| 72 run). However, this precludes using base::Passed (see below). | 71 run). However, this precludes using base::Passed (see below). |
| 73 | 72 |
| 74 ```cpp | 73 ```cpp |
| 75 void DoSomething(const base::Callback<double(double)>& callback) { | 74 void DoSomething(const base::Callback<double(double)>& callback) { |
| 76 double myresult = callback.Run(3.14159); | 75 double myresult = callback.Run(3.14159); |
| 77 myresult += callback.Run(2.71828); | 76 myresult += callback.Run(2.71828); |
| 78 } | 77 } |
| 79 ``` | 78 ``` |
| 80 | 79 |
| 81 ## PASSING UNBOUND INPUT PARAMETERS | 80 ### Passing Unbound Input Parameters |
| 82 | 81 |
| 83 Unbound parameters are specified at the time a callback is `Run()`. They are | 82 Unbound parameters are specified at the time a callback is `Run()`. They are |
| 84 specified in the `Callback` template type: | 83 specified in the `Callback` template type: |
| 85 | 84 |
| 86 ```cpp | 85 ```cpp |
| 87 void MyFunc(int i, const std::string& str) {} | 86 void MyFunc(int i, const std::string& str) {} |
| 88 base::Callback<void(int, const std::string&)> cb = base::Bind(&MyFunc); | 87 base::Callback<void(int, const std::string&)> cb = base::Bind(&MyFunc); |
| 89 cb.Run(23, "hello, world"); | 88 cb.Run(23, "hello, world"); |
| 90 ``` | 89 ``` |
| 91 | 90 |
| 92 ## PASSING BOUND INPUT PARAMETERS | 91 ### Passing Bound Input Parameters |
| 93 | 92 |
| 94 Bound parameters are specified when you create the callback as arguments | 93 Bound parameters are specified when you create the callback as arguments to |
| 95 to `Bind()`. They will be passed to the function and the `Run()`ner of the | 94 `Bind()`. They will be passed to the function and the `Run()`ner of the callback |
| 96 callback doesn't see those values or even know that the function it's | 95 doesn't see those values or even know that the function it's calling. |
| 97 calling. | |
| 98 | 96 |
| 99 ```cpp | 97 ```cpp |
| 100 void MyFunc(int i, const std::string& str) {} | 98 void MyFunc(int i, const std::string& str) {} |
| 101 base::Callback<void()> cb = base::Bind(&MyFunc, 23, "hello world"); | 99 base::Callback<void()> cb = base::Bind(&MyFunc, 23, "hello world"); |
| 102 cb.Run(); | 100 cb.Run(); |
| 103 ``` | 101 ``` |
| 104 | 102 |
| 105 A callback with no unbound input parameters (`base::Callback<void()>`) | 103 A callback with no unbound input parameters (`base::Callback<void()>`) is called |
| 106 is called a `base::Closure`. So we could have also written: | 104 a `base::Closure`. So we could have also written: |
| 107 | 105 |
| 108 ```cpp | 106 ```cpp |
| 109 base::Closure cb = base::Bind(&MyFunc, 23, "hello world"); | 107 base::Closure cb = base::Bind(&MyFunc, 23, "hello world"); |
| 110 ``` | 108 ``` |
| 111 | 109 |
| 112 When calling member functions, bound parameters just go after the object | 110 When calling member functions, bound parameters just go after the object |
| 113 pointer. | 111 pointer. |
| 114 | 112 |
| 115 ```cpp | 113 ```cpp |
| 116 base::Closure cb = base::Bind(&MyClass::MyFunc, this, 23, "hello world"); | 114 base::Closure cb = base::Bind(&MyClass::MyFunc, this, 23, "hello world"); |
| 117 ``` | 115 ``` |
| 118 | 116 |
| 119 ## PARTIAL BINDING OF PARAMETERS | 117 ### Partial Binding Of Parameters |
| 120 | 118 |
| 121 You can specify some parameters when you create the callback, and specify | 119 You can specify some parameters when you create the callback, and specify the |
| 122 the rest when you execute the callback. | 120 rest when you execute the callback. |
| 123 | 121 |
| 124 ```cpp | 122 ```cpp |
| 125 void MyFunc(int i, const std::string& str) {} | 123 void MyFunc(int i, const std::string& str) {} |
| 126 base::Callback<void(const std::string&)> cb = base::Bind(&MyFunc, 23); | 124 base::Callback<void(const std::string&)> cb = base::Bind(&MyFunc, 23); |
| 127 cb.Run("hello world"); | 125 cb.Run("hello world"); |
| 128 ``` | 126 ``` |
| 129 | 127 |
| 130 When calling a function bound parameters are first, followed by unbound | 128 When calling a function bound parameters are first, followed by unbound |
| 131 parameters. | 129 parameters. |
| 132 | 130 |
| 131 ## Quick reference for advanced binding |
| 133 | 132 |
| 134 # Quick reference for advanced binding | 133 ### Binding A Class Method With Weak Pointers |
| 135 | |
| 136 ## BINDING A CLASS METHOD WITH WEAK POINTERS | |
| 137 | 134 |
| 138 ```cpp | 135 ```cpp |
| 139 base::Bind(&MyClass::Foo, GetWeakPtr()); | 136 base::Bind(&MyClass::Foo, GetWeakPtr()); |
| 140 `` | 137 ``` |
| 141 | 138 |
| 142 The callback will not be run if the object has already been destroyed. | 139 The callback will not be run if the object has already been destroyed. |
| 143 DANGER: weak pointers are not threadsafe, so don't use this | 140 **DANGER**: weak pointers are not threadsafe, so don't use this when passing bet
ween |
| 144 when passing between threads! | 141 threads! |
| 145 | 142 |
| 146 ## BINDING A CLASS METHOD WITH MANUAL LIFETIME MANAGEMENT | 143 ### Binding A Class Method With Manual Lifetime Management |
| 147 | 144 |
| 148 ```cpp | 145 ```cpp |
| 149 base::Bind(&MyClass::Foo, base::Unretained(this)); | 146 base::Bind(&MyClass::Foo, base::Unretained(this)); |
| 150 ``` | 147 ``` |
| 151 | 148 |
| 152 This disables all lifetime management on the object. You're responsible | 149 This disables all lifetime management on the object. You're responsible for |
| 153 for making sure the object is alive at the time of the call. You break it, | 150 making sure the object is alive at the time of the call. You break it, you own |
| 154 you own it! | 151 it! |
| 155 | 152 |
| 156 ## BINDING A CLASS METHOD AND HAVING THE CALLBACK OWN THE CLASS | 153 ### Binding A Class Method And Having The Callback Own The Class |
| 157 | 154 |
| 158 ```cpp | 155 ```cpp |
| 159 MyClass* myclass = new MyClass; | 156 MyClass* myclass = new MyClass; |
| 160 base::Bind(&MyClass::Foo, base::Owned(myclass)); | 157 base::Bind(&MyClass::Foo, base::Owned(myclass)); |
| 161 ``` | 158 ``` |
| 162 | 159 |
| 163 The object will be deleted when the callback is destroyed, even if it's | 160 The object will be deleted when the callback is destroyed, even if it's not run |
| 164 not run (like if you post a task during shutdown). Potentially useful for | 161 (like if you post a task during shutdown). Potentially useful for "fire and |
| 165 "fire and forget" cases. | 162 forget" cases. |
| 166 | 163 |
| 167 ## IGNORING RETURN VALUES | 164 ### Ignoring Return Values |
| 168 | 165 |
| 169 Sometimes you want to call a function that returns a value in a callback | 166 Sometimes you want to call a function that returns a value in a callback that |
| 170 that doesn't expect a return value. | 167 doesn't expect a return value. |
| 171 | 168 |
| 172 ```cpp | 169 ```cpp |
| 173 int DoSomething(int arg) { cout << arg << endl; } | 170 int DoSomething(int arg) { cout << arg << endl; } |
| 174 base::Callback<void(int)> cb = | 171 base::Callback<void(int)> cb = |
| 175 base::Bind(base::IgnoreResult(&DoSomething)); | 172 base::Bind(base::IgnoreResult(&DoSomething)); |
| 176 ``` | 173 ``` |
| 177 | 174 |
| 178 # Quick reference for binding parameters to Bind() | 175 ## Quick reference for binding parameters to Bind() |
| 179 | 176 |
| 180 Bound parameters are specified as arguments to `Bind()` and are passed to the | 177 Bound parameters are specified as arguments to `Bind()` and are passed to the |
| 181 function. A callback with no parameters or no unbound parameters is called a | 178 function. A callback with no parameters or no unbound parameters is called a |
| 182 `Closure` (`base::Callback<void()>` and `base::Closure` are the same thing). | 179 `Closure` (`base::Callback<void()>` and `base::Closure` are the same thing). |
| 183 | 180 |
| 184 ## PASSING PARAMETERS OWNED BY THE CALLBACK | 181 ### Passing Parameters Owned By The Callback |
| 185 | 182 |
| 186 ```cpp | 183 ```cpp |
| 187 void Foo(int* arg) { cout << *arg << endl; } | 184 void Foo(int* arg) { cout << *arg << endl; } |
| 188 int* pn = new int(1); | 185 int* pn = new int(1); |
| 189 base::Closure foo_callback = base::Bind(&foo, base::Owned(pn)); | 186 base::Closure foo_callback = base::Bind(&foo, base::Owned(pn)); |
| 190 ``` | 187 ``` |
| 191 | 188 |
| 192 The parameter will be deleted when the callback is destroyed, even if it's | 189 The parameter will be deleted when the callback is destroyed, even if it's not |
| 193 not run (like if you post a task during shutdown). | 190 run (like if you post a task during shutdown). |
| 194 | 191 |
| 195 ## PASSING PARAMETERS AS A scoped_ptr | 192 ### Passing Parameters As A unique_ptr |
| 196 | 193 |
| 197 ```cpp | 194 ```cpp |
| 198 void TakesOwnership(std::unique_ptr<Foo> arg) {} | 195 void TakesOwnership(std::unique_ptr<Foo> arg) {} |
| 199 std::unique_ptr<Foo> f(new Foo); | 196 std::unique_ptr<Foo> f(new Foo); |
| 200 // f becomes null during the following call. | 197 // f becomes null during the following call. |
| 201 base::Closure cb = base::Bind(&TakesOwnership, base::Passed(&f)); | 198 base::Closure cb = base::Bind(&TakesOwnership, base::Passed(&f)); |
| 202 ``` | 199 ``` |
| 203 | 200 |
| 204 Ownership of the parameter will be with the callback until the callback is | 201 Ownership of the parameter will be with the callback until the callback is run, |
| 205 run, and then ownership is passed to the callback function. This means the | 202 and then ownership is passed to the callback function. This means the callback |
| 206 callback can only be run once. If the callback is never run, it will delete | 203 can only be run once. If the callback is never run, it will delete the object |
| 207 the object when it's destroyed. | 204 when it's destroyed. |
| 208 | 205 |
| 209 ## PASSING PARAMETERS AS A scoped_refptr | 206 ### Passing Parameters As A scoped_refptr |
| 210 | 207 |
| 211 ```cpp | 208 ```cpp |
| 212 void TakesOneRef(scoped_refptr<Foo> arg) {} | 209 void TakesOneRef(scoped_refptr<Foo> arg) {} |
| 213 scoped_refptr<Foo> f(new Foo) | 210 scoped_refptr<Foo> f(new Foo) |
| 214 base::Closure cb = base::Bind(&TakesOneRef, f); | 211 base::Closure cb = base::Bind(&TakesOneRef, f); |
| 215 ``` | 212 ``` |
| 216 | 213 |
| 217 This should "just work." The closure will take a reference as long as it | 214 This should "just work." The closure will take a reference as long as it is |
| 218 is alive, and another reference will be taken for the called function. | 215 alive, and another reference will be taken for the called function. |
| 219 | 216 |
| 220 ## PASSING PARAMETERS BY REFERENCE | 217 ### Passing Parameters By Reference |
| 221 | 218 |
| 222 Const references are *copied* unless `ConstRef` is used. Example: | 219 Const references are *copied* unless `ConstRef` is used. Example: |
| 223 | 220 |
| 224 ```cpp | 221 ```cpp |
| 225 void foo(const int& arg) { printf("%d %p\n", arg, &arg); } | 222 void foo(const int& arg) { printf("%d %p\n", arg, &arg); } |
| 226 int n = 1; | 223 int n = 1; |
| 227 base::Closure has_copy = base::Bind(&foo, n); | 224 base::Closure has_copy = base::Bind(&foo, n); |
| 228 base::Closure has_ref = base::Bind(&foo, base::ConstRef(n)); | 225 base::Closure has_ref = base::Bind(&foo, base::ConstRef(n)); |
| 229 n = 2; | 226 n = 2; |
| 230 foo(n); // Prints "2 0xaaaaaaaaaaaa" | 227 foo(n); // Prints "2 0xaaaaaaaaaaaa" |
| 231 has_copy.Run(); // Prints "1 0xbbbbbbbbbbbb" | 228 has_copy.Run(); // Prints "1 0xbbbbbbbbbbbb" |
| 232 has_ref.Run(); // Prints "2 0xaaaaaaaaaaaa" | 229 has_ref.Run(); // Prints "2 0xaaaaaaaaaaaa" |
| 233 ``` | 230 ``` |
| 234 | 231 |
| 235 Normally parameters are copied in the closure. DANGER: ConstRef stores a | 232 Normally parameters are copied in the closure. |
| 236 const reference instead, referencing the original parameter. This means | 233 **DANGER**: ConstRef stores a const reference instead, referencing the original |
| 237 that you must ensure the object outlives the callback! | 234 parameter. This means that you must ensure the object outlives the callback! |
| 238 | 235 |
| 236 ## Implementation notes |
| 239 | 237 |
| 240 # Implementation notes | 238 ### Where Is This Design From: |
| 241 | 239 |
| 242 ## WHERE IS THIS DESIGN FROM: | 240 The design `Callback` and Bind is heavily influenced by C++'s `tr1::function` / |
| 241 `tr1::bind`, and by the "Google Callback" system used inside Google. |
| 243 | 242 |
| 244 The design `Callback` and Bind is heavily influenced by C++'s | 243 ### How The Implementation Works: |
| 245 `tr1::function`/`tr1::bind`, and by the "Google Callback" system used inside | |
| 246 Google. | |
| 247 | |
| 248 ## HOW THE IMPLEMENTATION WORKS: | |
| 249 | 244 |
| 250 There are three main components to the system: | 245 There are three main components to the system: |
| 251 1) The Callback classes. | 246 1) The Callback classes. |
| 252 2) The `Bind()` functions. | 247 2) The `Bind()` functions. |
| 253 3) The arguments wrappers (e.g., `Unretained()` and `ConstRef()`). | 248 3) The arguments wrappers (e.g., `Unretained()` and `ConstRef()`). |
| 254 | 249 |
| 255 The Callback classes represent a generic function pointer. Internally, | 250 The Callback classes represent a generic function pointer. Internally, it stores |
| 256 it stores a refcounted piece of state that represents the target function | 251 a refcounted piece of state that represents the target function and all its |
| 257 and all its bound parameters. Each `Callback` specialization has a templated | 252 bound parameters. Each `Callback` specialization has a templated constructor |
| 258 constructor that takes an `BindState<>*`. In the context of the constructor, | 253 that takes an `BindState<>*`. In the context of the constructor, the static |
| 259 the static type of this `BindState<>` pointer uniquely identifies the | 254 type of this `BindState<>` pointer uniquely identifies the function it is |
| 260 function it is representing, all its bound parameters, and a `Run()` method | 255 representing, all its bound parameters, and a `Run()` method that is capable of |
| 261 that is capable of invoking the target. | 256 invoking the target. |
| 262 | 257 |
| 263 `Callback`'s constructor takes the `BindState<>*` that has the full static type | 258 `Callback`'s constructor takes the `BindState<>*` that has the full static type |
| 264 and erases the target function type as well as the types of the bound | 259 and erases the target function type as well as the types of the bound |
| 265 parameters. It does this by storing a pointer to the specific `Run()` | 260 parameters. It does this by storing a pointer to the specific `Run()` function, |
| 266 function, and upcasting the state of `BindState<>*` to a | 261 and upcasting the state of `BindState<>*` to a `BindStateBase*`. This is safe as |
| 267 `BindStateBase*`. This is safe as long as this `BindStateBase` pointer | 262 long as this `BindStateBase` pointer is only used with the stored `Run()` |
| 268 is only used with the stored `Run()` pointer. | 263 pointer. |
| 269 | 264 |
| 270 To `BindState<>` objects are created inside the `Bind()` functions. | 265 To `BindState<>` objects are created inside the `Bind()` functions. |
| 271 These functions, along with a set of internal templates, are responsible for | 266 These functions, along with a set of internal templates, are responsible for |
| 272 | 267 |
| 273 - Unwrapping the function signature into return type, and parameters | 268 - Unwrapping the function signature into return type, and parameters |
| 274 - Determining the number of parameters that are bound | 269 - Determining the number of parameters that are bound |
| 275 - Creating the BindState storing the bound parameters | 270 - Creating the BindState storing the bound parameters |
| 276 - Performing compile-time asserts to avoid error-prone behavior | 271 - Performing compile-time asserts to avoid error-prone behavior |
| 277 - Returning an `Callback<>` with an arity matching the number of unbound | 272 - Returning an `Callback<>` with an arity matching the number of unbound |
| 278 parameters and that knows the correct refcounting semantics for the | 273 parameters and that knows the correct refcounting semantics for the |
| 279 target object if we are binding a method. | 274 target object if we are binding a method. |
| 280 | 275 |
| 281 The `Bind` functions do the above using type-inference, and template | 276 The `Bind` functions do the above using type-inference, and template |
| 282 specializations. | 277 specializations. |
| 283 | 278 |
| 284 By default `Bind()` will store copies of all bound parameters, and attempt | 279 By default `Bind()` will store copies of all bound parameters, and attempt to |
| 285 to refcount a target object if the function being bound is a class method. | 280 refcount a target object if the function being bound is a class method. These |
| 286 These copies are created even if the function takes parameters as const | 281 copies are created even if the function takes parameters as const |
| 287 references. (Binding to non-const references is forbidden, see bind.h.) | 282 references. (Binding to non-const references is forbidden, see bind.h.) |
| 288 | 283 |
| 289 To change this behavior, we introduce a set of argument wrappers | 284 To change this behavior, we introduce a set of argument wrappers (e.g., |
| 290 (e.g., `Unretained()`, and `ConstRef()`). These are simple container templates | 285 `Unretained()`, and `ConstRef()`). These are simple container templates that |
| 291 that are passed by value, and wrap a pointer to argument. See the | 286 are passed by value, and wrap a pointer to argument. See the file-level comment |
| 292 file-level comment in base/bind_helpers.h for more info. | 287 in base/bind_helpers.h for more info. |
| 293 | 288 |
| 294 These types are passed to the `Unwrap()` functions, and the `MaybeRefcount()` | 289 These types are passed to the `Unwrap()` functions, and the `MaybeRefcount()` |
| 295 functions respectively to modify the behavior of `Bind()`. The `Unwrap()` | 290 functions respectively to modify the behavior of `Bind()`. The `Unwrap()` and |
| 296 and `MaybeRefcount()` functions change behavior by doing partial | 291 `MaybeRefcount()` functions change behavior by doing partial specialization |
| 297 specialization based on whether or not a parameter is a wrapper type. | 292 based on whether or not a parameter is a wrapper type. |
| 298 | 293 |
| 299 `ConstRef()` is similar to `tr1::cref`. `Unretained()` is specific to Chromium. | 294 `ConstRef()` is similar to `tr1::cref`. `Unretained()` is specific to Chromium. |
| 300 | 295 |
| 301 | 296 ### Why Not Tr1 Function/Bind? |
| 302 ## WHY NOT TR1 FUNCTION/BIND? | |
| 303 | 297 |
| 304 Direct use of `tr1::function` and `tr1::bind` was considered, but ultimately | 298 Direct use of `tr1::function` and `tr1::bind` was considered, but ultimately |
| 305 rejected because of the number of copy constructors invocations involved | 299 rejected because of the number of copy constructors invocations involved in the |
| 306 in the binding of arguments during construction, and the forwarding of | 300 binding of arguments during construction, and the forwarding of arguments during |
| 307 arguments during invocation. These copies will no longer be an issue in | 301 invocation. These copies will no longer be an issue in C++0x because C++0x will |
| 308 C++0x because C++0x will support rvalue reference allowing for the compiler | 302 support rvalue reference allowing for the compiler to avoid these copies. |
| 309 to avoid these copies. However, waiting for C++0x is not an option. | 303 However, waiting for C++0x is not an option. |
| 310 | 304 |
| 311 Measured with valgrind on gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5), the | 305 Measured with valgrind on gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5), the |
| 312 `tr1::bind` call itself will invoke a non-trivial copy constructor three times | 306 `tr1::bind` call itself will invoke a non-trivial copy constructor three times |
| 313 for each bound parameter. Also, each when passing a `tr1::function`, each | 307 for each bound parameter. Also, each when passing a `tr1::function`, each bound |
| 314 bound argument will be copied again. | 308 argument will be copied again. |
| 315 | 309 |
| 316 In addition to the copies taken at binding and invocation, copying a | 310 In addition to the copies taken at binding and invocation, copying a |
| 317 `tr1::function` causes a copy to be made of all the bound parameters and | 311 `tr1::function` causes a copy to be made of all the bound parameters and state. |
| 318 state. | |
| 319 | 312 |
| 320 Furthermore, in Chromium, it is desirable for the `Callback` to take a | 313 Furthermore, in Chromium, it is desirable for the `Callback` to take a reference |
| 321 reference on a target object when representing a class method call. This | 314 on a target object when representing a class method call. This is not supported |
| 322 is not supported by tr1. | 315 by tr1. |
| 323 | 316 |
| 324 Lastly, `tr1::function` and `tr1::bind` has a more general and flexible API. | 317 Lastly, `tr1::function` and `tr1::bind` has a more general and flexible |
| 325 This includes things like argument reordering by use of | 318 API. This includes things like argument reordering by use of |
| 326 `tr1::bind::placeholder`, support for non-const reference parameters, and some | 319 `tr1::bind::placeholder`, support for non-const reference parameters, and some |
| 327 limited amount of subtyping of the `tr1::function` object (e.g., | 320 limited amount of subtyping of the `tr1::function` object (e.g., |
| 328 `tr1::function<int(int)>` is convertible to `tr1::function<void(int)>`). | 321 `tr1::function<int(int)>` is convertible to `tr1::function<void(int)>`). |
| 329 | 322 |
| 330 These are not features that are required in Chromium. Some of them, such as | 323 These are not features that are required in Chromium. Some of them, such as |
| 331 allowing for reference parameters, and subtyping of functions, may actually | 324 allowing for reference parameters, and subtyping of functions, may actually |
| 332 become a source of errors. Removing support for these features actually | 325 become a source of errors. Removing support for these features actually allows |
| 333 allows for a simpler implementation, and a terser Currying API. | 326 for a simpler implementation, and a terser Currying API. |
| 334 | 327 |
| 335 ## WHY NOT GOOGLE CALLBACKS? | 328 ### Why Not Google Callbacks? |
| 336 | 329 |
| 337 The Google callback system also does not support refcounting. Furthermore, | 330 The Google callback system also does not support refcounting. Furthermore, its |
| 338 its implementation has a number of strange edge cases with respect to type | 331 implementation has a number of strange edge cases with respect to type |
| 339 conversion of its arguments. In particular, the argument's constness must | 332 conversion of its arguments. In particular, the argument's constness must at |
| 340 at times match exactly the function signature, or the type-inference might | 333 times match exactly the function signature, or the type-inference might |
| 341 break. Given the above, writing a custom solution was easier. | 334 break. Given the above, writing a custom solution was easier. |
| 342 | 335 |
| 343 | 336 ### Missing Functionality |
| 344 ## MISSING FUNCTIONALITY | |
| 345 - Invoking the return of `Bind`. `Bind(&foo).Run()` does not work; | 337 - Invoking the return of `Bind`. `Bind(&foo).Run()` does not work; |
| 346 - Binding arrays to functions that take a non-const pointer. | 338 - Binding arrays to functions that take a non-const pointer. |
| 347 Example: | 339 Example: |
| 348 ```cpp | 340 ```cpp |
| 349 void Foo(const char* ptr); | 341 void Foo(const char* ptr); |
| 350 void Bar(char* ptr); | 342 void Bar(char* ptr); |
| 351 Bind(&Foo, "test"); | 343 Bind(&Foo, "test"); |
| 352 Bind(&Bar, "test"); // This fails because ptr is not const. | 344 Bind(&Bar, "test"); // This fails because ptr is not const. |
| 353 ``` | 345 ``` |
| 354 | 346 |
| 355 If you are thinking of forward declaring `Callback` in your own header file, | 347 If you are thinking of forward declaring `Callback` in your own header file, |
| 356 please include "base/callback_forward.h" instead. | 348 please include "base/callback_forward.h" instead. |
| OLD | NEW |