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