Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: base/callback.h

Issue 8738001: Remove BindStateHolder and have Bind() return a Callback<> object directly. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix callback_unittest.cc Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // This file was GENERATED by command: 1 // This file was GENERATED by command:
2 // pump.py callback.h.pump 2 // pump.py callback.h.pump
3 // DO NOT EDIT BY HAND!!! 3 // DO NOT EDIT BY HAND!!!
4 4
5 5
6 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 6 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be 7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. 8 // found in the LICENSE file.
9 9
10 #ifndef BASE_CALLBACK_H_ 10 #ifndef BASE_CALLBACK_H_
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 // HOW THE IMPLEMENTATION WORKS: 118 // HOW THE IMPLEMENTATION WORKS:
119 // 119 //
120 // There are three main components to the system: 120 // There are three main components to the system:
121 // 1) The Callback classes. 121 // 1) The Callback classes.
122 // 2) The Bind() functions. 122 // 2) The Bind() functions.
123 // 3) The arguments wrappers (eg., Unretained() and ConstRef()). 123 // 3) The arguments wrappers (eg., Unretained() and ConstRef()).
124 // 124 //
125 // The Callback classes represent a generic function pointer. Internally, 125 // The Callback classes represent a generic function pointer. Internally,
126 // it stores a refcounted piece of state that represents the target function 126 // it stores a refcounted piece of state that represents the target function
127 // and all its bound parameters. Each Callback specialization has a templated 127 // and all its bound parameters. Each Callback specialization has a templated
128 // constructor that takes an BindStateHolder<> object. In the context of 128 // constructor that takes an BindState<>*. In the context of the constructor,
129 // the constructor, the static type of this BindStateHolder<> object 129 // the static type of this BindState<> pointer uniquely identifies the
130 // uniquely identifies the function it is representing, all its bound 130 // function it is representing, all its bound parameters, and a Run() method
131 // parameters, and a DoInvoke() that is capable of invoking the target. 131 // that is capable of invoking the target.
132 // 132 //
133 // Callback's constructor is takes the BindStateHolder<> that has the 133 // Callback's constructor takes the BindState<>* that has the full static type
134 // full static type and erases the target function type, and the bound 134 // and erases the target function type as well as the types of the bound
135 // parameters. It does this by storing a pointer to the specific DoInvoke() 135 // parameters. It does this by storing a pointer to the specific Run()
136 // function, and upcasting the state of BindStateHolder<> to a 136 // function, and upcasting the state of BindState<>* to a
137 // BindStateBase. This is safe as long as this BindStateBase pointer 137 // BindStateBase*. This is safe as long as this BindStateBase pointer
138 // is only used with the stored DoInvoke() pointer. 138 // is only used with the stored Run() pointer.
139 // 139 //
140 // To create BindStateHolder<> objects, we use the Bind() functions. 140 // To BindState<> objects are created inside the Bind() functions.
141 // These functions, along with a set of internal templates, are reponsible for 141 // These functions, along with a set of internal templates, are reponsible for
akalin 2011/12/02 18:54:45 reponsible -> responsible
awong 2011/12/06 00:21:12 Done.
142 // 142 //
143 // - Unwrapping the function signature into return type, and parameters 143 // - Unwrapping the function signature into return type, and parameters
144 // - Determining the number of parameters that are bound 144 // - Determining the number of parameters that are bound
145 // - Creating the storage for the bound parameters 145 // - Creating the BindState storing the bound parameters
146 // - Performing compile-time asserts to avoid error-prone behavior 146 // - Performing compile-time asserts to avoid error-prone behavior
147 // - Returning an BindStateHolder<> with an DoInvoke() that has an arity 147 // - Returning an Callback<> with an arity matching the number of unbound
148 // matching the number of unbound parameters, and knows the correct 148 // parameters and that knows the correct refcounting semantics for the
149 // refcounting semantics for the target object if we are binding a class 149 // target object if we are binding a method.
150 // method.
151 // 150 //
152 // The Bind functions do the above using type-inference, and template 151 // The Bind functions do the above using type-inference, and template
153 // specializations. 152 // specializations.
154 // 153 //
155 // By default Bind() will store copies of all bound parameters, and attempt 154 // By default Bind() will store copies of all bound parameters, and attempt
156 // to refcount a target object if the function being bound is a class method. 155 // to refcount a target object if the function being bound is a class method.
157 // 156 //
158 // To change this behavior, we introduce a set of argument wrappers 157 // To change this behavior, we introduce a set of argument wrappers
159 // (eg. Unretained(), and ConstRef()). These are simple container templates 158 // (eg. Unretained(), and ConstRef()). These are simple container templates
160 // that are passed by value, and wrap a pointer to argument. See the 159 // that are passed by value, and wrap a pointer to argument. See the
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 // First, we forward declare the Callback class template. This informs the 224 // First, we forward declare the Callback class template. This informs the
226 // compiler that the template only has 1 type parameter which is the function 225 // compiler that the template only has 1 type parameter which is the function
227 // signature that the Callback is representing. 226 // signature that the Callback is representing.
228 // 227 //
229 // After this, create template specializations for 0-7 parameters. Note that 228 // After this, create template specializations for 0-7 parameters. Note that
230 // even though the template typelist grows, the specialization still 229 // even though the template typelist grows, the specialization still
231 // only has one type: the function signature. 230 // only has one type: the function signature.
232 template <typename Sig> 231 template <typename Sig>
233 class Callback; 232 class Callback;
234 233
234 namespace internal {
235 template <typename Runnable, typename RunType, typename BoundArgsType>
236 struct BindState;
237 } // namespace internal
235 template <typename R> 238 template <typename R>
236 class Callback<R(void)> : public internal::CallbackBase { 239 class Callback<R(void)> : public internal::CallbackBase {
237 public: 240 public:
238 typedef R(RunType)(); 241 typedef R(RunType)();
239 242
240 Callback() : CallbackBase(NULL, NULL) { } 243 Callback() : CallbackBase(NULL) { }
241 244
242 // We pass BindStateHolder by const ref to avoid incurring an
243 // unnecessary AddRef/Unref pair even though we will modify the object.
244 // We cannot use a normal reference because the compiler will warn
245 // since this is often used on a return value, which is a temporary.
246 //
247 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 245 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
248 // return the exact Callback<> type. See base/bind.h for details. 246 // return the exact Callback<> type. See base/bind.h for details.
249 template <typename T> 247 template <typename Runnable, typename RunType, typename BoundArgsType>
250 Callback(const internal::BindStateHolder<T>& bind_state_holder) 248 Callback(internal::BindState<Runnable, RunType, BoundArgsType>* bind_state)
251 : CallbackBase(NULL, &bind_state_holder.bind_state_) { 249 : CallbackBase(bind_state) {
252 // Force the assignment to a location variable of PolymorphicInvoke 250
251 // Force the assignment to a local variable of PolymorphicInvoke
253 // so the compiler will typecheck that the passed in Run() method has 252 // so the compiler will typecheck that the passed in Run() method has
254 // the correct type. 253 // the correct type.
255 PolymorphicInvoke invoke_func = &T::InvokerType::Run; 254 PolymorphicInvoke invoke_func =
255 &internal::BindState<Runnable, RunType, BoundArgsType>
256 ::InvokerType::Run;
256 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); 257 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
257 } 258 }
258 259
259 bool Equals(const Callback& other) const { 260 bool Equals(const Callback& other) const {
260 return CallbackBase::Equals(other); 261 return CallbackBase::Equals(other);
261 } 262 }
262 263
263 R Run() const { 264 R Run() const {
264 PolymorphicInvoke f = 265 PolymorphicInvoke f =
265 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); 266 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
266 267
267 return f(bind_state_.get()); 268 return f(bind_state_.get());
268 } 269 }
269 270
270 private: 271 private:
271 typedef R(*PolymorphicInvoke)( 272 typedef R(*PolymorphicInvoke)(
272 internal::BindStateBase*); 273 internal::BindStateBase*);
273 274
274 }; 275 };
275 276
276 template <typename R, typename A1> 277 template <typename R, typename A1>
277 class Callback<R(A1)> : public internal::CallbackBase { 278 class Callback<R(A1)> : public internal::CallbackBase {
278 public: 279 public:
279 typedef R(RunType)(A1); 280 typedef R(RunType)(A1);
280 281
281 Callback() : CallbackBase(NULL, NULL) { } 282 Callback() : CallbackBase(NULL) { }
282 283
283 // We pass BindStateHolder by const ref to avoid incurring an
284 // unnecessary AddRef/Unref pair even though we will modify the object.
285 // We cannot use a normal reference because the compiler will warn
286 // since this is often used on a return value, which is a temporary.
287 //
288 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 284 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
289 // return the exact Callback<> type. See base/bind.h for details. 285 // return the exact Callback<> type. See base/bind.h for details.
290 template <typename T> 286 template <typename Runnable, typename RunType, typename BoundArgsType>
291 Callback(const internal::BindStateHolder<T>& bind_state_holder) 287 Callback(internal::BindState<Runnable, RunType, BoundArgsType>* bind_state)
292 : CallbackBase(NULL, &bind_state_holder.bind_state_) { 288 : CallbackBase(bind_state) {
293 // Force the assignment to a location variable of PolymorphicInvoke 289
290 // Force the assignment to a local variable of PolymorphicInvoke
294 // so the compiler will typecheck that the passed in Run() method has 291 // so the compiler will typecheck that the passed in Run() method has
295 // the correct type. 292 // the correct type.
296 PolymorphicInvoke invoke_func = &T::InvokerType::Run; 293 PolymorphicInvoke invoke_func =
294 &internal::BindState<Runnable, RunType, BoundArgsType>
295 ::InvokerType::Run;
297 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); 296 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
298 } 297 }
299 298
300 bool Equals(const Callback& other) const { 299 bool Equals(const Callback& other) const {
301 return CallbackBase::Equals(other); 300 return CallbackBase::Equals(other);
302 } 301 }
303 302
304 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1) const { 303 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1) const {
305 PolymorphicInvoke f = 304 PolymorphicInvoke f =
306 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); 305 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
307 306
308 return f(bind_state_.get(), a1); 307 return f(bind_state_.get(), a1);
309 } 308 }
310 309
311 private: 310 private:
312 typedef R(*PolymorphicInvoke)( 311 typedef R(*PolymorphicInvoke)(
313 internal::BindStateBase*, 312 internal::BindStateBase*,
314 typename internal::CallbackParamTraits<A1>::ForwardType); 313 typename internal::CallbackParamTraits<A1>::ForwardType);
315 314
316 }; 315 };
317 316
318 template <typename R, typename A1, typename A2> 317 template <typename R, typename A1, typename A2>
319 class Callback<R(A1, A2)> : public internal::CallbackBase { 318 class Callback<R(A1, A2)> : public internal::CallbackBase {
320 public: 319 public:
321 typedef R(RunType)(A1, A2); 320 typedef R(RunType)(A1, A2);
322 321
323 Callback() : CallbackBase(NULL, NULL) { } 322 Callback() : CallbackBase(NULL) { }
324 323
325 // We pass BindStateHolder by const ref to avoid incurring an
326 // unnecessary AddRef/Unref pair even though we will modify the object.
327 // We cannot use a normal reference because the compiler will warn
328 // since this is often used on a return value, which is a temporary.
329 //
330 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 324 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
331 // return the exact Callback<> type. See base/bind.h for details. 325 // return the exact Callback<> type. See base/bind.h for details.
332 template <typename T> 326 template <typename Runnable, typename RunType, typename BoundArgsType>
333 Callback(const internal::BindStateHolder<T>& bind_state_holder) 327 Callback(internal::BindState<Runnable, RunType, BoundArgsType>* bind_state)
334 : CallbackBase(NULL, &bind_state_holder.bind_state_) { 328 : CallbackBase(bind_state) {
335 // Force the assignment to a location variable of PolymorphicInvoke 329
330 // Force the assignment to a local variable of PolymorphicInvoke
336 // so the compiler will typecheck that the passed in Run() method has 331 // so the compiler will typecheck that the passed in Run() method has
337 // the correct type. 332 // the correct type.
338 PolymorphicInvoke invoke_func = &T::InvokerType::Run; 333 PolymorphicInvoke invoke_func =
334 &internal::BindState<Runnable, RunType, BoundArgsType>
335 ::InvokerType::Run;
339 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); 336 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
340 } 337 }
341 338
342 bool Equals(const Callback& other) const { 339 bool Equals(const Callback& other) const {
343 return CallbackBase::Equals(other); 340 return CallbackBase::Equals(other);
344 } 341 }
345 342
346 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1, 343 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
347 typename internal::CallbackParamTraits<A2>::ForwardType a2) const { 344 typename internal::CallbackParamTraits<A2>::ForwardType a2) const {
348 PolymorphicInvoke f = 345 PolymorphicInvoke f =
349 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_); 346 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
350 347
351 return f(bind_state_.get(), a1, 348 return f(bind_state_.get(), a1,
352 a2); 349 a2);
353 } 350 }
354 351
355 private: 352 private:
356 typedef R(*PolymorphicInvoke)( 353 typedef R(*PolymorphicInvoke)(
357 internal::BindStateBase*, 354 internal::BindStateBase*,
358 typename internal::CallbackParamTraits<A1>::ForwardType, 355 typename internal::CallbackParamTraits<A1>::ForwardType,
359 typename internal::CallbackParamTraits<A2>::ForwardType); 356 typename internal::CallbackParamTraits<A2>::ForwardType);
360 357
361 }; 358 };
362 359
363 template <typename R, typename A1, typename A2, typename A3> 360 template <typename R, typename A1, typename A2, typename A3>
364 class Callback<R(A1, A2, A3)> : public internal::CallbackBase { 361 class Callback<R(A1, A2, A3)> : public internal::CallbackBase {
365 public: 362 public:
366 typedef R(RunType)(A1, A2, A3); 363 typedef R(RunType)(A1, A2, A3);
367 364
368 Callback() : CallbackBase(NULL, NULL) { } 365 Callback() : CallbackBase(NULL) { }
369 366
370 // We pass BindStateHolder by const ref to avoid incurring an
371 // unnecessary AddRef/Unref pair even though we will modify the object.
372 // We cannot use a normal reference because the compiler will warn
373 // since this is often used on a return value, which is a temporary.
374 //
375 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 367 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
376 // return the exact Callback<> type. See base/bind.h for details. 368 // return the exact Callback<> type. See base/bind.h for details.
377 template <typename T> 369 template <typename Runnable, typename RunType, typename BoundArgsType>
378 Callback(const internal::BindStateHolder<T>& bind_state_holder) 370 Callback(internal::BindState<Runnable, RunType, BoundArgsType>* bind_state)
379 : CallbackBase(NULL, &bind_state_holder.bind_state_) { 371 : CallbackBase(bind_state) {
380 // Force the assignment to a location variable of PolymorphicInvoke 372
373 // Force the assignment to a local variable of PolymorphicInvoke
381 // so the compiler will typecheck that the passed in Run() method has 374 // so the compiler will typecheck that the passed in Run() method has
382 // the correct type. 375 // the correct type.
383 PolymorphicInvoke invoke_func = &T::InvokerType::Run; 376 PolymorphicInvoke invoke_func =
377 &internal::BindState<Runnable, RunType, BoundArgsType>
378 ::InvokerType::Run;
384 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); 379 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
385 } 380 }
386 381
387 bool Equals(const Callback& other) const { 382 bool Equals(const Callback& other) const {
388 return CallbackBase::Equals(other); 383 return CallbackBase::Equals(other);
389 } 384 }
390 385
391 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1, 386 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
392 typename internal::CallbackParamTraits<A2>::ForwardType a2, 387 typename internal::CallbackParamTraits<A2>::ForwardType a2,
393 typename internal::CallbackParamTraits<A3>::ForwardType a3) const { 388 typename internal::CallbackParamTraits<A3>::ForwardType a3) const {
(...skipping 12 matching lines...) Expand all
406 typename internal::CallbackParamTraits<A2>::ForwardType, 401 typename internal::CallbackParamTraits<A2>::ForwardType,
407 typename internal::CallbackParamTraits<A3>::ForwardType); 402 typename internal::CallbackParamTraits<A3>::ForwardType);
408 403
409 }; 404 };
410 405
411 template <typename R, typename A1, typename A2, typename A3, typename A4> 406 template <typename R, typename A1, typename A2, typename A3, typename A4>
412 class Callback<R(A1, A2, A3, A4)> : public internal::CallbackBase { 407 class Callback<R(A1, A2, A3, A4)> : public internal::CallbackBase {
413 public: 408 public:
414 typedef R(RunType)(A1, A2, A3, A4); 409 typedef R(RunType)(A1, A2, A3, A4);
415 410
416 Callback() : CallbackBase(NULL, NULL) { } 411 Callback() : CallbackBase(NULL) { }
417 412
418 // We pass BindStateHolder by const ref to avoid incurring an
419 // unnecessary AddRef/Unref pair even though we will modify the object.
420 // We cannot use a normal reference because the compiler will warn
421 // since this is often used on a return value, which is a temporary.
422 //
423 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 413 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
424 // return the exact Callback<> type. See base/bind.h for details. 414 // return the exact Callback<> type. See base/bind.h for details.
425 template <typename T> 415 template <typename Runnable, typename RunType, typename BoundArgsType>
426 Callback(const internal::BindStateHolder<T>& bind_state_holder) 416 Callback(internal::BindState<Runnable, RunType, BoundArgsType>* bind_state)
427 : CallbackBase(NULL, &bind_state_holder.bind_state_) { 417 : CallbackBase(bind_state) {
428 // Force the assignment to a location variable of PolymorphicInvoke 418
419 // Force the assignment to a local variable of PolymorphicInvoke
429 // so the compiler will typecheck that the passed in Run() method has 420 // so the compiler will typecheck that the passed in Run() method has
430 // the correct type. 421 // the correct type.
431 PolymorphicInvoke invoke_func = &T::InvokerType::Run; 422 PolymorphicInvoke invoke_func =
423 &internal::BindState<Runnable, RunType, BoundArgsType>
424 ::InvokerType::Run;
432 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); 425 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
433 } 426 }
434 427
435 bool Equals(const Callback& other) const { 428 bool Equals(const Callback& other) const {
436 return CallbackBase::Equals(other); 429 return CallbackBase::Equals(other);
437 } 430 }
438 431
439 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1, 432 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
440 typename internal::CallbackParamTraits<A2>::ForwardType a2, 433 typename internal::CallbackParamTraits<A2>::ForwardType a2,
441 typename internal::CallbackParamTraits<A3>::ForwardType a3, 434 typename internal::CallbackParamTraits<A3>::ForwardType a3,
(...skipping 16 matching lines...) Expand all
458 typename internal::CallbackParamTraits<A4>::ForwardType); 451 typename internal::CallbackParamTraits<A4>::ForwardType);
459 452
460 }; 453 };
461 454
462 template <typename R, typename A1, typename A2, typename A3, typename A4, 455 template <typename R, typename A1, typename A2, typename A3, typename A4,
463 typename A5> 456 typename A5>
464 class Callback<R(A1, A2, A3, A4, A5)> : public internal::CallbackBase { 457 class Callback<R(A1, A2, A3, A4, A5)> : public internal::CallbackBase {
465 public: 458 public:
466 typedef R(RunType)(A1, A2, A3, A4, A5); 459 typedef R(RunType)(A1, A2, A3, A4, A5);
467 460
468 Callback() : CallbackBase(NULL, NULL) { } 461 Callback() : CallbackBase(NULL) { }
469 462
470 // We pass BindStateHolder by const ref to avoid incurring an
471 // unnecessary AddRef/Unref pair even though we will modify the object.
472 // We cannot use a normal reference because the compiler will warn
473 // since this is often used on a return value, which is a temporary.
474 //
475 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 463 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
476 // return the exact Callback<> type. See base/bind.h for details. 464 // return the exact Callback<> type. See base/bind.h for details.
477 template <typename T> 465 template <typename Runnable, typename RunType, typename BoundArgsType>
478 Callback(const internal::BindStateHolder<T>& bind_state_holder) 466 Callback(internal::BindState<Runnable, RunType, BoundArgsType>* bind_state)
479 : CallbackBase(NULL, &bind_state_holder.bind_state_) { 467 : CallbackBase(bind_state) {
480 // Force the assignment to a location variable of PolymorphicInvoke 468
469 // Force the assignment to a local variable of PolymorphicInvoke
481 // so the compiler will typecheck that the passed in Run() method has 470 // so the compiler will typecheck that the passed in Run() method has
482 // the correct type. 471 // the correct type.
483 PolymorphicInvoke invoke_func = &T::InvokerType::Run; 472 PolymorphicInvoke invoke_func =
473 &internal::BindState<Runnable, RunType, BoundArgsType>
474 ::InvokerType::Run;
484 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); 475 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
485 } 476 }
486 477
487 bool Equals(const Callback& other) const { 478 bool Equals(const Callback& other) const {
488 return CallbackBase::Equals(other); 479 return CallbackBase::Equals(other);
489 } 480 }
490 481
491 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1, 482 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
492 typename internal::CallbackParamTraits<A2>::ForwardType a2, 483 typename internal::CallbackParamTraits<A2>::ForwardType a2,
493 typename internal::CallbackParamTraits<A3>::ForwardType a3, 484 typename internal::CallbackParamTraits<A3>::ForwardType a3,
(...skipping 19 matching lines...) Expand all
513 typename internal::CallbackParamTraits<A5>::ForwardType); 504 typename internal::CallbackParamTraits<A5>::ForwardType);
514 505
515 }; 506 };
516 507
517 template <typename R, typename A1, typename A2, typename A3, typename A4, 508 template <typename R, typename A1, typename A2, typename A3, typename A4,
518 typename A5, typename A6> 509 typename A5, typename A6>
519 class Callback<R(A1, A2, A3, A4, A5, A6)> : public internal::CallbackBase { 510 class Callback<R(A1, A2, A3, A4, A5, A6)> : public internal::CallbackBase {
520 public: 511 public:
521 typedef R(RunType)(A1, A2, A3, A4, A5, A6); 512 typedef R(RunType)(A1, A2, A3, A4, A5, A6);
522 513
523 Callback() : CallbackBase(NULL, NULL) { } 514 Callback() : CallbackBase(NULL) { }
524 515
525 // We pass BindStateHolder by const ref to avoid incurring an
526 // unnecessary AddRef/Unref pair even though we will modify the object.
527 // We cannot use a normal reference because the compiler will warn
528 // since this is often used on a return value, which is a temporary.
529 //
530 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 516 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
531 // return the exact Callback<> type. See base/bind.h for details. 517 // return the exact Callback<> type. See base/bind.h for details.
532 template <typename T> 518 template <typename Runnable, typename RunType, typename BoundArgsType>
533 Callback(const internal::BindStateHolder<T>& bind_state_holder) 519 Callback(internal::BindState<Runnable, RunType, BoundArgsType>* bind_state)
534 : CallbackBase(NULL, &bind_state_holder.bind_state_) { 520 : CallbackBase(bind_state) {
535 // Force the assignment to a location variable of PolymorphicInvoke 521
522 // Force the assignment to a local variable of PolymorphicInvoke
536 // so the compiler will typecheck that the passed in Run() method has 523 // so the compiler will typecheck that the passed in Run() method has
537 // the correct type. 524 // the correct type.
538 PolymorphicInvoke invoke_func = &T::InvokerType::Run; 525 PolymorphicInvoke invoke_func =
526 &internal::BindState<Runnable, RunType, BoundArgsType>
527 ::InvokerType::Run;
539 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); 528 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
540 } 529 }
541 530
542 bool Equals(const Callback& other) const { 531 bool Equals(const Callback& other) const {
543 return CallbackBase::Equals(other); 532 return CallbackBase::Equals(other);
544 } 533 }
545 534
546 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1, 535 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
547 typename internal::CallbackParamTraits<A2>::ForwardType a2, 536 typename internal::CallbackParamTraits<A2>::ForwardType a2,
548 typename internal::CallbackParamTraits<A3>::ForwardType a3, 537 typename internal::CallbackParamTraits<A3>::ForwardType a3,
(...skipping 22 matching lines...) Expand all
571 typename internal::CallbackParamTraits<A6>::ForwardType); 560 typename internal::CallbackParamTraits<A6>::ForwardType);
572 561
573 }; 562 };
574 563
575 template <typename R, typename A1, typename A2, typename A3, typename A4, 564 template <typename R, typename A1, typename A2, typename A3, typename A4,
576 typename A5, typename A6, typename A7> 565 typename A5, typename A6, typename A7>
577 class Callback<R(A1, A2, A3, A4, A5, A6, A7)> : public internal::CallbackBase { 566 class Callback<R(A1, A2, A3, A4, A5, A6, A7)> : public internal::CallbackBase {
578 public: 567 public:
579 typedef R(RunType)(A1, A2, A3, A4, A5, A6, A7); 568 typedef R(RunType)(A1, A2, A3, A4, A5, A6, A7);
580 569
581 Callback() : CallbackBase(NULL, NULL) { } 570 Callback() : CallbackBase(NULL) { }
582 571
583 // We pass BindStateHolder by const ref to avoid incurring an
584 // unnecessary AddRef/Unref pair even though we will modify the object.
585 // We cannot use a normal reference because the compiler will warn
586 // since this is often used on a return value, which is a temporary.
587 //
588 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 572 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
589 // return the exact Callback<> type. See base/bind.h for details. 573 // return the exact Callback<> type. See base/bind.h for details.
590 template <typename T> 574 template <typename Runnable, typename RunType, typename BoundArgsType>
591 Callback(const internal::BindStateHolder<T>& bind_state_holder) 575 Callback(internal::BindState<Runnable, RunType, BoundArgsType>* bind_state)
592 : CallbackBase(NULL, &bind_state_holder.bind_state_) { 576 : CallbackBase(bind_state) {
593 // Force the assignment to a location variable of PolymorphicInvoke 577
578 // Force the assignment to a local variable of PolymorphicInvoke
594 // so the compiler will typecheck that the passed in Run() method has 579 // so the compiler will typecheck that the passed in Run() method has
595 // the correct type. 580 // the correct type.
596 PolymorphicInvoke invoke_func = &T::InvokerType::Run; 581 PolymorphicInvoke invoke_func =
582 &internal::BindState<Runnable, RunType, BoundArgsType>
583 ::InvokerType::Run;
597 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func); 584 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
598 } 585 }
599 586
600 bool Equals(const Callback& other) const { 587 bool Equals(const Callback& other) const {
601 return CallbackBase::Equals(other); 588 return CallbackBase::Equals(other);
602 } 589 }
603 590
604 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1, 591 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
605 typename internal::CallbackParamTraits<A2>::ForwardType a2, 592 typename internal::CallbackParamTraits<A2>::ForwardType a2,
606 typename internal::CallbackParamTraits<A3>::ForwardType a3, 593 typename internal::CallbackParamTraits<A3>::ForwardType a3,
(...skipping 27 matching lines...) Expand all
634 }; 621 };
635 622
636 623
637 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it 624 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it
638 // will be used in a lot of APIs with delayed execution. 625 // will be used in a lot of APIs with delayed execution.
639 typedef Callback<void(void)> Closure; 626 typedef Callback<void(void)> Closure;
640 627
641 } // namespace base 628 } // namespace base
642 629
643 #endif // BASE_CALLBACK_H 630 #endif // BASE_CALLBACK_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698