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: | |
72 void Qux(OnceCallback<void(int)> cb) { | 70 void Qux(OnceCallback<void(int)> cb) { |
73 PostTask(FROM_HERE, | 71 PostTask(FROM_HERE, |
74 base::BindOnce(std::move(cb), 42)); // not yet implemented! | 72 base::BindOnce(std::move(cb), 42)); |
75 } | 73 } |
76 ``` | 74 ``` |
77 | 75 |
78 When you pass a `Callback` object to a function parameter, use `std::move()` if | 76 When you pass a `Callback` object to a function parameter, use `std::move()` if |
79 you don't need to keep a reference to it, otherwise, pass the object directly. | 77 you don't need to keep a reference to it, otherwise, pass the object directly. |
80 You may see a compile error when the function requires the exclusive ownership, | 78 You may see a compile error when the function requires the exclusive ownership, |
81 and you didn't pass the callback by move. | 79 and you didn't pass the callback by move. |
82 | 80 |
83 ## Quick reference for basic stuff | 81 ## Quick reference for basic stuff |
84 | 82 |
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 Example: | 456 Example: |
459 ```cpp | 457 ```cpp |
460 void Foo(const char* ptr); | 458 void Foo(const char* ptr); |
461 void Bar(char* ptr); | 459 void Bar(char* ptr); |
462 Bind(&Foo, "test"); | 460 Bind(&Foo, "test"); |
463 Bind(&Bar, "test"); // This fails because ptr is not const. | 461 Bind(&Bar, "test"); // This fails because ptr is not const. |
464 ``` | 462 ``` |
465 | 463 |
466 If you are thinking of forward declaring `Callback` in your own header file, | 464 If you are thinking of forward declaring `Callback` in your own header file, |
467 please include "base/callback_forward.h" instead. | 465 please include "base/callback_forward.h" instead. |
OLD | NEW |