OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This defines a set of argument wrappers and related factory methods that | 5 // This defines a set of argument wrappers and related factory methods that |
6 // can be used specify the refcounting and reference semantics of arguments | 6 // can be used specify the refcounting and reference semantics of arguments |
7 // that are bound by the Bind() function in base/bind.h. | 7 // that are bound by the Bind() function in base/bind.h. |
8 // | 8 // |
9 // It also defines a set of simple functions and utilities that people want | 9 // It also defines a set of simple functions and utilities that people want |
10 // when using Callback<> and Bind(). | 10 // when using Callback<> and Bind(). |
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 template <typename T> | 428 template <typename T> |
429 struct UnwrapTraits<PassedWrapper<T> > { | 429 struct UnwrapTraits<PassedWrapper<T> > { |
430 typedef T ForwardType; | 430 typedef T ForwardType; |
431 static T Unwrap(PassedWrapper<T>& o) { | 431 static T Unwrap(PassedWrapper<T>& o) { |
432 return o.Pass(); | 432 return o.Pass(); |
433 } | 433 } |
434 }; | 434 }; |
435 | 435 |
436 // Utility for handling different refcounting semantics in the Bind() | 436 // Utility for handling different refcounting semantics in the Bind() |
437 // function. | 437 // function. |
438 template <bool is_method, typename T> | 438 template <bool is_method, typename... T> |
439 struct MaybeRefcount; | 439 struct MaybeScopedRefPtr; |
440 | 440 |
441 template <typename T> | 441 template <bool is_method> |
442 struct MaybeRefcount<false, T> { | 442 struct MaybeScopedRefPtr<is_method> { |
443 static void AddRef(const T&) {} | 443 MaybeScopedRefPtr() {} |
444 static void Release(const T&) {} | |
445 }; | 444 }; |
446 | 445 |
447 template <typename T, size_t n> | 446 template <typename T, typename... Rest> |
448 struct MaybeRefcount<false, T[n]> { | 447 struct MaybeScopedRefPtr<false, T, Rest...> { |
449 static void AddRef(const T*) {} | 448 MaybeScopedRefPtr(const T&, const Rest&...) {} |
450 static void Release(const T*) {} | |
451 }; | 449 }; |
452 | 450 |
453 template <typename T> | 451 template <typename T, size_t n, typename... Rest> |
454 struct MaybeRefcount<true, T> { | 452 struct MaybeScopedRefPtr<false, T[n], Rest...> { |
455 static void AddRef(const T&) {} | 453 MaybeScopedRefPtr(const T*, const Rest&...) {} |
456 static void Release(const T&) {} | |
457 }; | 454 }; |
458 | 455 |
459 template <typename T> | 456 template <typename T, typename... Rest> |
460 struct MaybeRefcount<true, T*> { | 457 struct MaybeScopedRefPtr<true, T, Rest...> { |
461 static void AddRef(T* o) { o->AddRef(); } | 458 MaybeScopedRefPtr(const T& o, const Rest&...) {} |
462 static void Release(T* o) { o->Release(); } | |
463 }; | 459 }; |
464 | 460 |
465 // No need to additionally AddRef() and Release() since we are storing a | 461 template <typename T, typename... Rest> |
466 // scoped_refptr<> inside the storage object already. | 462 struct MaybeScopedRefPtr<true, T*, Rest...> { |
467 template <typename T> | 463 MaybeScopedRefPtr(T* o, const Rest&...) : ref_(o) {} |
468 struct MaybeRefcount<true, scoped_refptr<T> > { | 464 scoped_refptr<T> ref_; |
469 static void AddRef(const scoped_refptr<T>& o) {} | |
470 static void Release(const scoped_refptr<T>& o) {} | |
471 }; | 465 }; |
472 | 466 |
473 template <typename T> | 467 template <typename T, typename... Rest> |
474 struct MaybeRefcount<true, const T*> { | 468 struct MaybeScopedRefPtr<true, scoped_refptr<T>, Rest...> { |
475 static void AddRef(const T* o) { o->AddRef(); } | 469 MaybeScopedRefPtr(const scoped_refptr<T>&, const Rest&...) {} |
476 static void Release(const T* o) { o->Release(); } | 470 }; |
| 471 |
| 472 template <typename T, typename... Rest> |
| 473 struct MaybeScopedRefPtr<true, const T*, Rest...> { |
| 474 MaybeScopedRefPtr(const T* o, const Rest&...) : ref_(o) {} |
| 475 scoped_refptr<const T> ref_; |
477 }; | 476 }; |
478 | 477 |
479 // IsWeakMethod is a helper that determine if we are binding a WeakPtr<> to a | 478 // IsWeakMethod is a helper that determine if we are binding a WeakPtr<> to a |
480 // method. It is used internally by Bind() to select the correct | 479 // method. It is used internally by Bind() to select the correct |
481 // InvokeHelper that will no-op itself in the event the WeakPtr<> for | 480 // InvokeHelper that will no-op itself in the event the WeakPtr<> for |
482 // the target object is invalidated. | 481 // the target object is invalidated. |
483 // | 482 // |
484 // P1 should be the type of the object that will be received of the method. | 483 // P1 should be the type of the object that will be received of the method. |
485 template <bool IsMethod, typename P1> | 484 template <bool IsMethod, typename... Args> |
486 struct IsWeakMethod : public false_type {}; | 485 struct IsWeakMethod : public false_type {}; |
487 | 486 |
488 template <typename T> | 487 template <typename T, typename... Args> |
489 struct IsWeakMethod<true, WeakPtr<T> > : public true_type {}; | 488 struct IsWeakMethod<true, WeakPtr<T>, Args...> : public true_type {}; |
490 | 489 |
491 template <typename T> | 490 template <typename T, typename... Args> |
492 struct IsWeakMethod<true, ConstRefWrapper<WeakPtr<T> > > : public true_type {}; | 491 struct IsWeakMethod<true, ConstRefWrapper<WeakPtr<T> >, Args...> |
| 492 : public true_type {}; |
493 | 493 |
494 } // namespace internal | 494 } // namespace internal |
495 | 495 |
496 template <typename T> | 496 template <typename T> |
497 static inline internal::UnretainedWrapper<T> Unretained(T* o) { | 497 static inline internal::UnretainedWrapper<T> Unretained(T* o) { |
498 return internal::UnretainedWrapper<T>(o); | 498 return internal::UnretainedWrapper<T>(o); |
499 } | 499 } |
500 | 500 |
501 template <typename T> | 501 template <typename T> |
502 static inline internal::ConstRefWrapper<T> ConstRef(const T& o) { | 502 static inline internal::ConstRefWrapper<T> ConstRef(const T& o) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 BASE_EXPORT void DoNothing(); | 535 BASE_EXPORT void DoNothing(); |
536 | 536 |
537 template<typename T> | 537 template<typename T> |
538 void DeletePointer(T* obj) { | 538 void DeletePointer(T* obj) { |
539 delete obj; | 539 delete obj; |
540 } | 540 } |
541 | 541 |
542 } // namespace base | 542 } // namespace base |
543 | 543 |
544 #endif // BASE_BIND_HELPERS_H_ | 544 #endif // BASE_BIND_HELPERS_H_ |
OLD | NEW |