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, |
| 74 base::BindOnce(std::move(cb), 42)); // not yet implemented! |
72 } | 75 } |
73 ``` | 76 ``` |
74 | 77 |
75 When you pass a `Callback` object to a function parameter, use `std::move()` if | 78 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. | 79 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, | 80 You may see a compile error when the function requires the exclusive ownership, |
78 and you didn't pass the callback by move. | 81 and you didn't pass the callback by move. |
79 | 82 |
80 ## Quick reference for basic stuff | 83 ## Quick reference for basic stuff |
81 | 84 |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 Example: | 447 Example: |
445 ```cpp | 448 ```cpp |
446 void Foo(const char* ptr); | 449 void Foo(const char* ptr); |
447 void Bar(char* ptr); | 450 void Bar(char* ptr); |
448 Bind(&Foo, "test"); | 451 Bind(&Foo, "test"); |
449 Bind(&Bar, "test"); // This fails because ptr is not const. | 452 Bind(&Bar, "test"); // This fails because ptr is not const. |
450 ``` | 453 ``` |
451 | 454 |
452 If you are thinking of forward declaring `Callback` in your own header file, | 455 If you are thinking of forward declaring `Callback` in your own header file, |
453 please include "base/callback_forward.h" instead. | 456 please include "base/callback_forward.h" instead. |
OLD | NEW |