tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 1 | # Callback<> and Bind() |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 2 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 3 | ## Introduction |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 4 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 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 |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 7 | performing partial application of functions. |
| 8 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 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. |
| 11 | This can be used to pass around a unit of delayed execution, much like lexical |
| 12 | closures are used in other languages. For example, it is used in Chromium code |
| 13 | to schedule tasks on different MessageLoops. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 14 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 15 | A callback with no unbound input parameters (`Callback<void()>`) is called a |
| 16 | `Closure`. Note that this is NOT the same as what other languages refer to as a |
| 17 | closure -- it does not retain a reference to its enclosing environment. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 18 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 19 | ### OnceCallback<> And RepeatingCallback<> |
| 20 | |
| 21 | `OnceCallback<>` and `RepeatingCallback<>` are next gen callback classes, which |
| 22 | are under development. |
| 23 | |
| 24 | `OnceCallback<>` is created by `BindOnce()`. This is a callback variant that is |
| 25 | a move-only type and can be run only once. This moves out bound parameters from |
| 26 | its internal storage to the bound function by default, so it's easier to use |
| 27 | with movable types. This should be the preferred callback type: since the |
| 28 | lifetime of the callback is clear, it's simpler to reason about when a callback |
| 29 | that is passed between threads is destroyed. |
| 30 | |
| 31 | `RepeatingCallback<>` is created by `BindRepeating()`. This is a callback |
| 32 | variant that is copyable that can be run multiple times. It uses internal |
| 33 | ref-counting to make copies cheap. However, since ownership is shared, it is |
| 34 | harder to reason about when the callback and the bound state are destroyed, |
| 35 | especially when the callback is passed between threads. |
| 36 | |
| 37 | The legacy `Callback<>` is currently aliased to `RepeatingCallback<>`. In new |
| 38 | code, prefer `OnceCallback<>` where possible, and use `RepeatingCallback<>` |
| 39 | otherwise. Once the migration is complete, the type alias will be removed and |
| 40 | `OnceCallback<>` will be renamed to `Callback<>` to emphasize that it should be |
| 41 | preferred. |
| 42 | |
| 43 | `RepeatingCallback<>` is convertible to `OnceCallback<>` by the implicit |
| 44 | conversion. |
| 45 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 46 | ### Memory Management And Passing |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 47 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 48 | Pass `Callback` objects by value if ownership is transferred; otherwise, pass it |
| 49 | by const-reference. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 50 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 51 | ```cpp |
| 52 | // |Foo| just refers to |cb| but doesn't store it nor consume it. |
| 53 | bool Foo(const OnceCallback<void(int)>& cb) { |
| 54 | return cb.is_null(); |
| 55 | } |
| 56 | |
| 57 | // |Bar| takes the ownership of |cb| and stores |cb| into |g_cb|. |
| 58 | OnceCallback<void(int)> g_cb; |
| 59 | void Bar(OnceCallback<void(int)> cb) { |
| 60 | g_cb = std::move(cb); |
| 61 | } |
| 62 | |
| 63 | // |Baz| takes the ownership of |cb| and consumes |cb| by Run(). |
| 64 | void Baz(OnceCallback<void(int)> cb) { |
| 65 | std::move(cb).Run(42); |
| 66 | } |
| 67 | |
| 68 | // |Qux| takes the ownership of |cb| and transfers ownership to PostTask(), |
| 69 | // which also takes the ownership of |cb|. |
michaelpg | 126f704d1 | 2017-03-14 23:22:53 | [diff] [blame^] | 70 | // NOTE: TaskRunner is not actually migrated to OnceClosure yet. Once TaskRunner |
| 71 | // supports OnceClosure, a OnceCallback can be posted as follows: |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 72 | void Qux(OnceCallback<void(int)> cb) { |
michaelpg | 126f704d1 | 2017-03-14 23:22:53 | [diff] [blame^] | 73 | PostTask(FROM_HERE, |
| 74 | base::BindOnce(std::move(cb), 42)); // not yet implemented! |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 75 | } |
| 76 | ``` |
| 77 | |
| 78 | 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. |
| 80 | You may see a compile error when the function requires the exclusive ownership, |
| 81 | and you didn't pass the callback by move. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 82 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 83 | ## Quick reference for basic stuff |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 84 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 85 | ### Binding A Bare Function |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 86 | |
| 87 | ```cpp |
| 88 | int Return5() { return 5; } |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 89 | OnceCallback<int()> func_cb = BindOnce(&Return5); |
| 90 | LOG(INFO) << std::move(func_cb).Run(); // Prints 5. |
| 91 | ``` |
| 92 | |
| 93 | ```cpp |
| 94 | int Return5() { return 5; } |
| 95 | RepeatingCallback<int()> func_cb = BindRepeating(&Return5); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 96 | LOG(INFO) << func_cb.Run(); // Prints 5. |
| 97 | ``` |
| 98 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 99 | ### Binding A Captureless Lambda |
| 100 | |
| 101 | ```cpp |
| 102 | Callback<int()> lambda_cb = Bind([] { return 4; }); |
| 103 | LOG(INFO) << lambda_cb.Run(); // Print 4. |
| 104 | |
| 105 | OnceCallback<int()> lambda_cb2 = BindOnce([] { return 3; }); |
| 106 | LOG(INFO) << std::move(lambda_cb2).Run(); // Print 3. |
| 107 | ``` |
| 108 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 109 | ### Binding A Class Method |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 110 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 111 | The first argument to bind is the member function to call, the second is the |
| 112 | object on which to call it. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 113 | |
| 114 | ```cpp |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 115 | class Ref : public RefCountedThreadSafe<Ref> { |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 116 | public: |
| 117 | int Foo() { return 3; } |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 118 | }; |
| 119 | scoped_refptr<Ref> ref = new Ref(); |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 120 | Callback<void()> ref_cb = Bind(&Ref::Foo, ref); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 121 | LOG(INFO) << ref_cb.Run(); // Prints out 3. |
| 122 | ``` |
| 123 | |
| 124 | By default the object must support RefCounted or you will get a compiler |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 125 | error. If you're passing between threads, be sure it's RefCountedThreadSafe! See |
| 126 | "Advanced binding of member functions" below if you don't want to use reference |
| 127 | counting. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 128 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 129 | ### Running A Callback |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 130 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 131 | Callbacks can be run with their `Run` method, which has the same signature as |
| 132 | the template argument to the callback. Note that `OnceCallback::Run` consumes |
| 133 | the callback object and can only be invoked on a callback rvalue. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 134 | |
| 135 | ```cpp |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 136 | void DoSomething(const Callback<void(int, std::string)>& callback) { |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 137 | callback.Run(5, "hello"); |
| 138 | } |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 139 | |
| 140 | void DoSomethingOther(OnceCallback<void(int, std::string)> callback) { |
| 141 | std::move(callback).Run(5, "hello"); |
| 142 | } |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 143 | ``` |
| 144 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 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). |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 147 | |
| 148 | ```cpp |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 149 | void DoSomething(const RepeatingCallback<double(double)>& callback) { |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 150 | double myresult = callback.Run(3.14159); |
| 151 | myresult += callback.Run(2.71828); |
| 152 | } |
| 153 | ``` |
| 154 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 155 | ### Passing Unbound Input Parameters |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 156 | |
| 157 | Unbound parameters are specified at the time a callback is `Run()`. They are |
| 158 | specified in the `Callback` template type: |
| 159 | |
| 160 | ```cpp |
| 161 | void MyFunc(int i, const std::string& str) {} |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 162 | Callback<void(int, const std::string&)> cb = Bind(&MyFunc); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 163 | cb.Run(23, "hello, world"); |
| 164 | ``` |
| 165 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 166 | ### Passing Bound Input Parameters |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 167 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 168 | Bound parameters are specified when you create the callback as arguments to |
| 169 | `Bind()`. They will be passed to the function and the `Run()`ner of the callback |
| 170 | doesn't see those values or even know that the function it's calling. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 171 | |
| 172 | ```cpp |
| 173 | void MyFunc(int i, const std::string& str) {} |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 174 | Callback<void()> cb = Bind(&MyFunc, 23, "hello world"); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 175 | cb.Run(); |
| 176 | ``` |
| 177 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 178 | A callback with no unbound input parameters (`Callback<void()>`) is called a |
| 179 | `Closure`. So we could have also written: |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 180 | |
| 181 | ```cpp |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 182 | Closure cb = Bind(&MyFunc, 23, "hello world"); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 183 | ``` |
| 184 | |
| 185 | When calling member functions, bound parameters just go after the object |
| 186 | pointer. |
| 187 | |
| 188 | ```cpp |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 189 | Closure cb = Bind(&MyClass::MyFunc, this, 23, "hello world"); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 190 | ``` |
| 191 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 192 | ### Partial Binding Of Parameters |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 193 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 194 | You can specify some parameters when you create the callback, and specify the |
| 195 | rest when you execute the callback. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 196 | |
| 197 | ```cpp |
| 198 | void MyFunc(int i, const std::string& str) {} |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 199 | Callback<void(const std::string&)> cb = Bind(&MyFunc, 23); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 200 | cb.Run("hello world"); |
| 201 | ``` |
| 202 | |
| 203 | When calling a function bound parameters are first, followed by unbound |
| 204 | parameters. |
| 205 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 206 | ### Avoiding Copies with Callback Parameters |
| 207 | |
| 208 | A parameter of `Bind()` is moved into its internal storage if it is passed as a |
| 209 | rvalue. |
| 210 | |
| 211 | ```cpp |
| 212 | std::vector<int> v = {1, 2, 3}; |
| 213 | // |v| is moved into the internal storage without copy. |
| 214 | Bind(&Foo, std::move(v)); |
| 215 | ``` |
| 216 | |
| 217 | ```cpp |
| 218 | std::vector<int> v = {1, 2, 3}; |
| 219 | // The vector is moved into the internal storage without copy. |
| 220 | Bind(&Foo, std::vector<int>({1, 2, 3})); |
| 221 | ``` |
| 222 | |
| 223 | A bound object is moved out to the target function if you use `Passed()` for |
| 224 | the parameter. If you use `BindOnce()`, the bound object is moved out even |
| 225 | without `Passed()`. |
| 226 | |
| 227 | ```cpp |
| 228 | void Foo(std::unique_ptr<int>) {} |
| 229 | std::unique_ptr<int> p(new int(42)); |
| 230 | |
| 231 | // |p| is moved into the internal storage of Bind(), and moved out to |Foo|. |
| 232 | BindOnce(&Foo, std::move(p)); |
| 233 | BindRepeating(&Foo, Passed(&p)); |
| 234 | ``` |
| 235 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 236 | ## Quick reference for advanced binding |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 237 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 238 | ### Binding A Class Method With Weak Pointers |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 239 | |
| 240 | ```cpp |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 241 | Bind(&MyClass::Foo, GetWeakPtr()); |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 242 | ``` |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 243 | |
| 244 | The callback will not be run if the object has already been destroyed. |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 245 | **DANGER**: weak pointers are not threadsafe, so don't use this when passing between |
| 246 | threads! |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 247 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 248 | ### Binding A Class Method With Manual Lifetime Management |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 249 | |
| 250 | ```cpp |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 251 | Bind(&MyClass::Foo, Unretained(this)); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 252 | ``` |
| 253 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 254 | This disables all lifetime management on the object. You're responsible for |
| 255 | making sure the object is alive at the time of the call. You break it, you own |
| 256 | it! |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 257 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 258 | ### Binding A Class Method And Having The Callback Own The Class |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 259 | |
| 260 | ```cpp |
| 261 | MyClass* myclass = new MyClass; |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 262 | Bind(&MyClass::Foo, Owned(myclass)); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 263 | ``` |
| 264 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 265 | The object will be deleted when the callback is destroyed, even if it's not run |
| 266 | (like if you post a task during shutdown). Potentially useful for "fire and |
| 267 | forget" cases. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 268 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 269 | Smart pointers (e.g. `std::unique_ptr<>`) are also supported as the receiver. |
| 270 | |
| 271 | ```cpp |
| 272 | std::unique_ptr<MyClass> myclass(new MyClass); |
| 273 | Bind(&MyClass::Foo, std::move(myclass)); |
| 274 | ``` |
| 275 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 276 | ### Ignoring Return Values |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 277 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 278 | Sometimes you want to call a function that returns a value in a callback that |
| 279 | doesn't expect a return value. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 280 | |
| 281 | ```cpp |
| 282 | int DoSomething(int arg) { cout << arg << endl; } |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 283 | Callback<void(int)> cb = |
| 284 | Bind(IgnoreResult(&DoSomething)); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 285 | ``` |
| 286 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 287 | ## Quick reference for binding parameters to Bind() |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 288 | |
| 289 | Bound parameters are specified as arguments to `Bind()` and are passed to the |
| 290 | function. A callback with no parameters or no unbound parameters is called a |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 291 | `Closure` (`Callback<void()>` and `Closure` are the same thing). |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 292 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 293 | ### Passing Parameters Owned By The Callback |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 294 | |
| 295 | ```cpp |
| 296 | void Foo(int* arg) { cout << *arg << endl; } |
| 297 | int* pn = new int(1); |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 298 | Closure foo_callback = Bind(&foo, Owned(pn)); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 299 | ``` |
| 300 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 301 | The parameter will be deleted when the callback is destroyed, even if it's not |
| 302 | run (like if you post a task during shutdown). |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 303 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 304 | ### Passing Parameters As A unique_ptr |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 305 | |
| 306 | ```cpp |
| 307 | void TakesOwnership(std::unique_ptr<Foo> arg) {} |
| 308 | std::unique_ptr<Foo> f(new Foo); |
| 309 | // f becomes null during the following call. |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 310 | RepeatingClosure cb = BindRepeating(&TakesOwnership, Passed(&f)); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 311 | ``` |
| 312 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 313 | Ownership of the parameter will be with the callback until the callback is run, |
| 314 | and then ownership is passed to the callback function. This means the callback |
| 315 | can only be run once. If the callback is never run, it will delete the object |
| 316 | when it's destroyed. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 317 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 318 | ### Passing Parameters As A scoped_refptr |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 319 | |
| 320 | ```cpp |
| 321 | void TakesOneRef(scoped_refptr<Foo> arg) {} |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 322 | scoped_refptr<Foo> f(new Foo); |
| 323 | Closure cb = Bind(&TakesOneRef, f); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 324 | ``` |
| 325 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 326 | This should "just work." The closure will take a reference as long as it is |
| 327 | alive, and another reference will be taken for the called function. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 328 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 329 | ```cpp |
| 330 | void DontTakeRef(Foo* arg) {} |
| 331 | scoped_refptr<Foo> f(new Foo); |
| 332 | Closure cb = Bind(&DontTakeRef, RetainedRef(f)); |
| 333 | ``` |
| 334 | |
| 335 | `RetainedRef` holds a reference to the object and passes a raw pointer to |
| 336 | the object when the Callback is run. |
| 337 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 338 | ### Passing Parameters By Reference |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 339 | |
| 340 | Const references are *copied* unless `ConstRef` is used. Example: |
| 341 | |
| 342 | ```cpp |
| 343 | void foo(const int& arg) { printf("%d %p\n", arg, &arg); } |
| 344 | int n = 1; |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 345 | Closure has_copy = Bind(&foo, n); |
| 346 | Closure has_ref = Bind(&foo, ConstRef(n)); |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 347 | n = 2; |
| 348 | foo(n); // Prints "2 0xaaaaaaaaaaaa" |
| 349 | has_copy.Run(); // Prints "1 0xbbbbbbbbbbbb" |
| 350 | has_ref.Run(); // Prints "2 0xaaaaaaaaaaaa" |
| 351 | ``` |
| 352 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 353 | Normally parameters are copied in the closure. |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 354 | **DANGER**: `ConstRef` stores a const reference instead, referencing the |
| 355 | original parameter. This means that you must ensure the object outlives the |
| 356 | callback! |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 357 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 358 | ## Implementation notes |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 359 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 360 | ### Where Is This Design From: |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 361 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 362 | The design `Callback` and Bind is heavily influenced by C++'s `tr1::function` / |
| 363 | `tr1::bind`, and by the "Google Callback" system used inside Google. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 364 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 365 | ### Customizing the behavior |
| 366 | |
| 367 | There are several injection points that controls `Bind` behavior from outside of |
| 368 | its implementation. |
| 369 | |
| 370 | ```cpp |
| 371 | template <typename Receiver> |
| 372 | struct IsWeakReceiver { |
| 373 | static constexpr bool value = false; |
| 374 | }; |
| 375 | |
| 376 | template <typename Obj> |
| 377 | struct UnwrapTraits { |
| 378 | template <typename T> |
| 379 | T&& Unwrap(T&& obj) { |
| 380 | return std::forward<T>(obj); |
| 381 | } |
| 382 | }; |
| 383 | ``` |
| 384 | |
| 385 | If `IsWeakReceiver<Receiver>::value` is true on a receiver of a method, `Bind` |
| 386 | checks if the receiver is evaluated to true and cancels the invocation if it's |
| 387 | evaluated to false. You can specialize `IsWeakReceiver` to make an external |
| 388 | smart pointer as a weak pointer. |
| 389 | |
| 390 | `UnwrapTraits<BoundObject>::Unwrap()` is called for each bound arguments right |
| 391 | before `Callback` calls the target function. You can specialize this to define |
| 392 | an argument wrapper such as `Unretained`, `ConstRef`, `Owned`, `RetainedRef` and |
| 393 | `Passed`. |
| 394 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 395 | ### How The Implementation Works: |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 396 | |
| 397 | There are three main components to the system: |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 398 | 1) The `Callback<>` classes. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 399 | 2) The `Bind()` functions. |
| 400 | 3) The arguments wrappers (e.g., `Unretained()` and `ConstRef()`). |
| 401 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 402 | The Callback classes represent a generic function pointer. Internally, it stores |
| 403 | a refcounted piece of state that represents the target function and all its |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 404 | bound parameters. The `Callback` constructor takes a `BindStateBase*`, which is |
| 405 | upcasted from a `BindState<>`. In the context of the constructor, the static |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 406 | type of this `BindState<>` pointer uniquely identifies the function it is |
| 407 | representing, all its bound parameters, and a `Run()` method that is capable of |
| 408 | invoking the target. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 409 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 410 | `Bind()` creates the `BindState<>` that has the full static type, and erases the |
| 411 | target function type as well as the types of the bound parameters. It does this |
| 412 | by storing a pointer to the specific `Run()` function, and upcasting the state |
| 413 | of `BindState<>*` to a `BindStateBase*`. This is safe as long as this |
| 414 | `BindStateBase` pointer is only used with the stored `Run()` pointer. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 415 | |
| 416 | To `BindState<>` objects are created inside the `Bind()` functions. |
| 417 | These functions, along with a set of internal templates, are responsible for |
| 418 | |
| 419 | - Unwrapping the function signature into return type, and parameters |
| 420 | - Determining the number of parameters that are bound |
| 421 | - Creating the BindState storing the bound parameters |
| 422 | - Performing compile-time asserts to avoid error-prone behavior |
| 423 | - Returning an `Callback<>` with an arity matching the number of unbound |
| 424 | parameters and that knows the correct refcounting semantics for the |
| 425 | target object if we are binding a method. |
| 426 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 427 | The `Bind` functions do the above using type-inference and variadic templates. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 428 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 429 | By default `Bind()` will store copies of all bound parameters, and attempt to |
| 430 | refcount a target object if the function being bound is a class method. These |
| 431 | copies are created even if the function takes parameters as const |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 432 | references. (Binding to non-const references is forbidden, see bind.h.) |
| 433 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 434 | To change this behavior, we introduce a set of argument wrappers (e.g., |
| 435 | `Unretained()`, and `ConstRef()`). These are simple container templates that |
| 436 | are passed by value, and wrap a pointer to argument. See the file-level comment |
| 437 | in base/bind_helpers.h for more info. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 438 | |
tzik | 7c0c0cf1 | 2016-10-05 08:14:05 | [diff] [blame] | 439 | These types are passed to the `Unwrap()` functions to modify the behavior of |
| 440 | `Bind()`. The `Unwrap()` functions change behavior by doing partial |
| 441 | specialization based on whether or not a parameter is a wrapper type. |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 442 | |
| 443 | `ConstRef()` is similar to `tr1::cref`. `Unretained()` is specific to Chromium. |
| 444 | |
tzik | a431351 | 2016-09-06 06:51:12 | [diff] [blame] | 445 | ### Missing Functionality |
tzik | 703f156 | 2016-09-02 07:36:55 | [diff] [blame] | 446 | - Binding arrays to functions that take a non-const pointer. |
| 447 | Example: |
| 448 | ```cpp |
| 449 | void Foo(const char* ptr); |
| 450 | void Bar(char* ptr); |
| 451 | Bind(&Foo, "test"); |
| 452 | Bind(&Bar, "test"); // This fails because ptr is not const. |
| 453 | ``` |
| 454 | |
| 455 | If you are thinking of forward declaring `Callback` in your own header file, |
| 456 | please include "base/callback_forward.h" instead. |