[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 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 | #ifndef BASE_BIND_INTERNAL_H_ |
| 6 | #define BASE_BIND_INTERNAL_H_ |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 7 | |
vmpstr | c52317f | 2015-11-18 08:43:26 | [diff] [blame] | 8 | #include <type_traits> |
| 9 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 10 | #include "base/bind_helpers.h" |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 11 | #include "base/callback_internal.h" |
[email protected] | 8217d454 | 2011-10-01 06:31:41 | [diff] [blame] | 12 | #include "base/memory/raw_scoped_refptr_mismatch_checker.h" |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 14 | #include "base/template_util.h" |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 15 | #include "base/tuple.h" |
[email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 16 | #include "build/build_config.h" |
| 17 | |
| 18 | #if defined(OS_WIN) |
| 19 | #include "base/bind_internal_win.h" |
| 20 | #endif |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 21 | |
| 22 | namespace base { |
| 23 | namespace internal { |
| 24 | |
[email protected] | 2429264 | 2012-07-12 20:06:40 | [diff] [blame] | 25 | // See base/callback.h for user documentation. |
| 26 | // |
| 27 | // |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 28 | // CONCEPTS: |
| 29 | // Runnable -- A type (really a type class) that has a single Run() method |
| 30 | // and a RunType typedef that corresponds to the type of Run(). |
| 31 | // A Runnable can declare that it should treated like a method |
| 32 | // call by including a typedef named IsMethod. The value of |
| 33 | // this typedef is NOT inspected, only the existence. When a |
| 34 | // Runnable declares itself a method, Bind() will enforce special |
| 35 | // refcounting + WeakPtr handling semantics for the first |
| 36 | // parameter which is expected to be an object. |
| 37 | // Functor -- A copyable type representing something that should be called. |
| 38 | // All function pointers, Callback<>, and Runnables are functors |
| 39 | // even if the invocation syntax differs. |
| 40 | // RunType -- A function type (as opposed to function _pointer_ type) for |
| 41 | // a Run() function. Usually just a convenience typedef. |
tzik | ce3ecf8 | 2015-12-15 06:41:49 | [diff] [blame] | 42 | // (Bound)Args -- A set of types that stores the arguments. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 43 | // |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 44 | // Types: |
| 45 | // RunnableAdapter<> -- Wraps the various "function" pointer types into an |
| 46 | // object that adheres to the Runnable interface. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 47 | // ForceVoidReturn<> -- Helper class for translating function signatures to |
| 48 | // equivalent forms with a "void" return type. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 49 | // FunctorTraits<> -- Type traits used determine the correct RunType and |
| 50 | // RunnableType for a Functor. This is where function |
| 51 | // signature adapters are applied. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 52 | // MakeRunnable<> -- Takes a Functor and returns an object in the Runnable |
| 53 | // type class that represents the underlying Functor. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 54 | // InvokeHelper<> -- Take a Runnable + arguments and actully invokes it. |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 55 | // Handle the differing syntaxes needed for WeakPtr<> |
| 56 | // support, and for ignoring return values. This is separate |
| 57 | // from Invoker to avoid creating multiple version of |
| 58 | // Invoker<>. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 59 | // Invoker<> -- Unwraps the curried parameters and executes the Runnable. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 60 | // BindState<> -- Stores the curried parameters, and is the main entry point |
| 61 | // into the Bind() system, doing most of the type resolution. |
| 62 | // There are ARITY BindState types. |
[email protected] | 4346ef91 | 2011-02-19 00:52:15 | [diff] [blame] | 63 | |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 64 | // HasNonConstReferenceParam selects true_type when any of the parameters in |
| 65 | // |Sig| is a non-const reference. |
| 66 | // Implementation note: This non-specialized case handles zero-arity case only. |
| 67 | // Non-zero-arity cases should be handled by the specialization below. |
tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 68 | template <typename List> |
| 69 | struct HasNonConstReferenceItem : false_type {}; |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 70 | |
| 71 | // Implementation note: Select true_type if the first parameter is a non-const |
| 72 | // reference. Otherwise, skip the first parameter and check rest of parameters |
| 73 | // recursively. |
tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 74 | template <typename T, typename... Args> |
| 75 | struct HasNonConstReferenceItem<TypeList<T, Args...>> |
vmpstr | c52317f | 2015-11-18 08:43:26 | [diff] [blame] | 76 | : std::conditional<is_non_const_reference<T>::value, |
| 77 | true_type, |
tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 78 | HasNonConstReferenceItem<TypeList<Args...>>>::type {}; |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 79 | |
| 80 | // HasRefCountedTypeAsRawPtr selects true_type when any of the |Args| is a raw |
| 81 | // pointer to a RefCounted type. |
| 82 | // Implementation note: This non-specialized case handles zero-arity case only. |
| 83 | // Non-zero-arity cases should be handled by the specialization below. |
| 84 | template <typename... Args> |
| 85 | struct HasRefCountedTypeAsRawPtr : false_type {}; |
| 86 | |
| 87 | // Implementation note: Select true_type if the first parameter is a raw pointer |
| 88 | // to a RefCounted type. Otherwise, skip the first parameter and check rest of |
| 89 | // parameters recursively. |
| 90 | template <typename T, typename... Args> |
| 91 | struct HasRefCountedTypeAsRawPtr<T, Args...> |
vmpstr | c52317f | 2015-11-18 08:43:26 | [diff] [blame] | 92 | : std::conditional<NeedsScopedRefptrButGetsRawPtr<T>::value, |
| 93 | true_type, |
| 94 | HasRefCountedTypeAsRawPtr<Args...>>::type {}; |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 95 | |
| 96 | // BindsArrayToFirstArg selects true_type when |is_method| is true and the first |
| 97 | // item of |Args| is an array type. |
| 98 | // Implementation note: This non-specialized case handles !is_method case and |
| 99 | // zero-arity case only. Other cases should be handled by the specialization |
| 100 | // below. |
| 101 | template <bool is_method, typename... Args> |
| 102 | struct BindsArrayToFirstArg : false_type {}; |
| 103 | |
| 104 | template <typename T, typename... Args> |
| 105 | struct BindsArrayToFirstArg<true, T, Args...> : is_array<T> {}; |
| 106 | |
| 107 | // HasRefCountedParamAsRawPtr is the same to HasRefCountedTypeAsRawPtr except |
| 108 | // when |is_method| is true HasRefCountedParamAsRawPtr skips the first argument. |
| 109 | // Implementation note: This non-specialized case handles !is_method case and |
| 110 | // zero-arity case only. Other cases should be handled by the specialization |
| 111 | // below. |
| 112 | template <bool is_method, typename... Args> |
| 113 | struct HasRefCountedParamAsRawPtr : HasRefCountedTypeAsRawPtr<Args...> {}; |
| 114 | |
| 115 | template <typename T, typename... Args> |
| 116 | struct HasRefCountedParamAsRawPtr<true, T, Args...> |
| 117 | : HasRefCountedTypeAsRawPtr<Args...> {}; |
| 118 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 119 | // RunnableAdapter<> |
| 120 | // |
| 121 | // The RunnableAdapter<> templates provide a uniform interface for invoking |
| 122 | // a function pointer, method pointer, or const method pointer. The adapter |
| 123 | // exposes a Run() method with an appropriate signature. Using this wrapper |
| 124 | // allows for writing code that supports all three pointer types without |
| 125 | // undue repetition. Without it, a lot of code would need to be repeated 3 |
| 126 | // times. |
| 127 | // |
| 128 | // For method pointers and const method pointers the first argument to Run() |
| 129 | // is considered to be the received of the method. This is similar to STL's |
| 130 | // mem_fun(). |
| 131 | // |
| 132 | // This class also exposes a RunType typedef that is the function type of the |
| 133 | // Run() function. |
| 134 | // |
| 135 | // If and only if the wrapper contains a method or const method pointer, an |
| 136 | // IsMethod typedef is exposed. The existence of this typedef (NOT the value) |
| 137 | // marks that the wrapper should be considered a method wrapper. |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 138 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 139 | template <typename Functor> |
| 140 | class RunnableAdapter; |
[email protected] | 4346ef91 | 2011-02-19 00:52:15 | [diff] [blame] | 141 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 142 | // Function. |
| 143 | template <typename R, typename... Args> |
| 144 | class RunnableAdapter<R(*)(Args...)> { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 145 | public: |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 146 | typedef R (RunType)(Args...); |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 147 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 148 | explicit RunnableAdapter(R(*function)(Args...)) |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 149 | : function_(function) { |
| 150 | } |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 151 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 152 | R Run(typename CallbackParamTraits<Args>::ForwardType... args) { |
| 153 | return function_(CallbackForward(args)...); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private: |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 157 | R (*function_)(Args...); |
[email protected] | 4346ef91 | 2011-02-19 00:52:15 | [diff] [blame] | 158 | }; |
| 159 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 160 | // Method. |
| 161 | template <typename R, typename T, typename... Args> |
| 162 | class RunnableAdapter<R(T::*)(Args...)> { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 163 | public: |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 164 | typedef R (RunType)(T*, Args...); |
[email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 165 | typedef true_type IsMethod; |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 166 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 167 | explicit RunnableAdapter(R(T::*method)(Args...)) |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 168 | : method_(method) { |
| 169 | } |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 170 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 171 | R Run(T* object, typename CallbackParamTraits<Args>::ForwardType... args) { |
| 172 | return (object->*method_)(CallbackForward(args)...); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 173 | } |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 174 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 175 | private: |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 176 | R (T::*method_)(Args...); |
[email protected] | 4346ef91 | 2011-02-19 00:52:15 | [diff] [blame] | 177 | }; |
| 178 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 179 | // Const Method. |
| 180 | template <typename R, typename T, typename... Args> |
| 181 | class RunnableAdapter<R(T::*)(Args...) const> { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 182 | public: |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 183 | typedef R (RunType)(const T*, Args...); |
[email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 184 | typedef true_type IsMethod; |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 185 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 186 | explicit RunnableAdapter(R(T::*method)(Args...) const) |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 187 | : method_(method) { |
| 188 | } |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 189 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 190 | R Run(const T* object, |
| 191 | typename CallbackParamTraits<Args>::ForwardType... args) { |
| 192 | return (object->*method_)(CallbackForward(args)...); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 193 | } |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 194 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 195 | private: |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 196 | R (T::*method_)(Args...) const; |
[email protected] | 4346ef91 | 2011-02-19 00:52:15 | [diff] [blame] | 197 | }; |
| 198 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 199 | |
| 200 | // ForceVoidReturn<> |
| 201 | // |
| 202 | // Set of templates that support forcing the function return type to void. |
| 203 | template <typename Sig> |
| 204 | struct ForceVoidReturn; |
| 205 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 206 | template <typename R, typename... Args> |
| 207 | struct ForceVoidReturn<R(Args...)> { |
| 208 | typedef void(RunType)(Args...); |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 209 | }; |
| 210 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 211 | |
| 212 | // FunctorTraits<> |
| 213 | // |
| 214 | // See description at top of file. |
| 215 | template <typename T> |
| 216 | struct FunctorTraits { |
| 217 | typedef RunnableAdapter<T> RunnableType; |
| 218 | typedef typename RunnableType::RunType RunType; |
| 219 | }; |
| 220 | |
| 221 | template <typename T> |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 222 | struct FunctorTraits<IgnoreResultHelper<T>> { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 223 | typedef typename FunctorTraits<T>::RunnableType RunnableType; |
| 224 | typedef typename ForceVoidReturn< |
| 225 | typename RunnableType::RunType>::RunType RunType; |
| 226 | }; |
| 227 | |
| 228 | template <typename T> |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 229 | struct FunctorTraits<Callback<T>> { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 230 | typedef Callback<T> RunnableType; |
| 231 | typedef typename Callback<T>::RunType RunType; |
| 232 | }; |
| 233 | |
| 234 | |
| 235 | // MakeRunnable<> |
| 236 | // |
| 237 | // Converts a passed in functor to a RunnableType using type inference. |
| 238 | |
| 239 | template <typename T> |
| 240 | typename FunctorTraits<T>::RunnableType MakeRunnable(const T& t) { |
| 241 | return RunnableAdapter<T>(t); |
| 242 | } |
| 243 | |
| 244 | template <typename T> |
| 245 | typename FunctorTraits<T>::RunnableType |
| 246 | MakeRunnable(const IgnoreResultHelper<T>& t) { |
| 247 | return MakeRunnable(t.functor_); |
| 248 | } |
| 249 | |
| 250 | template <typename T> |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 251 | const typename FunctorTraits<Callback<T>>::RunnableType& |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 252 | MakeRunnable(const Callback<T>& t) { |
[email protected] | 8cf362c | 2012-11-20 08:28:14 | [diff] [blame] | 253 | DCHECK(!t.is_null()); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 254 | return t; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | // InvokeHelper<> |
| 259 | // |
| 260 | // There are 3 logical InvokeHelper<> specializations: normal, void-return, |
| 261 | // WeakCalls. |
| 262 | // |
| 263 | // The normal type just calls the underlying runnable. |
| 264 | // |
| 265 | // We need a InvokeHelper to handle void return types in order to support |
| 266 | // IgnoreResult(). Normally, if the Runnable's RunType had a void return, |
| 267 | // the template system would just accept "return functor.Run()" ignoring |
| 268 | // the fact that a void function is being used with return. This piece of |
| 269 | // sugar breaks though when the Runnable's RunType is not void. Thus, we |
| 270 | // need a partial specialization to change the syntax to drop the "return" |
| 271 | // from the invocation call. |
| 272 | // |
| 273 | // WeakCalls similarly need special syntax that is applied to the first |
| 274 | // argument to check if they should no-op themselves. |
| 275 | template <bool IsWeakCall, typename ReturnType, typename Runnable, |
| 276 | typename ArgsType> |
| 277 | struct InvokeHelper; |
| 278 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 279 | template <typename ReturnType, typename Runnable, typename... Args> |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 280 | struct InvokeHelper<false, ReturnType, Runnable, TypeList<Args...>> { |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 281 | static ReturnType MakeItSo(Runnable runnable, Args... args) { |
| 282 | return runnable.Run(CallbackForward(args)...); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 283 | } |
| 284 | }; |
| 285 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 286 | template <typename Runnable, typename... Args> |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 287 | struct InvokeHelper<false, void, Runnable, TypeList<Args...>> { |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 288 | static void MakeItSo(Runnable runnable, Args... args) { |
| 289 | runnable.Run(CallbackForward(args)...); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 290 | } |
| 291 | }; |
| 292 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 293 | template <typename Runnable, typename BoundWeakPtr, typename... Args> |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 294 | struct InvokeHelper<true, void, Runnable, TypeList<BoundWeakPtr, Args...>> { |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 295 | static void MakeItSo(Runnable runnable, BoundWeakPtr weak_ptr, Args... args) { |
[email protected] | f0737afd | 2013-06-03 22:15:21 | [diff] [blame] | 296 | if (!weak_ptr.get()) { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 297 | return; |
| 298 | } |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 299 | runnable.Run(weak_ptr.get(), CallbackForward(args)...); |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 300 | } |
| 301 | }; |
| 302 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 303 | #if !defined(_MSC_VER) |
| 304 | |
| 305 | template <typename ReturnType, typename Runnable, typename ArgsType> |
| 306 | struct InvokeHelper<true, ReturnType, Runnable, ArgsType> { |
| 307 | // WeakCalls are only supported for functions with a void return type. |
| 308 | // Otherwise, the function result would be undefined if the the WeakPtr<> |
| 309 | // is invalidated. |
avi | 4ec0dff | 2015-11-24 14:26:24 | [diff] [blame] | 310 | static_assert(is_void<ReturnType>::value, |
| 311 | "weak_ptrs can only bind to methods without return values"); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 312 | }; |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 313 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 314 | #endif |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 315 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 316 | // Invoker<> |
| 317 | // |
| 318 | // See description at the top of the file. |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 319 | template <typename BoundIndices, |
| 320 | typename StorageType, typename Unwrappers, |
| 321 | typename InvokeHelperType, typename UnboundForwardRunType> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 322 | struct Invoker; |
| 323 | |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 324 | template <size_t... bound_indices, |
| 325 | typename StorageType, |
| 326 | typename... Unwrappers, |
| 327 | typename InvokeHelperType, |
| 328 | typename R, |
| 329 | typename... UnboundForwardArgs> |
| 330 | struct Invoker<IndexSequence<bound_indices...>, |
| 331 | StorageType, TypeList<Unwrappers...>, |
| 332 | InvokeHelperType, R(UnboundForwardArgs...)> { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 333 | static R Run(BindStateBase* base, |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 334 | UnboundForwardArgs... unbound_args) { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 335 | StorageType* storage = static_cast<StorageType*>(base); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 336 | // Local references to make debugger stepping easier. If in a debugger, |
| 337 | // you really want to warp ahead and step through the |
| 338 | // InvokeHelper<>::MakeItSo() call below. |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 339 | return InvokeHelperType::MakeItSo( |
| 340 | storage->runnable_, |
| 341 | Unwrappers::Unwrap(get<bound_indices>(storage->bound_args_))..., |
| 342 | CallbackForward(unbound_args)...); |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 343 | } |
| 344 | }; |
| 345 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 346 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 347 | // BindState<> |
| 348 | // |
| 349 | // This stores all the state passed into Bind() and is also where most |
| 350 | // of the template resolution magic occurs. |
| 351 | // |
| 352 | // Runnable is the functor we are binding arguments to. |
| 353 | // RunType is type of the Run() function that the Invoker<> should use. |
| 354 | // Normally, this is the same as the RunType of the Runnable, but it can |
| 355 | // be different if an adapter like IgnoreResult() has been used. |
| 356 | // |
tzik | ce3ecf8 | 2015-12-15 06:41:49 | [diff] [blame] | 357 | // BoundArgs contains the storage type for all the bound arguments. |
| 358 | template <typename Runnable, typename RunType, typename... BoundArgs> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 359 | struct BindState; |
| 360 | |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 361 | template <typename Runnable, |
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 362 | typename R, |
| 363 | typename... Args, |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 364 | typename... BoundArgs> |
tzik | ce3ecf8 | 2015-12-15 06:41:49 | [diff] [blame] | 365 | struct BindState<Runnable, R(Args...), BoundArgs...> final |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 366 | : public BindStateBase { |
| 367 | private: |
tzik | ce3ecf8 | 2015-12-15 06:41:49 | [diff] [blame] | 368 | using StorageType = BindState<Runnable, R(Args...), BoundArgs...>; |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 369 | using RunnableType = Runnable; |
| 370 | |
| 371 | // true_type if Runnable is a method invocation and the first bound argument |
| 372 | // is a WeakPtr. |
| 373 | using IsWeakCall = |
| 374 | IsWeakMethod<HasIsMethodTag<Runnable>::value, BoundArgs...>; |
| 375 | |
| 376 | using BoundIndices = MakeIndexSequence<sizeof...(BoundArgs)>; |
| 377 | using Unwrappers = TypeList<UnwrapTraits<BoundArgs>...>; |
| 378 | using UnboundForwardArgs = DropTypeListItem< |
| 379 | sizeof...(BoundArgs), |
| 380 | TypeList<typename CallbackParamTraits<Args>::ForwardType...>>; |
| 381 | using UnboundForwardRunType = MakeFunctionType<R, UnboundForwardArgs>; |
| 382 | |
| 383 | using InvokeHelperArgs = ConcatTypeLists< |
| 384 | TypeList<typename UnwrapTraits<BoundArgs>::ForwardType...>, |
| 385 | UnboundForwardArgs>; |
| 386 | using InvokeHelperType = |
| 387 | InvokeHelper<IsWeakCall::value, R, Runnable, InvokeHelperArgs>; |
| 388 | |
| 389 | using UnboundArgs = DropTypeListItem<sizeof...(BoundArgs), TypeList<Args...>>; |
| 390 | |
| 391 | public: |
| 392 | using InvokerType = Invoker<BoundIndices, StorageType, Unwrappers, |
| 393 | InvokeHelperType, UnboundForwardRunType>; |
| 394 | using UnboundRunType = MakeFunctionType<R, UnboundArgs>; |
| 395 | |
| 396 | BindState(const Runnable& runnable, const BoundArgs&... bound_args) |
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 397 | : BindStateBase(&Destroy), |
| 398 | runnable_(runnable), |
| 399 | ref_(bound_args...), |
| 400 | bound_args_(bound_args...) {} |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 401 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 402 | RunnableType runnable_; |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 403 | MaybeScopedRefPtr<HasIsMethodTag<Runnable>::value, BoundArgs...> ref_; |
| 404 | Tuple<BoundArgs...> bound_args_; |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 405 | |
| 406 | private: |
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 407 | ~BindState() {} |
| 408 | |
| 409 | static void Destroy(BindStateBase* self) { |
| 410 | delete static_cast<BindState*>(self); |
| 411 | } |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 412 | }; |
| 413 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 414 | } // namespace internal |
| 415 | } // namespace base |
| 416 | |
| 417 | #endif // BASE_BIND_INTERNAL_H_ |