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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 #include <stddef.h> | 163 #include <stddef.h> |
164 | 164 |
165 #include <type_traits> | 165 #include <type_traits> |
166 #include <utility> | 166 #include <utility> |
167 | 167 |
168 #include "base/callback.h" | 168 #include "base/callback.h" |
169 #include "base/memory/weak_ptr.h" | 169 #include "base/memory/weak_ptr.h" |
170 #include "build/build_config.h" | 170 #include "build/build_config.h" |
171 | 171 |
172 namespace base { | 172 namespace base { |
| 173 |
| 174 template <typename T> |
| 175 struct IsWeakReceiver; |
| 176 |
173 namespace internal { | 177 namespace internal { |
174 | 178 |
175 // Use the Substitution Failure Is Not An Error (SFINAE) trick to inspect T | 179 // Use the Substitution Failure Is Not An Error (SFINAE) trick to inspect T |
176 // for the existence of AddRef() and Release() functions of the correct | 180 // for the existence of AddRef() and Release() functions of the correct |
177 // signature. | 181 // signature. |
178 // | 182 // |
179 // http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error | 183 // http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error |
180 // http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-templat
e-to-check-for-a-functions-existence | 184 // http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-templat
e-to-check-for-a-functions-existence |
181 // http://stackoverflow.com/questions/4358584/sfinae-approach-comparison | 185 // http://stackoverflow.com/questions/4358584/sfinae-approach-comparison |
182 // http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-memb
er-functions | 186 // http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-memb
er-functions |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 } | 456 } |
453 | 457 |
454 // IsWeakMethod is a helper that determine if we are binding a WeakPtr<> to a | 458 // IsWeakMethod is a helper that determine if we are binding a WeakPtr<> to a |
455 // method. It is used internally by Bind() to select the correct | 459 // method. It is used internally by Bind() to select the correct |
456 // InvokeHelper that will no-op itself in the event the WeakPtr<> for | 460 // InvokeHelper that will no-op itself in the event the WeakPtr<> for |
457 // the target object is invalidated. | 461 // the target object is invalidated. |
458 // | 462 // |
459 // The first argument should be the type of the object that will be received by | 463 // The first argument should be the type of the object that will be received by |
460 // the method. | 464 // the method. |
461 template <bool IsMethod, typename... Args> | 465 template <bool IsMethod, typename... Args> |
462 struct IsWeakMethod : public std::false_type {}; | 466 struct IsWeakMethod : std::false_type {}; |
463 | 467 |
464 template <typename T, typename... Args> | 468 template <typename T, typename... Args> |
465 struct IsWeakMethod<true, WeakPtr<T>, Args...> : public std::true_type {}; | 469 struct IsWeakMethod<true, T, Args...> : IsWeakReceiver<T> {}; |
466 | |
467 template <typename T, typename... Args> | |
468 struct IsWeakMethod<true, ConstRefWrapper<WeakPtr<T>>, Args...> | |
469 : public std::true_type {}; | |
470 | |
471 | 470 |
472 // Packs a list of types to hold them in a single type. | 471 // Packs a list of types to hold them in a single type. |
473 template <typename... Types> | 472 template <typename... Types> |
474 struct TypeList {}; | 473 struct TypeList {}; |
475 | 474 |
476 // Used for DropTypeListItem implementation. | 475 // Used for DropTypeListItem implementation. |
477 template <size_t n, typename List> | 476 template <size_t n, typename List> |
478 struct DropTypeListItemImpl; | 477 struct DropTypeListItemImpl; |
479 | 478 |
480 // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure. | 479 // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure. |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 return internal::IgnoreResultHelper<Callback<T> >(data); | 619 return internal::IgnoreResultHelper<Callback<T> >(data); |
621 } | 620 } |
622 | 621 |
623 BASE_EXPORT void DoNothing(); | 622 BASE_EXPORT void DoNothing(); |
624 | 623 |
625 template<typename T> | 624 template<typename T> |
626 void DeletePointer(T* obj) { | 625 void DeletePointer(T* obj) { |
627 delete obj; | 626 delete obj; |
628 } | 627 } |
629 | 628 |
| 629 // An injection point to control |this| pointer behavior on a method invocation. |
| 630 // If IsWeakReceiver<> is true_type for |T| and |T| is used for a receiver of a |
| 631 // method, base::Bind cancels the method invocation if the receiver is tested as |
| 632 // false. |
| 633 // E.g. Foo::bar() is not called: |
| 634 // struct Foo : base::SupportsWeakPtr<Foo> { |
| 635 // void bar() {} |
| 636 // }; |
| 637 // |
| 638 // WeakPtr<Foo> oo = nullptr; |
| 639 // base::Bind(&Foo::bar, oo).Run(); |
| 640 template <typename T> |
| 641 struct IsWeakReceiver : std::false_type {}; |
| 642 |
| 643 template <typename T> |
| 644 struct IsWeakReceiver<internal::ConstRefWrapper<T>> : IsWeakReceiver<T> {}; |
| 645 |
| 646 template <typename T> |
| 647 struct IsWeakReceiver<WeakPtr<T>> : std::true_type {}; |
| 648 |
630 } // namespace base | 649 } // namespace base |
631 | 650 |
632 #endif // BASE_BIND_HELPERS_H_ | 651 #endif // BASE_BIND_HELPERS_H_ |
OLD | NEW |