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

Side by Side Diff: base/bind_helpers.h

Issue 2048023004: Introduce base::IsWeakReceiver for base::Bind to support external weak pointers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: +comment Created 4 years, 6 months 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
« no previous file with comments | « no previous file | base/bind_internal.h » ('j') | base/bind_internal.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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 null.
danakj 2016/06/10 23:36:18 "if the receiver is tested as false" may be more a
tzik 2016/06/14 12:06:10 Done.
632 // E.g. Foo::bar() is not called:
633 // struct Foo : base::SupportsWeakPtr<Foo> {
634 // void bar() {}
635 // };
636 //
637 // WeakPtr<Foo> oo = nullptr;
638 // base::Bind(&Foo::bar, oo).Run();
639 template <typename T>
640 struct IsWeakReceiver : std::false_type {};
641
642 template <typename T>
643 struct IsWeakReceiver<WeakPtr<T>> : std::true_type {};
644
645 template <typename T>
646 struct IsWeakReceiver<internal::ConstRefWrapper<T>> : IsWeakReceiver<T> {};
danakj 2016/06/10 23:36:17 nit: Does it compile if you put this above the Wea
tzik 2016/06/14 12:06:10 Done.
647
630 } // namespace base 648 } // namespace base
631 649
632 #endif // BASE_BIND_HELPERS_H_ 650 #endif // BASE_BIND_HELPERS_H_
OLDNEW
« no previous file with comments | « no previous file | base/bind_internal.h » ('j') | base/bind_internal.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698