OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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_CALLBACK_TUPLE_H_ |
| 6 #define BASE_CALLBACK_TUPLE_H_ |
| 7 |
| 8 #include "base/callback_internal.h" |
| 9 |
| 10 namespace base { |
| 11 namespace internal { |
| 12 |
| 13 template <typename... Types> |
| 14 struct TypeList {}; |
| 15 |
| 16 template <bool IsWeakCall, typename ReturnType, typename Runnable, |
| 17 typename ArgsType> |
| 18 struct InvokeHelper; |
| 19 |
| 20 class BindStateBase; |
| 21 |
| 22 template <int index, typename... Args> |
| 23 struct TupleImpl {}; |
| 24 |
| 25 template <int index, typename T, typename... Rest> |
| 26 struct TupleImpl<index, T, Rest...> : TupleImpl<index + 1, Rest...> { |
| 27 TupleImpl(const T& value, const Rest&... rest) |
| 28 : TupleImpl<index + 1, Rest...>(rest...), |
| 29 value_(value) {} |
| 30 T value_; |
| 31 }; |
| 32 |
| 33 template <typename... Args> |
| 34 struct Tuple : TupleImpl<0, Args...> { |
| 35 Tuple(const Args&... args) : TupleImpl<0, Args...>(args...) {} |
| 36 }; |
| 37 |
| 38 template <int i, typename... Types> |
| 39 struct NthType; |
| 40 |
| 41 template <typename T, typename... Rest> |
| 42 struct NthType<0, T, Rest...> { |
| 43 typedef T Result; |
| 44 }; |
| 45 |
| 46 template <int i, typename T, typename... Rest> |
| 47 struct NthType<i, T, Rest...> : NthType<i-1, Rest...> { |
| 48 }; |
| 49 |
| 50 template <int i, typename TupleType> |
| 51 struct NthTupleType; |
| 52 |
| 53 template <int i, typename... Args> |
| 54 struct NthTupleType<i, Tuple<Args...> > : NthType<i, Args...> { |
| 55 }; |
| 56 |
| 57 template <int i, typename T, typename... Rest> |
| 58 T* GetNth(TupleImpl<i, T, Rest...>* tuple) { |
| 59 return &tuple->value_; |
| 60 } |
| 61 |
| 62 template <int n, typename R, typename... Args> |
| 63 struct RunTypeHelper; |
| 64 |
| 65 template <typename R, typename... Args> |
| 66 struct RunTypeHelper<0, R, Args...> { |
| 67 typedef R(RunType)( |
| 68 BindStateBase*, |
| 69 typename CallbackParamTraits<Args>::ForwardType...); |
| 70 typedef R(UnboundRunType)(Args...); |
| 71 }; |
| 72 |
| 73 template <typename R, typename T, typename... Args> |
| 74 struct RunTypeHelper<0, R, T, Args...> { |
| 75 typedef R(RunType)( |
| 76 BindStateBase*, |
| 77 typename CallbackParamTraits<T>::ForwardType, |
| 78 typename CallbackParamTraits<Args>::ForwardType...); |
| 79 typedef R(UnboundRunType)(T, Args...); |
| 80 }; |
| 81 |
| 82 template <int n, typename R, typename T, typename... Args> |
| 83 struct RunTypeHelper<n, R, T, Args...> |
| 84 : public RunTypeHelper<n-1, R, Args...> { |
| 85 }; |
| 86 |
| 87 template <int i, typename StorageType, typename R, typename... Args> |
| 88 struct ExtractHelper { |
| 89 static R Invoke(StorageType* storage, Args... args) { |
| 90 typedef typename NthTupleType< |
| 91 i-1, typename StorageType::BoundType>::Result T; |
| 92 typedef UnwrapTraits<T> Unwrapper; |
| 93 typename Unwrapper::ForwardType forwarding = |
| 94 Unwrapper::Unwrap(*GetNth<i-1>(&storage->bound_args_)); |
| 95 |
| 96 return ExtractHelper<i-1, StorageType, R, |
| 97 typename Unwrapper::ForwardType, |
| 98 Args...>::Invoke( |
| 99 storage, |
| 100 CallbackForward(forwarding), |
| 101 CallbackForward(args)...); |
| 102 } |
| 103 }; |
| 104 |
| 105 template <typename StorageType, typename R, typename... Args> |
| 106 struct ExtractHelper<0, StorageType, R, Args...> { |
| 107 static R Invoke(StorageType* storage, Args... args) { |
| 108 return InvokeHelper< |
| 109 StorageType::IsWeakCall::value, R, |
| 110 typename StorageType::RunnableType, |
| 111 TypeList<typename CallbackParamTraits<Args>::ForwardType...>> |
| 112 ::MakeItSo(storage->runnable_, CallbackForward(args)...); |
| 113 } |
| 114 }; |
| 115 |
| 116 } // namespace internal |
| 117 } // namespace base |
| 118 |
| 119 #endif // BASE_CALLBACK_TUPLE_H_ |
OLD | NEW |