blob: 590d788b96e0a650e22065884d4c8dc41d63531e [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// 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//
[email protected]c6944272012-01-06 22:12:289// 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//
[email protected]cd106ff2014-04-25 23:13:4415// The wrapper functions are base::Unretained(), base::Owned(), base::Passed(),
[email protected]206a2ae82011-12-22 21:12:5816// base::ConstRef(), and base::IgnoreResult().
[email protected]e8bfc31d2011-09-28 00:26:3717//
[email protected]08aa4552011-10-15 00:34:4218// Unretained() allows Bind() to bind a non-refcounted class, and to disable
19// refcounting on arguments that are refcounted objects.
[email protected]206a2ae82011-12-22 21:12:5820//
[email protected]08aa4552011-10-15 00:34:4221// Owned() transfers ownership of an object to the Callback resulting from
22// bind; the object will be deleted when the Callback is deleted.
[email protected]206a2ae82011-12-22 21:12:5823//
24// Passed() is for transferring movable-but-not-copyable types (eg. scoped_ptr)
25// 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//
vmpstr1d492be2016-03-18 20:46:4131// 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//
[email protected]b38d3572011-02-15 01:27:3834// ConstRef() allows binding a constant reference to an argument rather
35// than a copy.
36//
[email protected]206a2ae82011-12-22 21:12:5837// 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.
[email protected]b38d3572011-02-15 01:27:3841//
42// EXAMPLE OF Unretained():
43//
44// class Foo {
45// public:
[email protected]e8bfc31d2011-09-28 00:26:3746// void func() { cout << "Foo:f" << endl; }
[email protected]b38d3572011-02-15 01:27:3847// };
48//
49// // In some function somewhere.
50// Foo foo;
[email protected]08aa4552011-10-15 00:34:4251// Closure foo_callback =
[email protected]b38d3572011-02-15 01:27:3852// 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//
[email protected]08aa4552011-10-15 00:34:4259// 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//
vmpstr1d492be2016-03-18 20:46:4177// 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//
[email protected]08aa4552011-10-15 00:34:4290//
[email protected]e8bfc31d2011-09-28 00:26:3791// EXAMPLE OF ConstRef():
[email protected]08aa4552011-10-15 00:34:4292//
[email protected]b38d3572011-02-15 01:27:3893// void foo(int arg) { cout << arg << endl }
94//
95// int n = 1;
[email protected]08aa4552011-10-15 00:34:4296// Closure no_ref = Bind(&foo, n);
97// Closure has_ref = Bind(&foo, ConstRef(n));
[email protected]b38d3572011-02-15 01:27:3898//
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//
[email protected]e8bfc31d2011-09-28 00:26:37109//
[email protected]206a2ae82011-12-22 21:12:58110// EXAMPLE OF IgnoreResult():
[email protected]08aa4552011-10-15 00:34:42111//
[email protected]e8bfc31d2011-09-28 00:26:37112// int DoSomething(int arg) { cout << arg << endl; }
[email protected]206a2ae82011-12-22 21:12:58113//
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//
dcheng093de9b2016-04-04 21:25:51124// void TakesOwnership(std::unique_ptr<Foo> arg) { }
125// std::unique_ptr<Foo> CreateFoo() { return std::unique_ptr<Foo>(new Foo());
126// }
[email protected]206a2ae82011-12-22 21:12:58127//
dcheng093de9b2016-04-04 21:25:51128// std::unique_ptr<Foo> f(new Foo());
[email protected]206a2ae82011-12-22 21:12:58129//
130// // |cb| is given ownership of Foo(). |f| is now NULL.
danakj314d1f42015-12-08 00:44:41131// // You can use std::move(f) in place of &f, but it's more verbose.
[email protected]206a2ae82011-12-22 21:12:58132// 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
skyostil054861d2015-04-30 19:06:15149// or if it is posted to a TaskRunner.
[email protected]c6944272012-01-06 22:12:28150//
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.
[email protected]b38d3572011-02-15 01:27:38159
160#ifndef BASE_BIND_HELPERS_H_
161#define BASE_BIND_HELPERS_H_
[email protected]b38d3572011-02-15 01:27:38162
avi9b6f42932015-12-26 22:15:14163#include <stddef.h>
164
danakj314d1f42015-12-08 00:44:41165#include <type_traits>
166#include <utility>
167
[email protected]e8bfc31d2011-09-28 00:26:37168#include "base/callback.h"
[email protected]93540582011-05-16 22:35:14169#include "base/memory/weak_ptr.h"
avi9b6f42932015-12-26 22:15:14170#include "build/build_config.h"
[email protected]b38d3572011-02-15 01:27:38171
172namespace base {
173namespace internal {
174
175// Use the Substitution Failure Is Not An Error (SFINAE) trick to inspect T
176// for the existence of AddRef() and Release() functions of the correct
177// signature.
178//
179// http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error
180// http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence
181// http://stackoverflow.com/questions/4358584/sfinae-approach-comparison
182// http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-member-functions
183//
184// The last link in particular show the method used below.
185//
186// For SFINAE to work with inherited methods, we need to pull some extra tricks
187// with multiple inheritance. In the more standard formulation, the overloads
188// of Check would be:
189//
190// template <typename C>
191// Yes NotTheCheckWeWant(Helper<&C::TargetFunc>*);
192//
193// template <typename C>
194// No NotTheCheckWeWant(...);
195//
196// static const bool value = sizeof(NotTheCheckWeWant<T>(0)) == sizeof(Yes);
197//
198// The problem here is that template resolution will not match
199// C::TargetFunc if TargetFunc does not exist directly in C. That is, if
200// TargetFunc in inherited from an ancestor, &C::TargetFunc will not match,
201// |value| will be false. This formulation only checks for whether or
202// not TargetFunc exist directly in the class being introspected.
203//
204// To get around this, we play a dirty trick with multiple inheritance.
205// First, We create a class BaseMixin that declares each function that we
206// want to probe for. Then we create a class Base that inherits from both T
207// (the class we wish to probe) and BaseMixin. Note that the function
208// signature in BaseMixin does not need to match the signature of the function
tzik3bc7779b2015-12-19 09:18:46209// we are probing for; thus it's easiest to just use void().
[email protected]b38d3572011-02-15 01:27:38210//
211// Now, if TargetFunc exists somewhere in T, then &Base::TargetFunc has an
212// ambiguous resolution between BaseMixin and T. This lets us write the
213// following:
214//
215// template <typename C>
216// No GoodCheck(Helper<&C::TargetFunc>*);
217//
218// template <typename C>
219// Yes GoodCheck(...);
220//
221// static const bool value = sizeof(GoodCheck<Base>(0)) == sizeof(Yes);
222//
223// Notice here that the variadic version of GoodCheck() returns Yes here
224// instead of No like the previous one. Also notice that we calculate |value|
225// by specializing GoodCheck() on Base instead of T.
226//
227// We've reversed the roles of the variadic, and Helper overloads.
228// GoodCheck(Helper<&C::TargetFunc>*), when C = Base, fails to be a valid
229// substitution if T::TargetFunc exists. Thus GoodCheck<Base>(0) will resolve
230// to the variadic version if T has TargetFunc. If T::TargetFunc does not
231// exist, then &C::TargetFunc is not ambiguous, and the overload resolution
232// will prefer GoodCheck(Helper<&C::TargetFunc>*).
233//
234// This method of SFINAE will correctly probe for inherited names, but it cannot
235// typecheck those names. It's still a good enough sanity check though.
236//
237// Works on gcc-4.2, gcc-4.4, and Visual Studio 2008.
238//
239// TODO(ajwong): Move to ref_counted.h or template_util.h when we've vetted
240// this works well.
[email protected]7a1f7c6f2011-05-10 21:17:48241//
242// TODO(ajwong): Make this check for Release() as well.
243// See http://crbug.com/82038.
[email protected]b38d3572011-02-15 01:27:38244template <typename T>
245class SupportsAddRefAndRelease {
tzik3bc7779b2015-12-19 09:18:46246 using Yes = char[1];
247 using No = char[2];
[email protected]b38d3572011-02-15 01:27:38248
249 struct BaseMixin {
250 void AddRef();
[email protected]b38d3572011-02-15 01:27:38251 };
252
[email protected]690bda882011-04-13 22:40:46253// MSVC warns when you try to use Base if T has a private destructor, the
254// common pattern for refcounted types. It does this even though no attempt to
255// instantiate Base is made. We disable the warning for this definition.
256#if defined(OS_WIN)
[email protected]793b6c22013-07-31 05:22:02257#pragma warning(push)
[email protected]690bda882011-04-13 22:40:46258#pragma warning(disable:4624)
259#endif
[email protected]b38d3572011-02-15 01:27:38260 struct Base : public T, public BaseMixin {
261 };
[email protected]690bda882011-04-13 22:40:46262#if defined(OS_WIN)
[email protected]793b6c22013-07-31 05:22:02263#pragma warning(pop)
[email protected]690bda882011-04-13 22:40:46264#endif
[email protected]b38d3572011-02-15 01:27:38265
tzik3bc7779b2015-12-19 09:18:46266 template <void(BaseMixin::*)()> struct Helper {};
[email protected]b38d3572011-02-15 01:27:38267
268 template <typename C>
[email protected]7a1f7c6f2011-05-10 21:17:48269 static No& Check(Helper<&C::AddRef>*);
[email protected]b38d3572011-02-15 01:27:38270
271 template <typename >
272 static Yes& Check(...);
273
274 public:
[email protected]e160b442014-08-01 22:44:13275 enum { value = sizeof(Check<Base>(0)) == sizeof(Yes) };
[email protected]b38d3572011-02-15 01:27:38276};
277
[email protected]b38d3572011-02-15 01:27:38278// Helpers to assert that arguments of a recounted type are bound with a
279// scoped_refptr.
280template <bool IsClasstype, typename T>
tzik403cb6c2016-03-10 07:17:25281struct UnsafeBindtoRefCountedArgHelper : std::false_type {
[email protected]b38d3572011-02-15 01:27:38282};
283
284template <typename T>
285struct UnsafeBindtoRefCountedArgHelper<true, T>
tzik403cb6c2016-03-10 07:17:25286 : std::integral_constant<bool, SupportsAddRefAndRelease<T>::value> {
[email protected]b38d3572011-02-15 01:27:38287};
288
289template <typename T>
tzik403cb6c2016-03-10 07:17:25290struct UnsafeBindtoRefCountedArg : std::false_type {
[email protected]c18b1052011-03-24 02:02:17291};
292
293template <typename T>
294struct UnsafeBindtoRefCountedArg<T*>
tzik403cb6c2016-03-10 07:17:25295 : UnsafeBindtoRefCountedArgHelper<std::is_class<T>::value, T> {
[email protected]b38d3572011-02-15 01:27:38296};
297
[email protected]7296f2762011-11-21 19:23:44298template <typename T>
299class HasIsMethodTag {
tzik3bc7779b2015-12-19 09:18:46300 using Yes = char[1];
301 using No = char[2];
[email protected]7296f2762011-11-21 19:23:44302
303 template <typename U>
304 static Yes& Check(typename U::IsMethod*);
305
306 template <typename U>
307 static No& Check(...);
308
309 public:
[email protected]e160b442014-08-01 22:44:13310 enum { value = sizeof(Check<T>(0)) == sizeof(Yes) };
[email protected]7296f2762011-11-21 19:23:44311};
[email protected]b38d3572011-02-15 01:27:38312
313template <typename T>
314class UnretainedWrapper {
315 public:
[email protected]08aa4552011-10-15 00:34:42316 explicit UnretainedWrapper(T* o) : ptr_(o) {}
317 T* get() const { return ptr_; }
[email protected]b38d3572011-02-15 01:27:38318 private:
[email protected]08aa4552011-10-15 00:34:42319 T* ptr_;
[email protected]b38d3572011-02-15 01:27:38320};
321
322template <typename T>
323class ConstRefWrapper {
324 public:
325 explicit ConstRefWrapper(const T& o) : ptr_(&o) {}
[email protected]08aa4552011-10-15 00:34:42326 const T& get() const { return *ptr_; }
[email protected]b38d3572011-02-15 01:27:38327 private:
328 const T* ptr_;
329};
330
[email protected]7296f2762011-11-21 19:23:44331template <typename T>
vmpstr1d492be2016-03-18 20:46:41332class RetainedRefWrapper {
333 public:
334 explicit RetainedRefWrapper(T* o) : ptr_(o) {}
335 explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {}
336 T* get() const { return ptr_.get(); }
337 private:
338 scoped_refptr<T> ptr_;
339};
340
341template <typename T>
[email protected]7296f2762011-11-21 19:23:44342struct IgnoreResultHelper {
343 explicit IgnoreResultHelper(T functor) : functor_(functor) {}
344
345 T functor_;
346};
347
348template <typename T>
349struct IgnoreResultHelper<Callback<T> > {
350 explicit IgnoreResultHelper(const Callback<T>& functor) : functor_(functor) {}
351
352 const Callback<T>& functor_;
353};
354
[email protected]08aa4552011-10-15 00:34:42355// An alternate implementation is to avoid the destructive copy, and instead
356// specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to
dcheng093de9b2016-04-04 21:25:51357// a class that is essentially a std::unique_ptr<>.
[email protected]08aa4552011-10-15 00:34:42358//
359// The current implementation has the benefit though of leaving ParamTraits<>
360// fully in callback_internal.h as well as avoiding type conversions during
361// storage.
362template <typename T>
363class OwnedWrapper {
364 public:
365 explicit OwnedWrapper(T* o) : ptr_(o) {}
366 ~OwnedWrapper() { delete ptr_; }
367 T* get() const { return ptr_; }
368 OwnedWrapper(const OwnedWrapper& other) {
369 ptr_ = other.ptr_;
370 other.ptr_ = NULL;
371 }
372
373 private:
374 mutable T* ptr_;
375};
376
[email protected]206a2ae82011-12-22 21:12:58377// PassedWrapper is a copyable adapter for a scoper that ignores const.
378//
379// It is needed to get around the fact that Bind() takes a const reference to
380// all its arguments. Because Bind() takes a const reference to avoid
381// unnecessary copies, it is incompatible with movable-but-not-copyable
382// types; doing a destructive "move" of the type into Bind() would violate
383// the const correctness.
384//
385// This conundrum cannot be solved without either C++11 rvalue references or
386// a O(2^n) blowup of Bind() templates to handle each combination of regular
387// types and movable-but-not-copyable types. Thus we introduce a wrapper type
388// that is copyable to transmit the correct type information down into
389// BindState<>. Ignoring const in this type makes sense because it is only
390// created when we are explicitly trying to do a destructive move.
391//
392// Two notes:
danakj314d1f42015-12-08 00:44:41393// 1) PassedWrapper supports any type that has a move constructor, however
394// the type will need to be specifically whitelisted in order for it to be
395// bound to a Callback. We guard this explicitly at the call of Passed()
396// to make for clear errors. Things not given to Passed() will be forwarded
397// and stored by value which will not work for general move-only types.
[email protected]206a2ae82011-12-22 21:12:58398// 2) is_valid_ is distinct from NULL because it is valid to bind a "NULL"
399// scoper to a Callback and allow the Callback to execute once.
400template <typename T>
401class PassedWrapper {
402 public:
danakj314d1f42015-12-08 00:44:41403 explicit PassedWrapper(T&& scoper)
404 : is_valid_(true), scoper_(std::move(scoper)) {}
[email protected]206a2ae82011-12-22 21:12:58405 PassedWrapper(const PassedWrapper& other)
danakj314d1f42015-12-08 00:44:41406 : is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {}
tzik463eb422016-02-16 15:04:09407 T Take() const {
[email protected]206a2ae82011-12-22 21:12:58408 CHECK(is_valid_);
409 is_valid_ = false;
danakj314d1f42015-12-08 00:44:41410 return std::move(scoper_);
[email protected]206a2ae82011-12-22 21:12:58411 }
412
413 private:
414 mutable bool is_valid_;
415 mutable T scoper_;
416};
417
[email protected]b38d3572011-02-15 01:27:38418// Unwrap the stored parameters for the wrappers above.
419template <typename T>
tzik463eb422016-02-16 15:04:09420const T& Unwrap(const T& o) {
421 return o;
422}
[email protected]b38d3572011-02-15 01:27:38423
424template <typename T>
tzik463eb422016-02-16 15:04:09425T* Unwrap(UnretainedWrapper<T> unretained) {
426 return unretained.get();
427}
[email protected]b38d3572011-02-15 01:27:38428
429template <typename T>
tzik463eb422016-02-16 15:04:09430const T& Unwrap(ConstRefWrapper<T> const_ref) {
431 return const_ref.get();
432}
[email protected]b38d3572011-02-15 01:27:38433
[email protected]7a15d1172011-10-07 00:25:29434template <typename T>
vmpstr1d492be2016-03-18 20:46:41435T* Unwrap(const RetainedRefWrapper<T>& o) {
436 return o.get();
437}
438
439template <typename T>
tzik463eb422016-02-16 15:04:09440const WeakPtr<T>& Unwrap(const WeakPtr<T>& o) {
441 return o;
442}
[email protected]b38d3572011-02-15 01:27:38443
[email protected]08aa4552011-10-15 00:34:42444template <typename T>
tzik463eb422016-02-16 15:04:09445T* Unwrap(const OwnedWrapper<T>& o) {
446 return o.get();
447}
[email protected]08aa4552011-10-15 00:34:42448
[email protected]206a2ae82011-12-22 21:12:58449template <typename T>
tzik463eb422016-02-16 15:04:09450T Unwrap(PassedWrapper<T>& o) {
451 return o.Take();
452}
[email protected]206a2ae82011-12-22 21:12:58453
[email protected]7296f2762011-11-21 19:23:44454// IsWeakMethod is a helper that determine if we are binding a WeakPtr<> to a
[email protected]206a2ae82011-12-22 21:12:58455// method. It is used internally by Bind() to select the correct
[email protected]7296f2762011-11-21 19:23:44456// InvokeHelper that will no-op itself in the event the WeakPtr<> for
457// the target object is invalidated.
458//
tzik8ce65702015-02-05 19:11:26459// The first argument should be the type of the object that will be received by
460// the method.
461template <bool IsMethod, typename... Args>
tzik403cb6c2016-03-10 07:17:25462struct IsWeakMethod : public std::false_type {};
[email protected]7296f2762011-11-21 19:23:44463
tzik8ce65702015-02-05 19:11:26464template <typename T, typename... Args>
tzik403cb6c2016-03-10 07:17:25465struct IsWeakMethod<true, WeakPtr<T>, Args...> : public std::true_type {};
[email protected]7296f2762011-11-21 19:23:44466
tzik8ce65702015-02-05 19:11:26467template <typename T, typename... Args>
468struct IsWeakMethod<true, ConstRefWrapper<WeakPtr<T>>, Args...>
tzik403cb6c2016-03-10 07:17:25469 : public std::true_type {};
tzik8ce65702015-02-05 19:11:26470
471
472// Packs a list of types to hold them in a single type.
473template <typename... Types>
474struct TypeList {};
475
476// Used for DropTypeListItem implementation.
477template <size_t n, typename List>
478struct DropTypeListItemImpl;
479
480// Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure.
481template <size_t n, typename T, typename... List>
482struct DropTypeListItemImpl<n, TypeList<T, List...>>
483 : DropTypeListItemImpl<n - 1, TypeList<List...>> {};
484
485template <typename T, typename... List>
486struct DropTypeListItemImpl<0, TypeList<T, List...>> {
tzik3bc7779b2015-12-19 09:18:46487 using Type = TypeList<T, List...>;
tzik8ce65702015-02-05 19:11:26488};
489
490template <>
491struct DropTypeListItemImpl<0, TypeList<>> {
tzik3bc7779b2015-12-19 09:18:46492 using Type = TypeList<>;
tzik8ce65702015-02-05 19:11:26493};
494
495// A type-level function that drops |n| list item from given TypeList.
496template <size_t n, typename List>
497using DropTypeListItem = typename DropTypeListItemImpl<n, List>::Type;
498
tzik7fe3a682015-12-18 02:23:26499// Used for TakeTypeListItem implementation.
500template <size_t n, typename List, typename... Accum>
501struct TakeTypeListItemImpl;
502
503// Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure.
504template <size_t n, typename T, typename... List, typename... Accum>
505struct TakeTypeListItemImpl<n, TypeList<T, List...>, Accum...>
506 : TakeTypeListItemImpl<n - 1, TypeList<List...>, Accum..., T> {};
507
508template <typename T, typename... List, typename... Accum>
509struct TakeTypeListItemImpl<0, TypeList<T, List...>, Accum...> {
510 using Type = TypeList<Accum...>;
511};
512
513template <typename... Accum>
514struct TakeTypeListItemImpl<0, TypeList<>, Accum...> {
515 using Type = TypeList<Accum...>;
516};
517
518// A type-level function that takes first |n| list item from given TypeList.
519// E.g. TakeTypeListItem<3, TypeList<A, B, C, D>> is evaluated to
520// TypeList<A, B, C>.
521template <size_t n, typename List>
522using TakeTypeListItem = typename TakeTypeListItemImpl<n, List>::Type;
523
tzik8ce65702015-02-05 19:11:26524// Used for ConcatTypeLists implementation.
525template <typename List1, typename List2>
526struct ConcatTypeListsImpl;
527
528template <typename... Types1, typename... Types2>
529struct ConcatTypeListsImpl<TypeList<Types1...>, TypeList<Types2...>> {
tzik3bc7779b2015-12-19 09:18:46530 using Type = TypeList<Types1..., Types2...>;
tzik8ce65702015-02-05 19:11:26531};
532
533// A type-level function that concats two TypeLists.
534template <typename List1, typename List2>
535using ConcatTypeLists = typename ConcatTypeListsImpl<List1, List2>::Type;
536
tzik8ce65702015-02-05 19:11:26537// Used for MakeFunctionType implementation.
538template <typename R, typename ArgList>
539struct MakeFunctionTypeImpl;
540
541template <typename R, typename... Args>
542struct MakeFunctionTypeImpl<R, TypeList<Args...>> {
tzik3bc7779b2015-12-19 09:18:46543 // MSVC 2013 doesn't support Type Alias of function types.
544 // Revisit this after we update it to newer version.
545 typedef R Type(Args...);
tzik8ce65702015-02-05 19:11:26546};
547
548// A type-level function that constructs a function type that has |R| as its
549// return type and has TypeLists items as its arguments.
550template <typename R, typename ArgList>
551using MakeFunctionType = typename MakeFunctionTypeImpl<R, ArgList>::Type;
[email protected]7296f2762011-11-21 19:23:44552
tzik7fe3a682015-12-18 02:23:26553// Used for ExtractArgs.
554template <typename Signature>
555struct ExtractArgsImpl;
556
557template <typename R, typename... Args>
558struct ExtractArgsImpl<R(Args...)> {
559 using Type = TypeList<Args...>;
560};
561
562// A type-level function that extracts function arguments into a TypeList.
563// E.g. ExtractArgs<R(A, B, C)> is evaluated to TypeList<A, B, C>.
564template <typename Signature>
565using ExtractArgs = typename ExtractArgsImpl<Signature>::Type;
566
[email protected]b38d3572011-02-15 01:27:38567} // namespace internal
568
569template <typename T>
[email protected]7296f2762011-11-21 19:23:44570static inline internal::UnretainedWrapper<T> Unretained(T* o) {
[email protected]b38d3572011-02-15 01:27:38571 return internal::UnretainedWrapper<T>(o);
572}
573
574template <typename T>
vmpstr1d492be2016-03-18 20:46:41575static inline internal::RetainedRefWrapper<T> RetainedRef(T* o) {
576 return internal::RetainedRefWrapper<T>(o);
577}
578
579template <typename T>
580static inline internal::RetainedRefWrapper<T> RetainedRef(scoped_refptr<T> o) {
581 return internal::RetainedRefWrapper<T>(std::move(o));
582}
583
584template <typename T>
[email protected]7296f2762011-11-21 19:23:44585static inline internal::ConstRefWrapper<T> ConstRef(const T& o) {
[email protected]b38d3572011-02-15 01:27:38586 return internal::ConstRefWrapper<T>(o);
587}
588
[email protected]08aa4552011-10-15 00:34:42589template <typename T>
[email protected]7296f2762011-11-21 19:23:44590static inline internal::OwnedWrapper<T> Owned(T* o) {
[email protected]08aa4552011-10-15 00:34:42591 return internal::OwnedWrapper<T>(o);
592}
593
danakj314d1f42015-12-08 00:44:41594// We offer 2 syntaxes for calling Passed(). The first takes an rvalue and
595// is best suited for use with the return value of a function or other temporary
596// rvalues. The second takes a pointer to the scoper and is just syntactic sugar
597// to avoid having to write Passed(std::move(scoper)).
598//
599// Both versions of Passed() prevent T from being an lvalue reference. The first
600// via use of enable_if, and the second takes a T* which will not bind to T&.
601template <typename T,
tzika43eff02016-03-09 05:46:05602 typename std::enable_if<!std::is_lvalue_reference<T>::value>::type* =
danakj314d1f42015-12-08 00:44:41603 nullptr>
604static inline internal::PassedWrapper<T> Passed(T&& scoper) {
605 return internal::PassedWrapper<T>(std::move(scoper));
[email protected]206a2ae82011-12-22 21:12:58606}
tzika43eff02016-03-09 05:46:05607template <typename T>
[email protected]206a2ae82011-12-22 21:12:58608static inline internal::PassedWrapper<T> Passed(T* scoper) {
danakj314d1f42015-12-08 00:44:41609 return internal::PassedWrapper<T>(std::move(*scoper));
[email protected]206a2ae82011-12-22 21:12:58610}
611
[email protected]7296f2762011-11-21 19:23:44612template <typename T>
613static inline internal::IgnoreResultHelper<T> IgnoreResult(T data) {
614 return internal::IgnoreResultHelper<T>(data);
615}
616
617template <typename T>
618static inline internal::IgnoreResultHelper<Callback<T> >
619IgnoreResult(const Callback<T>& data) {
620 return internal::IgnoreResultHelper<Callback<T> >(data);
621}
622
[email protected]c6944272012-01-06 22:12:28623BASE_EXPORT void DoNothing();
624
625template<typename T>
626void DeletePointer(T* obj) {
627 delete obj;
628}
629
[email protected]b38d3572011-02-15 01:27:38630} // namespace base
631
632#endif // BASE_BIND_HELPERS_H_