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