• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
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 //
9 // 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 //
15 // The wrapper functions are base::Unretained(), base::Owned(), base::Passed(),
16 // base::ConstRef(), and base::IgnoreResult().
17 //
18 // Unretained() allows Bind() to bind a non-refcounted class, and to disable
19 // refcounting on arguments that are refcounted objects.
20 //
21 // Owned() transfers ownership of an object to the Callback resulting from
22 // bind; the object will be deleted when the Callback is deleted.
23 //
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 //
31 // ConstRef() allows binding a constant reference to an argument rather
32 // than a copy.
33 //
34 // IgnoreResult() is used to adapt a function or Callback with a return type to
35 // one with a void return. This is most useful if you have a function with,
36 // say, a pesky ignorable bool return that you want to use with PostTask or
37 // something else that expect a Callback with a void return.
38 //
39 // EXAMPLE OF Unretained():
40 //
41 //   class Foo {
42 //    public:
43 //     void func() { cout << "Foo:f" << endl; }
44 //   };
45 //
46 //   // In some function somewhere.
47 //   Foo foo;
48 //   Closure foo_callback =
49 //       Bind(&Foo::func, Unretained(&foo));
50 //   foo_callback.Run();  // Prints "Foo:f".
51 //
52 // Without the Unretained() wrapper on |&foo|, the above call would fail
53 // to compile because Foo does not support the AddRef() and Release() methods.
54 //
55 //
56 // EXAMPLE OF Owned():
57 //
58 //   void foo(int* arg) { cout << *arg << endl }
59 //
60 //   int* pn = new int(1);
61 //   Closure foo_callback = Bind(&foo, Owned(pn));
62 //
63 //   foo_callback.Run();  // Prints "1"
64 //   foo_callback.Run();  // Prints "1"
65 //   *n = 2;
66 //   foo_callback.Run();  // Prints "2"
67 //
68 //   foo_callback.Reset();  // |pn| is deleted.  Also will happen when
69 //                          // |foo_callback| goes out of scope.
70 //
71 // Without Owned(), someone would have to know to delete |pn| when the last
72 // reference to the Callback is deleted.
73 //
74 //
75 // EXAMPLE OF ConstRef():
76 //
77 //   void foo(int arg) { cout << arg << endl }
78 //
79 //   int n = 1;
80 //   Closure no_ref = Bind(&foo, n);
81 //   Closure has_ref = Bind(&foo, ConstRef(n));
82 //
83 //   no_ref.Run();  // Prints "1"
84 //   has_ref.Run();  // Prints "1"
85 //
86 //   n = 2;
87 //   no_ref.Run();  // Prints "1"
88 //   has_ref.Run();  // Prints "2"
89 //
90 // Note that because ConstRef() takes a reference on |n|, |n| must outlive all
91 // its bound callbacks.
92 //
93 //
94 // EXAMPLE OF IgnoreResult():
95 //
96 //   int DoSomething(int arg) { cout << arg << endl; }
97 //
98 //   // Assign to a Callback with a void return type.
99 //   Callback<void(int)> cb = Bind(IgnoreResult(&DoSomething));
100 //   cb->Run(1);  // Prints "1".
101 //
102 //   // Prints "1" on |ml|.
103 //   ml->PostTask(FROM_HERE, Bind(IgnoreResult(&DoSomething), 1);
104 //
105 //
106 // EXAMPLE OF Passed():
107 //
108 //   void TakesOwnership(scoped_ptr<Foo> arg) { }
109 //   scoped_ptr<Foo> CreateFoo() { return scoped_ptr<Foo>(new Foo()); }
110 //
111 //   scoped_ptr<Foo> f(new Foo());
112 //
113 //   // |cb| is given ownership of Foo(). |f| is now NULL.
114 //   // You can use std::move(f) in place of &f, but it's more verbose.
115 //   Closure cb = Bind(&TakesOwnership, Passed(&f));
116 //
117 //   // Run was never called so |cb| still owns Foo() and deletes
118 //   // it on Reset().
119 //   cb.Reset();
120 //
121 //   // |cb| is given a new Foo created by CreateFoo().
122 //   cb = Bind(&TakesOwnership, Passed(CreateFoo()));
123 //
124 //   // |arg| in TakesOwnership() is given ownership of Foo(). |cb|
125 //   // no longer owns Foo() and, if reset, would not delete Foo().
126 //   cb.Run();  // Foo() is now transferred to |arg| and deleted.
127 //   cb.Run();  // This CHECK()s since Foo() already been used once.
128 //
129 // Passed() is particularly useful with PostTask() when you are transferring
130 // ownership of an argument into a task, but don't necessarily know if the
131 // task will always be executed. This can happen if the task is cancellable
132 // or if it is posted to a TaskRunner.
133 //
134 //
135 // SIMPLE FUNCTIONS AND UTILITIES.
136 //
137 //   DoNothing() - Useful for creating a Closure that does nothing when called.
138 //   DeletePointer<T>() - Useful for creating a Closure that will delete a
139 //                        pointer when invoked. Only use this when necessary.
140 //                        In most cases MessageLoop::DeleteSoon() is a better
141 //                        fit.
142 
143 #ifndef BASE_BIND_HELPERS_H_
144 #define BASE_BIND_HELPERS_H_
145 
146 #include <stddef.h>
147 
148 #include <map>
149 #include <memory>
150 #include <type_traits>
151 #include <utility>
152 #include <vector>
153 
154 #include "base/callback.h"
155 #include "base/memory/weak_ptr.h"
156 #include "base/template_util.h"
157 #include "build/build_config.h"
158 
159 namespace base {
160 namespace internal {
161 
162 // Use the Substitution Failure Is Not An Error (SFINAE) trick to inspect T
163 // for the existence of AddRef() and Release() functions of the correct
164 // signature.
165 //
166 // http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error
167 // http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence
168 // http://stackoverflow.com/questions/4358584/sfinae-approach-comparison
169 // http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-member-functions
170 //
171 // The last link in particular show the method used below.
172 //
173 // For SFINAE to work with inherited methods, we need to pull some extra tricks
174 // with multiple inheritance.  In the more standard formulation, the overloads
175 // of Check would be:
176 //
177 //   template <typename C>
178 //   Yes NotTheCheckWeWant(Helper<&C::TargetFunc>*);
179 //
180 //   template <typename C>
181 //   No NotTheCheckWeWant(...);
182 //
183 //   static const bool value = sizeof(NotTheCheckWeWant<T>(0)) == sizeof(Yes);
184 //
185 // The problem here is that template resolution will not match
186 // C::TargetFunc if TargetFunc does not exist directly in C.  That is, if
187 // TargetFunc in inherited from an ancestor, &C::TargetFunc will not match,
188 // |value| will be false.  This formulation only checks for whether or
189 // not TargetFunc exist directly in the class being introspected.
190 //
191 // To get around this, we play a dirty trick with multiple inheritance.
192 // First, We create a class BaseMixin that declares each function that we
193 // want to probe for.  Then we create a class Base that inherits from both T
194 // (the class we wish to probe) and BaseMixin.  Note that the function
195 // signature in BaseMixin does not need to match the signature of the function
196 // we are probing for; thus it's easiest to just use void().
197 //
198 // Now, if TargetFunc exists somewhere in T, then &Base::TargetFunc has an
199 // ambiguous resolution between BaseMixin and T.  This lets us write the
200 // following:
201 //
202 //   template <typename C>
203 //   No GoodCheck(Helper<&C::TargetFunc>*);
204 //
205 //   template <typename C>
206 //   Yes GoodCheck(...);
207 //
208 //   static const bool value = sizeof(GoodCheck<Base>(0)) == sizeof(Yes);
209 //
210 // Notice here that the variadic version of GoodCheck() returns Yes here
211 // instead of No like the previous one. Also notice that we calculate |value|
212 // by specializing GoodCheck() on Base instead of T.
213 //
214 // We've reversed the roles of the variadic, and Helper overloads.
215 // GoodCheck(Helper<&C::TargetFunc>*), when C = Base, fails to be a valid
216 // substitution if T::TargetFunc exists. Thus GoodCheck<Base>(0) will resolve
217 // to the variadic version if T has TargetFunc.  If T::TargetFunc does not
218 // exist, then &C::TargetFunc is not ambiguous, and the overload resolution
219 // will prefer GoodCheck(Helper<&C::TargetFunc>*).
220 //
221 // This method of SFINAE will correctly probe for inherited names, but it cannot
222 // typecheck those names.  It's still a good enough sanity check though.
223 //
224 // Works on gcc-4.2, gcc-4.4, and Visual Studio 2008.
225 //
226 // TODO(ajwong): Move to ref_counted.h or template_util.h when we've vetted
227 // this works well.
228 //
229 // TODO(ajwong): Make this check for Release() as well.
230 // See http://crbug.com/82038.
231 template <typename T>
232 class SupportsAddRefAndRelease {
233   using Yes = char[1];
234   using No = char[2];
235 
236   struct BaseMixin {
237     void AddRef();
238   };
239 
240 // MSVC warns when you try to use Base if T has a private destructor, the
241 // common pattern for refcounted types. It does this even though no attempt to
242 // instantiate Base is made.  We disable the warning for this definition.
243 #if defined(OS_WIN)
244 #pragma warning(push)
245 #pragma warning(disable:4624)
246 #endif
247   struct Base : public T, public BaseMixin {
248   };
249 #if defined(OS_WIN)
250 #pragma warning(pop)
251 #endif
252 
253   template <void(BaseMixin::*)()> struct Helper {};
254 
255   template <typename C>
256   static No& Check(Helper<&C::AddRef>*);
257 
258   template <typename >
259   static Yes& Check(...);
260 
261  public:
262   enum { value = sizeof(Check<Base>(0)) == sizeof(Yes) };
263 };
264 
265 // Helpers to assert that arguments of a recounted type are bound with a
266 // scoped_refptr.
267 template <bool IsClasstype, typename T>
268 struct UnsafeBindtoRefCountedArgHelper : false_type {
269 };
270 
271 template <typename T>
272 struct UnsafeBindtoRefCountedArgHelper<true, T>
273     : integral_constant<bool, SupportsAddRefAndRelease<T>::value> {
274 };
275 
276 template <typename T>
277 struct UnsafeBindtoRefCountedArg : false_type {
278 };
279 
280 template <typename T>
281 struct UnsafeBindtoRefCountedArg<T*>
282     : UnsafeBindtoRefCountedArgHelper<is_class<T>::value, T> {
283 };
284 
285 template <typename T>
286 class HasIsMethodTag {
287   using Yes = char[1];
288   using No = char[2];
289 
290   template <typename U>
291   static Yes& Check(typename U::IsMethod*);
292 
293   template <typename U>
294   static No& Check(...);
295 
296  public:
297   enum { value = sizeof(Check<T>(0)) == sizeof(Yes) };
298 };
299 
300 template <typename T>
301 class UnretainedWrapper {
302  public:
303   explicit UnretainedWrapper(T* o) : ptr_(o) {}
304   T* get() const { return ptr_; }
305  private:
306   T* ptr_;
307 };
308 
309 template <typename T>
310 class ConstRefWrapper {
311  public:
312   explicit ConstRefWrapper(const T& o) : ptr_(&o) {}
313   const T& get() const { return *ptr_; }
314  private:
315   const T* ptr_;
316 };
317 
318 template <typename T>
319 struct IgnoreResultHelper {
320   explicit IgnoreResultHelper(T functor) : functor_(functor) {}
321 
322   T functor_;
323 };
324 
325 template <typename T>
326 struct IgnoreResultHelper<Callback<T> > {
327   explicit IgnoreResultHelper(const Callback<T>& functor) : functor_(functor) {}
328 
329   const Callback<T>& functor_;
330 };
331 
332 // An alternate implementation is to avoid the destructive copy, and instead
333 // specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to
334 // a class that is essentially a scoped_ptr<>.
335 //
336 // The current implementation has the benefit though of leaving ParamTraits<>
337 // fully in callback_internal.h as well as avoiding type conversions during
338 // storage.
339 template <typename T>
340 class OwnedWrapper {
341  public:
342   explicit OwnedWrapper(T* o) : ptr_(o) {}
343   ~OwnedWrapper() { delete ptr_; }
344   T* get() const { return ptr_; }
345   OwnedWrapper(const OwnedWrapper& other) {
346     ptr_ = other.ptr_;
347     other.ptr_ = NULL;
348   }
349 
350  private:
351   mutable T* ptr_;
352 };
353 
354 // PassedWrapper is a copyable adapter for a scoper that ignores const.
355 //
356 // It is needed to get around the fact that Bind() takes a const reference to
357 // all its arguments.  Because Bind() takes a const reference to avoid
358 // unnecessary copies, it is incompatible with movable-but-not-copyable
359 // types; doing a destructive "move" of the type into Bind() would violate
360 // the const correctness.
361 //
362 // This conundrum cannot be solved without either C++11 rvalue references or
363 // a O(2^n) blowup of Bind() templates to handle each combination of regular
364 // types and movable-but-not-copyable types.  Thus we introduce a wrapper type
365 // that is copyable to transmit the correct type information down into
366 // BindState<>. Ignoring const in this type makes sense because it is only
367 // created when we are explicitly trying to do a destructive move.
368 //
369 // Two notes:
370 //  1) PassedWrapper supports any type that has a move constructor, however
371 //     the type will need to be specifically whitelisted in order for it to be
372 //     bound to a Callback. We guard this explicitly at the call of Passed()
373 //     to make for clear errors. Things not given to Passed() will be forwarded
374 //     and stored by value which will not work for general move-only types.
375 //  2) is_valid_ is distinct from NULL because it is valid to bind a "NULL"
376 //     scoper to a Callback and allow the Callback to execute once.
377 template <typename T>
378 class PassedWrapper {
379  public:
380   explicit PassedWrapper(T&& scoper)
381       : is_valid_(true), scoper_(std::move(scoper)) {}
382   PassedWrapper(const PassedWrapper& other)
383       : is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {}
384   T Pass() const {
385     CHECK(is_valid_);
386     is_valid_ = false;
387     return std::move(scoper_);
388   }
389 
390  private:
391   mutable bool is_valid_;
392   mutable T scoper_;
393 };
394 
395 // Specialize PassedWrapper for std::unique_ptr used by base::Passed().
396 // Use std::move() to transfer the data from one storage to another.
397 template <typename T, typename D>
398 class PassedWrapper<std::unique_ptr<T, D>> {
399  public:
400   explicit PassedWrapper(std::unique_ptr<T, D> scoper)
401       : is_valid_(true), scoper_(std::move(scoper)) {}
402   PassedWrapper(const PassedWrapper& other)
403       : is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {}
404 
405   std::unique_ptr<T, D> Pass() const {
406     CHECK(is_valid_);
407     is_valid_ = false;
408     return std::move(scoper_);
409   }
410 
411  private:
412   mutable bool is_valid_;
413   mutable std::unique_ptr<T, D> scoper_;
414 };
415 
416 // Specialize PassedWrapper for std::vector<std::unique_ptr<T>>.
417 template <typename T, typename D, typename A>
418 class PassedWrapper<std::vector<std::unique_ptr<T, D>, A>> {
419  public:
420   explicit PassedWrapper(std::vector<std::unique_ptr<T, D>, A> scoper)
421       : is_valid_(true), scoper_(std::move(scoper)) {}
422   PassedWrapper(const PassedWrapper& other)
423       : is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {}
424 
425   std::vector<std::unique_ptr<T, D>, A> Pass() const {
426     CHECK(is_valid_);
427     is_valid_ = false;
428     return std::move(scoper_);
429   }
430 
431  private:
432   mutable bool is_valid_;
433   mutable std::vector<std::unique_ptr<T, D>, A> scoper_;
434 };
435 
436 // Specialize PassedWrapper for std::map<K, std::unique_ptr<T>>.
437 template <typename K, typename T, typename D, typename C, typename A>
438 class PassedWrapper<std::map<K, std::unique_ptr<T, D>, C, A>> {
439  public:
440   explicit PassedWrapper(std::map<K, std::unique_ptr<T, D>, C, A> scoper)
441       : is_valid_(true), scoper_(std::move(scoper)) {}
442   PassedWrapper(const PassedWrapper& other)
443       : is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {}
444 
445   std::map<K, std::unique_ptr<T, D>, C, A> Pass() const {
446     CHECK(is_valid_);
447     is_valid_ = false;
448     return std::move(scoper_);
449   }
450 
451  private:
452   mutable bool is_valid_;
453   mutable std::map<K, std::unique_ptr<T, D>, C, A> scoper_;
454 };
455 
456 // Unwrap the stored parameters for the wrappers above.
457 template <typename T>
458 struct UnwrapTraits {
459   using ForwardType = const T&;
460   static ForwardType Unwrap(const T& o) { return o; }
461 };
462 
463 template <typename T>
464 struct UnwrapTraits<UnretainedWrapper<T> > {
465   using ForwardType = T*;
466   static ForwardType Unwrap(UnretainedWrapper<T> unretained) {
467     return unretained.get();
468   }
469 };
470 
471 template <typename T>
472 struct UnwrapTraits<ConstRefWrapper<T> > {
473   using ForwardType = const T&;
474   static ForwardType Unwrap(ConstRefWrapper<T> const_ref) {
475     return const_ref.get();
476   }
477 };
478 
479 template <typename T>
480 struct UnwrapTraits<scoped_refptr<T> > {
481   using ForwardType = T*;
482   static ForwardType Unwrap(const scoped_refptr<T>& o) { return o.get(); }
483 };
484 
485 template <typename T>
486 struct UnwrapTraits<WeakPtr<T> > {
487   using ForwardType = const WeakPtr<T>&;
488   static ForwardType Unwrap(const WeakPtr<T>& o) { return o; }
489 };
490 
491 template <typename T>
492 struct UnwrapTraits<OwnedWrapper<T> > {
493   using ForwardType = T*;
494   static ForwardType Unwrap(const OwnedWrapper<T>& o) {
495     return o.get();
496   }
497 };
498 
499 template <typename T>
500 struct UnwrapTraits<PassedWrapper<T> > {
501   using ForwardType = T;
502   static T Unwrap(PassedWrapper<T>& o) {
503     return o.Pass();
504   }
505 };
506 
507 // Utility for handling different refcounting semantics in the Bind()
508 // function.
509 template <bool is_method, typename... T>
510 struct MaybeScopedRefPtr;
511 
512 template <bool is_method>
513 struct MaybeScopedRefPtr<is_method> {
514   MaybeScopedRefPtr() {}
515 };
516 
517 template <typename T, typename... Rest>
518 struct MaybeScopedRefPtr<false, T, Rest...> {
519   MaybeScopedRefPtr(const T&, const Rest&...) {}
520 };
521 
522 template <typename T, size_t n, typename... Rest>
523 struct MaybeScopedRefPtr<false, T[n], Rest...> {
524   MaybeScopedRefPtr(const T*, const Rest&...) {}
525 };
526 
527 template <typename T, typename... Rest>
528 struct MaybeScopedRefPtr<true, T, Rest...> {
529   MaybeScopedRefPtr(const T& /* o */, const Rest&...) {}
530 };
531 
532 template <typename T, typename... Rest>
533 struct MaybeScopedRefPtr<true, T*, Rest...> {
534   MaybeScopedRefPtr(T* o, const Rest&...) : ref_(o) {}
535   scoped_refptr<T> ref_;
536 };
537 
538 // No need to additionally AddRef() and Release() since we are storing a
539 // scoped_refptr<> inside the storage object already.
540 template <typename T, typename... Rest>
541 struct MaybeScopedRefPtr<true, scoped_refptr<T>, Rest...> {
542   MaybeScopedRefPtr(const scoped_refptr<T>&, const Rest&...) {}
543 };
544 
545 template <typename T, typename... Rest>
546 struct MaybeScopedRefPtr<true, const T*, Rest...> {
547   MaybeScopedRefPtr(const T* o, const Rest&...) : ref_(o) {}
548   scoped_refptr<const T> ref_;
549 };
550 
551 // IsWeakMethod is a helper that determine if we are binding a WeakPtr<> to a
552 // method.  It is used internally by Bind() to select the correct
553 // InvokeHelper that will no-op itself in the event the WeakPtr<> for
554 // the target object is invalidated.
555 //
556 // The first argument should be the type of the object that will be received by
557 // the method.
558 template <bool IsMethod, typename... Args>
559 struct IsWeakMethod : public false_type {};
560 
561 template <typename T, typename... Args>
562 struct IsWeakMethod<true, WeakPtr<T>, Args...> : public true_type {};
563 
564 template <typename T, typename... Args>
565 struct IsWeakMethod<true, ConstRefWrapper<WeakPtr<T>>, Args...>
566     : public true_type {};
567 
568 
569 // Packs a list of types to hold them in a single type.
570 template <typename... Types>
571 struct TypeList {};
572 
573 // Used for DropTypeListItem implementation.
574 template <size_t n, typename List>
575 struct DropTypeListItemImpl;
576 
577 // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure.
578 template <size_t n, typename T, typename... List>
579 struct DropTypeListItemImpl<n, TypeList<T, List...>>
580     : DropTypeListItemImpl<n - 1, TypeList<List...>> {};
581 
582 template <typename T, typename... List>
583 struct DropTypeListItemImpl<0, TypeList<T, List...>> {
584   using Type = TypeList<T, List...>;
585 };
586 
587 template <>
588 struct DropTypeListItemImpl<0, TypeList<>> {
589   using Type = TypeList<>;
590 };
591 
592 // A type-level function that drops |n| list item from given TypeList.
593 template <size_t n, typename List>
594 using DropTypeListItem = typename DropTypeListItemImpl<n, List>::Type;
595 
596 // Used for TakeTypeListItem implementation.
597 template <size_t n, typename List, typename... Accum>
598 struct TakeTypeListItemImpl;
599 
600 // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure.
601 template <size_t n, typename T, typename... List, typename... Accum>
602 struct TakeTypeListItemImpl<n, TypeList<T, List...>, Accum...>
603     : TakeTypeListItemImpl<n - 1, TypeList<List...>, Accum..., T> {};
604 
605 template <typename T, typename... List, typename... Accum>
606 struct TakeTypeListItemImpl<0, TypeList<T, List...>, Accum...> {
607   using Type = TypeList<Accum...>;
608 };
609 
610 template <typename... Accum>
611 struct TakeTypeListItemImpl<0, TypeList<>, Accum...> {
612   using Type = TypeList<Accum...>;
613 };
614 
615 // A type-level function that takes first |n| list item from given TypeList.
616 // E.g. TakeTypeListItem<3, TypeList<A, B, C, D>> is evaluated to
617 // TypeList<A, B, C>.
618 template <size_t n, typename List>
619 using TakeTypeListItem = typename TakeTypeListItemImpl<n, List>::Type;
620 
621 // Used for ConcatTypeLists implementation.
622 template <typename List1, typename List2>
623 struct ConcatTypeListsImpl;
624 
625 template <typename... Types1, typename... Types2>
626 struct ConcatTypeListsImpl<TypeList<Types1...>, TypeList<Types2...>> {
627   using Type = TypeList<Types1..., Types2...>;
628 };
629 
630 // A type-level function that concats two TypeLists.
631 template <typename List1, typename List2>
632 using ConcatTypeLists = typename ConcatTypeListsImpl<List1, List2>::Type;
633 
634 // Used for MakeFunctionType implementation.
635 template <typename R, typename ArgList>
636 struct MakeFunctionTypeImpl;
637 
638 template <typename R, typename... Args>
639 struct MakeFunctionTypeImpl<R, TypeList<Args...>> {
640   // MSVC 2013 doesn't support Type Alias of function types.
641   // Revisit this after we update it to newer version.
642   typedef R Type(Args...);
643 };
644 
645 // A type-level function that constructs a function type that has |R| as its
646 // return type and has TypeLists items as its arguments.
647 template <typename R, typename ArgList>
648 using MakeFunctionType = typename MakeFunctionTypeImpl<R, ArgList>::Type;
649 
650 // Used for ExtractArgs.
651 template <typename Signature>
652 struct ExtractArgsImpl;
653 
654 template <typename R, typename... Args>
655 struct ExtractArgsImpl<R(Args...)> {
656   using Type = TypeList<Args...>;
657 };
658 
659 // A type-level function that extracts function arguments into a TypeList.
660 // E.g. ExtractArgs<R(A, B, C)> is evaluated to TypeList<A, B, C>.
661 template <typename Signature>
662 using ExtractArgs = typename ExtractArgsImpl<Signature>::Type;
663 
664 }  // namespace internal
665 
666 template <typename T>
667 static inline internal::UnretainedWrapper<T> Unretained(T* o) {
668   return internal::UnretainedWrapper<T>(o);
669 }
670 
671 template <typename T>
672 static inline internal::ConstRefWrapper<T> ConstRef(const T& o) {
673   return internal::ConstRefWrapper<T>(o);
674 }
675 
676 template <typename T>
677 static inline internal::OwnedWrapper<T> Owned(T* o) {
678   return internal::OwnedWrapper<T>(o);
679 }
680 
681 // We offer 2 syntaxes for calling Passed().  The first takes an rvalue and
682 // is best suited for use with the return value of a function or other temporary
683 // rvalues. The second takes a pointer to the scoper and is just syntactic sugar
684 // to avoid having to write Passed(std::move(scoper)).
685 //
686 // Both versions of Passed() prevent T from being an lvalue reference. The first
687 // via use of enable_if, and the second takes a T* which will not bind to T&.
688 template <typename T,
689           typename std::enable_if<internal::IsMoveOnlyType<T>::value &&
690                                   !std::is_lvalue_reference<T>::value>::type* =
691               nullptr>
692 static inline internal::PassedWrapper<T> Passed(T&& scoper) {
693   return internal::PassedWrapper<T>(std::move(scoper));
694 }
695 template <typename T,
696           typename std::enable_if<internal::IsMoveOnlyType<T>::value>::type* =
697               nullptr>
698 static inline internal::PassedWrapper<T> Passed(T* scoper) {
699   return internal::PassedWrapper<T>(std::move(*scoper));
700 }
701 
702 template <typename T>
703 static inline internal::IgnoreResultHelper<T> IgnoreResult(T data) {
704   return internal::IgnoreResultHelper<T>(data);
705 }
706 
707 template <typename T>
708 static inline internal::IgnoreResultHelper<Callback<T> >
709 IgnoreResult(const Callback<T>& data) {
710   return internal::IgnoreResultHelper<Callback<T> >(data);
711 }
712 
713 BASE_EXPORT void DoNothing();
714 
715 template<typename T>
716 void DeletePointer(T* obj) {
717   delete obj;
718 }
719 
720 }  // namespace base
721 
722 #endif  // BASE_BIND_HELPERS_H_
723