blob: 970ddccba30008765d08b0c3538e1475f2308a1a [file] [log] [blame]
[email protected]b38d3572011-02-15 01:27:381// 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
3// found in the LICENSE file.
4
5#ifndef BASE_BIND_INTERNAL_H_
6#define BASE_BIND_INTERNAL_H_
[email protected]b38d3572011-02-15 01:27:387
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
9
vmpstrc52317f2015-11-18 08:43:2610#include <type_traits>
Jeremy Roman84956fa2017-08-16 15:55:2011#include <utility>
vmpstrc52317f2015-11-18 08:43:2612
[email protected]59eff912011-02-18 23:29:3113#include "base/callback_internal.h"
Sylvain Defresneec3270c2018-05-31 17:19:1514#include "base/compiler_specific.h"
[email protected]8217d4542011-10-01 06:31:4115#include "base/memory/raw_scoped_refptr_mismatch_checker.h"
[email protected]93540582011-05-16 22:35:1416#include "base/memory/weak_ptr.h"
[email protected]b38d3572011-02-15 01:27:3817#include "base/template_util.h"
[email protected]054ac7542011-02-27 01:25:5918#include "build/build_config.h"
19
Sylvain Defresneec3270c2018-05-31 17:19:1520#if defined(OS_MACOSX) && !HAS_FEATURE(objc_arc)
21#include "base/mac/scoped_block.h"
22#endif
23
[email protected]24292642012-07-12 20:06:4024// See base/callback.h for user documentation.
25//
26//
[email protected]7296f2762011-11-21 19:23:4427// CONCEPTS:
tzik99de02b2016-07-01 05:54:1228// Functor -- A movable type representing something that should be called.
29// All function pointers and Callback<> are functors even if the
30// invocation syntax differs.
[email protected]7296f2762011-11-21 19:23:4431// RunType -- A function type (as opposed to function _pointer_ type) for
tzik99de02b2016-07-01 05:54:1232// a Callback<>::Run(). Usually just a convenience typedef.
tzikce3ecf82015-12-15 06:41:4933// (Bound)Args -- A set of types that stores the arguments.
[email protected]b38d3572011-02-15 01:27:3834//
[email protected]7296f2762011-11-21 19:23:4435// Types:
[email protected]7296f2762011-11-21 19:23:4436// ForceVoidReturn<> -- Helper class for translating function signatures to
37// equivalent forms with a "void" return type.
tzik99de02b2016-07-01 05:54:1238// FunctorTraits<> -- Type traits used to determine the correct RunType and
39// invocation manner for a Functor. This is where function
[email protected]7296f2762011-11-21 19:23:4440// signature adapters are applied.
tzik99de02b2016-07-01 05:54:1241// InvokeHelper<> -- Take a Functor + arguments and actully invokes it.
tzik8ce65702015-02-05 19:11:2642// Handle the differing syntaxes needed for WeakPtr<>
tzik99de02b2016-07-01 05:54:1243// support. This is separate from Invoker to avoid creating
44// multiple version of Invoker<>.
45// Invoker<> -- Unwraps the curried parameters and executes the Functor.
[email protected]7296f2762011-11-21 19:23:4446// BindState<> -- Stores the curried parameters, and is the main entry point
tzik99de02b2016-07-01 05:54:1247// into the Bind() system.
[email protected]4346ef912011-02-19 00:52:1548
tzikc44f8102018-07-24 09:49:1949#if defined(OS_WIN)
50namespace Microsoft {
51namespace WRL {
52template <typename>
53class ComPtr;
54} // namespace WRL
55} // namespace Microsoft
56#endif
57
Peter Kastinga85265e32018-02-15 08:30:2358namespace base {
59
60template <typename T>
61struct IsWeakReceiver;
62
63template <typename>
64struct BindUnwrapTraits;
65
66template <typename Functor, typename BoundArgsTuple, typename SFINAE = void>
67struct CallbackCancellationTraits;
68
69namespace internal {
70
71template <typename Functor, typename SFINAE = void>
72struct FunctorTraits;
73
74template <typename T>
75class UnretainedWrapper {
76 public:
77 explicit UnretainedWrapper(T* o) : ptr_(o) {}
78 T* get() const { return ptr_; }
79
80 private:
81 T* ptr_;
82};
83
84template <typename T>
85class ConstRefWrapper {
86 public:
87 explicit ConstRefWrapper(const T& o) : ptr_(&o) {}
88 const T& get() const { return *ptr_; }
89
90 private:
91 const T* ptr_;
92};
93
94template <typename T>
95class RetainedRefWrapper {
96 public:
97 explicit RetainedRefWrapper(T* o) : ptr_(o) {}
98 explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {}
99 T* get() const { return ptr_.get(); }
100
101 private:
102 scoped_refptr<T> ptr_;
103};
104
105template <typename T>
106struct IgnoreResultHelper {
107 explicit IgnoreResultHelper(T functor) : functor_(std::move(functor)) {}
108 explicit operator bool() const { return !!functor_; }
109
110 T functor_;
111};
112
113// An alternate implementation is to avoid the destructive copy, and instead
114// specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to
115// a class that is essentially a std::unique_ptr<>.
116//
117// The current implementation has the benefit though of leaving ParamTraits<>
118// fully in callback_internal.h as well as avoiding type conversions during
119// storage.
120template <typename T>
121class OwnedWrapper {
122 public:
123 explicit OwnedWrapper(T* o) : ptr_(o) {}
124 ~OwnedWrapper() { delete ptr_; }
125 T* get() const { return ptr_; }
126 OwnedWrapper(OwnedWrapper&& other) {
127 ptr_ = other.ptr_;
128 other.ptr_ = NULL;
129 }
130
131 private:
132 mutable T* ptr_;
133};
134
135// PassedWrapper is a copyable adapter for a scoper that ignores const.
136//
137// It is needed to get around the fact that Bind() takes a const reference to
138// all its arguments. Because Bind() takes a const reference to avoid
139// unnecessary copies, it is incompatible with movable-but-not-copyable
140// types; doing a destructive "move" of the type into Bind() would violate
141// the const correctness.
142//
143// This conundrum cannot be solved without either C++11 rvalue references or
144// a O(2^n) blowup of Bind() templates to handle each combination of regular
145// types and movable-but-not-copyable types. Thus we introduce a wrapper type
146// that is copyable to transmit the correct type information down into
147// BindState<>. Ignoring const in this type makes sense because it is only
148// created when we are explicitly trying to do a destructive move.
149//
150// Two notes:
151// 1) PassedWrapper supports any type that has a move constructor, however
152// the type will need to be specifically whitelisted in order for it to be
153// bound to a Callback. We guard this explicitly at the call of Passed()
154// to make for clear errors. Things not given to Passed() will be forwarded
155// and stored by value which will not work for general move-only types.
156// 2) is_valid_ is distinct from NULL because it is valid to bind a "NULL"
157// scoper to a Callback and allow the Callback to execute once.
158template <typename T>
159class PassedWrapper {
160 public:
161 explicit PassedWrapper(T&& scoper)
162 : is_valid_(true), scoper_(std::move(scoper)) {}
163 PassedWrapper(PassedWrapper&& other)
164 : is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {}
165 T Take() const {
166 CHECK(is_valid_);
167 is_valid_ = false;
168 return std::move(scoper_);
169 }
170
171 private:
172 mutable bool is_valid_;
173 mutable T scoper_;
174};
175
176template <typename T>
177using Unwrapper = BindUnwrapTraits<std::decay_t<T>>;
178
179template <typename T>
Peter Kastingc2f8749bf2018-03-31 03:32:37180decltype(auto) Unwrap(T&& o) {
Peter Kastinga85265e32018-02-15 08:30:23181 return Unwrapper<T>::Unwrap(std::forward<T>(o));
182}
183
184// IsWeakMethod is a helper that determine if we are binding a WeakPtr<> to a
185// method. It is used internally by Bind() to select the correct
186// InvokeHelper that will no-op itself in the event the WeakPtr<> for
187// the target object is invalidated.
188//
189// The first argument should be the type of the object that will be received by
190// the method.
191template <bool is_method, typename... Args>
192struct IsWeakMethod : std::false_type {};
193
194template <typename T, typename... Args>
195struct IsWeakMethod<true, T, Args...> : IsWeakReceiver<T> {};
196
197// Packs a list of types to hold them in a single type.
198template <typename... Types>
199struct TypeList {};
200
201// Used for DropTypeListItem implementation.
202template <size_t n, typename List>
203struct DropTypeListItemImpl;
204
205// Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure.
206template <size_t n, typename T, typename... List>
207struct DropTypeListItemImpl<n, TypeList<T, List...>>
208 : DropTypeListItemImpl<n - 1, TypeList<List...>> {};
209
210template <typename T, typename... List>
211struct DropTypeListItemImpl<0, TypeList<T, List...>> {
212 using Type = TypeList<T, List...>;
213};
214
215template <>
216struct DropTypeListItemImpl<0, TypeList<>> {
217 using Type = TypeList<>;
218};
219
220// A type-level function that drops |n| list item from given TypeList.
221template <size_t n, typename List>
222using DropTypeListItem = typename DropTypeListItemImpl<n, List>::Type;
223
224// Used for TakeTypeListItem implementation.
225template <size_t n, typename List, typename... Accum>
226struct TakeTypeListItemImpl;
227
228// Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure.
229template <size_t n, typename T, typename... List, typename... Accum>
230struct TakeTypeListItemImpl<n, TypeList<T, List...>, Accum...>
231 : TakeTypeListItemImpl<n - 1, TypeList<List...>, Accum..., T> {};
232
233template <typename T, typename... List, typename... Accum>
234struct TakeTypeListItemImpl<0, TypeList<T, List...>, Accum...> {
235 using Type = TypeList<Accum...>;
236};
237
238template <typename... Accum>
239struct TakeTypeListItemImpl<0, TypeList<>, Accum...> {
240 using Type = TypeList<Accum...>;
241};
242
243// A type-level function that takes first |n| list item from given TypeList.
244// E.g. TakeTypeListItem<3, TypeList<A, B, C, D>> is evaluated to
245// TypeList<A, B, C>.
246template <size_t n, typename List>
247using TakeTypeListItem = typename TakeTypeListItemImpl<n, List>::Type;
248
249// Used for ConcatTypeLists implementation.
250template <typename List1, typename List2>
251struct ConcatTypeListsImpl;
252
253template <typename... Types1, typename... Types2>
254struct ConcatTypeListsImpl<TypeList<Types1...>, TypeList<Types2...>> {
255 using Type = TypeList<Types1..., Types2...>;
256};
257
258// A type-level function that concats two TypeLists.
259template <typename List1, typename List2>
260using ConcatTypeLists = typename ConcatTypeListsImpl<List1, List2>::Type;
261
262// Used for MakeFunctionType implementation.
263template <typename R, typename ArgList>
264struct MakeFunctionTypeImpl;
265
266template <typename R, typename... Args>
267struct MakeFunctionTypeImpl<R, TypeList<Args...>> {
268 // MSVC 2013 doesn't support Type Alias of function types.
269 // Revisit this after we update it to newer version.
270 typedef R Type(Args...);
271};
272
273// A type-level function that constructs a function type that has |R| as its
274// return type and has TypeLists items as its arguments.
275template <typename R, typename ArgList>
276using MakeFunctionType = typename MakeFunctionTypeImpl<R, ArgList>::Type;
277
278// Used for ExtractArgs and ExtractReturnType.
279template <typename Signature>
280struct ExtractArgsImpl;
281
282template <typename R, typename... Args>
283struct ExtractArgsImpl<R(Args...)> {
284 using ReturnType = R;
285 using ArgsList = TypeList<Args...>;
286};
287
288// A type-level function that extracts function arguments into a TypeList.
289// E.g. ExtractArgs<R(A, B, C)> is evaluated to TypeList<A, B, C>.
290template <typename Signature>
291using ExtractArgs = typename ExtractArgsImpl<Signature>::ArgsList;
292
293// A type-level function that extracts the return type of a function.
294// E.g. ExtractReturnType<R(A, B, C)> is evaluated to R.
295template <typename Signature>
296using ExtractReturnType = typename ExtractArgsImpl<Signature>::ReturnType;
297
tzikc1db72652016-07-08 09:42:38298template <typename Callable,
299 typename Signature = decltype(&Callable::operator())>
300struct ExtractCallableRunTypeImpl;
301
302template <typename Callable, typename R, typename... Args>
tzikf98654b2017-12-02 03:28:58303struct ExtractCallableRunTypeImpl<Callable, R (Callable::*)(Args...)> {
304 using Type = R(Args...);
305};
306
307template <typename Callable, typename R, typename... Args>
308struct ExtractCallableRunTypeImpl<Callable, R (Callable::*)(Args...) const> {
tzikc1db72652016-07-08 09:42:38309 using Type = R(Args...);
310};
311
312// Evaluated to RunType of the given callable type.
313// Example:
314// auto f = [](int, char*) { return 0.1; };
315// ExtractCallableRunType<decltype(f)>
316// is evaluated to
317// double(int, char*);
318template <typename Callable>
319using ExtractCallableRunType =
320 typename ExtractCallableRunTypeImpl<Callable>::Type;
321
tzikf98654b2017-12-02 03:28:58322// IsCallableObject<Functor> is std::true_type if |Functor| has operator().
323// Otherwise, it's std::false_type.
tzikc1db72652016-07-08 09:42:38324// Example:
tzikf98654b2017-12-02 03:28:58325// IsCallableObject<void(*)()>::value is false.
tzikc1db72652016-07-08 09:42:38326//
327// struct Foo {};
tzikf98654b2017-12-02 03:28:58328// IsCallableObject<void(Foo::*)()>::value is false.
tzikc1db72652016-07-08 09:42:38329//
330// int i = 0;
tzikf98654b2017-12-02 03:28:58331// auto f = [i]() {};
332// IsCallableObject<decltype(f)>::value is false.
tzikc1db72652016-07-08 09:42:38333template <typename Functor, typename SFINAE = void>
tzikf98654b2017-12-02 03:28:58334struct IsCallableObject : std::false_type {};
tzikc1db72652016-07-08 09:42:38335
336template <typename Callable>
tzikf98654b2017-12-02 03:28:58337struct IsCallableObject<Callable, void_t<decltype(&Callable::operator())>>
338 : std::true_type {};
tzikc1db72652016-07-08 09:42:38339
tzik401dd3672014-11-26 07:54:58340// HasRefCountedTypeAsRawPtr selects true_type when any of the |Args| is a raw
341// pointer to a RefCounted type.
342// Implementation note: This non-specialized case handles zero-arity case only.
343// Non-zero-arity cases should be handled by the specialization below.
344template <typename... Args>
tzik403cb6c2016-03-10 07:17:25345struct HasRefCountedTypeAsRawPtr : std::false_type {};
tzik401dd3672014-11-26 07:54:58346
347// Implementation note: Select true_type if the first parameter is a raw pointer
348// to a RefCounted type. Otherwise, skip the first parameter and check rest of
349// parameters recursively.
350template <typename T, typename... Args>
351struct HasRefCountedTypeAsRawPtr<T, Args...>
Jeremy Roman35a317432017-08-16 22:20:53352 : std::conditional_t<NeedsScopedRefptrButGetsRawPtr<T>::value,
353 std::true_type,
354 HasRefCountedTypeAsRawPtr<Args...>> {};
tzik401dd3672014-11-26 07:54:58355
[email protected]7296f2762011-11-21 19:23:44356// ForceVoidReturn<>
357//
358// Set of templates that support forcing the function return type to void.
359template <typename Sig>
360struct ForceVoidReturn;
361
tzikc82149922014-11-20 10:09:45362template <typename R, typename... Args>
363struct ForceVoidReturn<R(Args...)> {
tzik99de02b2016-07-01 05:54:12364 using RunType = void(Args...);
[email protected]fccef1552011-11-28 22:13:54365};
366
[email protected]7296f2762011-11-21 19:23:44367// FunctorTraits<>
368//
369// See description at top of file.
tzik6c92eab2016-11-25 15:56:36370template <typename Functor, typename SFINAE>
tzik99de02b2016-07-01 05:54:12371struct FunctorTraits;
372
tzikf98654b2017-12-02 03:28:58373// For empty callable types.
tzikc1db72652016-07-08 09:42:38374// This specialization is intended to allow binding captureless lambdas by
tzikf98654b2017-12-02 03:28:58375// base::Bind(), based on the fact that captureless lambdas are empty while
376// capturing lambdas are not. This also allows any functors as far as it's an
377// empty class.
378// Example:
379//
380// // Captureless lambdas are allowed.
381// []() {return 42;};
382//
383// // Capturing lambdas are *not* allowed.
384// int x;
385// [x]() {return x;};
386//
387// // Any empty class with operator() is allowed.
388// struct Foo {
389// void operator()() const {}
390// // No non-static member variable and no virtual functions.
391// };
tzikc1db72652016-07-08 09:42:38392template <typename Functor>
Jeremy Roman35a317432017-08-16 22:20:53393struct FunctorTraits<Functor,
tzikf98654b2017-12-02 03:28:58394 std::enable_if_t<IsCallableObject<Functor>::value &&
395 std::is_empty<Functor>::value>> {
tzikc1db72652016-07-08 09:42:38396 using RunType = ExtractCallableRunType<Functor>;
397 static constexpr bool is_method = false;
398 static constexpr bool is_nullable = false;
399
tzikf98654b2017-12-02 03:28:58400 template <typename RunFunctor, typename... RunArgs>
401 static ExtractReturnType<RunType> Invoke(RunFunctor&& functor,
402 RunArgs&&... args) {
403 return std::forward<RunFunctor>(functor)(std::forward<RunArgs>(args)...);
tzikc1db72652016-07-08 09:42:38404 }
405};
406
tzik99de02b2016-07-01 05:54:12407// For functions.
408template <typename R, typename... Args>
409struct FunctorTraits<R (*)(Args...)> {
410 using RunType = R(Args...);
411 static constexpr bool is_method = false;
tzikc1db72652016-07-08 09:42:38412 static constexpr bool is_nullable = true;
tzik99de02b2016-07-01 05:54:12413
tzikd58a89232018-04-26 04:29:53414 template <typename Function, typename... RunArgs>
415 static R Invoke(Function&& function, RunArgs&&... args) {
416 return std::forward<Function>(function)(std::forward<RunArgs>(args)...);
tzik99de02b2016-07-01 05:54:12417 }
[email protected]7296f2762011-11-21 19:23:44418};
419
tzik99de02b2016-07-01 05:54:12420#if defined(OS_WIN) && !defined(ARCH_CPU_X86_64)
421
422// For functions.
423template <typename R, typename... Args>
424struct FunctorTraits<R(__stdcall*)(Args...)> {
425 using RunType = R(Args...);
426 static constexpr bool is_method = false;
tzikc1db72652016-07-08 09:42:38427 static constexpr bool is_nullable = true;
tzik99de02b2016-07-01 05:54:12428
429 template <typename... RunArgs>
430 static R Invoke(R(__stdcall* function)(Args...), RunArgs&&... args) {
431 return function(std::forward<RunArgs>(args)...);
432 }
433};
434
435// For functions.
436template <typename R, typename... Args>
437struct FunctorTraits<R(__fastcall*)(Args...)> {
438 using RunType = R(Args...);
439 static constexpr bool is_method = false;
tzikc1db72652016-07-08 09:42:38440 static constexpr bool is_nullable = true;
tzik99de02b2016-07-01 05:54:12441
442 template <typename... RunArgs>
443 static R Invoke(R(__fastcall* function)(Args...), RunArgs&&... args) {
444 return function(std::forward<RunArgs>(args)...);
445 }
446};
447
448#endif // defined(OS_WIN) && !defined(ARCH_CPU_X86_64)
449
Sylvain Defresneec3270c2018-05-31 17:19:15450#if defined(OS_MACOSX)
451
452// Support for Objective-C blocks. There are two implementation depending
453// on whether Automated Reference Counting (ARC) is enabled. When ARC is
454// enabled, then the block itself can be bound as the compiler will ensure
455// its lifetime will be correctly managed. Otherwise, require the block to
456// be wrapped in a base::mac::ScopedBlock (via base::RetainBlock) that will
457// correctly manage the block lifetime.
458//
459// The two implementation ensure that the One Definition Rule (ODR) is not
460// broken (it is not possible to write a template base::RetainBlock that would
461// work correctly both with ARC enabled and disabled).
462
463#if HAS_FEATURE(objc_arc)
464
465template <typename R, typename... Args>
466struct FunctorTraits<R (^)(Args...)> {
467 using RunType = R(Args...);
468 static constexpr bool is_method = false;
469 static constexpr bool is_nullable = true;
470
471 template <typename BlockType, typename... RunArgs>
472 static R Invoke(BlockType&& block, RunArgs&&... args) {
473 // According to LLVM documentation (§ 6.3), "local variables of automatic
474 // storage duration do not have precise lifetime." Use objc_precise_lifetime
475 // to ensure that the Objective-C block is not deallocated until it has
476 // finished executing even if the Callback<> is destroyed during the block
477 // execution.
478 // https://clang.llvm.org/docs/AutomaticReferenceCounting.html#precise-lifetime-semantics
479 __attribute__((objc_precise_lifetime)) R (^scoped_block)(Args...) = block;
480 return scoped_block(std::forward<RunArgs>(args)...);
481 }
482};
483
484#else // HAS_FEATURE(objc_arc)
485
486template <typename R, typename... Args>
487struct FunctorTraits<base::mac::ScopedBlock<R (^)(Args...)>> {
488 using RunType = R(Args...);
489 static constexpr bool is_method = false;
490 static constexpr bool is_nullable = true;
491
492 template <typename BlockType, typename... RunArgs>
493 static R Invoke(BlockType&& block, RunArgs&&... args) {
494 // Copy the block to ensure that the Objective-C block is not deallocated
495 // until it has finished executing even if the Callback<> is destroyed
496 // during the block execution.
497 base::mac::ScopedBlock<R (^)(Args...)> scoped_block(block);
498 return scoped_block.get()(std::forward<RunArgs>(args)...);
499 }
500};
501
502#endif // HAS_FEATURE(objc_arc)
503#endif // defined(OS_MACOSX)
504
tzik99de02b2016-07-01 05:54:12505// For methods.
506template <typename R, typename Receiver, typename... Args>
507struct FunctorTraits<R (Receiver::*)(Args...)> {
508 using RunType = R(Receiver*, Args...);
509 static constexpr bool is_method = true;
tzikc1db72652016-07-08 09:42:38510 static constexpr bool is_nullable = true;
tzik99de02b2016-07-01 05:54:12511
tzikd58a89232018-04-26 04:29:53512 template <typename Method, typename ReceiverPtr, typename... RunArgs>
513 static R Invoke(Method method,
tzik99de02b2016-07-01 05:54:12514 ReceiverPtr&& receiver_ptr,
515 RunArgs&&... args) {
tzik75851f42017-06-14 06:57:01516 return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...);
tzik99de02b2016-07-01 05:54:12517 }
518};
519
520// For const methods.
521template <typename R, typename Receiver, typename... Args>
522struct FunctorTraits<R (Receiver::*)(Args...) const> {
523 using RunType = R(const Receiver*, Args...);
524 static constexpr bool is_method = true;
tzikc1db72652016-07-08 09:42:38525 static constexpr bool is_nullable = true;
tzik99de02b2016-07-01 05:54:12526
tzikd58a89232018-04-26 04:29:53527 template <typename Method, typename ReceiverPtr, typename... RunArgs>
528 static R Invoke(Method method,
tzik99de02b2016-07-01 05:54:12529 ReceiverPtr&& receiver_ptr,
530 RunArgs&&... args) {
tzik75851f42017-06-14 06:57:01531 return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...);
tzik99de02b2016-07-01 05:54:12532 }
533};
534
tzikd58a89232018-04-26 04:29:53535#ifdef __cpp_noexcept_function_type
536// noexcept makes a distinct function type in C++17.
537// I.e. `void(*)()` and `void(*)() noexcept` are same in pre-C++17, and
538// different in C++17.
539template <typename R, typename... Args>
540struct FunctorTraits<R (*)(Args...) noexcept> : FunctorTraits<R (*)(Args...)> {
541};
542
543template <typename R, typename Receiver, typename... Args>
544struct FunctorTraits<R (Receiver::*)(Args...) noexcept>
545 : FunctorTraits<R (Receiver::*)(Args...)> {};
546
547template <typename R, typename Receiver, typename... Args>
548struct FunctorTraits<R (Receiver::*)(Args...) const noexcept>
549 : FunctorTraits<R (Receiver::*)(Args...) const> {};
550#endif
551
tzik99de02b2016-07-01 05:54:12552// For IgnoreResults.
[email protected]7296f2762011-11-21 19:23:44553template <typename T>
tzik99de02b2016-07-01 05:54:12554struct FunctorTraits<IgnoreResultHelper<T>> : FunctorTraits<T> {
tzik3bc7779b2015-12-19 09:18:46555 using RunType =
tzik99de02b2016-07-01 05:54:12556 typename ForceVoidReturn<typename FunctorTraits<T>::RunType>::RunType;
557
558 template <typename IgnoreResultType, typename... RunArgs>
559 static void Invoke(IgnoreResultType&& ignore_result_helper,
560 RunArgs&&... args) {
tzikff54a5b152016-08-31 11:50:41561 FunctorTraits<T>::Invoke(
562 std::forward<IgnoreResultType>(ignore_result_helper).functor_,
563 std::forward<RunArgs>(args)...);
tzik99de02b2016-07-01 05:54:12564 }
[email protected]7296f2762011-11-21 19:23:44565};
566
tzikd4bb5b7d2017-08-28 19:08:52567// For OnceCallbacks.
568template <typename R, typename... Args>
569struct FunctorTraits<OnceCallback<R(Args...)>> {
570 using RunType = R(Args...);
571 static constexpr bool is_method = false;
572 static constexpr bool is_nullable = true;
573
574 template <typename CallbackType, typename... RunArgs>
575 static R Invoke(CallbackType&& callback, RunArgs&&... args) {
576 DCHECK(!callback.is_null());
577 return std::forward<CallbackType>(callback).Run(
578 std::forward<RunArgs>(args)...);
579 }
580};
581
582// For RepeatingCallbacks.
583template <typename R, typename... Args>
584struct FunctorTraits<RepeatingCallback<R(Args...)>> {
tzik99de02b2016-07-01 05:54:12585 using RunType = R(Args...);
586 static constexpr bool is_method = false;
tzikc1db72652016-07-08 09:42:38587 static constexpr bool is_nullable = true;
tzik99de02b2016-07-01 05:54:12588
589 template <typename CallbackType, typename... RunArgs>
590 static R Invoke(CallbackType&& callback, RunArgs&&... args) {
591 DCHECK(!callback.is_null());
592 return std::forward<CallbackType>(callback).Run(
593 std::forward<RunArgs>(args)...);
594 }
[email protected]7296f2762011-11-21 19:23:44595};
596
tzikae4202e2017-07-31 10:41:54597template <typename Functor>
Jeremy Roman35a317432017-08-16 22:20:53598using MakeFunctorTraits = FunctorTraits<std::decay_t<Functor>>;
tzikae4202e2017-07-31 10:41:54599
[email protected]7296f2762011-11-21 19:23:44600// InvokeHelper<>
601//
tzik99de02b2016-07-01 05:54:12602// There are 2 logical InvokeHelper<> specializations: normal, WeakCalls.
[email protected]7296f2762011-11-21 19:23:44603//
604// The normal type just calls the underlying runnable.
605//
tzik99de02b2016-07-01 05:54:12606// WeakCalls need special syntax that is applied to the first argument to check
607// if they should no-op themselves.
tzikee248722016-06-01 08:22:51608template <bool is_weak_call, typename ReturnType>
[email protected]7296f2762011-11-21 19:23:44609struct InvokeHelper;
610
tzikee248722016-06-01 08:22:51611template <typename ReturnType>
612struct InvokeHelper<false, ReturnType> {
tzik99de02b2016-07-01 05:54:12613 template <typename Functor, typename... RunArgs>
614 static inline ReturnType MakeItSo(Functor&& functor, RunArgs&&... args) {
tzikae4202e2017-07-31 10:41:54615 using Traits = MakeFunctorTraits<Functor>;
tzik99de02b2016-07-01 05:54:12616 return Traits::Invoke(std::forward<Functor>(functor),
617 std::forward<RunArgs>(args)...);
[email protected]7296f2762011-11-21 19:23:44618 }
619};
620
tzikee248722016-06-01 08:22:51621template <typename ReturnType>
622struct InvokeHelper<true, ReturnType> {
[email protected]7296f2762011-11-21 19:23:44623 // WeakCalls are only supported for functions with a void return type.
624 // Otherwise, the function result would be undefined if the the WeakPtr<>
625 // is invalidated.
tzik403cb6c2016-03-10 07:17:25626 static_assert(std::is_void<ReturnType>::value,
avi4ec0dff2015-11-24 14:26:24627 "weak_ptrs can only bind to methods without return values");
[email protected]c18b1052011-03-24 02:02:17628
tzik99de02b2016-07-01 05:54:12629 template <typename Functor, typename BoundWeakPtr, typename... RunArgs>
630 static inline void MakeItSo(Functor&& functor,
tzik33871d82016-07-14 12:12:06631 BoundWeakPtr&& weak_ptr,
tzik99de02b2016-07-01 05:54:12632 RunArgs&&... args) {
633 if (!weak_ptr)
634 return;
tzikae4202e2017-07-31 10:41:54635 using Traits = MakeFunctorTraits<Functor>;
tzik99de02b2016-07-01 05:54:12636 Traits::Invoke(std::forward<Functor>(functor),
637 std::forward<BoundWeakPtr>(weak_ptr),
638 std::forward<RunArgs>(args)...);
639 }
640};
[email protected]b38d3572011-02-15 01:27:38641
[email protected]7296f2762011-11-21 19:23:44642// Invoker<>
643//
644// See description at the top of the file.
tzikcaf1d84b2016-06-28 12:22:21645template <typename StorageType, typename UnboundRunType>
[email protected]7296f2762011-11-21 19:23:44646struct Invoker;
647
tzikcaf1d84b2016-06-28 12:22:21648template <typename StorageType, typename R, typename... UnboundArgs>
649struct Invoker<StorageType, R(UnboundArgs...)> {
Vladislav Kuzkokov6d208e12017-11-08 21:31:08650 static R RunOnce(BindStateBase* base,
tzik9bc6837b2018-06-28 20:20:47651 PassingType<UnboundArgs>... unbound_args) {
tzik27d1e312016-09-13 05:28:59652 // Local references to make debugger stepping easier. If in a debugger,
653 // you really want to warp ahead and step through the
654 // InvokeHelper<>::MakeItSo() call below.
655 StorageType* storage = static_cast<StorageType*>(base);
656 static constexpr size_t num_bound_args =
657 std::tuple_size<decltype(storage->bound_args_)>::value;
658 return RunImpl(std::move(storage->functor_),
659 std::move(storage->bound_args_),
Jeremy Roman84956fa2017-08-16 15:55:20660 std::make_index_sequence<num_bound_args>(),
tzik27d1e312016-09-13 05:28:59661 std::forward<UnboundArgs>(unbound_args)...);
662 }
663
tzik9bc6837b2018-06-28 20:20:47664 static R Run(BindStateBase* base, PassingType<UnboundArgs>... unbound_args) {
[email protected]7296f2762011-11-21 19:23:44665 // Local references to make debugger stepping easier. If in a debugger,
666 // you really want to warp ahead and step through the
667 // InvokeHelper<>::MakeItSo() call below.
tzikcaf1d84b2016-06-28 12:22:21668 const StorageType* storage = static_cast<StorageType*>(base);
669 static constexpr size_t num_bound_args =
670 std::tuple_size<decltype(storage->bound_args_)>::value;
Jeremy Roman84956fa2017-08-16 15:55:20671 return RunImpl(storage->functor_, storage->bound_args_,
672 std::make_index_sequence<num_bound_args>(),
tzikcaf1d84b2016-06-28 12:22:21673 std::forward<UnboundArgs>(unbound_args)...);
674 }
675
tzik99de02b2016-07-01 05:54:12676 private:
677 template <typename Functor, typename BoundArgsTuple, size_t... indices>
678 static inline R RunImpl(Functor&& functor,
tzikcaf1d84b2016-06-28 12:22:21679 BoundArgsTuple&& bound,
Jeremy Roman84956fa2017-08-16 15:55:20680 std::index_sequence<indices...>,
tzikcaf1d84b2016-06-28 12:22:21681 UnboundArgs&&... unbound_args) {
tzikae4202e2017-07-31 10:41:54682 static constexpr bool is_method = MakeFunctorTraits<Functor>::is_method;
tzikcaf1d84b2016-06-28 12:22:21683
Jeremy Roman35a317432017-08-16 22:20:53684 using DecayedArgsTuple = std::decay_t<BoundArgsTuple>;
tzikcaf1d84b2016-06-28 12:22:21685 static constexpr bool is_weak_call =
686 IsWeakMethod<is_method,
Jeremy Roman35a317432017-08-16 22:20:53687 std::tuple_element_t<indices, DecayedArgsTuple>...>();
tzikcaf1d84b2016-06-28 12:22:21688
tzikee248722016-06-01 08:22:51689 return InvokeHelper<is_weak_call, R>::MakeItSo(
tzik99de02b2016-07-01 05:54:12690 std::forward<Functor>(functor),
tzikf7c47572017-04-05 21:45:03691 Unwrap(std::get<indices>(std::forward<BoundArgsTuple>(bound)))...,
tzika43eff02016-03-09 05:46:05692 std::forward<UnboundArgs>(unbound_args)...);
[email protected]fccef1552011-11-28 22:13:54693 }
694};
695
tzikae4202e2017-07-31 10:41:54696// Extracts necessary type info from Functor and BoundArgs.
697// Used to implement MakeUnboundRunType, BindOnce and BindRepeating.
tzikcaf1d84b2016-06-28 12:22:21698template <typename Functor, typename... BoundArgs>
tzikae4202e2017-07-31 10:41:54699struct BindTypeHelper {
700 static constexpr size_t num_bounds = sizeof...(BoundArgs);
701 using FunctorTraits = MakeFunctorTraits<Functor>;
702
703 // Example:
704 // When Functor is `double (Foo::*)(int, const std::string&)`, and BoundArgs
705 // is a template pack of `Foo*` and `int16_t`:
706 // - RunType is `double(Foo*, int, const std::string&)`,
707 // - ReturnType is `double`,
708 // - RunParamsList is `TypeList<Foo*, int, const std::string&>`,
709 // - BoundParamsList is `TypeList<Foo*, int>`,
710 // - UnboundParamsList is `TypeList<const std::string&>`,
711 // - BoundArgsList is `TypeList<Foo*, int16_t>`,
712 // - UnboundRunType is `double(const std::string&)`.
713 using RunType = typename FunctorTraits::RunType;
tzikcaf1d84b2016-06-28 12:22:21714 using ReturnType = ExtractReturnType<RunType>;
tzikae4202e2017-07-31 10:41:54715
716 using RunParamsList = ExtractArgs<RunType>;
717 using BoundParamsList = TakeTypeListItem<num_bounds, RunParamsList>;
718 using UnboundParamsList = DropTypeListItem<num_bounds, RunParamsList>;
719
720 using BoundArgsList = TypeList<BoundArgs...>;
721
722 using UnboundRunType = MakeFunctionType<ReturnType, UnboundParamsList>;
tzikcaf1d84b2016-06-28 12:22:21723};
tzikae4202e2017-07-31 10:41:54724
tzikc1db72652016-07-08 09:42:38725template <typename Functor>
Jeremy Roman35a317432017-08-16 22:20:53726std::enable_if_t<FunctorTraits<Functor>::is_nullable, bool> IsNull(
727 const Functor& functor) {
tzikc1db72652016-07-08 09:42:38728 return !functor;
729}
730
731template <typename Functor>
Jeremy Roman35a317432017-08-16 22:20:53732std::enable_if_t<!FunctorTraits<Functor>::is_nullable, bool> IsNull(
733 const Functor&) {
tzikc1db72652016-07-08 09:42:38734 return false;
735}
tzikcaf1d84b2016-06-28 12:22:21736
tzik9697d49e2018-08-02 13:35:19737// Used by QueryCancellationTraits below.
tzik6c92eab2016-11-25 15:56:36738template <typename Functor, typename BoundArgsTuple, size_t... indices>
tzik9697d49e2018-08-02 13:35:19739bool QueryCancellationTraitsImpl(BindStateBase::CancellationQueryMode mode,
740 const Functor& functor,
741 const BoundArgsTuple& bound_args,
742 std::index_sequence<indices...>) {
743 switch (mode) {
744 case BindStateBase::IS_CANCELLED:
745 return CallbackCancellationTraits<Functor, BoundArgsTuple>::IsCancelled(
746 functor, std::get<indices>(bound_args)...);
747 case BindStateBase::MAYBE_VALID:
748 return CallbackCancellationTraits<Functor, BoundArgsTuple>::MaybeValid(
749 functor, std::get<indices>(bound_args)...);
750 }
751 NOTREACHED();
tzik6c92eab2016-11-25 15:56:36752}
tzik59aa6bb12016-09-08 10:58:53753
tzik6c92eab2016-11-25 15:56:36754// Relays |base| to corresponding CallbackCancellationTraits<>::Run(). Returns
755// true if the callback |base| represents is canceled.
756template <typename BindStateType>
tzik9697d49e2018-08-02 13:35:19757bool QueryCancellationTraits(const BindStateBase* base,
758 BindStateBase::CancellationQueryMode mode) {
tzik6c92eab2016-11-25 15:56:36759 const BindStateType* storage = static_cast<const BindStateType*>(base);
760 static constexpr size_t num_bound_args =
761 std::tuple_size<decltype(storage->bound_args_)>::value;
tzik9697d49e2018-08-02 13:35:19762 return QueryCancellationTraitsImpl(
763 mode, storage->functor_, storage->bound_args_,
Nicolas Ouellet-payeur40f8e9a2018-07-30 16:26:48764 std::make_index_sequence<num_bound_args>());
Nicolas Ouellet-payeur40f8e9a2018-07-30 16:26:48765}
766
[email protected]7296f2762011-11-21 19:23:44767// BindState<>
768//
tzik99de02b2016-07-01 05:54:12769// This stores all the state passed into Bind().
770template <typename Functor, typename... BoundArgs>
771struct BindState final : BindStateBase {
tzik1fdcca32016-09-14 07:15:00772 using IsCancellable = std::integral_constant<
tzik6c92eab2016-11-25 15:56:36773 bool,
774 CallbackCancellationTraits<Functor,
775 std::tuple<BoundArgs...>>::is_cancellable>;
tzik1fdcca32016-09-14 07:15:00776
tzikbfe66122016-07-08 14:14:01777 template <typename ForwardFunctor, typename... ForwardBoundArgs>
tzik1886c272016-09-08 05:45:38778 explicit BindState(BindStateBase::InvokeFuncStorage invoke_func,
779 ForwardFunctor&& functor,
780 ForwardBoundArgs&&... bound_args)
tzik6c92eab2016-11-25 15:56:36781 // IsCancellable is std::false_type if
782 // CallbackCancellationTraits<>::IsCancelled returns always false.
783 // Otherwise, it's std::true_type.
tzik1fdcca32016-09-14 07:15:00784 : BindState(IsCancellable{},
785 invoke_func,
786 std::forward<ForwardFunctor>(functor),
tzikf98654b2017-12-02 03:28:58787 std::forward<ForwardBoundArgs>(bound_args)...) {}
tzik1fdcca32016-09-14 07:15:00788
789 Functor functor_;
790 std::tuple<BoundArgs...> bound_args_;
791
792 private:
793 template <typename ForwardFunctor, typename... ForwardBoundArgs>
794 explicit BindState(std::true_type,
795 BindStateBase::InvokeFuncStorage invoke_func,
796 ForwardFunctor&& functor,
797 ForwardBoundArgs&&... bound_args)
tzik6c92eab2016-11-25 15:56:36798 : BindStateBase(invoke_func,
799 &Destroy,
tzik9697d49e2018-08-02 13:35:19800 &QueryCancellationTraits<BindState>),
tzikff54a5b152016-08-31 11:50:41801 functor_(std::forward<ForwardFunctor>(functor)),
tzik99de02b2016-07-01 05:54:12802 bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) {
tzikc1db72652016-07-08 09:42:38803 DCHECK(!IsNull(functor_));
tzik99de02b2016-07-01 05:54:12804 }
[email protected]7296f2762011-11-21 19:23:44805
tzik1fdcca32016-09-14 07:15:00806 template <typename ForwardFunctor, typename... ForwardBoundArgs>
807 explicit BindState(std::false_type,
808 BindStateBase::InvokeFuncStorage invoke_func,
809 ForwardFunctor&& functor,
810 ForwardBoundArgs&&... bound_args)
811 : BindStateBase(invoke_func, &Destroy),
812 functor_(std::forward<ForwardFunctor>(functor)),
813 bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) {
814 DCHECK(!IsNull(functor_));
815 }
dmichael7d09007e2014-12-18 22:30:11816
Chris Watkins091d6292017-12-13 04:25:58817 ~BindState() = default;
taptede7e804c2015-05-14 08:03:32818
tzik30e0c312016-09-21 08:06:54819 static void Destroy(const BindStateBase* self) {
820 delete static_cast<const BindState*>(self);
taptede7e804c2015-05-14 08:03:32821 }
[email protected]fccef1552011-11-28 22:13:54822};
823
tzik99de02b2016-07-01 05:54:12824// Used to implement MakeBindStateType.
825template <bool is_method, typename Functor, typename... BoundArgs>
826struct MakeBindStateTypeImpl;
827
828template <typename Functor, typename... BoundArgs>
829struct MakeBindStateTypeImpl<false, Functor, BoundArgs...> {
Jeremy Roman35a317432017-08-16 22:20:53830 static_assert(!HasRefCountedTypeAsRawPtr<std::decay_t<BoundArgs>...>::value,
tzik99de02b2016-07-01 05:54:12831 "A parameter is a refcounted type and needs scoped_refptr.");
Jeremy Roman35a317432017-08-16 22:20:53832 using Type = BindState<std::decay_t<Functor>, std::decay_t<BoundArgs>...>;
tzik99de02b2016-07-01 05:54:12833};
834
835template <typename Functor>
836struct MakeBindStateTypeImpl<true, Functor> {
Jeremy Roman35a317432017-08-16 22:20:53837 using Type = BindState<std::decay_t<Functor>>;
tzik99de02b2016-07-01 05:54:12838};
839
840template <typename Functor, typename Receiver, typename... BoundArgs>
841struct MakeBindStateTypeImpl<true, Functor, Receiver, BoundArgs...> {
tzik99de02b2016-07-01 05:54:12842 private:
Jeremy Roman35a317432017-08-16 22:20:53843 using DecayedReceiver = std::decay_t<Receiver>;
tzik99de02b2016-07-01 05:54:12844
tzik4625ac612018-02-28 09:43:55845 static_assert(!std::is_array<std::remove_reference_t<Receiver>>::value,
846 "First bound argument to a method cannot be an array.");
847 static_assert(
848 !std::is_pointer<DecayedReceiver>::value ||
849 IsRefCountedType<std::remove_pointer_t<DecayedReceiver>>::value,
850 "Receivers may not be raw pointers. If using a raw pointer here is safe"
851 " and has no lifetime concerns, use base::Unretained() and document why"
852 " it's safe.");
853 static_assert(!HasRefCountedTypeAsRawPtr<std::decay_t<BoundArgs>...>::value,
854 "A parameter is a refcounted type and needs scoped_refptr.");
855
tzik99de02b2016-07-01 05:54:12856 public:
857 using Type = BindState<
Jeremy Roman35a317432017-08-16 22:20:53858 std::decay_t<Functor>,
859 std::conditional_t<std::is_pointer<DecayedReceiver>::value,
860 scoped_refptr<std::remove_pointer_t<DecayedReceiver>>,
861 DecayedReceiver>,
862 std::decay_t<BoundArgs>...>;
tzik99de02b2016-07-01 05:54:12863};
864
865template <typename Functor, typename... BoundArgs>
tzikae4202e2017-07-31 10:41:54866using MakeBindStateType =
867 typename MakeBindStateTypeImpl<MakeFunctorTraits<Functor>::is_method,
868 Functor,
869 BoundArgs...>::Type;
tzik99de02b2016-07-01 05:54:12870
[email protected]b38d3572011-02-15 01:27:38871} // namespace internal
tzikcaf1d84b2016-06-28 12:22:21872
Peter Kastinga85265e32018-02-15 08:30:23873// An injection point to control |this| pointer behavior on a method invocation.
874// If IsWeakReceiver<> is true_type for |T| and |T| is used for a receiver of a
875// method, base::Bind cancels the method invocation if the receiver is tested as
876// false.
877// E.g. Foo::bar() is not called:
878// struct Foo : base::SupportsWeakPtr<Foo> {
879// void bar() {}
880// };
881//
882// WeakPtr<Foo> oo = nullptr;
883// base::Bind(&Foo::bar, oo).Run();
884template <typename T>
885struct IsWeakReceiver : std::false_type {};
886
887template <typename T>
888struct IsWeakReceiver<internal::ConstRefWrapper<T>> : IsWeakReceiver<T> {};
889
890template <typename T>
891struct IsWeakReceiver<WeakPtr<T>> : std::true_type {};
892
893// An injection point to control how bound objects passed to the target
894// function. BindUnwrapTraits<>::Unwrap() is called for each bound objects right
895// before the target function is invoked.
896template <typename>
897struct BindUnwrapTraits {
898 template <typename T>
899 static T&& Unwrap(T&& o) {
900 return std::forward<T>(o);
901 }
902};
903
904template <typename T>
905struct BindUnwrapTraits<internal::UnretainedWrapper<T>> {
906 static T* Unwrap(const internal::UnretainedWrapper<T>& o) { return o.get(); }
907};
908
909template <typename T>
910struct BindUnwrapTraits<internal::ConstRefWrapper<T>> {
911 static const T& Unwrap(const internal::ConstRefWrapper<T>& o) {
912 return o.get();
913 }
914};
915
916template <typename T>
917struct BindUnwrapTraits<internal::RetainedRefWrapper<T>> {
918 static T* Unwrap(const internal::RetainedRefWrapper<T>& o) { return o.get(); }
919};
920
921template <typename T>
922struct BindUnwrapTraits<internal::OwnedWrapper<T>> {
923 static T* Unwrap(const internal::OwnedWrapper<T>& o) { return o.get(); }
924};
925
926template <typename T>
927struct BindUnwrapTraits<internal::PassedWrapper<T>> {
928 static T Unwrap(const internal::PassedWrapper<T>& o) { return o.Take(); }
929};
930
tzikc44f8102018-07-24 09:49:19931#if defined(OS_WIN)
932template <typename T>
933struct BindUnwrapTraits<Microsoft::WRL::ComPtr<T>> {
934 static T* Unwrap(const Microsoft::WRL::ComPtr<T>& ptr) { return ptr.Get(); }
935};
936#endif
937
Peter Kastinga85265e32018-02-15 08:30:23938// CallbackCancellationTraits allows customization of Callback's cancellation
939// semantics. By default, callbacks are not cancellable. A specialization should
940// set is_cancellable = true and implement an IsCancelled() that returns if the
941// callback should be cancelled.
942template <typename Functor, typename BoundArgsTuple, typename SFINAE>
943struct CallbackCancellationTraits {
944 static constexpr bool is_cancellable = false;
945};
946
947// Specialization for method bound to weak pointer receiver.
948template <typename Functor, typename... BoundArgs>
949struct CallbackCancellationTraits<
950 Functor,
951 std::tuple<BoundArgs...>,
952 std::enable_if_t<
953 internal::IsWeakMethod<internal::FunctorTraits<Functor>::is_method,
954 BoundArgs...>::value>> {
955 static constexpr bool is_cancellable = true;
956
957 template <typename Receiver, typename... Args>
958 static bool IsCancelled(const Functor&,
959 const Receiver& receiver,
960 const Args&...) {
961 return !receiver;
962 }
Nicolas Ouellet-payeur40f8e9a2018-07-30 16:26:48963
964 template <typename Receiver, typename... Args>
965 static bool MaybeValid(const Functor&,
966 const Receiver& receiver,
967 const Args&...) {
968 return receiver.MaybeValid();
969 }
Peter Kastinga85265e32018-02-15 08:30:23970};
971
972// Specialization for a nested bind.
973template <typename Signature, typename... BoundArgs>
974struct CallbackCancellationTraits<OnceCallback<Signature>,
975 std::tuple<BoundArgs...>> {
976 static constexpr bool is_cancellable = true;
977
978 template <typename Functor>
979 static bool IsCancelled(const Functor& functor, const BoundArgs&...) {
980 return functor.IsCancelled();
981 }
Nicolas Ouellet-payeur40f8e9a2018-07-30 16:26:48982
983 template <typename Functor>
984 static bool MaybeValid(const Functor& functor, const BoundArgs&...) {
985 return functor.MaybeValid();
986 }
Peter Kastinga85265e32018-02-15 08:30:23987};
988
989template <typename Signature, typename... BoundArgs>
990struct CallbackCancellationTraits<RepeatingCallback<Signature>,
991 std::tuple<BoundArgs...>> {
992 static constexpr bool is_cancellable = true;
993
994 template <typename Functor>
995 static bool IsCancelled(const Functor& functor, const BoundArgs&...) {
996 return functor.IsCancelled();
997 }
Nicolas Ouellet-payeur40f8e9a2018-07-30 16:26:48998
999 template <typename Functor>
1000 static bool MaybeValid(const Functor& functor, const BoundArgs&...) {
1001 return functor.MaybeValid();
1002 }
Peter Kastinga85265e32018-02-15 08:30:231003};
1004
tzikcaf1d84b2016-06-28 12:22:211005// Returns a RunType of bound functor.
1006// E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C).
1007template <typename Functor, typename... BoundArgs>
1008using MakeUnboundRunType =
tzikae4202e2017-07-31 10:41:541009 typename internal::BindTypeHelper<Functor, BoundArgs...>::UnboundRunType;
tzikcaf1d84b2016-06-28 12:22:211010
[email protected]b38d3572011-02-15 01:27:381011} // namespace base
1012
1013#endif // BASE_BIND_INTERNAL_H_