blob: 7b3d7d34740e45433136339a109f73a29b545bb6 [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// This defines a set of argument wrappers and related factory methods that
6// can be used specify the refcounting and reference semantics of arguments
7// that are bound by the Bind() function in base/bind.h.
8//
ajwong@chromium.orge4f3dc32012-01-07 07:12:28 +09009// It also defines a set of simple functions and utilities that people want
10// when using Callback<> and Bind().
11//
12//
13// ARGUMENT BINDING WRAPPERS
14//
tnagel@chromium.org7643c212014-04-26 08:13:44 +090015// The wrapper functions are base::Unretained(), base::Owned(), base::Passed(),
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +090016// base::ConstRef(), and base::IgnoreResult().
ajwong@chromium.org718dddf2011-09-28 09:26:37 +090017//
ajwong@chromium.org41339142011-10-15 09:34:42 +090018// Unretained() allows Bind() to bind a non-refcounted class, and to disable
19// refcounting on arguments that are refcounted objects.
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +090020//
ajwong@chromium.org41339142011-10-15 09:34:42 +090021// Owned() transfers ownership of an object to the Callback resulting from
22// bind; the object will be deleted when the Callback is deleted.
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +090023//
scheib8c398ef2017-01-11 19:40:49 +090024// Passed() is for transferring movable-but-not-copyable types (eg. unique_ptr)
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +090025// through a Callback. Logically, this signifies a destructive transfer of
26// the state of the argument into the target function. Invoking
27// Callback::Run() twice on a Callback that was created with a Passed()
28// argument will CHECK() because the first invocation would have already
29// transferred ownership to the target function.
30//
vmpstr9645ac82016-03-19 05:46:41 +090031// RetainedRef() accepts a ref counted object and retains a reference to it.
32// When the callback is called, the object is passed as a raw pointer.
33//
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090034// ConstRef() allows binding a constant reference to an argument rather
35// than a copy.
36//
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +090037// IgnoreResult() is used to adapt a function or Callback with a return type to
38// one with a void return. This is most useful if you have a function with,
39// say, a pesky ignorable bool return that you want to use with PostTask or
40// something else that expect a Callback with a void return.
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090041//
42// EXAMPLE OF Unretained():
43//
44// class Foo {
45// public:
ajwong@chromium.org718dddf2011-09-28 09:26:37 +090046// void func() { cout << "Foo:f" << endl; }
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090047// };
48//
49// // In some function somewhere.
50// Foo foo;
ajwong@chromium.org41339142011-10-15 09:34:42 +090051// Closure foo_callback =
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090052// Bind(&Foo::func, Unretained(&foo));
53// foo_callback.Run(); // Prints "Foo:f".
54//
55// Without the Unretained() wrapper on |&foo|, the above call would fail
56// to compile because Foo does not support the AddRef() and Release() methods.
57//
58//
ajwong@chromium.org41339142011-10-15 09:34:42 +090059// EXAMPLE OF Owned():
60//
61// void foo(int* arg) { cout << *arg << endl }
62//
63// int* pn = new int(1);
64// Closure foo_callback = Bind(&foo, Owned(pn));
65//
66// foo_callback.Run(); // Prints "1"
67// foo_callback.Run(); // Prints "1"
68// *n = 2;
69// foo_callback.Run(); // Prints "2"
70//
71// foo_callback.Reset(); // |pn| is deleted. Also will happen when
72// // |foo_callback| goes out of scope.
73//
74// Without Owned(), someone would have to know to delete |pn| when the last
75// reference to the Callback is deleted.
76//
vmpstr9645ac82016-03-19 05:46:41 +090077// EXAMPLE OF RetainedRef():
78//
79// void foo(RefCountedBytes* bytes) {}
80//
81// scoped_refptr<RefCountedBytes> bytes = ...;
82// Closure callback = Bind(&foo, base::RetainedRef(bytes));
83// callback.Run();
84//
85// Without RetainedRef, the scoped_refptr would try to implicitly convert to
86// a raw pointer and fail compilation:
87//
88// Closure callback = Bind(&foo, bytes); // ERROR!
89//
ajwong@chromium.org41339142011-10-15 09:34:42 +090090//
ajwong@chromium.org718dddf2011-09-28 09:26:37 +090091// EXAMPLE OF ConstRef():
ajwong@chromium.org41339142011-10-15 09:34:42 +090092//
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090093// void foo(int arg) { cout << arg << endl }
94//
95// int n = 1;
ajwong@chromium.org41339142011-10-15 09:34:42 +090096// Closure no_ref = Bind(&foo, n);
97// Closure has_ref = Bind(&foo, ConstRef(n));
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090098//
99// no_ref.Run(); // Prints "1"
100// has_ref.Run(); // Prints "1"
101//
102// n = 2;
103// no_ref.Run(); // Prints "1"
104// has_ref.Run(); // Prints "2"
105//
106// Note that because ConstRef() takes a reference on |n|, |n| must outlive all
107// its bound callbacks.
108//
ajwong@chromium.org718dddf2011-09-28 09:26:37 +0900109//
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900110// EXAMPLE OF IgnoreResult():
ajwong@chromium.org41339142011-10-15 09:34:42 +0900111//
ajwong@chromium.org718dddf2011-09-28 09:26:37 +0900112// int DoSomething(int arg) { cout << arg << endl; }
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900113//
114// // Assign to a Callback with a void return type.
115// Callback<void(int)> cb = Bind(IgnoreResult(&DoSomething));
116// cb->Run(1); // Prints "1".
117//
118// // Prints "1" on |ml|.
119// ml->PostTask(FROM_HERE, Bind(IgnoreResult(&DoSomething), 1);
120//
121//
122// EXAMPLE OF Passed():
123//
dchengcc8e4d82016-04-05 06:25:51 +0900124// void TakesOwnership(std::unique_ptr<Foo> arg) { }
125// std::unique_ptr<Foo> CreateFoo() { return std::unique_ptr<Foo>(new Foo());
126// }
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900127//
dchengcc8e4d82016-04-05 06:25:51 +0900128// std::unique_ptr<Foo> f(new Foo());
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900129//
130// // |cb| is given ownership of Foo(). |f| is now NULL.
danakja7589e72015-12-08 09:44:41 +0900131// // You can use std::move(f) in place of &f, but it's more verbose.
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900132// Closure cb = Bind(&TakesOwnership, Passed(&f));
133//
134// // Run was never called so |cb| still owns Foo() and deletes
135// // it on Reset().
136// cb.Reset();
137//
138// // |cb| is given a new Foo created by CreateFoo().
139// cb = Bind(&TakesOwnership, Passed(CreateFoo()));
140//
141// // |arg| in TakesOwnership() is given ownership of Foo(). |cb|
142// // no longer owns Foo() and, if reset, would not delete Foo().
143// cb.Run(); // Foo() is now transferred to |arg| and deleted.
144// cb.Run(); // This CHECK()s since Foo() already been used once.
145//
146// Passed() is particularly useful with PostTask() when you are transferring
147// ownership of an argument into a task, but don't necessarily know if the
148// task will always be executed. This can happen if the task is cancellable
skyostil97aefe12015-05-01 04:06:15 +0900149// or if it is posted to a TaskRunner.
ajwong@chromium.orge4f3dc32012-01-07 07:12:28 +0900150//
151//
152// SIMPLE FUNCTIONS AND UTILITIES.
153//
154// DoNothing() - Useful for creating a Closure that does nothing when called.
155// DeletePointer<T>() - Useful for creating a Closure that will delete a
156// pointer when invoked. Only use this when necessary.
157// In most cases MessageLoop::DeleteSoon() is a better
158// fit.
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900159
160#ifndef BASE_BIND_HELPERS_H_
161#define BASE_BIND_HELPERS_H_
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900162
avia6a6a682015-12-27 07:15:14 +0900163#include <stddef.h>
164
danakja7589e72015-12-08 09:44:41 +0900165#include <type_traits>
166#include <utility>
167
ajwong@chromium.org718dddf2011-09-28 09:26:37 +0900168#include "base/callback.h"
ajwong@chromium.orgc711b822011-05-17 07:35:14 +0900169#include "base/memory/weak_ptr.h"
avia6a6a682015-12-27 07:15:14 +0900170#include "build/build_config.h"
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900171
172namespace base {
tzikc44f1fd2016-06-14 22:17:31 +0900173
174template <typename T>
175struct IsWeakReceiver;
176
tzik66d9b742016-08-29 23:44:43 +0900177template <typename>
178struct BindUnwrapTraits;
179
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900180namespace internal {
181
tzike6e61952016-11-26 00:56:36 +0900182template <typename Functor, typename SFINAE = void>
183struct FunctorTraits;
184
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900185template <typename T>
186class UnretainedWrapper {
187 public:
ajwong@chromium.org41339142011-10-15 09:34:42 +0900188 explicit UnretainedWrapper(T* o) : ptr_(o) {}
189 T* get() const { return ptr_; }
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900190 private:
ajwong@chromium.org41339142011-10-15 09:34:42 +0900191 T* ptr_;
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900192};
193
194template <typename T>
195class ConstRefWrapper {
196 public:
197 explicit ConstRefWrapper(const T& o) : ptr_(&o) {}
ajwong@chromium.org41339142011-10-15 09:34:42 +0900198 const T& get() const { return *ptr_; }
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900199 private:
200 const T* ptr_;
201};
202
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900203template <typename T>
vmpstr9645ac82016-03-19 05:46:41 +0900204class RetainedRefWrapper {
205 public:
206 explicit RetainedRefWrapper(T* o) : ptr_(o) {}
207 explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {}
208 T* get() const { return ptr_.get(); }
209 private:
210 scoped_refptr<T> ptr_;
211};
212
213template <typename T>
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900214struct IgnoreResultHelper {
tzik3af517a2016-06-29 17:18:37 +0900215 explicit IgnoreResultHelper(T functor) : functor_(std::move(functor)) {}
tzik9f27e1f2016-07-01 14:54:12 +0900216 explicit operator bool() const { return !!functor_; }
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900217
218 T functor_;
219};
220
ajwong@chromium.org41339142011-10-15 09:34:42 +0900221// An alternate implementation is to avoid the destructive copy, and instead
222// specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to
dchengcc8e4d82016-04-05 06:25:51 +0900223// a class that is essentially a std::unique_ptr<>.
ajwong@chromium.org41339142011-10-15 09:34:42 +0900224//
225// The current implementation has the benefit though of leaving ParamTraits<>
226// fully in callback_internal.h as well as avoiding type conversions during
227// storage.
228template <typename T>
229class OwnedWrapper {
230 public:
231 explicit OwnedWrapper(T* o) : ptr_(o) {}
232 ~OwnedWrapper() { delete ptr_; }
233 T* get() const { return ptr_; }
tzikca4b25e2016-07-06 15:39:19 +0900234 OwnedWrapper(OwnedWrapper&& other) {
ajwong@chromium.org41339142011-10-15 09:34:42 +0900235 ptr_ = other.ptr_;
236 other.ptr_ = NULL;
237 }
238
239 private:
240 mutable T* ptr_;
241};
242
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900243// PassedWrapper is a copyable adapter for a scoper that ignores const.
244//
245// It is needed to get around the fact that Bind() takes a const reference to
246// all its arguments. Because Bind() takes a const reference to avoid
247// unnecessary copies, it is incompatible with movable-but-not-copyable
248// types; doing a destructive "move" of the type into Bind() would violate
249// the const correctness.
250//
251// This conundrum cannot be solved without either C++11 rvalue references or
252// a O(2^n) blowup of Bind() templates to handle each combination of regular
253// types and movable-but-not-copyable types. Thus we introduce a wrapper type
254// that is copyable to transmit the correct type information down into
255// BindState<>. Ignoring const in this type makes sense because it is only
256// created when we are explicitly trying to do a destructive move.
257//
258// Two notes:
danakja7589e72015-12-08 09:44:41 +0900259// 1) PassedWrapper supports any type that has a move constructor, however
260// the type will need to be specifically whitelisted in order for it to be
261// bound to a Callback. We guard this explicitly at the call of Passed()
262// to make for clear errors. Things not given to Passed() will be forwarded
263// and stored by value which will not work for general move-only types.
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900264// 2) is_valid_ is distinct from NULL because it is valid to bind a "NULL"
265// scoper to a Callback and allow the Callback to execute once.
266template <typename T>
267class PassedWrapper {
268 public:
danakja7589e72015-12-08 09:44:41 +0900269 explicit PassedWrapper(T&& scoper)
270 : is_valid_(true), scoper_(std::move(scoper)) {}
tzikca4b25e2016-07-06 15:39:19 +0900271 PassedWrapper(PassedWrapper&& other)
danakja7589e72015-12-08 09:44:41 +0900272 : is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {}
tzikc9adefe2016-02-17 00:04:09 +0900273 T Take() const {
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900274 CHECK(is_valid_);
275 is_valid_ = false;
danakja7589e72015-12-08 09:44:41 +0900276 return std::move(scoper_);
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900277 }
278
279 private:
280 mutable bool is_valid_;
281 mutable T scoper_;
282};
283
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900284template <typename T>
tzik66d9b742016-08-29 23:44:43 +0900285using Unwrapper = BindUnwrapTraits<typename std::decay<T>::type>;
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900286
287template <typename T>
tzik66d9b742016-08-29 23:44:43 +0900288auto Unwrap(T&& o) -> decltype(Unwrapper<T>::Unwrap(std::forward<T>(o))) {
289 return Unwrapper<T>::Unwrap(std::forward<T>(o));
tzikc9adefe2016-02-17 00:04:09 +0900290}
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900291
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900292// IsWeakMethod is a helper that determine if we are binding a WeakPtr<> to a
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900293// method. It is used internally by Bind() to select the correct
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900294// InvokeHelper that will no-op itself in the event the WeakPtr<> for
295// the target object is invalidated.
296//
tzik07e99402015-02-06 04:11:26 +0900297// The first argument should be the type of the object that will be received by
298// the method.
tzikfa6c9852016-06-28 21:22:21 +0900299template <bool is_method, typename... Args>
tzikc44f1fd2016-06-14 22:17:31 +0900300struct IsWeakMethod : std::false_type {};
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900301
tzik07e99402015-02-06 04:11:26 +0900302template <typename T, typename... Args>
tzikc44f1fd2016-06-14 22:17:31 +0900303struct IsWeakMethod<true, T, Args...> : IsWeakReceiver<T> {};
tzik07e99402015-02-06 04:11:26 +0900304
305// Packs a list of types to hold them in a single type.
306template <typename... Types>
307struct TypeList {};
308
309// Used for DropTypeListItem implementation.
310template <size_t n, typename List>
311struct DropTypeListItemImpl;
312
313// Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure.
314template <size_t n, typename T, typename... List>
315struct DropTypeListItemImpl<n, TypeList<T, List...>>
316 : DropTypeListItemImpl<n - 1, TypeList<List...>> {};
317
318template <typename T, typename... List>
319struct DropTypeListItemImpl<0, TypeList<T, List...>> {
tzik260fab52015-12-19 18:18:46 +0900320 using Type = TypeList<T, List...>;
tzik07e99402015-02-06 04:11:26 +0900321};
322
323template <>
324struct DropTypeListItemImpl<0, TypeList<>> {
tzik260fab52015-12-19 18:18:46 +0900325 using Type = TypeList<>;
tzik07e99402015-02-06 04:11:26 +0900326};
327
328// A type-level function that drops |n| list item from given TypeList.
329template <size_t n, typename List>
330using DropTypeListItem = typename DropTypeListItemImpl<n, List>::Type;
331
tzik5b8bf3f2015-12-18 11:23:26 +0900332// Used for TakeTypeListItem implementation.
333template <size_t n, typename List, typename... Accum>
334struct TakeTypeListItemImpl;
335
336// Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure.
337template <size_t n, typename T, typename... List, typename... Accum>
338struct TakeTypeListItemImpl<n, TypeList<T, List...>, Accum...>
339 : TakeTypeListItemImpl<n - 1, TypeList<List...>, Accum..., T> {};
340
341template <typename T, typename... List, typename... Accum>
342struct TakeTypeListItemImpl<0, TypeList<T, List...>, Accum...> {
343 using Type = TypeList<Accum...>;
344};
345
346template <typename... Accum>
347struct TakeTypeListItemImpl<0, TypeList<>, Accum...> {
348 using Type = TypeList<Accum...>;
349};
350
351// A type-level function that takes first |n| list item from given TypeList.
352// E.g. TakeTypeListItem<3, TypeList<A, B, C, D>> is evaluated to
353// TypeList<A, B, C>.
354template <size_t n, typename List>
355using TakeTypeListItem = typename TakeTypeListItemImpl<n, List>::Type;
356
tzik07e99402015-02-06 04:11:26 +0900357// Used for ConcatTypeLists implementation.
358template <typename List1, typename List2>
359struct ConcatTypeListsImpl;
360
361template <typename... Types1, typename... Types2>
362struct ConcatTypeListsImpl<TypeList<Types1...>, TypeList<Types2...>> {
tzik260fab52015-12-19 18:18:46 +0900363 using Type = TypeList<Types1..., Types2...>;
tzik07e99402015-02-06 04:11:26 +0900364};
365
366// A type-level function that concats two TypeLists.
367template <typename List1, typename List2>
368using ConcatTypeLists = typename ConcatTypeListsImpl<List1, List2>::Type;
369
tzik07e99402015-02-06 04:11:26 +0900370// Used for MakeFunctionType implementation.
371template <typename R, typename ArgList>
372struct MakeFunctionTypeImpl;
373
374template <typename R, typename... Args>
375struct MakeFunctionTypeImpl<R, TypeList<Args...>> {
tzik260fab52015-12-19 18:18:46 +0900376 // MSVC 2013 doesn't support Type Alias of function types.
377 // Revisit this after we update it to newer version.
378 typedef R Type(Args...);
tzik07e99402015-02-06 04:11:26 +0900379};
380
381// A type-level function that constructs a function type that has |R| as its
382// return type and has TypeLists items as its arguments.
383template <typename R, typename ArgList>
384using MakeFunctionType = typename MakeFunctionTypeImpl<R, ArgList>::Type;
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900385
tzikfa6c9852016-06-28 21:22:21 +0900386// Used for ExtractArgs and ExtractReturnType.
tzik5b8bf3f2015-12-18 11:23:26 +0900387template <typename Signature>
388struct ExtractArgsImpl;
389
390template <typename R, typename... Args>
391struct ExtractArgsImpl<R(Args...)> {
tzikfa6c9852016-06-28 21:22:21 +0900392 using ReturnType = R;
393 using ArgsList = TypeList<Args...>;
tzik5b8bf3f2015-12-18 11:23:26 +0900394};
395
396// A type-level function that extracts function arguments into a TypeList.
397// E.g. ExtractArgs<R(A, B, C)> is evaluated to TypeList<A, B, C>.
398template <typename Signature>
tzikfa6c9852016-06-28 21:22:21 +0900399using ExtractArgs = typename ExtractArgsImpl<Signature>::ArgsList;
400
401// A type-level function that extracts the return type of a function.
402// E.g. ExtractReturnType<R(A, B, C)> is evaluated to R.
403template <typename Signature>
404using ExtractReturnType = typename ExtractArgsImpl<Signature>::ReturnType;
tzik5b8bf3f2015-12-18 11:23:26 +0900405
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900406} // namespace internal
407
408template <typename T>
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900409static inline internal::UnretainedWrapper<T> Unretained(T* o) {
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900410 return internal::UnretainedWrapper<T>(o);
411}
412
413template <typename T>
vmpstr9645ac82016-03-19 05:46:41 +0900414static inline internal::RetainedRefWrapper<T> RetainedRef(T* o) {
415 return internal::RetainedRefWrapper<T>(o);
416}
417
418template <typename T>
419static inline internal::RetainedRefWrapper<T> RetainedRef(scoped_refptr<T> o) {
420 return internal::RetainedRefWrapper<T>(std::move(o));
421}
422
423template <typename T>
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900424static inline internal::ConstRefWrapper<T> ConstRef(const T& o) {
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900425 return internal::ConstRefWrapper<T>(o);
426}
427
ajwong@chromium.org41339142011-10-15 09:34:42 +0900428template <typename T>
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900429static inline internal::OwnedWrapper<T> Owned(T* o) {
ajwong@chromium.org41339142011-10-15 09:34:42 +0900430 return internal::OwnedWrapper<T>(o);
431}
432
danakja7589e72015-12-08 09:44:41 +0900433// We offer 2 syntaxes for calling Passed(). The first takes an rvalue and
434// is best suited for use with the return value of a function or other temporary
435// rvalues. The second takes a pointer to the scoper and is just syntactic sugar
436// to avoid having to write Passed(std::move(scoper)).
437//
438// Both versions of Passed() prevent T from being an lvalue reference. The first
439// via use of enable_if, and the second takes a T* which will not bind to T&.
440template <typename T,
tzik0d4bab22016-03-09 14:46:05 +0900441 typename std::enable_if<!std::is_lvalue_reference<T>::value>::type* =
danakja7589e72015-12-08 09:44:41 +0900442 nullptr>
443static inline internal::PassedWrapper<T> Passed(T&& scoper) {
444 return internal::PassedWrapper<T>(std::move(scoper));
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900445}
tzik0d4bab22016-03-09 14:46:05 +0900446template <typename T>
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900447static inline internal::PassedWrapper<T> Passed(T* scoper) {
danakja7589e72015-12-08 09:44:41 +0900448 return internal::PassedWrapper<T>(std::move(*scoper));
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900449}
450
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900451template <typename T>
452static inline internal::IgnoreResultHelper<T> IgnoreResult(T data) {
tzik3af517a2016-06-29 17:18:37 +0900453 return internal::IgnoreResultHelper<T>(std::move(data));
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900454}
455
ajwong@chromium.orge4f3dc32012-01-07 07:12:28 +0900456BASE_EXPORT void DoNothing();
457
458template<typename T>
459void DeletePointer(T* obj) {
460 delete obj;
461}
462
tzikc44f1fd2016-06-14 22:17:31 +0900463// An injection point to control |this| pointer behavior on a method invocation.
464// If IsWeakReceiver<> is true_type for |T| and |T| is used for a receiver of a
465// method, base::Bind cancels the method invocation if the receiver is tested as
466// false.
467// E.g. Foo::bar() is not called:
468// struct Foo : base::SupportsWeakPtr<Foo> {
469// void bar() {}
470// };
471//
472// WeakPtr<Foo> oo = nullptr;
473// base::Bind(&Foo::bar, oo).Run();
474template <typename T>
475struct IsWeakReceiver : std::false_type {};
476
477template <typename T>
478struct IsWeakReceiver<internal::ConstRefWrapper<T>> : IsWeakReceiver<T> {};
479
480template <typename T>
481struct IsWeakReceiver<WeakPtr<T>> : std::true_type {};
482
tzik66d9b742016-08-29 23:44:43 +0900483// An injection point to control how bound objects passed to the target
484// function. BindUnwrapTraits<>::Unwrap() is called for each bound objects right
485// before the target function is invoked.
486template <typename>
487struct BindUnwrapTraits {
488 template <typename T>
489 static T&& Unwrap(T&& o) { return std::forward<T>(o); }
490};
491
492template <typename T>
493struct BindUnwrapTraits<internal::UnretainedWrapper<T>> {
494 static T* Unwrap(const internal::UnretainedWrapper<T>& o) {
495 return o.get();
496 }
497};
498
499template <typename T>
500struct BindUnwrapTraits<internal::ConstRefWrapper<T>> {
501 static const T& Unwrap(const internal::ConstRefWrapper<T>& o) {
502 return o.get();
503 }
504};
505
506template <typename T>
507struct BindUnwrapTraits<internal::RetainedRefWrapper<T>> {
508 static T* Unwrap(const internal::RetainedRefWrapper<T>& o) {
509 return o.get();
510 }
511};
512
513template <typename T>
514struct BindUnwrapTraits<internal::OwnedWrapper<T>> {
515 static T* Unwrap(const internal::OwnedWrapper<T>& o) {
516 return o.get();
517 }
518};
519
520template <typename T>
521struct BindUnwrapTraits<internal::PassedWrapper<T>> {
522 static T Unwrap(const internal::PassedWrapper<T>& o) {
523 return o.Take();
524 }
525};
526
tzike6e61952016-11-26 00:56:36 +0900527// CallbackCancellationTraits allows customization of Callback's cancellation
528// semantics. By default, callbacks are not cancellable. A specialization should
529// set is_cancellable = true and implement an IsCancelled() that returns if the
530// callback should be cancelled.
531template <typename Functor, typename BoundArgsTuple, typename SFINAE = void>
532struct CallbackCancellationTraits {
533 static constexpr bool is_cancellable = false;
534};
535
536// Specialization for method bound to weak pointer receiver.
537template <typename Functor, typename... BoundArgs>
538struct CallbackCancellationTraits<
539 Functor,
540 std::tuple<BoundArgs...>,
541 typename std::enable_if<
542 internal::IsWeakMethod<internal::FunctorTraits<Functor>::is_method,
543 BoundArgs...>::value>::type> {
544 static constexpr bool is_cancellable = true;
545
546 template <typename Receiver, typename... Args>
547 static bool IsCancelled(const Functor&,
548 const Receiver& receiver,
549 const Args&...) {
550 return !receiver;
551 }
552};
553
554// Specialization for a nested bind.
555template <typename Signature,
556 typename... BoundArgs,
557 internal::CopyMode copy_mode,
558 internal::RepeatMode repeat_mode>
559struct CallbackCancellationTraits<Callback<Signature, copy_mode, repeat_mode>,
560 std::tuple<BoundArgs...>> {
561 static constexpr bool is_cancellable = true;
562
563 template <typename Functor>
564 static bool IsCancelled(const Functor& functor, const BoundArgs&...) {
565 return functor.IsCancelled();
566 }
567};
568
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900569} // namespace base
570
571#endif // BASE_BIND_HELPERS_H_