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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 g_cb = std::move(cb); | 60 g_cb = std::move(cb); |
| 61 } | 61 } |
| 62 | 62 |
| 63 // |Baz| takes the ownership of |cb| and consumes |cb| by Run(). | 63 // |Baz| takes the ownership of |cb| and consumes |cb| by Run(). |
| 64 void Baz(OnceCallback<void(int)> cb) { | 64 void Baz(OnceCallback<void(int)> cb) { |
| 65 std::move(cb).Run(42); | 65 std::move(cb).Run(42); |
| 66 } | 66 } |
| 67 | 67 |
| 68 // |Qux| takes the ownership of |cb| and transfers ownership to PostTask(), | 68 // |Qux| takes the ownership of |cb| and transfers ownership to PostTask(), |
| 69 // which also takes the ownership of |cb|. | 69 // which also takes the ownership of |cb|. |
| 70 // NOTE: TaskRunner is not actually migrated to OnceClosure yet. Once TaskRunner | |
| 71 // supports OnceClosure, a OnceCallback can be posted as follows: | |
| 70 void Qux(OnceCallback<void(int)> cb) { | 72 void Qux(OnceCallback<void(int)> cb) { |
| 71 PostTask(FROM_HERE, std::move(cb)); | 73 PostTask(FROM_HERE, base::Bind(std::move(cb), 42)); // not yet implemented! |
|
tzik
2017/03/14 03:25:06
Could you use BindOnce instead of Bind?
michaelpg
2017/03/14 05:02:09
yeah, that would make sense, wouldn't it... done
| |
| 72 } | 74 } |
| 73 ``` | 75 ``` |
| 74 | 76 |
| 75 When you pass a `Callback` object to a function parameter, use `std::move()` if | 77 When you pass a `Callback` object to a function parameter, use `std::move()` if |
| 76 you don't need to keep a reference to it, otherwise, pass the object directly. | 78 you don't need to keep a reference to it, otherwise, pass the object directly. |
| 77 You may see a compile error when the function requires the exclusive ownership, | 79 You may see a compile error when the function requires the exclusive ownership, |
| 78 and you didn't pass the callback by move. | 80 and you didn't pass the callback by move. |
| 79 | 81 |
| 80 ## Quick reference for basic stuff | 82 ## Quick reference for basic stuff |
| 81 | 83 |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 444 Example: | 446 Example: |
| 445 ```cpp | 447 ```cpp |
| 446 void Foo(const char* ptr); | 448 void Foo(const char* ptr); |
| 447 void Bar(char* ptr); | 449 void Bar(char* ptr); |
| 448 Bind(&Foo, "test"); | 450 Bind(&Foo, "test"); |
| 449 Bind(&Bar, "test"); // This fails because ptr is not const. | 451 Bind(&Bar, "test"); // This fails because ptr is not const. |
| 450 ``` | 452 ``` |
| 451 | 453 |
| 452 If you are thinking of forward declaring `Callback` in your own header file, | 454 If you are thinking of forward declaring `Callback` in your own header file, |
| 453 please include "base/callback_forward.h" instead. | 455 please include "base/callback_forward.h" instead. |
| OLD | NEW |