Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Callback<> and Bind() | 1 # Callback<> and Bind() |
| 2 | 2 |
| 3 ## Introduction | 3 ## Introduction |
| 4 | 4 |
| 5 The templated `Callback<>` class is a generalized function object. Together with | 5 The templated `Callback<>` class is a generalized function object. Together with |
| 6 the `Bind()` function in base/bind.h, they provide a type-safe method for | 6 the `Bind()` function in base/bind.h, they provide a type-safe method for |
| 7 performing partial application of functions. | 7 performing partial application of functions. |
| 8 | 8 |
| 9 Partial application (or "currying") is the process of binding a subset of a | 9 Partial application (or "currying") is the process of binding a subset of a |
| 10 function's arguments to produce another function that takes fewer arguments. | 10 function's arguments to produce another function that takes fewer arguments. |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 RepeatingCallbacks can be run more than once (they don't get deleted or marked | 145 RepeatingCallbacks can be run more than once (they don't get deleted or marked |
| 146 when run). However, this precludes using Passed (see below). | 146 when run). However, this precludes using Passed (see below). |
| 147 | 147 |
| 148 ```cpp | 148 ```cpp |
| 149 void DoSomething(const RepeatingCallback<double(double)>& callback) { | 149 void DoSomething(const RepeatingCallback<double(double)>& callback) { |
| 150 double myresult = callback.Run(3.14159); | 150 double myresult = callback.Run(3.14159); |
| 151 myresult += callback.Run(2.71828); | 151 myresult += callback.Run(2.71828); |
| 152 } | 152 } |
| 153 ``` | 153 ``` |
| 154 | 154 |
| 155 If running a callback could result in its own destruction (e.g., if the callback | |
| 156 recipient deletes the object the callback is a member of), the callback should | |
| 157 be moved before it can be safely invoked. The `base::ResetAndReturn` method | |
| 158 provides this functionality. | |
| 159 | |
| 160 ```cpp | |
| 161 Foo::RunCallback() { | |
|
tzik
2017/03/17 14:01:06
nit: void Foo::RunCallback() {
michaelpg
2017/03/17 22:58:20
whoops, done
| |
| 162 base::ResetAndReturn(&foo_deleter_callback_).Run(); | |
| 163 } | |
| 164 ``` | |
| 165 | |
| 155 ### Passing Unbound Input Parameters | 166 ### Passing Unbound Input Parameters |
| 156 | 167 |
| 157 Unbound parameters are specified at the time a callback is `Run()`. They are | 168 Unbound parameters are specified at the time a callback is `Run()`. They are |
| 158 specified in the `Callback` template type: | 169 specified in the `Callback` template type: |
| 159 | 170 |
| 160 ```cpp | 171 ```cpp |
| 161 void MyFunc(int i, const std::string& str) {} | 172 void MyFunc(int i, const std::string& str) {} |
| 162 Callback<void(int, const std::string&)> cb = Bind(&MyFunc); | 173 Callback<void(int, const std::string&)> cb = Bind(&MyFunc); |
| 163 cb.Run(23, "hello, world"); | 174 cb.Run(23, "hello, world"); |
| 164 ``` | 175 ``` |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 447 Example: | 458 Example: |
| 448 ```cpp | 459 ```cpp |
| 449 void Foo(const char* ptr); | 460 void Foo(const char* ptr); |
| 450 void Bar(char* ptr); | 461 void Bar(char* ptr); |
| 451 Bind(&Foo, "test"); | 462 Bind(&Foo, "test"); |
| 452 Bind(&Bar, "test"); // This fails because ptr is not const. | 463 Bind(&Bar, "test"); // This fails because ptr is not const. |
| 453 ``` | 464 ``` |
| 454 | 465 |
| 455 If you are thinking of forward declaring `Callback` in your own header file, | 466 If you are thinking of forward declaring `Callback` in your own header file, |
| 456 please include "base/callback_forward.h" instead. | 467 please include "base/callback_forward.h" instead. |
| OLD | NEW |