blob: 185e00dc56949baf9aa3e381ce142b5011d575b6 [file] [log] [blame]
ajwong@chromium.orge2cca632011-02-15 10:27:38 +09001// 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_
ajwong@chromium.orge2cca632011-02-15 10:27:38 +09007
avia6a6a682015-12-27 07:15:14 +09008#include <stddef.h>
9
vmpstr495da402015-11-18 17:43:26 +090010#include <type_traits>
Jeremy Roman773e7f22017-08-17 00:55:20 +090011#include <utility>
vmpstr495da402015-11-18 17:43:26 +090012
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090013#include "base/bind_helpers.h"
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090014#include "base/callback_internal.h"
ajwong@chromium.org27e56852011-10-01 15:31:41 +090015#include "base/memory/raw_scoped_refptr_mismatch_checker.h"
ajwong@chromium.orgc711b822011-05-17 07:35:14 +090016#include "base/memory/weak_ptr.h"
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090017#include "base/template_util.h"
ajwong@chromium.orgcb175342011-02-27 10:25:59 +090018#include "build/build_config.h"
19
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090020namespace base {
21namespace internal {
22
brettw@chromium.org11e3bfd2012-07-13 05:06:40 +090023// See base/callback.h for user documentation.
24//
25//
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090026// CONCEPTS:
tzik9f27e1f2016-07-01 14:54:12 +090027// Functor -- A movable type representing something that should be called.
28// All function pointers and Callback<> are functors even if the
29// invocation syntax differs.
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090030// RunType -- A function type (as opposed to function _pointer_ type) for
tzik9f27e1f2016-07-01 14:54:12 +090031// a Callback<>::Run(). Usually just a convenience typedef.
tzikb0632222015-12-15 15:41:49 +090032// (Bound)Args -- A set of types that stores the arguments.
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090033//
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090034// Types:
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090035// ForceVoidReturn<> -- Helper class for translating function signatures to
36// equivalent forms with a "void" return type.
tzik9f27e1f2016-07-01 14:54:12 +090037// FunctorTraits<> -- Type traits used to determine the correct RunType and
38// invocation manner for a Functor. This is where function
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090039// signature adapters are applied.
tzik9f27e1f2016-07-01 14:54:12 +090040// InvokeHelper<> -- Take a Functor + arguments and actully invokes it.
tzik07e99402015-02-06 04:11:26 +090041// Handle the differing syntaxes needed for WeakPtr<>
tzik9f27e1f2016-07-01 14:54:12 +090042// support. This is separate from Invoker to avoid creating
43// multiple version of Invoker<>.
44// Invoker<> -- Unwraps the curried parameters and executes the Functor.
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090045// BindState<> -- Stores the curried parameters, and is the main entry point
tzik9f27e1f2016-07-01 14:54:12 +090046// into the Bind() system.
ajwong@chromium.orge0648232011-02-19 09:52:15 +090047
tzik31d3fae2016-07-08 18:42:38 +090048template <typename Callable,
49 typename Signature = decltype(&Callable::operator())>
50struct ExtractCallableRunTypeImpl;
51
52template <typename Callable, typename R, typename... Args>
53struct ExtractCallableRunTypeImpl<Callable, R(Callable::*)(Args...) const> {
54 using Type = R(Args...);
55};
56
57// Evaluated to RunType of the given callable type.
58// Example:
59// auto f = [](int, char*) { return 0.1; };
60// ExtractCallableRunType<decltype(f)>
61// is evaluated to
62// double(int, char*);
63template <typename Callable>
64using ExtractCallableRunType =
65 typename ExtractCallableRunTypeImpl<Callable>::Type;
66
67// IsConvertibleToRunType<Functor> is std::true_type if |Functor| has operator()
68// and convertible to the corresponding function pointer. Otherwise, it's
69// std::false_type.
70// Example:
71// IsConvertibleToRunType<void(*)()>::value is false.
72//
73// struct Foo {};
74// IsConvertibleToRunType<void(Foo::*)()>::value is false.
75//
76// auto f = []() {};
77// IsConvertibleToRunType<decltype(f)>::value is true.
78//
79// int i = 0;
80// auto g = [i]() {};
81// IsConvertibleToRunType<decltype(g)>::value is false.
82template <typename Functor, typename SFINAE = void>
83struct IsConvertibleToRunType : std::false_type {};
84
85template <typename Callable>
86struct IsConvertibleToRunType<Callable, void_t<decltype(&Callable::operator())>>
87 : std::is_convertible<Callable, ExtractCallableRunType<Callable>*> {};
88
tzik8df08a52014-11-26 16:54:58 +090089// HasRefCountedTypeAsRawPtr selects true_type when any of the |Args| is a raw
90// pointer to a RefCounted type.
91// Implementation note: This non-specialized case handles zero-arity case only.
92// Non-zero-arity cases should be handled by the specialization below.
93template <typename... Args>
tzik56bd7652016-03-10 16:17:25 +090094struct HasRefCountedTypeAsRawPtr : std::false_type {};
tzik8df08a52014-11-26 16:54:58 +090095
96// Implementation note: Select true_type if the first parameter is a raw pointer
97// to a RefCounted type. Otherwise, skip the first parameter and check rest of
98// parameters recursively.
99template <typename T, typename... Args>
100struct HasRefCountedTypeAsRawPtr<T, Args...>
vmpstr495da402015-11-18 17:43:26 +0900101 : std::conditional<NeedsScopedRefptrButGetsRawPtr<T>::value,
tzik56bd7652016-03-10 16:17:25 +0900102 std::true_type,
vmpstr495da402015-11-18 17:43:26 +0900103 HasRefCountedTypeAsRawPtr<Args...>>::type {};
tzik8df08a52014-11-26 16:54:58 +0900104
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900105// ForceVoidReturn<>
106//
107// Set of templates that support forcing the function return type to void.
108template <typename Sig>
109struct ForceVoidReturn;
110
tzik012481a2014-11-20 19:09:45 +0900111template <typename R, typename... Args>
112struct ForceVoidReturn<R(Args...)> {
tzik9f27e1f2016-07-01 14:54:12 +0900113 using RunType = void(Args...);
ajwong@chromium.org6f015bd2011-11-29 07:13:54 +0900114};
115
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900116// FunctorTraits<>
117//
118// See description at top of file.
tzike6e61952016-11-26 00:56:36 +0900119template <typename Functor, typename SFINAE>
tzik9f27e1f2016-07-01 14:54:12 +0900120struct FunctorTraits;
121
tzik31d3fae2016-07-08 18:42:38 +0900122// For a callable type that is convertible to the corresponding function type.
123// This specialization is intended to allow binding captureless lambdas by
124// base::Bind(), based on the fact that captureless lambdas can be convertible
125// to the function type while capturing lambdas can't.
126template <typename Functor>
127struct FunctorTraits<
128 Functor,
129 typename std::enable_if<IsConvertibleToRunType<Functor>::value>::type> {
130 using RunType = ExtractCallableRunType<Functor>;
131 static constexpr bool is_method = false;
132 static constexpr bool is_nullable = false;
133
134 template <typename... RunArgs>
135 static ExtractReturnType<RunType>
136 Invoke(const Functor& functor, RunArgs&&... args) {
137 return functor(std::forward<RunArgs>(args)...);
138 }
139};
140
tzik9f27e1f2016-07-01 14:54:12 +0900141// For functions.
142template <typename R, typename... Args>
143struct FunctorTraits<R (*)(Args...)> {
144 using RunType = R(Args...);
145 static constexpr bool is_method = false;
tzik31d3fae2016-07-08 18:42:38 +0900146 static constexpr bool is_nullable = true;
tzik9f27e1f2016-07-01 14:54:12 +0900147
148 template <typename... RunArgs>
149 static R Invoke(R (*function)(Args...), RunArgs&&... args) {
150 return function(std::forward<RunArgs>(args)...);
151 }
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900152};
153
tzik9f27e1f2016-07-01 14:54:12 +0900154#if defined(OS_WIN) && !defined(ARCH_CPU_X86_64)
155
156// For functions.
157template <typename R, typename... Args>
158struct FunctorTraits<R(__stdcall*)(Args...)> {
159 using RunType = R(Args...);
160 static constexpr bool is_method = false;
tzik31d3fae2016-07-08 18:42:38 +0900161 static constexpr bool is_nullable = true;
tzik9f27e1f2016-07-01 14:54:12 +0900162
163 template <typename... RunArgs>
164 static R Invoke(R(__stdcall* function)(Args...), RunArgs&&... args) {
165 return function(std::forward<RunArgs>(args)...);
166 }
167};
168
169// For functions.
170template <typename R, typename... Args>
171struct FunctorTraits<R(__fastcall*)(Args...)> {
172 using RunType = R(Args...);
173 static constexpr bool is_method = false;
tzik31d3fae2016-07-08 18:42:38 +0900174 static constexpr bool is_nullable = true;
tzik9f27e1f2016-07-01 14:54:12 +0900175
176 template <typename... RunArgs>
177 static R Invoke(R(__fastcall* function)(Args...), RunArgs&&... args) {
178 return function(std::forward<RunArgs>(args)...);
179 }
180};
181
182#endif // defined(OS_WIN) && !defined(ARCH_CPU_X86_64)
183
184// For methods.
185template <typename R, typename Receiver, typename... Args>
186struct FunctorTraits<R (Receiver::*)(Args...)> {
187 using RunType = R(Receiver*, Args...);
188 static constexpr bool is_method = true;
tzik31d3fae2016-07-08 18:42:38 +0900189 static constexpr bool is_nullable = true;
tzik9f27e1f2016-07-01 14:54:12 +0900190
191 template <typename ReceiverPtr, typename... RunArgs>
192 static R Invoke(R (Receiver::*method)(Args...),
193 ReceiverPtr&& receiver_ptr,
194 RunArgs&&... args) {
tzik8bcf5ef2017-06-14 15:57:01 +0900195 return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...);
tzik9f27e1f2016-07-01 14:54:12 +0900196 }
197};
198
199// For const methods.
200template <typename R, typename Receiver, typename... Args>
201struct FunctorTraits<R (Receiver::*)(Args...) const> {
202 using RunType = R(const Receiver*, Args...);
203 static constexpr bool is_method = true;
tzik31d3fae2016-07-08 18:42:38 +0900204 static constexpr bool is_nullable = true;
tzik9f27e1f2016-07-01 14:54:12 +0900205
206 template <typename ReceiverPtr, typename... RunArgs>
207 static R Invoke(R (Receiver::*method)(Args...) const,
208 ReceiverPtr&& receiver_ptr,
209 RunArgs&&... args) {
tzik8bcf5ef2017-06-14 15:57:01 +0900210 return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...);
tzik9f27e1f2016-07-01 14:54:12 +0900211 }
212};
213
214// For IgnoreResults.
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900215template <typename T>
tzik9f27e1f2016-07-01 14:54:12 +0900216struct FunctorTraits<IgnoreResultHelper<T>> : FunctorTraits<T> {
tzik260fab52015-12-19 18:18:46 +0900217 using RunType =
tzik9f27e1f2016-07-01 14:54:12 +0900218 typename ForceVoidReturn<typename FunctorTraits<T>::RunType>::RunType;
219
220 template <typename IgnoreResultType, typename... RunArgs>
221 static void Invoke(IgnoreResultType&& ignore_result_helper,
222 RunArgs&&... args) {
tzike467aa42016-08-31 20:50:41 +0900223 FunctorTraits<T>::Invoke(
224 std::forward<IgnoreResultType>(ignore_result_helper).functor_,
225 std::forward<RunArgs>(args)...);
tzik9f27e1f2016-07-01 14:54:12 +0900226 }
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900227};
228
tzik9f27e1f2016-07-01 14:54:12 +0900229// For Callbacks.
tzikc60a8122016-09-13 14:28:59 +0900230template <typename R, typename... Args,
231 CopyMode copy_mode, RepeatMode repeat_mode>
232struct FunctorTraits<Callback<R(Args...), copy_mode, repeat_mode>> {
tzik9f27e1f2016-07-01 14:54:12 +0900233 using RunType = R(Args...);
234 static constexpr bool is_method = false;
tzik31d3fae2016-07-08 18:42:38 +0900235 static constexpr bool is_nullable = true;
tzik9f27e1f2016-07-01 14:54:12 +0900236
237 template <typename CallbackType, typename... RunArgs>
238 static R Invoke(CallbackType&& callback, RunArgs&&... args) {
239 DCHECK(!callback.is_null());
240 return std::forward<CallbackType>(callback).Run(
241 std::forward<RunArgs>(args)...);
242 }
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900243};
244
tzik103f4732017-07-31 19:41:54 +0900245template <typename Functor>
246using MakeFunctorTraits = FunctorTraits<typename std::decay<Functor>::type>;
247
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900248// InvokeHelper<>
249//
tzik9f27e1f2016-07-01 14:54:12 +0900250// There are 2 logical InvokeHelper<> specializations: normal, WeakCalls.
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900251//
252// The normal type just calls the underlying runnable.
253//
tzik9f27e1f2016-07-01 14:54:12 +0900254// WeakCalls need special syntax that is applied to the first argument to check
255// if they should no-op themselves.
tzike1ad6f62016-06-01 17:22:51 +0900256template <bool is_weak_call, typename ReturnType>
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900257struct InvokeHelper;
258
tzike1ad6f62016-06-01 17:22:51 +0900259template <typename ReturnType>
260struct InvokeHelper<false, ReturnType> {
tzik9f27e1f2016-07-01 14:54:12 +0900261 template <typename Functor, typename... RunArgs>
262 static inline ReturnType MakeItSo(Functor&& functor, RunArgs&&... args) {
tzik103f4732017-07-31 19:41:54 +0900263 using Traits = MakeFunctorTraits<Functor>;
tzik9f27e1f2016-07-01 14:54:12 +0900264 return Traits::Invoke(std::forward<Functor>(functor),
265 std::forward<RunArgs>(args)...);
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900266 }
267};
268
tzike1ad6f62016-06-01 17:22:51 +0900269template <typename ReturnType>
270struct InvokeHelper<true, ReturnType> {
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900271 // WeakCalls are only supported for functions with a void return type.
272 // Otherwise, the function result would be undefined if the the WeakPtr<>
273 // is invalidated.
tzik56bd7652016-03-10 16:17:25 +0900274 static_assert(std::is_void<ReturnType>::value,
avi486c61f2015-11-24 23:26:24 +0900275 "weak_ptrs can only bind to methods without return values");
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900276
tzik9f27e1f2016-07-01 14:54:12 +0900277 template <typename Functor, typename BoundWeakPtr, typename... RunArgs>
278 static inline void MakeItSo(Functor&& functor,
tzik6bb5cb42016-07-14 21:12:06 +0900279 BoundWeakPtr&& weak_ptr,
tzik9f27e1f2016-07-01 14:54:12 +0900280 RunArgs&&... args) {
281 if (!weak_ptr)
282 return;
tzik103f4732017-07-31 19:41:54 +0900283 using Traits = MakeFunctorTraits<Functor>;
tzik9f27e1f2016-07-01 14:54:12 +0900284 Traits::Invoke(std::forward<Functor>(functor),
285 std::forward<BoundWeakPtr>(weak_ptr),
286 std::forward<RunArgs>(args)...);
287 }
288};
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900289
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900290// Invoker<>
291//
292// See description at the top of the file.
tzikfa6c9852016-06-28 21:22:21 +0900293template <typename StorageType, typename UnboundRunType>
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900294struct Invoker;
295
tzikfa6c9852016-06-28 21:22:21 +0900296template <typename StorageType, typename R, typename... UnboundArgs>
297struct Invoker<StorageType, R(UnboundArgs...)> {
tzikc60a8122016-09-13 14:28:59 +0900298 static R RunOnce(BindStateBase* base, UnboundArgs&&... unbound_args) {
299 // Local references to make debugger stepping easier. If in a debugger,
300 // you really want to warp ahead and step through the
301 // InvokeHelper<>::MakeItSo() call below.
302 StorageType* storage = static_cast<StorageType*>(base);
303 static constexpr size_t num_bound_args =
304 std::tuple_size<decltype(storage->bound_args_)>::value;
305 return RunImpl(std::move(storage->functor_),
306 std::move(storage->bound_args_),
Jeremy Roman773e7f22017-08-17 00:55:20 +0900307 std::make_index_sequence<num_bound_args>(),
tzikc60a8122016-09-13 14:28:59 +0900308 std::forward<UnboundArgs>(unbound_args)...);
309 }
310
tzik0d4bab22016-03-09 14:46:05 +0900311 static R Run(BindStateBase* base, UnboundArgs&&... unbound_args) {
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900312 // Local references to make debugger stepping easier. If in a debugger,
313 // you really want to warp ahead and step through the
314 // InvokeHelper<>::MakeItSo() call below.
tzikfa6c9852016-06-28 21:22:21 +0900315 const StorageType* storage = static_cast<StorageType*>(base);
316 static constexpr size_t num_bound_args =
317 std::tuple_size<decltype(storage->bound_args_)>::value;
Jeremy Roman773e7f22017-08-17 00:55:20 +0900318 return RunImpl(storage->functor_, storage->bound_args_,
319 std::make_index_sequence<num_bound_args>(),
tzikfa6c9852016-06-28 21:22:21 +0900320 std::forward<UnboundArgs>(unbound_args)...);
321 }
322
tzik9f27e1f2016-07-01 14:54:12 +0900323 private:
324 template <typename Functor, typename BoundArgsTuple, size_t... indices>
325 static inline R RunImpl(Functor&& functor,
tzikfa6c9852016-06-28 21:22:21 +0900326 BoundArgsTuple&& bound,
Jeremy Roman773e7f22017-08-17 00:55:20 +0900327 std::index_sequence<indices...>,
tzikfa6c9852016-06-28 21:22:21 +0900328 UnboundArgs&&... unbound_args) {
tzik103f4732017-07-31 19:41:54 +0900329 static constexpr bool is_method = MakeFunctorTraits<Functor>::is_method;
tzikfa6c9852016-06-28 21:22:21 +0900330
331 using DecayedArgsTuple = typename std::decay<BoundArgsTuple>::type;
332 static constexpr bool is_weak_call =
333 IsWeakMethod<is_method,
334 typename std::tuple_element<
335 indices,
336 DecayedArgsTuple>::type...>::value;
337
tzike1ad6f62016-06-01 17:22:51 +0900338 return InvokeHelper<is_weak_call, R>::MakeItSo(
tzik9f27e1f2016-07-01 14:54:12 +0900339 std::forward<Functor>(functor),
tzik3c147c42017-04-06 06:45:03 +0900340 Unwrap(std::get<indices>(std::forward<BoundArgsTuple>(bound)))...,
tzik0d4bab22016-03-09 14:46:05 +0900341 std::forward<UnboundArgs>(unbound_args)...);
ajwong@chromium.org6f015bd2011-11-29 07:13:54 +0900342 }
343};
344
tzik103f4732017-07-31 19:41:54 +0900345// Extracts necessary type info from Functor and BoundArgs.
346// Used to implement MakeUnboundRunType, BindOnce and BindRepeating.
tzikfa6c9852016-06-28 21:22:21 +0900347template <typename Functor, typename... BoundArgs>
tzik103f4732017-07-31 19:41:54 +0900348struct BindTypeHelper {
349 static constexpr size_t num_bounds = sizeof...(BoundArgs);
350 using FunctorTraits = MakeFunctorTraits<Functor>;
351
352 // Example:
353 // When Functor is `double (Foo::*)(int, const std::string&)`, and BoundArgs
354 // is a template pack of `Foo*` and `int16_t`:
355 // - RunType is `double(Foo*, int, const std::string&)`,
356 // - ReturnType is `double`,
357 // - RunParamsList is `TypeList<Foo*, int, const std::string&>`,
358 // - BoundParamsList is `TypeList<Foo*, int>`,
359 // - UnboundParamsList is `TypeList<const std::string&>`,
360 // - BoundArgsList is `TypeList<Foo*, int16_t>`,
361 // - UnboundRunType is `double(const std::string&)`.
362 using RunType = typename FunctorTraits::RunType;
tzikfa6c9852016-06-28 21:22:21 +0900363 using ReturnType = ExtractReturnType<RunType>;
tzik103f4732017-07-31 19:41:54 +0900364
365 using RunParamsList = ExtractArgs<RunType>;
366 using BoundParamsList = TakeTypeListItem<num_bounds, RunParamsList>;
367 using UnboundParamsList = DropTypeListItem<num_bounds, RunParamsList>;
368
369 using BoundArgsList = TypeList<BoundArgs...>;
370
371 using UnboundRunType = MakeFunctionType<ReturnType, UnboundParamsList>;
tzikfa6c9852016-06-28 21:22:21 +0900372};
tzik103f4732017-07-31 19:41:54 +0900373
tzik31d3fae2016-07-08 18:42:38 +0900374template <typename Functor>
375typename std::enable_if<FunctorTraits<Functor>::is_nullable, bool>::type
376IsNull(const Functor& functor) {
377 return !functor;
378}
379
380template <typename Functor>
381typename std::enable_if<!FunctorTraits<Functor>::is_nullable, bool>::type
382IsNull(const Functor&) {
383 return false;
384}
tzikfa6c9852016-06-28 21:22:21 +0900385
tzike6e61952016-11-26 00:56:36 +0900386// Used by ApplyCancellationTraits below.
387template <typename Functor, typename BoundArgsTuple, size_t... indices>
388bool ApplyCancellationTraitsImpl(const Functor& functor,
389 const BoundArgsTuple& bound_args,
Jeremy Roman773e7f22017-08-17 00:55:20 +0900390 std::index_sequence<indices...>) {
tzike6e61952016-11-26 00:56:36 +0900391 return CallbackCancellationTraits<Functor, BoundArgsTuple>::IsCancelled(
tzik3c147c42017-04-06 06:45:03 +0900392 functor, std::get<indices>(bound_args)...);
tzike6e61952016-11-26 00:56:36 +0900393}
tzik058872d2016-09-08 19:58:53 +0900394
tzike6e61952016-11-26 00:56:36 +0900395// Relays |base| to corresponding CallbackCancellationTraits<>::Run(). Returns
396// true if the callback |base| represents is canceled.
397template <typename BindStateType>
398bool ApplyCancellationTraits(const BindStateBase* base) {
399 const BindStateType* storage = static_cast<const BindStateType*>(base);
400 static constexpr size_t num_bound_args =
401 std::tuple_size<decltype(storage->bound_args_)>::value;
Jeremy Roman773e7f22017-08-17 00:55:20 +0900402 return ApplyCancellationTraitsImpl(
403 storage->functor_, storage->bound_args_,
404 std::make_index_sequence<num_bound_args>());
tzik058872d2016-09-08 19:58:53 +0900405};
406
dchengb78a84f2016-09-24 14:05:57 +0900407// Template helpers to detect using Bind() on a base::Callback without any
408// additional arguments. In that case, the original base::Callback object should
409// just be directly used.
410template <typename Functor, typename... BoundArgs>
411struct BindingCallbackWithNoArgs {
412 static constexpr bool value = false;
413};
414
415template <typename Signature,
416 typename... BoundArgs,
417 CopyMode copy_mode,
418 RepeatMode repeat_mode>
419struct BindingCallbackWithNoArgs<Callback<Signature, copy_mode, repeat_mode>,
420 BoundArgs...> {
421 static constexpr bool value = sizeof...(BoundArgs) == 0;
422};
423
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900424// BindState<>
425//
tzik9f27e1f2016-07-01 14:54:12 +0900426// This stores all the state passed into Bind().
427template <typename Functor, typename... BoundArgs>
428struct BindState final : BindStateBase {
tzik1d2f3632016-09-14 16:15:00 +0900429 using IsCancellable = std::integral_constant<
tzike6e61952016-11-26 00:56:36 +0900430 bool,
431 CallbackCancellationTraits<Functor,
432 std::tuple<BoundArgs...>>::is_cancellable>;
tzik1d2f3632016-09-14 16:15:00 +0900433
tzikb4ec92d2016-07-08 23:14:01 +0900434 template <typename ForwardFunctor, typename... ForwardBoundArgs>
tzikd0473e62016-09-08 14:45:38 +0900435 explicit BindState(BindStateBase::InvokeFuncStorage invoke_func,
436 ForwardFunctor&& functor,
437 ForwardBoundArgs&&... bound_args)
tzike6e61952016-11-26 00:56:36 +0900438 // IsCancellable is std::false_type if
439 // CallbackCancellationTraits<>::IsCancelled returns always false.
440 // Otherwise, it's std::true_type.
tzik1d2f3632016-09-14 16:15:00 +0900441 : BindState(IsCancellable{},
442 invoke_func,
443 std::forward<ForwardFunctor>(functor),
dchengb78a84f2016-09-24 14:05:57 +0900444 std::forward<ForwardBoundArgs>(bound_args)...) {
445 static_assert(!BindingCallbackWithNoArgs<Functor, BoundArgs...>::value,
446 "Attempting to bind a base::Callback with no additional "
447 "arguments: save a heap allocation and use the original "
448 "base::Callback object");
449 }
tzik1d2f3632016-09-14 16:15:00 +0900450
451 Functor functor_;
452 std::tuple<BoundArgs...> bound_args_;
453
454 private:
455 template <typename ForwardFunctor, typename... ForwardBoundArgs>
456 explicit BindState(std::true_type,
457 BindStateBase::InvokeFuncStorage invoke_func,
458 ForwardFunctor&& functor,
459 ForwardBoundArgs&&... bound_args)
tzike6e61952016-11-26 00:56:36 +0900460 : BindStateBase(invoke_func,
461 &Destroy,
462 &ApplyCancellationTraits<BindState>),
tzike467aa42016-08-31 20:50:41 +0900463 functor_(std::forward<ForwardFunctor>(functor)),
tzik9f27e1f2016-07-01 14:54:12 +0900464 bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) {
tzik31d3fae2016-07-08 18:42:38 +0900465 DCHECK(!IsNull(functor_));
tzik9f27e1f2016-07-01 14:54:12 +0900466 }
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900467
tzik1d2f3632016-09-14 16:15:00 +0900468 template <typename ForwardFunctor, typename... ForwardBoundArgs>
469 explicit BindState(std::false_type,
470 BindStateBase::InvokeFuncStorage invoke_func,
471 ForwardFunctor&& functor,
472 ForwardBoundArgs&&... bound_args)
473 : BindStateBase(invoke_func, &Destroy),
474 functor_(std::forward<ForwardFunctor>(functor)),
475 bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) {
476 DCHECK(!IsNull(functor_));
477 }
dmichael2f7bc202014-12-19 07:30:11 +0900478
tapted9cef4212015-05-14 17:03:32 +0900479 ~BindState() {}
480
tzikfecd1862016-09-21 17:06:54 +0900481 static void Destroy(const BindStateBase* self) {
482 delete static_cast<const BindState*>(self);
tapted9cef4212015-05-14 17:03:32 +0900483 }
ajwong@chromium.org6f015bd2011-11-29 07:13:54 +0900484};
485
tzik9f27e1f2016-07-01 14:54:12 +0900486// Used to implement MakeBindStateType.
487template <bool is_method, typename Functor, typename... BoundArgs>
488struct MakeBindStateTypeImpl;
489
490template <typename Functor, typename... BoundArgs>
491struct MakeBindStateTypeImpl<false, Functor, BoundArgs...> {
tzik6c539cd2017-07-03 20:01:26 +0900492 static_assert(!HasRefCountedTypeAsRawPtr<
493 typename std::decay<BoundArgs>::type...>::value,
tzik9f27e1f2016-07-01 14:54:12 +0900494 "A parameter is a refcounted type and needs scoped_refptr.");
495 using Type = BindState<typename std::decay<Functor>::type,
496 typename std::decay<BoundArgs>::type...>;
497};
498
499template <typename Functor>
500struct MakeBindStateTypeImpl<true, Functor> {
501 using Type = BindState<typename std::decay<Functor>::type>;
502};
503
504template <typename Functor, typename Receiver, typename... BoundArgs>
505struct MakeBindStateTypeImpl<true, Functor, Receiver, BoundArgs...> {
506 static_assert(
507 !std::is_array<typename std::remove_reference<Receiver>::type>::value,
508 "First bound argument to a method cannot be an array.");
tzik6c539cd2017-07-03 20:01:26 +0900509 static_assert(!HasRefCountedTypeAsRawPtr<
510 typename std::decay<BoundArgs>::type...>::value,
tzik9f27e1f2016-07-01 14:54:12 +0900511 "A parameter is a refcounted type and needs scoped_refptr.");
512
513 private:
514 using DecayedReceiver = typename std::decay<Receiver>::type;
515
516 public:
517 using Type = BindState<
518 typename std::decay<Functor>::type,
519 typename std::conditional<
520 std::is_pointer<DecayedReceiver>::value,
521 scoped_refptr<typename std::remove_pointer<DecayedReceiver>::type>,
522 DecayedReceiver>::type,
523 typename std::decay<BoundArgs>::type...>;
524};
525
526template <typename Functor, typename... BoundArgs>
tzik103f4732017-07-31 19:41:54 +0900527using MakeBindStateType =
528 typename MakeBindStateTypeImpl<MakeFunctorTraits<Functor>::is_method,
529 Functor,
530 BoundArgs...>::Type;
tzik9f27e1f2016-07-01 14:54:12 +0900531
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900532} // namespace internal
tzikfa6c9852016-06-28 21:22:21 +0900533
534// Returns a RunType of bound functor.
535// E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C).
536template <typename Functor, typename... BoundArgs>
537using MakeUnboundRunType =
tzik103f4732017-07-31 19:41:54 +0900538 typename internal::BindTypeHelper<Functor, BoundArgs...>::UnboundRunType;
tzikfa6c9852016-06-28 21:22:21 +0900539
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900540} // namespace base
541
542#endif // BASE_BIND_INTERNAL_H_