Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 1 | # OnceCallback<> and BindOnce(), RepeatingCallback<> and BindRepeating() |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 2 | |
Raphael Kubo da Costa | 17c1618c | 2019-03-28 19:30:44 | [diff] [blame] | 3 | [TOC] |
| 4 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 5 | ## Introduction |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 6 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 7 | The templated `base::{Once, Repeating}Callback<>` classes are generalized |
| 8 | function objects. Together with the `base::Bind{Once, Repeating}()` functions in |
Avi Drissman | d4459db | 2023-01-18 02:45:14 | [diff] [blame] | 9 | base/functional/bind.h, they provide a type-safe method for performing partial |
| 10 | application of functions. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 11 | |
Matt Giuca | 7e81b22e | 2019-12-12 02:41:21 | [diff] [blame] | 12 | Partial application is the process of binding a subset of a function's arguments |
| 13 | to produce another function that takes fewer arguments. This can be used to pass |
| 14 | around a unit of delayed execution, much like lexical closures are used in other |
| 15 | languages. For example, it is used in Chromium code to schedule tasks on |
| 16 | different MessageLoops. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 17 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 18 | A callback with no unbound input parameters (`base::OnceCallback<void()>`) is |
| 19 | called a `base::OnceClosure`. The same pattern exists for |
Arthur Milchior | cc277f0 | 2023-07-06 07:59:03 | [diff] [blame] | 20 | base::RepeatingCallback, as `base::RepeatingClosure`. Note that this is NOT the |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 21 | same as what other languages refer to as a closure -- it does not retain a |
| 22 | reference to its enclosing environment. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 23 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 24 | ### OnceCallback<> And RepeatingCallback<> |
| 25 | |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 26 | `base::OnceCallback<>` is created by `base::BindOnce()`. This is a callback |
| 27 | variant that is a move-only type and can be run only once. This moves out bound |
| 28 | parameters from its internal storage to the bound function by default, so it's |
| 29 | easier to use with movable types. This should be the preferred callback type: |
| 30 | since the lifetime of the callback is clear, it's simpler to reason about when |
| 31 | a callback that is passed between threads is destroyed. |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 32 | |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 33 | `base::RepeatingCallback<>` is created by `base::BindRepeating()`. This is a |
| 34 | callback variant that is copyable that can be run multiple times. It uses |
| 35 | internal ref-counting to make copies cheap. However, since ownership is shared, |
| 36 | it is harder to reason about when the callback and the bound state are |
| 37 | destroyed, especially when the callback is passed between threads. |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 38 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 39 | Prefer `base::OnceCallback<>` where possible, and use `base::RepeatingCallback<>` |
| 40 | otherwise. |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 41 | |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 42 | `base::RepeatingCallback<>` is convertible to `base::OnceCallback<>` by the |
| 43 | implicit conversion. |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 44 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 45 | ### Memory Management And Passing |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 46 | |
danakj | e26d7cf | 2019-05-29 20:04:14 | [diff] [blame] | 47 | Pass `base::{Once,Repeating}Callback` objects by value if ownership is |
| 48 | transferred; otherwise, pass it by const-reference. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 49 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 50 | ```cpp |
| 51 | // |Foo| just refers to |cb| but doesn't store it nor consume it. |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 52 | bool Foo(const base::OnceCallback<void(int)>& cb) { |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 53 | return cb.is_null(); |
| 54 | } |
| 55 | |
| 56 | // |Bar| takes the ownership of |cb| and stores |cb| into |g_cb|. |
danakj | e26d7cf | 2019-05-29 20:04:14 | [diff] [blame] | 57 | base::RepeatingCallback<void(int)> g_cb; |
| 58 | void Bar(base::RepeatingCallback<void(int)> cb) { |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 59 | g_cb = std::move(cb); |
| 60 | } |
| 61 | |
| 62 | // |Baz| takes the ownership of |cb| and consumes |cb| by Run(). |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 63 | void Baz(base::OnceCallback<void(int)> cb) { |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 64 | std::move(cb).Run(42); |
| 65 | } |
| 66 | |
| 67 | // |Qux| takes the ownership of |cb| and transfers ownership to PostTask(), |
| 68 | // which also takes the ownership of |cb|. |
danakj | e26d7cf | 2019-05-29 20:04:14 | [diff] [blame] | 69 | void Qux(base::RepeatingCallback<void(int)> cb) { |
| 70 | PostTask(FROM_HERE, base::BindOnce(cb, 42)); |
| 71 | PostTask(FROM_HERE, base::BindOnce(std::move(cb), 43)); |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 72 | } |
| 73 | ``` |
| 74 | |
danakj | e26d7cf | 2019-05-29 20:04:14 | [diff] [blame] | 75 | When you pass a `base::{Once,Repeating}Callback` object to a function parameter, |
| 76 | use `std::move()` if you don't need to keep a reference to it, otherwise, pass the |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 77 | object directly. You may see a compile error when the function requires the |
| 78 | exclusive ownership, and you didn't pass the callback by move. Note that the |
danakj | e26d7cf | 2019-05-29 20:04:14 | [diff] [blame] | 79 | moved-from `base::{Once,Repeating}Callback` becomes null, as if its `Reset()` |
| 80 | method had been called. Afterward, its `is_null()` method will return true and |
| 81 | its `operator bool()` will return false. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 82 | |
danakj | fcc5e7c | 2020-10-23 17:43:27 | [diff] [blame] | 83 | ### Chaining callbacks |
| 84 | |
| 85 | When you have 2 callbacks that you wish to run in sequence, they can be joined |
| 86 | together into a single callback through the use of `Then()`. |
| 87 | |
| 88 | Calling `Then()` on a `base::OnceCallback` joins a second callback that will be |
| 89 | run together with, but after, the first callback. The return value from the |
| 90 | first callback is passed along to the second, and the return value from the |
| 91 | second callback is returned at the end. More concretely, calling `a.Then(b)` |
| 92 | produces a new `base::OnceCallback` that will run `b(a());`, returning the |
| 93 | result from `b`. |
| 94 | |
| 95 | This example uses `Then()` to join 2 `base::OnceCallback`s together: |
| 96 | ```cpp |
| 97 | int Floor(float f) { return std::floor(f); } |
| 98 | std::string IntToString(int i) { return base::NumberToString(i); } |
| 99 | |
| 100 | base::OnceCallback<int(float)> first = base::BindOnce(&Floor); |
| 101 | base::OnceCallback<std::string(int)> second = base::BindOnce(&IntToString); |
| 102 | |
| 103 | // This will run |first|, run and pass the result to |second|, then return |
| 104 | // the result from |second|. |
| 105 | std::string r = std::move(first).Then(std::move(second)).Run(3.5f); |
| 106 | // |r| will be "3". |first| and |second| are now both null, as they were |
| 107 | // consumed to perform the join operation. |
| 108 | ``` |
| 109 | |
| 110 | Similarly, `Then()` also works with `base::RepeatingCallback`; however, the |
| 111 | joined callback must also be a `base::RepeatingCallback` to ensure the resulting |
| 112 | callback can be invoked multiple times. |
| 113 | |
| 114 | This example uses `Then()` to join 2 `base::RepeatingCallback`s together: |
| 115 | ```cpp |
| 116 | int Floor(float f) { return std::floor(f); } |
| 117 | std::string IntToString(int i) { return base::NumberToString(i); } |
| 118 | |
| 119 | base::RepeatingCallback<int(float)> first = base::BindRepeating(&Floor); |
| 120 | base::RepeatingCallback<std::string(int)> second = base::BindRepeating(&IntToString); |
| 121 | |
| 122 | // This creates a RepeatingCallback that will run |first|, run and pass the |
| 123 | // result to |second|, then return the result from |second|. |
| 124 | base::RepeatingCallback<std::string(float)> joined = |
| 125 | std::move(first).Then(std::move(second)); |
| 126 | // |first| and |second| are now both null, as they were consumed to perform |
| 127 | // the join operation. |
| 128 | |
| 129 | // This runs the functor that was originally bound to |first|, then |second|. |
| 130 | std::string r = joined.Run(3.5); |
| 131 | // |r| will be "3". |
| 132 | |
| 133 | // It's valid to call it multiple times since all callbacks involved are |
| 134 | // base::RepeatingCallbacks. |
| 135 | r = joined.Run(2.5); |
| 136 | // |r| is set to "2". |
| 137 | ``` |
| 138 | |
| 139 | In the above example, casting the `base::RepeatingCallback` to an r-value with |
| 140 | `std::move()` causes `Then()` to destroy the original callback, in the same way |
| 141 | that occurs for joining `base::OnceCallback`s. However since a |
| 142 | `base::RepeatingCallback` can be run multiple times, it can be joined |
| 143 | non-destructively as well. |
| 144 | ```cpp |
| 145 | int Floor(float f) { return std::floor(f); } |
| 146 | std::string IntToString(int i) { return base::NumberToString(i); } |
| 147 | |
| 148 | base::RepeatingCallback<int(float)> first = base::BindRepeating(&Floor); |
| 149 | base::RepeatingCallback<std::string(int)> second = base::BindRepeating(&IntToString); |
| 150 | |
| 151 | // This creates a RepeatingCallback that will run |first|, run and pass the |
| 152 | // result to |second|, then return the result from |second|. |
| 153 | std::string r = first.Then(second).Run(3.5f); |
| 154 | // |r| will be 3, and |first| and |second| are still valid to use. |
| 155 | |
| 156 | // Runs Floor(). |
| 157 | int i = first.Run(5.5); |
| 158 | // Runs IntToString(). |
| 159 | std::string s = second.Run(9); |
| 160 | ``` |
| 161 | |
danakj | 9335cb1c | 2020-10-28 20:21:21 | [diff] [blame] | 162 | If the second callback does not want to receive a value from the first callback, |
| 163 | you may use `base::IgnoreResult` to drop the return value in between running the |
| 164 | two. |
| 165 | |
| 166 | ```cpp |
| 167 | // Returns an integer. |
| 168 | base::RepeatingCallback<int()> first = base::BindRepeating([](){ return 5; }); |
| 169 | // Does not want to receive an integer. |
| 170 | base::RepeatingClosure second = base::BindRepeating([](){}); |
| 171 | |
| 172 | // This will not compile, because |second| can not receive the return value from |
| 173 | // |first|. |
| 174 | // first.Then(second).Run(); |
| 175 | |
| 176 | // We can drop the result from |first| before running second. |
| 177 | base::BindRepeating(base::IgnoreResult(first)).Then(second).Run(); |
| 178 | // This will effectively create a callback that when Run() will call |
| 179 | // `first(); second();` instead of `second(first());`. |
| 180 | ``` |
| 181 | |
| 182 | Note that the return value from |first| will be lost in the above example, and |
| 183 | would be destroyed before |second| is run. If you want the return value from |
| 184 | |first| to be preserved and ultimately returned after running both |first| and |
| 185 | |second|, then you would need a primitive such as the `base::PassThrough<T>()` |
| 186 | helper in the [base::PassThrough CL](https://chromium-review.googlesource.com/c/chromium/src/+/2493243). |
| 187 | If this would be helpful for you, please let [email protected] know or ping |
| 188 | the CL. |
| 189 | |
kylechar | dde7d23 | 2020-11-16 17:35:09 | [diff] [blame] | 190 | ### Chaining callbacks across different task runners |
| 191 | |
| 192 | ```cpp |
| 193 | // The task runner for a different thread. |
| 194 | scoped_refptr<base::SequencedTaskRunner> other_task_runner = ...; |
| 195 | |
| 196 | // A function to compute some interesting result, except it can only be run |
| 197 | // safely from `other_task_runner` and not the current thread. |
| 198 | int ComputeResult(); |
| 199 | |
| 200 | base::OnceCallback<int()> compute_result_cb = base::BindOnce(&ComputeResult); |
| 201 | |
| 202 | // Task runner for the current thread. |
| 203 | scoped_refptr<base::SequencedTaskRunner> current_task_runner = |
Sean Maher | 70f294293 | 2023-01-04 22:15:06 | [diff] [blame] | 204 | base::SequencedTaskRunner::GetCurrentDefault(); |
kylechar | dde7d23 | 2020-11-16 17:35:09 | [diff] [blame] | 205 | |
| 206 | // A function to accept the result, except it can only be run safely from the |
| 207 | // current thread. |
| 208 | void ProvideResult(int result); |
| 209 | |
| 210 | base::OnceCallback<void(int)> provide_result_cb = |
| 211 | base::BindOnce(&ProvideResult); |
| 212 | ``` |
| 213 | |
| 214 | Using `Then()` to join `compute_result_cb` and `provide_result_cb` directly |
| 215 | would be inappropriate. `ComputeResult()` and `ProvideResult()` would run on the |
| 216 | same thread which isn't safe. However, `base::BindPostTask()` can be used to |
| 217 | ensure `provide_result_cb` will run on `current_task_runner`. |
| 218 | |
| 219 | ```cpp |
| 220 | // The following two statements post a task to `other_task_runner` to run |
| 221 | // `task`. This will invoke ComputeResult() on a different thread to get the |
| 222 | // result value then post a task back to `current_task_runner` to invoke |
| 223 | // ProvideResult() with the result. |
| 224 | OnceClosure task = |
| 225 | std::move(compute_result_cb) |
| 226 | .Then(base::BindPostTask(current_task_runner, |
| 227 | std::move(provide_result_cb))); |
| 228 | other_task_runner->PostTask(FROM_HERE, std::move(task)); |
| 229 | ``` |
| 230 | |
Thomas Guilbert | 5db5238 | 2020-12-17 22:33:14 | [diff] [blame] | 231 | ### Splitting a OnceCallback in two |
| 232 | |
| 233 | If a callback is only run once, but two references need to be held to the |
| 234 | callback, using a `base::OnceCallback` can be clearer than a |
| 235 | `base::RepeatingCallback`, from an intent and semantics point of view. |
| 236 | `base::SplitOnceCallback()` takes a `base::OnceCallback` and returns a pair of |
| 237 | callbacks with the same signature. When either of the returned callback is run, |
| 238 | the original callback is invoked. Running the leftover callback will result in a |
| 239 | crash. |
| 240 | This can be useful when passing a `base::OnceCallback` to a function that may or |
| 241 | may not take ownership of the callback. E.g, when an object creation could fail: |
| 242 | |
| 243 | ```cpp |
| 244 | std::unique_ptr<FooTask> CreateFooTask(base::OnceClosure task) { |
| 245 | std::pair<base::OnceClosure,base::OnceClosure> split |
| 246 | = base::SplitOnceCallback(std::move(task)); |
| 247 | |
| 248 | std::unique_ptr<FooTask> foo = TryCreateFooTask(std::move(split.first)); |
| 249 | if (foo) |
| 250 | return foo; |
| 251 | |
| 252 | return CreateFallbackFooTask(std::move(split.second)); |
| 253 | } |
| 254 | ``` |
| 255 | |
| 256 | While it is best to use a single callback to report success/failure, some APIs |
| 257 | already take multiple callbacks. `base::SplitOnceCallback()` can be used to |
| 258 | split a completion callback and help in such a case: |
| 259 | |
| 260 | ```cpp |
| 261 | using StatusCallback = base::OnceCallback<void(FooStatus)>; |
| 262 | void DoOperation(StatusCallback done_cb) { |
| 263 | std::pair<StatusCallback, StatusCallback> split |
| 264 | = base::SplitOnceCallback(std::move(done_cb)); |
| 265 | |
| 266 | InnerWork(BindOnce(std::move(split.first), STATUS_OK), |
| 267 | BindOnce(std::move(split.second), STATUS_ABORTED)); |
| 268 | } |
| 269 | |
| 270 | void InnerWork(base::OnceClosure work_done_cb, |
| 271 | base::OnceClosure work_aborted_cb); |
| 272 | ``` |
| 273 | |
Roland Bock | 6269edb | 2022-01-04 19:34:15 | [diff] [blame] | 274 | ### BarrierCallback<T> |
| 275 | |
| 276 | Sometimes you might need to request data from several sources, then do something |
| 277 | with the collective results once all data is available. You can do this with a |
| 278 | `BarrierCallback<T>`. The `BarrierCallback<T>` is created with two parameters: |
| 279 | |
| 280 | - `num_callbacks`: The number of times the `BarrierCallback` can be run, each |
| 281 | time being passed an object of type T. |
| 282 | - `done_callback`: This will be run once the `BarrierCallback` has been run |
| 283 | `num_callbacks` times. |
| 284 | |
| 285 | The `done_callback` will receive a `std::vector<T>` containing the |
| 286 | `num_callbacks` parameters passed in the respective `Run` calls. The order of |
| 287 | `Ts` in the `vector` is unspecified. |
| 288 | |
| 289 | Note that |
| 290 | |
| 291 | - barrier callback must not be run more than `num_callback` times, |
| 292 | - `done_callback` will be called on the same thread as the final call to the |
| 293 | barrier callback. `done_callback` will also be cleared on the same thread. |
| 294 | |
| 295 | Example: |
| 296 | |
| 297 | ```cpp |
| 298 | void Merge(const std::vector<Data>& data); |
| 299 | |
| 300 | void Collect(base::OnceCallback<void(Data)> collect_and_merge) { |
| 301 | // Do something, probably asynchronously, and at some point: |
| 302 | std::move(collect_and_merge).Run(data); |
| 303 | } |
| 304 | |
| 305 | CollectAndMerge() { |
| 306 | const auto collect_and_merge = |
Byron Lee | 71cf8b5 | 2023-08-16 09:10:33 | [diff] [blame] | 307 | base::BarrierCallback<Data>(sources_.size(), base::BindOnce(&Merge)); |
Roland Bock | 6269edb | 2022-01-04 19:34:15 | [diff] [blame] | 308 | for (const auto& source : sources_) { |
| 309 | // Copy the barrier callback for asynchronous data collection. |
| 310 | // Once all sources have called `collect_and_merge` with their respective |
| 311 | // data, |Merge| will be called with a vector of the collected data. |
| 312 | source.Collect(collect_and_merge); |
| 313 | } |
| 314 | } |
| 315 | ``` |
| 316 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 317 | ## Quick reference for basic stuff |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 318 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 319 | ### Binding A Bare Function |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 320 | |
| 321 | ```cpp |
| 322 | int Return5() { return 5; } |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 323 | base::OnceCallback<int()> func_cb = base::BindOnce(&Return5); |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 324 | LOG(INFO) << std::move(func_cb).Run(); // Prints 5. |
| 325 | ``` |
| 326 | |
| 327 | ```cpp |
| 328 | int Return5() { return 5; } |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 329 | base::RepeatingCallback<int()> func_cb = base::BindRepeating(&Return5); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 330 | LOG(INFO) << func_cb.Run(); // Prints 5. |
| 331 | ``` |
| 332 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 333 | ### Binding A Captureless Lambda |
| 334 | |
| 335 | ```cpp |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 336 | base::RepeatingCallback<int()> lambda_cb = base::BindRepeating([] { return 4; }); |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 337 | LOG(INFO) << lambda_cb.Run(); // Print 4. |
| 338 | |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 339 | base::OnceCallback<int()> lambda_cb2 = base::BindOnce([] { return 3; }); |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 340 | LOG(INFO) << std::move(lambda_cb2).Run(); // Print 3. |
Erik Chen | 9425c0f | 2020-09-11 21:41:09 | [diff] [blame] | 341 | |
| 342 | base::OnceCallback<int()> lambda_cb3 = base::BindOnce([] { return 2; }); |
| 343 | base::OnceCallback<int(base::OnceCallback<int()>)> lambda_cb4 = |
| 344 | base::BindOnce( |
| 345 | [](base::OnceCallback<int()> callback) { |
| 346 | return std::move(callback).Run(); }, |
| 347 | std::move(lambda_cb3)); |
| 348 | LOG(INFO) << std::move(lambda_cb4).Run(); // Print 2. |
| 349 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 350 | ``` |
| 351 | |
Raphael Kubo da Costa | 17c1618c | 2019-03-28 19:30:44 | [diff] [blame] | 352 | ### Binding A Capturing Lambda (In Tests) |
| 353 | |
| 354 | When writing tests, it is often useful to capture arguments that need to be |
| 355 | modified in a callback. |
| 356 | |
| 357 | ``` cpp |
Guido Urdaneta | ef4e9194 | 2020-11-09 15:06:24 | [diff] [blame] | 358 | #include "base/test/bind.h" |
Raphael Kubo da Costa | 17c1618c | 2019-03-28 19:30:44 | [diff] [blame] | 359 | |
| 360 | int i = 2; |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 361 | base::RepeatingCallback<void()> lambda_cb = base::BindLambdaForTesting([&]() { i++; }); |
Raphael Kubo da Costa | 17c1618c | 2019-03-28 19:30:44 | [diff] [blame] | 362 | lambda_cb.Run(); |
| 363 | LOG(INFO) << i; // Print 3; |
| 364 | ``` |
| 365 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 366 | ### Binding A Class Method |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 367 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 368 | The first argument to bind is the member function to call, the second is the |
| 369 | object on which to call it. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 370 | |
| 371 | ```cpp |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 372 | class Ref : public base::RefCountedThreadSafe<Ref> { |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 373 | public: |
| 374 | int Foo() { return 3; } |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 375 | }; |
| 376 | scoped_refptr<Ref> ref = new Ref(); |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 377 | base::RepeatingCallback<void()> ref_cb = base::BindRepeating(&Ref::Foo, ref); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 378 | LOG(INFO) << ref_cb.Run(); // Prints out 3. |
| 379 | ``` |
| 380 | |
| 381 | By default the object must support RefCounted or you will get a compiler |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 382 | error. If you're passing between threads, be sure it's RefCountedThreadSafe! See |
| 383 | "Advanced binding of member functions" below if you don't want to use reference |
| 384 | counting. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 385 | |
Slobodan Pejic | 12e1032 | 2024-01-29 19:00:48 | [diff] [blame] | 386 | Binding a non-const method with a const object is not allowed, for example: |
| 387 | |
| 388 | ```cpp |
| 389 | class MyClass { |
| 390 | public: |
| 391 | base::OnceClosure GetCallback() const { |
| 392 | base::BindOnce( |
| 393 | // A template error will prevent the non-const method from being bound |
| 394 | // to the the WeakPtr<const MyClass>. |
| 395 | &MyClass::OnCallback, |
| 396 | weak_factory_.GetWeakPtr()); |
| 397 | } |
| 398 | |
| 399 | private: |
| 400 | void OnCallback(); // non-const |
| 401 | base::WeakPtrFactory<MyClass> weak_factory_{this}; |
| 402 | } |
| 403 | ``` |
| 404 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 405 | ### Running A Callback |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 406 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 407 | Callbacks can be run with their `Run` method, which has the same signature as |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 408 | the template argument to the callback. Note that `base::OnceCallback::Run` |
| 409 | consumes the callback object and can only be invoked on a callback rvalue. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 410 | |
| 411 | ```cpp |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 412 | void DoSomething(const base::RepeatingCallback<void(int, std::string)>& callback) { |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 413 | callback.Run(5, "hello"); |
| 414 | } |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 415 | |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 416 | void DoSomethingOther(base::OnceCallback<void(int, std::string)> callback) { |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 417 | std::move(callback).Run(5, "hello"); |
| 418 | } |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 419 | ``` |
| 420 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 421 | RepeatingCallbacks can be run more than once (they don't get deleted or marked |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 422 | when run). However, this precludes using `base::Passed` (see below). |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 423 | |
| 424 | ```cpp |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 425 | void DoSomething(const base::RepeatingCallback<double(double)>& callback) { |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 426 | double myresult = callback.Run(3.14159); |
| 427 | myresult += callback.Run(2.71828); |
| 428 | } |
| 429 | ``` |
| 430 | |
michaelpg | 0f156e1 | 2017-03-18 02:49:09 | [diff] [blame] | 431 | If running a callback could result in its own destruction (e.g., if the callback |
| 432 | recipient deletes the object the callback is a member of), the callback should |
Greg Thompson | ddc84d4 | 2021-01-04 10:10:02 | [diff] [blame] | 433 | be moved or copied onto the stack before it can be safely invoked. (Note that |
| 434 | this is only an issue for RepeatingCallbacks, because a OnceCallback always has |
| 435 | to be moved for execution.) |
michaelpg | 0f156e1 | 2017-03-18 02:49:09 | [diff] [blame] | 436 | |
| 437 | ```cpp |
| 438 | void Foo::RunCallback() { |
Bence Béky | 1532745 | 2018-05-10 20:59:07 | [diff] [blame] | 439 | std::move(&foo_deleter_callback_).Run(); |
michaelpg | 0f156e1 | 2017-03-18 02:49:09 | [diff] [blame] | 440 | } |
| 441 | ``` |
| 442 | |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 443 | ### Creating a Callback That Does Nothing |
| 444 | |
| 445 | Sometimes you need a callback that does nothing when run (e.g. test code that |
| 446 | doesn't care to be notified about certain types of events). It may be tempting |
| 447 | to pass a default-constructed callback of the right type: |
| 448 | |
| 449 | ```cpp |
| 450 | using MyCallback = base::OnceCallback<void(bool arg)>; |
| 451 | void MyFunction(MyCallback callback) { |
| 452 | std::move(callback).Run(true); // Uh oh... |
| 453 | } |
| 454 | ... |
| 455 | MyFunction(MyCallback()); // ...this will crash when Run()! |
| 456 | ``` |
| 457 | |
| 458 | Default-constructed callbacks are null, and thus cannot be Run(). Instead, use |
| 459 | `base::DoNothing()`: |
| 460 | |
| 461 | ```cpp |
| 462 | ... |
| 463 | MyFunction(base::DoNothing()); // Can be Run(), will no-op |
| 464 | ``` |
| 465 | |
| 466 | `base::DoNothing()` can be passed for any OnceCallback or RepeatingCallback that |
| 467 | returns void. |
| 468 | |
| 469 | Implementation-wise, `base::DoNothing()` is actually a functor which produces a |
| 470 | callback from `operator()`. This makes it unusable when trying to bind other |
| 471 | arguments to it. Normally, the only reason to bind arguments to DoNothing() is |
| 472 | to manage object lifetimes, and in these cases, you should strive to use idioms |
| 473 | like DeleteSoon(), ReleaseSoon(), or RefCountedDeleteOnSequence instead. If you |
| 474 | truly need to bind an argument to DoNothing(), or if you need to explicitly |
| 475 | create a callback object (because implicit conversion through operator()() won't |
| 476 | compile), you can instantiate directly: |
| 477 | |
| 478 | ```cpp |
| 479 | // Binds |foo_ptr| to a no-op OnceCallback takes a scoped_refptr<Foo>. |
| 480 | // ANTIPATTERN WARNING: This should likely be changed to ReleaseSoon()! |
Dmitrii Kuragin | 2e7da865 | 2022-06-14 20:17:21 | [diff] [blame] | 481 | base::BindOnce(base::DoNothingAs<void(scoped_refptr<Foo>)>(), foo_ptr); |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 482 | ``` |
| 483 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 484 | ### Passing Unbound Input Parameters |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 485 | |
| 486 | Unbound parameters are specified at the time a callback is `Run()`. They are |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 487 | specified in the `base::{Once, Repeating}Callback` template type: |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 488 | |
| 489 | ```cpp |
| 490 | void MyFunc(int i, const std::string& str) {} |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 491 | base::RepeatingCallback<void(int, const std::string&)> cb = base::BindRepeating(&MyFunc); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 492 | cb.Run(23, "hello, world"); |
| 493 | ``` |
| 494 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 495 | ### Passing Bound Input Parameters |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 496 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 497 | Bound parameters are specified when you create the callback as arguments to |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 498 | `base::Bind{Once, Repeating}()`. They will be passed to the function and the `Run()`ner of the |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 499 | callback doesn't see those values or even know that the function it's calling. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 500 | |
| 501 | ```cpp |
| 502 | void MyFunc(int i, const std::string& str) {} |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 503 | base::RepeatingCallback<void()> cb = base::BindRepeating(&MyFunc, 23, "hello world"); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 504 | cb.Run(); |
| 505 | ``` |
| 506 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 507 | As described earlier, a callback with no unbound input parameters |
| 508 | (`base::RepeatingCallback<void()>`) is called a `base::RepeatingClosure`. So we |
| 509 | could have also written: |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 510 | |
| 511 | ```cpp |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 512 | base::RepeatingClosure cb = base::BindRepeating(&MyFunc, 23, "hello world"); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 513 | ``` |
| 514 | |
| 515 | When calling member functions, bound parameters just go after the object |
| 516 | pointer. |
| 517 | |
| 518 | ```cpp |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 519 | base::RepeatingClosure cb = base::BindRepeating(&MyClass::MyFunc, this, 23, "hello world"); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 520 | ``` |
| 521 | |
Matt Giuca | 7e81b22e | 2019-12-12 02:41:21 | [diff] [blame] | 522 | ### Partial Binding Of Parameters |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 523 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 524 | You can specify some parameters when you create the callback, and specify the |
| 525 | rest when you execute the callback. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 526 | |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 527 | When calling a function bound parameters are first, followed by unbound |
| 528 | parameters. |
| 529 | |
Gabriel Charette | 9048031 | 2018-02-16 15:10:05 | [diff] [blame] | 530 | ```cpp |
| 531 | void ReadIntFromFile(const std::string& filename, |
| 532 | base::OnceCallback<void(int)> on_read); |
| 533 | |
| 534 | void DisplayIntWithPrefix(const std::string& prefix, int result) { |
| 535 | LOG(INFO) << prefix << result; |
| 536 | } |
| 537 | |
| 538 | void AnotherFunc(const std::string& file) { |
| 539 | ReadIntFromFile(file, base::BindOnce(&DisplayIntWithPrefix, "MyPrefix: ")); |
| 540 | }; |
| 541 | ``` |
| 542 | |
Matt Giuca | 7e81b22e | 2019-12-12 02:41:21 | [diff] [blame] | 543 | This technique is known as [partial |
| 544 | application](http://en.wikipedia.org/wiki/Partial_application). It should be |
| 545 | used in lieu of creating an adapter class that holds the bound arguments. Notice |
| 546 | also that the `"MyPrefix: "` argument is actually a `const char*`, while |
| 547 | `DisplayIntWithPrefix` actually wants a `const std::string&`. Like normal |
| 548 | function dispatch, `base::Bind`, will coerce parameter types if possible. |
Gabriel Charette | 9048031 | 2018-02-16 15:10:05 | [diff] [blame] | 549 | |
Max Morin | b51cf51 | 2018-02-19 12:49:49 | [diff] [blame] | 550 | ### Avoiding Copies With Callback Parameters |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 551 | |
Max Morin | b51cf51 | 2018-02-19 12:49:49 | [diff] [blame] | 552 | A parameter of `base::BindRepeating()` or `base::BindOnce()` is moved into its |
| 553 | internal storage if it is passed as a rvalue. |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 554 | |
| 555 | ```cpp |
| 556 | std::vector<int> v = {1, 2, 3}; |
| 557 | // |v| is moved into the internal storage without copy. |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 558 | base::BindOnce(&Foo, std::move(v)); |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 559 | ``` |
| 560 | |
| 561 | ```cpp |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 562 | // The vector is moved into the internal storage without copy. |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 563 | base::BindOnce(&Foo, std::vector<int>({1, 2, 3})); |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 564 | ``` |
| 565 | |
Max Morin | b51cf51 | 2018-02-19 12:49:49 | [diff] [blame] | 566 | Arguments bound with `base::BindOnce()` are always moved, if possible, to the |
| 567 | target function. |
| 568 | A function parameter that is passed by value and has a move constructor will be |
| 569 | moved instead of copied. |
| 570 | This makes it easy to use move-only types with `base::BindOnce()`. |
| 571 | |
| 572 | In contrast, arguments bound with `base::BindRepeating()` are only moved to the |
| 573 | target function if the argument is bound with `base::Passed()`. |
| 574 | |
| 575 | **DANGER**: |
| 576 | A `base::RepeatingCallback` can only be run once if arguments were bound with |
| 577 | `base::Passed()`. |
| 578 | For this reason, avoid `base::Passed()`. |
| 579 | If you know a callback will only be called once, prefer to refactor code to |
| 580 | work with `base::OnceCallback` instead. |
| 581 | |
| 582 | Avoid using `base::Passed()` with `base::BindOnce()`, as `std::move()` does the |
| 583 | same thing and is more familiar. |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 584 | |
| 585 | ```cpp |
| 586 | void Foo(std::unique_ptr<int>) {} |
Max Morin | b51cf51 | 2018-02-19 12:49:49 | [diff] [blame] | 587 | auto p = std::make_unique<int>(42); |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 588 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 589 | // |p| is moved into the internal storage of BindOnce(), and moved out to |Foo|. |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 590 | base::BindOnce(&Foo, std::move(p)); |
Max Morin | b51cf51 | 2018-02-19 12:49:49 | [diff] [blame] | 591 | base::BindRepeating(&Foo, base::Passed(&p)); // Ok, but subtle. |
| 592 | base::BindRepeating(&Foo, base::Passed(std::move(p))); // Ok, but subtle. |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 593 | ``` |
| 594 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 595 | ## Quick reference for advanced binding |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 596 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 597 | ### Binding A Class Method With Weak Pointers |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 598 | |
Daniel Cheng | af16de5 | 2022-08-01 22:46:04 | [diff] [blame] | 599 | Callbacks to a class method may be bound using a weak pointer as the receiver. |
| 600 | A callback bound using a weak pointer receiver will be automatically cancelled |
| 601 | (calling `Run()` becomes a no-op) if the weak pointer is invalidated, e.g. its |
| 602 | associated class instance is destroyed. |
Wez | 3327626 | 2019-06-21 00:11:20 | [diff] [blame] | 603 | |
Daniel Cheng | af16de5 | 2022-08-01 22:46:04 | [diff] [blame] | 604 | The most common way to use this pattern is by embedding a `base::WeakPtrFactory` |
| 605 | field, e.g.: |
Wez | 3327626 | 2019-06-21 00:11:20 | [diff] [blame] | 606 | |
| 607 | ```cpp |
| 608 | class MyClass { |
Daniel Cheng | af16de5 | 2022-08-01 22:46:04 | [diff] [blame] | 609 | public: |
| 610 | MyClass(); |
| 611 | |
| 612 | void Foo(); |
| 613 | |
| 614 | private: |
| 615 | std::string data_; |
| 616 | |
| 617 | // Chrome's compiler toolchain enforces that any `WeakPtrFactory` |
| 618 | // fields are declared last, to avoid destruction ordering issues. |
Jeremy Roman | 0dd0b2f | 2019-07-16 21:00:43 | [diff] [blame] | 619 | base::WeakPtrFactory<MyClass> weak_factory_{this}; |
Wez | 3327626 | 2019-06-21 00:11:20 | [diff] [blame] | 620 | }; |
| 621 | ``` |
| 622 | |
Daniel Cheng | af16de5 | 2022-08-01 22:46:04 | [diff] [blame] | 623 | Then use `base::WeakPtrFactory<T>::GetWeakPtr()` as the receiver when |
| 624 | binding a callback: |
Wez | 3327626 | 2019-06-21 00:11:20 | [diff] [blame] | 625 | |
Daniel Cheng | af16de5 | 2022-08-01 22:46:04 | [diff] [blame] | 626 | ```cpp |
Sean Maher | 70f294293 | 2023-01-04 22:15:06 | [diff] [blame] | 627 | base::SequencedTaskRunner::GetCurrentDefault()->PostTask( |
Daniel Cheng | af16de5 | 2022-08-01 22:46:04 | [diff] [blame] | 628 | FROM_HERE, |
| 629 | base::BindOnce(&MyClass::Foo, weak_factory_.GetWeakPtr()); |
| 630 | ``` |
| 631 | |
| 632 | If `this` is destroyed before the posted callback runs, the callback will |
| 633 | simply become a no-op when run, rather than being a use-after-free bug on |
| 634 | the destroyed `MyClass` instance. |
| 635 | |
| 636 | **Sequence safety** |
| 637 | |
| 638 | Class method callbacks bound to `base::WeakPtr`s must be run on the same |
| 639 | sequence on which the object will be destroyed to avoid potential races |
| 640 | between object destruction and callback execution. The same caveat applies if |
| 641 | a class manually invalidates live `base::WeakPtr`s with |
| 642 | `base::WeakPtrFactory<T>::InvalidateWeakPtrs()`. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 643 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 644 | ### Binding A Class Method With Manual Lifetime Management |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 645 | |
Daniel Cheng | af16de5 | 2022-08-01 22:46:04 | [diff] [blame] | 646 | If a callback bound to a class method does not need cancel-on-destroy |
| 647 | semantics (because there is some external guarantee that the class instance will |
| 648 | always be live when running the callback), then use: |
| 649 | |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 650 | ```cpp |
Daniel Cheng | af16de5 | 2022-08-01 22:46:04 | [diff] [blame] | 651 | // base::Unretained() is safe since `this` joins `background_thread_` in the |
| 652 | // destructor. |
| 653 | background_thread_->PostTask( |
| 654 | FROM_HERE, base::BindOnce(&MyClass::Foo, base::Unretained(this))); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 655 | ``` |
| 656 | |
Daniel Cheng | af16de5 | 2022-08-01 22:46:04 | [diff] [blame] | 657 | It is often a good idea to add a brief comment to explain why |
| 658 | `base::Unretained()` is safe in this context; if nothing else, for future code |
| 659 | archaeologists trying to fix a use-after-free bug. |
| 660 | |
| 661 | An alternative is `base::WeakPtrFactory<T>::GetSafeRef()`: |
| 662 | |
| 663 | ```cpp |
| 664 | background_thread_->PostTask( |
| 665 | FROM_HERE, base::BindOnce(&MyClass::Foo, weak_factory_.GetSafeRef()); |
| 666 | ``` |
| 667 | |
| 668 | Similar to `base::Unretained()`, this disables cancel-on-destroy semantics; |
| 669 | unlike `base::Unretained()`, this is guaranteed to terminate safely if the |
| 670 | lifetime expectations are violated. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 671 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 672 | ### Binding A Class Method And Having The Callback Own The Class |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 673 | |
| 674 | ```cpp |
| 675 | MyClass* myclass = new MyClass; |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 676 | base::BindOnce(&MyClass::Foo, base::Owned(myclass)); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 677 | ``` |
| 678 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 679 | The object will be deleted when the callback is destroyed, even if it's not run |
| 680 | (like if you post a task during shutdown). Potentially useful for "fire and |
| 681 | forget" cases. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 682 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 683 | Smart pointers (e.g. `std::unique_ptr<>`) are also supported as the receiver. |
| 684 | |
| 685 | ```cpp |
| 686 | std::unique_ptr<MyClass> myclass(new MyClass); |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 687 | base::BindOnce(&MyClass::Foo, std::move(myclass)); |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 688 | ``` |
| 689 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 690 | ### Ignoring Return Values |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 691 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 692 | Sometimes you want to call a function that returns a value in a callback that |
| 693 | doesn't expect a return value. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 694 | |
| 695 | ```cpp |
Wen Fan | dd47202 | 2021-03-12 01:31:09 | [diff] [blame] | 696 | int DoSomething(int arg) { |
| 697 | cout << arg << endl; |
| 698 | return arg; |
| 699 | } |
danakj | 9335cb1c | 2020-10-28 20:21:21 | [diff] [blame] | 700 | base::RepeatingCallback<void(int)> cb = |
| 701 | base::BindRepeating(IgnoreResult(&DoSomething)); |
| 702 | ``` |
| 703 | |
| 704 | Similarly, you may want to use an existing callback that returns a value in a |
| 705 | place that expects a void return type. |
| 706 | |
| 707 | ```cpp |
| 708 | base::RepeatingCallback<int()> cb = base::BindRepeating([](){ return 5; }); |
| 709 | base::RepeatingClosure void_cb = base::BindRepeating(base::IgnoreResult(cb)); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 710 | ``` |
| 711 | |
Nicolas Dossou-Gbete | 51290c2f | 2022-10-19 12:50:34 | [diff] [blame] | 712 | ### Ignoring Arguments Values |
| 713 | |
Evan Stade | 5df7469 | 2023-10-02 23:15:57 | [diff] [blame] | 714 | Sometimes you want to use a function that takes fewer arguments than the |
| 715 | designated callback type expects. The extra arguments can be ignored as long |
| 716 | as they are leading. |
Nicolas Dossou-Gbete | 51290c2f | 2022-10-19 12:50:34 | [diff] [blame] | 717 | |
| 718 | ```cpp |
Keren Zhu | 9cd105c8 | 2024-08-03 00:00:12 | [diff] [blame] | 719 | bool LogError(char* error_message) { |
| 720 | if (error_message) { |
Evan Stade | 5df7469 | 2023-10-02 23:15:57 | [diff] [blame] | 721 | cout << "Log: " << error_message << endl; |
Keren Zhu | 9cd105c8 | 2024-08-03 00:00:12 | [diff] [blame] | 722 | return false; |
| 723 | } |
| 724 | return true; |
Nicolas Dossou-Gbete | 51290c2f | 2022-10-19 12:50:34 | [diff] [blame] | 725 | } |
Keren Zhu | 9cd105c8 | 2024-08-03 00:00:12 | [diff] [blame] | 726 | base::RepeatingCallback<bool(int, char*)> cb = |
Evan Stade | 5df7469 | 2023-10-02 23:15:57 | [diff] [blame] | 727 | base::IgnoreArgs<int>(base::BindRepeating(&LogError)); |
Keren Zhu | 9cd105c8 | 2024-08-03 00:00:12 | [diff] [blame] | 728 | CHECK_EQ(true, cb.Run(42, nullptr)); |
Nicolas Dossou-Gbete | 51290c2f | 2022-10-19 12:50:34 | [diff] [blame] | 729 | ``` |
| 730 | |
Evan Stade | 5df7469 | 2023-10-02 23:15:57 | [diff] [blame] | 731 | Note in the example above that the type(s) passed to `IgnoreArgs` represent |
| 732 | the additional prepended parameters (those which will be "ignored"). The other |
| 733 | arguments to `cb` are inferred from the callback that is being wrapped. |
| 734 | |
| 735 | `IgnoreArgs` can be used to adapt a closure to a callback, ignoring all the |
| 736 | arguments that are eventually passed: |
Nicolas Dossou-Gbete | 51290c2f | 2022-10-19 12:50:34 | [diff] [blame] | 737 | |
| 738 | ```cpp |
| 739 | base::OnceClosure closure = base::BindOnce([](){ cout << "Hello!" << endl; }); |
| 740 | base::OnceCallback<void(int)> int_cb = |
| 741 | base::IgnoreArgs<int>(std::move(closure)); |
| 742 | ``` |
| 743 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 744 | ## Quick reference for binding parameters to BindOnce() and BindRepeating() |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 745 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 746 | Bound parameters are specified as arguments to `base::Bind{Once, Repeating}()` |
| 747 | and are passed to the functions. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 748 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 749 | ### Passing Parameters Owned By The Callback |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 750 | |
| 751 | ```cpp |
| 752 | void Foo(int* arg) { cout << *arg << endl; } |
| 753 | int* pn = new int(1); |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 754 | base::RepeatingClosure foo_callback = base::BindRepeating(&foo, base::Owned(pn)); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 755 | ``` |
| 756 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 757 | The parameter will be deleted when the callback is destroyed, even if it's not |
| 758 | run (like if you post a task during shutdown). |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 759 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 760 | ### Passing Parameters As A unique_ptr |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 761 | |
| 762 | ```cpp |
| 763 | void TakesOwnership(std::unique_ptr<Foo> arg) {} |
Max Morin | b51cf51 | 2018-02-19 12:49:49 | [diff] [blame] | 764 | auto f = std::make_unique<Foo>(); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 765 | // f becomes null during the following call. |
Max Morin | b51cf51 | 2018-02-19 12:49:49 | [diff] [blame] | 766 | base::OnceClosure cb = base::BindOnce(&TakesOwnership, std::move(f)); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 767 | ``` |
| 768 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 769 | Ownership of the parameter will be with the callback until the callback is run, |
| 770 | and then ownership is passed to the callback function. This means the callback |
| 771 | can only be run once. If the callback is never run, it will delete the object |
| 772 | when it's destroyed. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 773 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 774 | ### Passing Parameters As A scoped_refptr |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 775 | |
| 776 | ```cpp |
| 777 | void TakesOneRef(scoped_refptr<Foo> arg) {} |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 778 | scoped_refptr<Foo> f(new Foo); |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 779 | base::RepeatingClosure cb = base::BindRepeating(&TakesOneRef, f); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 780 | ``` |
| 781 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 782 | This should "just work." The closure will take a reference as long as it is |
| 783 | alive, and another reference will be taken for the called function. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 784 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 785 | ```cpp |
| 786 | void DontTakeRef(Foo* arg) {} |
| 787 | scoped_refptr<Foo> f(new Foo); |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 788 | base::RepeatingClosure cb = base::BindRepeating(&DontTakeRef, base::RetainedRef(f)); |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 789 | ``` |
| 790 | |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 791 | `base::RetainedRef` holds a reference to the object and passes a raw pointer to |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 792 | the object when the Callback is run. |
| 793 | |
kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 794 | ### Binding Const Reference Parameters |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 795 | |
kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 796 | If the callback function takes a const reference parameter then the value is |
| 797 | *copied* when bound unless `std::ref` or `std::cref` is used. Example: |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 798 | |
| 799 | ```cpp |
| 800 | void foo(const int& arg) { printf("%d %p\n", arg, &arg); } |
| 801 | int n = 1; |
kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 802 | base::OnceClosure has_copy = base::BindOnce(&foo, n); |
| 803 | base::OnceClosure has_ref = base::BindOnce(&foo, std::cref(n)); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 804 | n = 2; |
kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 805 | foo(n); // Prints "2 0xaaaaaaaaaaaa" |
| 806 | std::move(has_copy).Run(); // Prints "1 0xbbbbbbbbbbbb" |
| 807 | std::move(has_ref).Run(); // Prints "2 0xaaaaaaaaaaaa" |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 808 | ``` |
| 809 | |
kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 810 | Normally parameters are copied in the closure. **DANGER**: `std::ref` and |
| 811 | `std::cref` store a (const) reference instead, referencing the original |
| 812 | parameter. This means that you must ensure the object outlives the callback! |
| 813 | |
| 814 | ### Binding Non-Const Reference Parameters |
| 815 | |
| 816 | If the callback function takes a non-const reference then the bind statement |
| 817 | must specify what behavior is desired. If a reference that can mutate the |
| 818 | original value is desired then `std::ref` is used. If the callback should take |
| 819 | ownership of the value, either by making a copy or moving an existing value, |
| 820 | then `base::OwnedRef` is used. If neither is used the bind statement will fail |
| 821 | to compile. Example: |
| 822 | |
| 823 | ```cpp |
| 824 | void foo(int& arg) { |
| 825 | printf("%d\n", arg); |
| 826 | ++arg; |
| 827 | } |
| 828 | |
| 829 | int n = 0; |
| 830 | base::RepeatingClosure has_ref = base::BindRepeating(&foo, std::ref(n)); |
| 831 | base::RepeatingClosure has_copy = base::BindRepeating(&foo, base::OwnedRef(n)); |
| 832 | |
| 833 | foo(n); // Prints "0" |
| 834 | has_ref.Run(); // Prints "1" |
| 835 | has_ref.Run(); // Prints "2" |
| 836 | foo(n); // Prints "3" |
| 837 | |
| 838 | has_copy.Run(); // Prints "0" |
| 839 | has_copy.Run(); // Prints "1" |
| 840 | |
| 841 | // This will fail to compile. |
| 842 | base::RepeatingClosure cb = base::BindRepeating(&foo, n); |
| 843 | ``` |
| 844 | |
| 845 | Normally parameters are copied in the closure. **DANGER**: `std::ref` stores a |
| 846 | reference instead, referencing the original parameter. This means that you must |
| 847 | ensure the object outlives the callback! |
| 848 | |
| 849 | If the callback function has an output reference parameter but the output value |
| 850 | isn't needed then `base::OwnedRef()` is a convenient way to handle it. The |
| 851 | callback owned value will be mutated by the callback function and then deleted |
| 852 | along with the callback. Example: |
| 853 | |
| 854 | ```cpp |
| 855 | bool Compute(size_t index, int& output); |
| 856 | |
| 857 | // The `output` parameter isn't important for the callback, it only cares about |
| 858 | // the return value. |
| 859 | base::OnceClosure cb = base::BindOnce(&Compute, index, base::OwnedRef(0)); |
| 860 | bool success = std::move(cb).Run(); |
| 861 | ``` |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 862 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 863 | ## Implementation notes |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 864 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 865 | ### Where Is This Design From: |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 866 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 867 | The design is heavily influenced by C++'s `tr1::function` / `tr1::bind`, and by |
| 868 | the "Google Callback" system used inside Google. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 869 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 870 | ### Customizing the behavior |
| 871 | |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 872 | There are several injection points that controls binding behavior from outside |
| 873 | of its implementation. |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 874 | |
| 875 | ```cpp |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 876 | namespace base { |
| 877 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 878 | template <typename Receiver> |
Peter Kasting | d077bb2 | 2023-12-16 08:40:00 | [diff] [blame] | 879 | struct IsWeakReceiver : std::false_type {}; |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 880 | |
| 881 | template <typename Obj> |
John Admanski | 5c308c5 | 2023-11-30 18:13:50 | [diff] [blame] | 882 | struct BindUnwrapTraits { |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 883 | template <typename T> |
| 884 | T&& Unwrap(T&& obj) { |
| 885 | return std::forward<T>(obj); |
| 886 | } |
| 887 | }; |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 888 | |
| 889 | } // namespace base |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 890 | ``` |
| 891 | |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 892 | If `base::IsWeakReceiver<Receiver>::value` is true on a receiver of a method, |
| 893 | `base::Bind` checks if the receiver is evaluated to true and cancels the invocation |
| 894 | if it's evaluated to false. You can specialize `base::IsWeakReceiver` to make |
| 895 | an external smart pointer as a weak pointer. |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 896 | |
John Admanski | 5c308c5 | 2023-11-30 18:13:50 | [diff] [blame] | 897 | `base::BindUnwrapTraits<BoundObject>::Unwrap()` is called for each bound argument |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 898 | right before the callback calls the target function. You can specialize this to |
| 899 | define an argument wrapper such as `base::Unretained`, `base::Owned`, |
jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame] | 900 | `base::RetainedRef` and `base::Passed`. |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 901 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 902 | ### How The Implementation Works: |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 903 | |
| 904 | There are three main components to the system: |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 905 | 1) The `base::{Once, Repeating}Callback<>` classes. |
| 906 | 2) The `base::BindOnce() and base::BindRepeating()` functions. |
jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame] | 907 | 3) The arguments wrappers (e.g., `base::Unretained()` and `base::Owned()`). |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 908 | |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 909 | The Callback classes represent a generic function pointer. Internally, it |
| 910 | stores a refcounted piece of state that represents the target function and all |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 911 | its bound parameters. The `base::{Once, Repeating}Callback` constructor takes a |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 912 | `base::BindStateBase*`, which is upcasted from a `base::BindState<>`. In the |
| 913 | context of the constructor, the static type of this `base::BindState<>` pointer |
| 914 | uniquely identifies the function it is representing, all its bound parameters, |
| 915 | and a `Run()` method that is capable of invoking the target. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 916 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 917 | base::BindOnce() or base::BindRepeating() creates the `base::BindState<>` that |
| 918 | has the full static type, and erases the target function type as well as the |
| 919 | types of the bound parameters. It does this by storing a pointer to the specific |
| 920 | `Run()` function, and upcasting the state of `base::BindState<>*` to a |
| 921 | `base::BindStateBase*`. This is safe as long as this `BindStateBase` pointer is |
| 922 | only used with the stored `Run()` pointer. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 923 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 924 | These bind functions, along with a set of internal templates, are responsible |
| 925 | for |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 926 | |
| 927 | - Unwrapping the function signature into return type, and parameters |
| 928 | - Determining the number of parameters that are bound |
| 929 | - Creating the BindState storing the bound parameters |
| 930 | - Performing compile-time asserts to avoid error-prone behavior |
Armando Miraglia | cce1eb4 | 2018-08-16 14:35:44 | [diff] [blame] | 931 | - Returning a `Callback<>` with an arity matching the number of unbound |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 932 | parameters and that knows the correct refcounting semantics for the |
| 933 | target object if we are binding a method. |
| 934 | |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 935 | The `base::Bind` functions do the above using type-inference and variadic |
| 936 | templates. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 937 | |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 938 | By default `base::Bind{Once, Repeating}()` will store copies of all bound parameters, and |
Brett Wilson | 508162c | 2017-09-27 22:24:46 | [diff] [blame] | 939 | attempt to refcount a target object if the function being bound is a class |
| 940 | method. These copies are created even if the function takes parameters as const |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 941 | references. (Binding to non-const references is forbidden, see bind.h.) |
| 942 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 943 | To change this behavior, we introduce a set of argument wrappers (e.g., |
jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame] | 944 | `base::Unretained()`). These are simple container templates that are passed by |
danakj | db9ae794 | 2020-11-11 16:01:35 | [diff] [blame] | 945 | value, and wrap a pointer to argument. Each helper has a comment describing it |
Avi Drissman | d4459db | 2023-01-18 02:45:14 | [diff] [blame] | 946 | in base/functional/bind.h. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 947 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 948 | These types are passed to the `Unwrap()` functions to modify the behavior of |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 949 | `base::Bind{Once, Repeating}()`. The `Unwrap()` functions change behavior by doing partial |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 950 | specialization based on whether or not a parameter is a wrapper type. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 951 | |
jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame] | 952 | `base::Unretained()` is specific to Chromium. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 953 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 954 | ### Missing Functionality |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 955 | - Binding arrays to functions that take a non-const pointer. |
| 956 | Example: |
| 957 | ```cpp |
| 958 | void Foo(const char* ptr); |
| 959 | void Bar(char* ptr); |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 960 | base::BindOnce(&Foo, "test"); |
| 961 | base::BindOnce(&Bar, "test"); // This fails because ptr is not const. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 962 | ``` |
Gayane Petrosyan | 7f71698 | 2018-03-09 15:17:34 | [diff] [blame] | 963 | - In case of partial binding of parameters a possibility of having unbound |
| 964 | parameters before bound parameters. Example: |
| 965 | ```cpp |
| 966 | void Foo(int x, bool y); |
Colin Blundell | ea615d42 | 2021-05-12 09:35:41 | [diff] [blame] | 967 | base::BindOnce(&Foo, _1, false); // _1 is a placeholder. |
Gayane Petrosyan | 7f71698 | 2018-03-09 15:17:34 | [diff] [blame] | 968 | ``` |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 969 | |
Avi Drissman | d4459db | 2023-01-18 02:45:14 | [diff] [blame] | 970 | If you are thinking of forward declaring `base::{Once, Repeating}Callback` in |
| 971 | your own header file, please include "base/functional/callback_forward.h" |
| 972 | instead. |