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 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
337 public: | 337 public: |
338 explicit RetainedRefWrapper(T* o) : ptr_(o) {} | 338 explicit RetainedRefWrapper(T* o) : ptr_(o) {} |
339 explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {} | 339 explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {} |
340 T* get() const { return ptr_.get(); } | 340 T* get() const { return ptr_.get(); } |
341 private: | 341 private: |
342 scoped_refptr<T> ptr_; | 342 scoped_refptr<T> ptr_; |
343 }; | 343 }; |
344 | 344 |
345 template <typename T> | 345 template <typename T> |
346 struct IgnoreResultHelper { | 346 struct IgnoreResultHelper { |
347 explicit IgnoreResultHelper(T functor) : functor_(functor) {} | 347 explicit IgnoreResultHelper(T functor) : functor_(std::move(functor)) {} |
Yuta Kitamura
2016/06/29 00:07:55
Do you expect |T| to be something like Callback<T>
tzik
2016/06/29 02:49:08
T is a Functor. That can be Callback<>, function p
| |
348 | 348 |
349 T functor_; | 349 T functor_; |
350 }; | 350 }; |
351 | 351 |
352 template <typename T> | |
353 struct IgnoreResultHelper<Callback<T> > { | |
354 explicit IgnoreResultHelper(const Callback<T>& functor) : functor_(functor) {} | |
355 | |
356 const Callback<T>& functor_; | |
357 }; | |
358 | |
359 // An alternate implementation is to avoid the destructive copy, and instead | 352 // An alternate implementation is to avoid the destructive copy, and instead |
360 // specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to | 353 // specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to |
361 // a class that is essentially a std::unique_ptr<>. | 354 // a class that is essentially a std::unique_ptr<>. |
362 // | 355 // |
363 // The current implementation has the benefit though of leaving ParamTraits<> | 356 // The current implementation has the benefit though of leaving ParamTraits<> |
364 // fully in callback_internal.h as well as avoiding type conversions during | 357 // fully in callback_internal.h as well as avoiding type conversions during |
365 // storage. | 358 // storage. |
366 template <typename T> | 359 template <typename T> |
367 class OwnedWrapper { | 360 class OwnedWrapper { |
368 public: | 361 public: |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
414 return std::move(scoper_); | 407 return std::move(scoper_); |
415 } | 408 } |
416 | 409 |
417 private: | 410 private: |
418 mutable bool is_valid_; | 411 mutable bool is_valid_; |
419 mutable T scoper_; | 412 mutable T scoper_; |
420 }; | 413 }; |
421 | 414 |
422 // Unwrap the stored parameters for the wrappers above. | 415 // Unwrap the stored parameters for the wrappers above. |
423 template <typename T> | 416 template <typename T> |
424 const T& Unwrap(const T& o) { | 417 T&& Unwrap(T&& o) { |
Yuta Kitamura
2016/06/29 00:07:55
aside: If the argument can be an rvalue reference,
tzik
2016/06/29 02:49:08
No. The constness of the internal storage is from
| |
425 return o; | 418 return std::forward<T>(o); |
426 } | 419 } |
427 | 420 |
428 template <typename T> | 421 template <typename T> |
429 T* Unwrap(const UnretainedWrapper<T>& unretained) { | 422 T* Unwrap(const UnretainedWrapper<T>& unretained) { |
430 return unretained.get(); | 423 return unretained.get(); |
431 } | 424 } |
432 | 425 |
433 template <typename T> | 426 template <typename T> |
434 const T& Unwrap(const ConstRefWrapper<T>& const_ref) { | 427 const T& Unwrap(const ConstRefWrapper<T>& const_ref) { |
435 return const_ref.get(); | 428 return const_ref.get(); |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
604 static inline internal::PassedWrapper<T> Passed(T&& scoper) { | 597 static inline internal::PassedWrapper<T> Passed(T&& scoper) { |
605 return internal::PassedWrapper<T>(std::move(scoper)); | 598 return internal::PassedWrapper<T>(std::move(scoper)); |
606 } | 599 } |
607 template <typename T> | 600 template <typename T> |
608 static inline internal::PassedWrapper<T> Passed(T* scoper) { | 601 static inline internal::PassedWrapper<T> Passed(T* scoper) { |
609 return internal::PassedWrapper<T>(std::move(*scoper)); | 602 return internal::PassedWrapper<T>(std::move(*scoper)); |
610 } | 603 } |
611 | 604 |
612 template <typename T> | 605 template <typename T> |
613 static inline internal::IgnoreResultHelper<T> IgnoreResult(T data) { | 606 static inline internal::IgnoreResultHelper<T> IgnoreResult(T data) { |
614 return internal::IgnoreResultHelper<T>(data); | 607 return internal::IgnoreResultHelper<T>(std::move(data)); |
615 } | |
616 | |
617 template <typename T> | |
618 static inline internal::IgnoreResultHelper<Callback<T> > | |
619 IgnoreResult(const Callback<T>& data) { | |
620 return internal::IgnoreResultHelper<Callback<T> >(data); | |
621 } | 608 } |
622 | 609 |
623 BASE_EXPORT void DoNothing(); | 610 BASE_EXPORT void DoNothing(); |
624 | 611 |
625 template<typename T> | 612 template<typename T> |
626 void DeletePointer(T* obj) { | 613 void DeletePointer(T* obj) { |
627 delete obj; | 614 delete obj; |
628 } | 615 } |
629 | 616 |
630 // An injection point to control |this| pointer behavior on a method invocation. | 617 // An injection point to control |this| pointer behavior on a method invocation. |
(...skipping 12 matching lines...) Expand all Loading... | |
643 | 630 |
644 template <typename T> | 631 template <typename T> |
645 struct IsWeakReceiver<internal::ConstRefWrapper<T>> : IsWeakReceiver<T> {}; | 632 struct IsWeakReceiver<internal::ConstRefWrapper<T>> : IsWeakReceiver<T> {}; |
646 | 633 |
647 template <typename T> | 634 template <typename T> |
648 struct IsWeakReceiver<WeakPtr<T>> : std::true_type {}; | 635 struct IsWeakReceiver<WeakPtr<T>> : std::true_type {}; |
649 | 636 |
650 } // namespace base | 637 } // namespace base |
651 | 638 |
652 #endif // BASE_BIND_HELPERS_H_ | 639 #endif // BASE_BIND_HELPERS_H_ |
OLD | NEW |