[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 | |
jdoerrie | c79c2fa2 | 2019-02-26 12:35:03 | [diff] [blame] | 10 | #include <functional> |
jdoerrie | 68f2d51b | 2019-02-28 17:32:44 | [diff] [blame] | 11 | #include <memory> |
jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 12 | #include <tuple> |
vmpstr | c52317f | 2015-11-18 08:43:26 | [diff] [blame] | 13 | #include <type_traits> |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 14 | #include <utility> |
vmpstr | c52317f | 2015-11-18 08:43:26 | [diff] [blame] | 15 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 16 | #include "base/bind.h" |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 17 | #include "base/callback_internal.h" |
Sylvain Defresne | ec3270c | 2018-05-31 17:19:15 | [diff] [blame] | 18 | #include "base/compiler_specific.h" |
[email protected] | 8217d454 | 2011-10-01 06:31:41 | [diff] [blame] | 19 | #include "base/memory/raw_scoped_refptr_mismatch_checker.h" |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 20 | #include "base/memory/weak_ptr.h" |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 21 | #include "base/template_util.h" |
[email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 22 | #include "build/build_config.h" |
| 23 | |
Sylvain Defresne | ec3270c | 2018-05-31 17:19:15 | [diff] [blame] | 24 | #if defined(OS_MACOSX) && !HAS_FEATURE(objc_arc) |
| 25 | #include "base/mac/scoped_block.h" |
| 26 | #endif |
| 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: |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 32 | // Functor -- A movable type representing something that should be called. |
| 33 | // All function pointers and Callback<> are functors even if the |
| 34 | // invocation syntax differs. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 35 | // RunType -- A function type (as opposed to function _pointer_ type) for |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 36 | // a Callback<>::Run(). Usually just a convenience typedef. |
tzik | ce3ecf8 | 2015-12-15 06:41:49 | [diff] [blame] | 37 | // (Bound)Args -- A set of types that stores the arguments. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 38 | // |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 39 | // Types: |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 40 | // ForceVoidReturn<> -- Helper class for translating function signatures to |
| 41 | // equivalent forms with a "void" return type. |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 42 | // FunctorTraits<> -- Type traits used to determine the correct RunType and |
| 43 | // invocation manner for a Functor. This is where function |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 44 | // signature adapters are applied. |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 45 | // InvokeHelper<> -- Take a Functor + arguments and actully invokes it. |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 46 | // Handle the differing syntaxes needed for WeakPtr<> |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 47 | // support. This is separate from Invoker to avoid creating |
| 48 | // multiple version of Invoker<>. |
| 49 | // Invoker<> -- Unwraps the curried parameters and executes the Functor. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 50 | // BindState<> -- Stores the curried parameters, and is the main entry point |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 51 | // into the Bind() system. |
[email protected] | 4346ef91 | 2011-02-19 00:52:15 | [diff] [blame] | 52 | |
tzik | c44f810 | 2018-07-24 09:49:19 | [diff] [blame] | 53 | #if defined(OS_WIN) |
| 54 | namespace Microsoft { |
| 55 | namespace WRL { |
| 56 | template <typename> |
| 57 | class ComPtr; |
| 58 | } // namespace WRL |
| 59 | } // namespace Microsoft |
| 60 | #endif |
| 61 | |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 62 | namespace base { |
| 63 | |
| 64 | template <typename T> |
| 65 | struct IsWeakReceiver; |
| 66 | |
| 67 | template <typename> |
| 68 | struct BindUnwrapTraits; |
| 69 | |
| 70 | template <typename Functor, typename BoundArgsTuple, typename SFINAE = void> |
| 71 | struct CallbackCancellationTraits; |
| 72 | |
| 73 | namespace internal { |
| 74 | |
| 75 | template <typename Functor, typename SFINAE = void> |
| 76 | struct FunctorTraits; |
| 77 | |
| 78 | template <typename T> |
| 79 | class UnretainedWrapper { |
| 80 | public: |
| 81 | explicit UnretainedWrapper(T* o) : ptr_(o) {} |
| 82 | T* get() const { return ptr_; } |
| 83 | |
| 84 | private: |
| 85 | T* ptr_; |
| 86 | }; |
| 87 | |
| 88 | template <typename T> |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 89 | class RetainedRefWrapper { |
| 90 | public: |
| 91 | explicit RetainedRefWrapper(T* o) : ptr_(o) {} |
| 92 | explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {} |
| 93 | T* get() const { return ptr_.get(); } |
| 94 | |
| 95 | private: |
| 96 | scoped_refptr<T> ptr_; |
| 97 | }; |
| 98 | |
| 99 | template <typename T> |
| 100 | struct IgnoreResultHelper { |
| 101 | explicit IgnoreResultHelper(T functor) : functor_(std::move(functor)) {} |
| 102 | explicit operator bool() const { return !!functor_; } |
| 103 | |
| 104 | T functor_; |
| 105 | }; |
| 106 | |
Marijn Kruisselbrink | c1e775f | 2019-12-19 01:24:22 | [diff] [blame] | 107 | template <typename T, typename Deleter = std::default_delete<T>> |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 108 | class OwnedWrapper { |
| 109 | public: |
| 110 | explicit OwnedWrapper(T* o) : ptr_(o) {} |
Marijn Kruisselbrink | c1e775f | 2019-12-19 01:24:22 | [diff] [blame] | 111 | explicit OwnedWrapper(std::unique_ptr<T, Deleter>&& ptr) |
| 112 | : ptr_(std::move(ptr)) {} |
jdoerrie | 68f2d51b | 2019-02-28 17:32:44 | [diff] [blame] | 113 | T* get() const { return ptr_.get(); } |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 114 | |
| 115 | private: |
Marijn Kruisselbrink | c1e775f | 2019-12-19 01:24:22 | [diff] [blame] | 116 | std::unique_ptr<T, Deleter> ptr_; |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | // PassedWrapper is a copyable adapter for a scoper that ignores const. |
| 120 | // |
| 121 | // It is needed to get around the fact that Bind() takes a const reference to |
| 122 | // all its arguments. Because Bind() takes a const reference to avoid |
| 123 | // unnecessary copies, it is incompatible with movable-but-not-copyable |
| 124 | // types; doing a destructive "move" of the type into Bind() would violate |
| 125 | // the const correctness. |
| 126 | // |
| 127 | // This conundrum cannot be solved without either C++11 rvalue references or |
| 128 | // a O(2^n) blowup of Bind() templates to handle each combination of regular |
| 129 | // types and movable-but-not-copyable types. Thus we introduce a wrapper type |
| 130 | // that is copyable to transmit the correct type information down into |
| 131 | // BindState<>. Ignoring const in this type makes sense because it is only |
| 132 | // created when we are explicitly trying to do a destructive move. |
| 133 | // |
| 134 | // Two notes: |
| 135 | // 1) PassedWrapper supports any type that has a move constructor, however |
| 136 | // the type will need to be specifically whitelisted in order for it to be |
| 137 | // bound to a Callback. We guard this explicitly at the call of Passed() |
| 138 | // to make for clear errors. Things not given to Passed() will be forwarded |
| 139 | // and stored by value which will not work for general move-only types. |
| 140 | // 2) is_valid_ is distinct from NULL because it is valid to bind a "NULL" |
| 141 | // scoper to a Callback and allow the Callback to execute once. |
| 142 | template <typename T> |
| 143 | class PassedWrapper { |
| 144 | public: |
| 145 | explicit PassedWrapper(T&& scoper) |
| 146 | : is_valid_(true), scoper_(std::move(scoper)) {} |
| 147 | PassedWrapper(PassedWrapper&& other) |
| 148 | : is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {} |
| 149 | T Take() const { |
| 150 | CHECK(is_valid_); |
| 151 | is_valid_ = false; |
| 152 | return std::move(scoper_); |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | mutable bool is_valid_; |
| 157 | mutable T scoper_; |
| 158 | }; |
| 159 | |
| 160 | template <typename T> |
| 161 | using Unwrapper = BindUnwrapTraits<std::decay_t<T>>; |
| 162 | |
| 163 | template <typename T> |
Peter Kasting | c2f8749bf | 2018-03-31 03:32:37 | [diff] [blame] | 164 | decltype(auto) Unwrap(T&& o) { |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 165 | return Unwrapper<T>::Unwrap(std::forward<T>(o)); |
| 166 | } |
| 167 | |
| 168 | // IsWeakMethod is a helper that determine if we are binding a WeakPtr<> to a |
| 169 | // method. It is used internally by Bind() to select the correct |
| 170 | // InvokeHelper that will no-op itself in the event the WeakPtr<> for |
| 171 | // the target object is invalidated. |
| 172 | // |
| 173 | // The first argument should be the type of the object that will be received by |
| 174 | // the method. |
| 175 | template <bool is_method, typename... Args> |
| 176 | struct IsWeakMethod : std::false_type {}; |
| 177 | |
| 178 | template <typename T, typename... Args> |
| 179 | struct IsWeakMethod<true, T, Args...> : IsWeakReceiver<T> {}; |
| 180 | |
| 181 | // Packs a list of types to hold them in a single type. |
| 182 | template <typename... Types> |
| 183 | struct TypeList {}; |
| 184 | |
| 185 | // Used for DropTypeListItem implementation. |
| 186 | template <size_t n, typename List> |
| 187 | struct DropTypeListItemImpl; |
| 188 | |
| 189 | // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure. |
| 190 | template <size_t n, typename T, typename... List> |
| 191 | struct DropTypeListItemImpl<n, TypeList<T, List...>> |
| 192 | : DropTypeListItemImpl<n - 1, TypeList<List...>> {}; |
| 193 | |
| 194 | template <typename T, typename... List> |
| 195 | struct DropTypeListItemImpl<0, TypeList<T, List...>> { |
| 196 | using Type = TypeList<T, List...>; |
| 197 | }; |
| 198 | |
| 199 | template <> |
| 200 | struct DropTypeListItemImpl<0, TypeList<>> { |
| 201 | using Type = TypeList<>; |
| 202 | }; |
| 203 | |
| 204 | // A type-level function that drops |n| list item from given TypeList. |
| 205 | template <size_t n, typename List> |
| 206 | using DropTypeListItem = typename DropTypeListItemImpl<n, List>::Type; |
| 207 | |
| 208 | // Used for TakeTypeListItem implementation. |
| 209 | template <size_t n, typename List, typename... Accum> |
| 210 | struct TakeTypeListItemImpl; |
| 211 | |
| 212 | // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure. |
| 213 | template <size_t n, typename T, typename... List, typename... Accum> |
| 214 | struct TakeTypeListItemImpl<n, TypeList<T, List...>, Accum...> |
| 215 | : TakeTypeListItemImpl<n - 1, TypeList<List...>, Accum..., T> {}; |
| 216 | |
| 217 | template <typename T, typename... List, typename... Accum> |
| 218 | struct TakeTypeListItemImpl<0, TypeList<T, List...>, Accum...> { |
| 219 | using Type = TypeList<Accum...>; |
| 220 | }; |
| 221 | |
| 222 | template <typename... Accum> |
| 223 | struct TakeTypeListItemImpl<0, TypeList<>, Accum...> { |
| 224 | using Type = TypeList<Accum...>; |
| 225 | }; |
| 226 | |
| 227 | // A type-level function that takes first |n| list item from given TypeList. |
| 228 | // E.g. TakeTypeListItem<3, TypeList<A, B, C, D>> is evaluated to |
| 229 | // TypeList<A, B, C>. |
| 230 | template <size_t n, typename List> |
| 231 | using TakeTypeListItem = typename TakeTypeListItemImpl<n, List>::Type; |
| 232 | |
| 233 | // Used for ConcatTypeLists implementation. |
| 234 | template <typename List1, typename List2> |
| 235 | struct ConcatTypeListsImpl; |
| 236 | |
| 237 | template <typename... Types1, typename... Types2> |
| 238 | struct ConcatTypeListsImpl<TypeList<Types1...>, TypeList<Types2...>> { |
| 239 | using Type = TypeList<Types1..., Types2...>; |
| 240 | }; |
| 241 | |
| 242 | // A type-level function that concats two TypeLists. |
| 243 | template <typename List1, typename List2> |
| 244 | using ConcatTypeLists = typename ConcatTypeListsImpl<List1, List2>::Type; |
| 245 | |
| 246 | // Used for MakeFunctionType implementation. |
| 247 | template <typename R, typename ArgList> |
| 248 | struct MakeFunctionTypeImpl; |
| 249 | |
| 250 | template <typename R, typename... Args> |
| 251 | struct MakeFunctionTypeImpl<R, TypeList<Args...>> { |
| 252 | // MSVC 2013 doesn't support Type Alias of function types. |
| 253 | // Revisit this after we update it to newer version. |
| 254 | typedef R Type(Args...); |
| 255 | }; |
| 256 | |
| 257 | // A type-level function that constructs a function type that has |R| as its |
| 258 | // return type and has TypeLists items as its arguments. |
| 259 | template <typename R, typename ArgList> |
| 260 | using MakeFunctionType = typename MakeFunctionTypeImpl<R, ArgList>::Type; |
| 261 | |
| 262 | // Used for ExtractArgs and ExtractReturnType. |
| 263 | template <typename Signature> |
| 264 | struct ExtractArgsImpl; |
| 265 | |
| 266 | template <typename R, typename... Args> |
| 267 | struct ExtractArgsImpl<R(Args...)> { |
| 268 | using ReturnType = R; |
| 269 | using ArgsList = TypeList<Args...>; |
| 270 | }; |
| 271 | |
| 272 | // A type-level function that extracts function arguments into a TypeList. |
| 273 | // E.g. ExtractArgs<R(A, B, C)> is evaluated to TypeList<A, B, C>. |
| 274 | template <typename Signature> |
| 275 | using ExtractArgs = typename ExtractArgsImpl<Signature>::ArgsList; |
| 276 | |
| 277 | // A type-level function that extracts the return type of a function. |
| 278 | // E.g. ExtractReturnType<R(A, B, C)> is evaluated to R. |
| 279 | template <typename Signature> |
| 280 | using ExtractReturnType = typename ExtractArgsImpl<Signature>::ReturnType; |
| 281 | |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 282 | template <typename Callable, |
| 283 | typename Signature = decltype(&Callable::operator())> |
| 284 | struct ExtractCallableRunTypeImpl; |
| 285 | |
| 286 | template <typename Callable, typename R, typename... Args> |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 287 | struct ExtractCallableRunTypeImpl<Callable, R (Callable::*)(Args...)> { |
| 288 | using Type = R(Args...); |
| 289 | }; |
| 290 | |
| 291 | template <typename Callable, typename R, typename... Args> |
| 292 | struct ExtractCallableRunTypeImpl<Callable, R (Callable::*)(Args...) const> { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 293 | using Type = R(Args...); |
| 294 | }; |
| 295 | |
| 296 | // Evaluated to RunType of the given callable type. |
| 297 | // Example: |
| 298 | // auto f = [](int, char*) { return 0.1; }; |
| 299 | // ExtractCallableRunType<decltype(f)> |
| 300 | // is evaluated to |
| 301 | // double(int, char*); |
| 302 | template <typename Callable> |
| 303 | using ExtractCallableRunType = |
| 304 | typename ExtractCallableRunTypeImpl<Callable>::Type; |
| 305 | |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 306 | // IsCallableObject<Functor> is std::true_type if |Functor| has operator(). |
| 307 | // Otherwise, it's std::false_type. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 308 | // Example: |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 309 | // IsCallableObject<void(*)()>::value is false. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 310 | // |
| 311 | // struct Foo {}; |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 312 | // IsCallableObject<void(Foo::*)()>::value is false. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 313 | // |
| 314 | // int i = 0; |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 315 | // auto f = [i]() {}; |
| 316 | // IsCallableObject<decltype(f)>::value is false. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 317 | template <typename Functor, typename SFINAE = void> |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 318 | struct IsCallableObject : std::false_type {}; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 319 | |
| 320 | template <typename Callable> |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 321 | struct IsCallableObject<Callable, void_t<decltype(&Callable::operator())>> |
| 322 | : std::true_type {}; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 323 | |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 324 | // HasRefCountedTypeAsRawPtr selects true_type when any of the |Args| is a raw |
| 325 | // pointer to a RefCounted type. |
| 326 | // Implementation note: This non-specialized case handles zero-arity case only. |
| 327 | // Non-zero-arity cases should be handled by the specialization below. |
| 328 | template <typename... Args> |
tzik | 403cb6c | 2016-03-10 07:17:25 | [diff] [blame] | 329 | struct HasRefCountedTypeAsRawPtr : std::false_type {}; |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 330 | |
| 331 | // Implementation note: Select true_type if the first parameter is a raw pointer |
| 332 | // to a RefCounted type. Otherwise, skip the first parameter and check rest of |
| 333 | // parameters recursively. |
| 334 | template <typename T, typename... Args> |
| 335 | struct HasRefCountedTypeAsRawPtr<T, Args...> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 336 | : std::conditional_t<NeedsScopedRefptrButGetsRawPtr<T>::value, |
| 337 | std::true_type, |
| 338 | HasRefCountedTypeAsRawPtr<Args...>> {}; |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 339 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 340 | // ForceVoidReturn<> |
| 341 | // |
| 342 | // Set of templates that support forcing the function return type to void. |
| 343 | template <typename Sig> |
| 344 | struct ForceVoidReturn; |
| 345 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 346 | template <typename R, typename... Args> |
| 347 | struct ForceVoidReturn<R(Args...)> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 348 | using RunType = void(Args...); |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 349 | }; |
| 350 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 351 | // FunctorTraits<> |
| 352 | // |
| 353 | // See description at top of file. |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 354 | template <typename Functor, typename SFINAE> |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 355 | struct FunctorTraits; |
| 356 | |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 357 | // For empty callable types. |
kylechar | 2255ccc | 2019-09-11 21:47:23 | [diff] [blame] | 358 | // This specialization is intended to allow binding captureless lambdas, based |
| 359 | // on the fact that captureless lambdas are empty while capturing lambdas are |
| 360 | // not. This also allows any functors as far as it's an empty class. |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 361 | // Example: |
| 362 | // |
| 363 | // // Captureless lambdas are allowed. |
| 364 | // []() {return 42;}; |
| 365 | // |
| 366 | // // Capturing lambdas are *not* allowed. |
| 367 | // int x; |
| 368 | // [x]() {return x;}; |
| 369 | // |
| 370 | // // Any empty class with operator() is allowed. |
| 371 | // struct Foo { |
| 372 | // void operator()() const {} |
| 373 | // // No non-static member variable and no virtual functions. |
| 374 | // }; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 375 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 376 | struct FunctorTraits<Functor, |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 377 | std::enable_if_t<IsCallableObject<Functor>::value && |
| 378 | std::is_empty<Functor>::value>> { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 379 | using RunType = ExtractCallableRunType<Functor>; |
| 380 | static constexpr bool is_method = false; |
| 381 | static constexpr bool is_nullable = false; |
| 382 | |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 383 | template <typename RunFunctor, typename... RunArgs> |
| 384 | static ExtractReturnType<RunType> Invoke(RunFunctor&& functor, |
| 385 | RunArgs&&... args) { |
| 386 | return std::forward<RunFunctor>(functor)(std::forward<RunArgs>(args)...); |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 387 | } |
| 388 | }; |
| 389 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 390 | // For functions. |
| 391 | template <typename R, typename... Args> |
| 392 | struct FunctorTraits<R (*)(Args...)> { |
| 393 | using RunType = R(Args...); |
| 394 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 395 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 396 | |
tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 397 | template <typename Function, typename... RunArgs> |
| 398 | static R Invoke(Function&& function, RunArgs&&... args) { |
| 399 | return std::forward<Function>(function)(std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 400 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 401 | }; |
| 402 | |
Gaurav Dhol | f6e3f59 | 2018-10-29 17:22:41 | [diff] [blame] | 403 | #if defined(OS_WIN) && !defined(ARCH_CPU_64_BITS) |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 404 | |
| 405 | // For functions. |
| 406 | template <typename R, typename... Args> |
| 407 | struct FunctorTraits<R(__stdcall*)(Args...)> { |
| 408 | using RunType = R(Args...); |
| 409 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 410 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 411 | |
| 412 | template <typename... RunArgs> |
| 413 | static R Invoke(R(__stdcall* function)(Args...), RunArgs&&... args) { |
| 414 | return function(std::forward<RunArgs>(args)...); |
| 415 | } |
| 416 | }; |
| 417 | |
| 418 | // For functions. |
| 419 | template <typename R, typename... Args> |
| 420 | struct FunctorTraits<R(__fastcall*)(Args...)> { |
| 421 | using RunType = R(Args...); |
| 422 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 423 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 424 | |
| 425 | template <typename... RunArgs> |
| 426 | static R Invoke(R(__fastcall* function)(Args...), RunArgs&&... args) { |
| 427 | return function(std::forward<RunArgs>(args)...); |
| 428 | } |
| 429 | }; |
| 430 | |
Gaurav Dhol | f6e3f59 | 2018-10-29 17:22:41 | [diff] [blame] | 431 | #endif // defined(OS_WIN) && !defined(ARCH_CPU_64_BITS) |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 432 | |
Sylvain Defresne | ec3270c | 2018-05-31 17:19:15 | [diff] [blame] | 433 | #if defined(OS_MACOSX) |
| 434 | |
| 435 | // Support for Objective-C blocks. There are two implementation depending |
| 436 | // on whether Automated Reference Counting (ARC) is enabled. When ARC is |
| 437 | // enabled, then the block itself can be bound as the compiler will ensure |
| 438 | // its lifetime will be correctly managed. Otherwise, require the block to |
| 439 | // be wrapped in a base::mac::ScopedBlock (via base::RetainBlock) that will |
| 440 | // correctly manage the block lifetime. |
| 441 | // |
| 442 | // The two implementation ensure that the One Definition Rule (ODR) is not |
| 443 | // broken (it is not possible to write a template base::RetainBlock that would |
| 444 | // work correctly both with ARC enabled and disabled). |
| 445 | |
| 446 | #if HAS_FEATURE(objc_arc) |
| 447 | |
| 448 | template <typename R, typename... Args> |
| 449 | struct FunctorTraits<R (^)(Args...)> { |
| 450 | using RunType = R(Args...); |
| 451 | static constexpr bool is_method = false; |
| 452 | static constexpr bool is_nullable = true; |
| 453 | |
| 454 | template <typename BlockType, typename... RunArgs> |
| 455 | static R Invoke(BlockType&& block, RunArgs&&... args) { |
| 456 | // According to LLVM documentation (§ 6.3), "local variables of automatic |
| 457 | // storage duration do not have precise lifetime." Use objc_precise_lifetime |
| 458 | // to ensure that the Objective-C block is not deallocated until it has |
| 459 | // finished executing even if the Callback<> is destroyed during the block |
| 460 | // execution. |
| 461 | // https://clang.llvm.org/docs/AutomaticReferenceCounting.html#precise-lifetime-semantics |
| 462 | __attribute__((objc_precise_lifetime)) R (^scoped_block)(Args...) = block; |
| 463 | return scoped_block(std::forward<RunArgs>(args)...); |
| 464 | } |
| 465 | }; |
| 466 | |
| 467 | #else // HAS_FEATURE(objc_arc) |
| 468 | |
| 469 | template <typename R, typename... Args> |
| 470 | struct FunctorTraits<base::mac::ScopedBlock<R (^)(Args...)>> { |
| 471 | using RunType = R(Args...); |
| 472 | static constexpr bool is_method = false; |
| 473 | static constexpr bool is_nullable = true; |
| 474 | |
| 475 | template <typename BlockType, typename... RunArgs> |
| 476 | static R Invoke(BlockType&& block, RunArgs&&... args) { |
| 477 | // Copy the block to ensure that the Objective-C block is not deallocated |
| 478 | // until it has finished executing even if the Callback<> is destroyed |
| 479 | // during the block execution. |
| 480 | base::mac::ScopedBlock<R (^)(Args...)> scoped_block(block); |
| 481 | return scoped_block.get()(std::forward<RunArgs>(args)...); |
| 482 | } |
| 483 | }; |
| 484 | |
| 485 | #endif // HAS_FEATURE(objc_arc) |
| 486 | #endif // defined(OS_MACOSX) |
| 487 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 488 | // For methods. |
| 489 | template <typename R, typename Receiver, typename... Args> |
| 490 | struct FunctorTraits<R (Receiver::*)(Args...)> { |
| 491 | using RunType = R(Receiver*, Args...); |
| 492 | static constexpr bool is_method = true; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 493 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 494 | |
tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 495 | template <typename Method, typename ReceiverPtr, typename... RunArgs> |
| 496 | static R Invoke(Method method, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 497 | ReceiverPtr&& receiver_ptr, |
| 498 | RunArgs&&... args) { |
tzik | 75851f4 | 2017-06-14 06:57:01 | [diff] [blame] | 499 | return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 500 | } |
| 501 | }; |
| 502 | |
| 503 | // For const methods. |
| 504 | template <typename R, typename Receiver, typename... Args> |
| 505 | struct FunctorTraits<R (Receiver::*)(Args...) const> { |
| 506 | using RunType = R(const Receiver*, Args...); |
| 507 | static constexpr bool is_method = true; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 508 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 509 | |
tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 510 | template <typename Method, typename ReceiverPtr, typename... RunArgs> |
| 511 | static R Invoke(Method method, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 512 | ReceiverPtr&& receiver_ptr, |
| 513 | RunArgs&&... args) { |
tzik | 75851f4 | 2017-06-14 06:57:01 | [diff] [blame] | 514 | return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 515 | } |
| 516 | }; |
| 517 | |
tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 518 | #ifdef __cpp_noexcept_function_type |
| 519 | // noexcept makes a distinct function type in C++17. |
| 520 | // I.e. `void(*)()` and `void(*)() noexcept` are same in pre-C++17, and |
| 521 | // different in C++17. |
| 522 | template <typename R, typename... Args> |
| 523 | struct FunctorTraits<R (*)(Args...) noexcept> : FunctorTraits<R (*)(Args...)> { |
| 524 | }; |
| 525 | |
| 526 | template <typename R, typename Receiver, typename... Args> |
| 527 | struct FunctorTraits<R (Receiver::*)(Args...) noexcept> |
| 528 | : FunctorTraits<R (Receiver::*)(Args...)> {}; |
| 529 | |
| 530 | template <typename R, typename Receiver, typename... Args> |
| 531 | struct FunctorTraits<R (Receiver::*)(Args...) const noexcept> |
| 532 | : FunctorTraits<R (Receiver::*)(Args...) const> {}; |
| 533 | #endif |
| 534 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 535 | // For IgnoreResults. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 536 | template <typename T> |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 537 | struct FunctorTraits<IgnoreResultHelper<T>> : FunctorTraits<T> { |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 538 | using RunType = |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 539 | typename ForceVoidReturn<typename FunctorTraits<T>::RunType>::RunType; |
| 540 | |
| 541 | template <typename IgnoreResultType, typename... RunArgs> |
| 542 | static void Invoke(IgnoreResultType&& ignore_result_helper, |
| 543 | RunArgs&&... args) { |
tzik | ff54a5b15 | 2016-08-31 11:50:41 | [diff] [blame] | 544 | FunctorTraits<T>::Invoke( |
| 545 | std::forward<IgnoreResultType>(ignore_result_helper).functor_, |
| 546 | std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 547 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 548 | }; |
| 549 | |
tzik | d4bb5b7d | 2017-08-28 19:08:52 | [diff] [blame] | 550 | // For OnceCallbacks. |
| 551 | template <typename R, typename... Args> |
| 552 | struct FunctorTraits<OnceCallback<R(Args...)>> { |
| 553 | using RunType = R(Args...); |
| 554 | static constexpr bool is_method = false; |
| 555 | static constexpr bool is_nullable = true; |
| 556 | |
| 557 | template <typename CallbackType, typename... RunArgs> |
| 558 | static R Invoke(CallbackType&& callback, RunArgs&&... args) { |
| 559 | DCHECK(!callback.is_null()); |
| 560 | return std::forward<CallbackType>(callback).Run( |
| 561 | std::forward<RunArgs>(args)...); |
| 562 | } |
| 563 | }; |
| 564 | |
| 565 | // For RepeatingCallbacks. |
| 566 | template <typename R, typename... Args> |
| 567 | struct FunctorTraits<RepeatingCallback<R(Args...)>> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 568 | using RunType = R(Args...); |
| 569 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 570 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 571 | |
| 572 | template <typename CallbackType, typename... RunArgs> |
| 573 | static R Invoke(CallbackType&& callback, RunArgs&&... args) { |
| 574 | DCHECK(!callback.is_null()); |
| 575 | return std::forward<CallbackType>(callback).Run( |
| 576 | std::forward<RunArgs>(args)...); |
| 577 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 578 | }; |
| 579 | |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 580 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 581 | using MakeFunctorTraits = FunctorTraits<std::decay_t<Functor>>; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 582 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 583 | // InvokeHelper<> |
| 584 | // |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 585 | // There are 2 logical InvokeHelper<> specializations: normal, WeakCalls. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 586 | // |
| 587 | // The normal type just calls the underlying runnable. |
| 588 | // |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 589 | // WeakCalls need special syntax that is applied to the first argument to check |
| 590 | // if they should no-op themselves. |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 591 | template <bool is_weak_call, typename ReturnType> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 592 | struct InvokeHelper; |
| 593 | |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 594 | template <typename ReturnType> |
| 595 | struct InvokeHelper<false, ReturnType> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 596 | template <typename Functor, typename... RunArgs> |
| 597 | static inline ReturnType MakeItSo(Functor&& functor, RunArgs&&... args) { |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 598 | using Traits = MakeFunctorTraits<Functor>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 599 | return Traits::Invoke(std::forward<Functor>(functor), |
| 600 | std::forward<RunArgs>(args)...); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 601 | } |
| 602 | }; |
| 603 | |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 604 | template <typename ReturnType> |
| 605 | struct InvokeHelper<true, ReturnType> { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 606 | // WeakCalls are only supported for functions with a void return type. |
| 607 | // Otherwise, the function result would be undefined if the the WeakPtr<> |
| 608 | // is invalidated. |
tzik | 403cb6c | 2016-03-10 07:17:25 | [diff] [blame] | 609 | static_assert(std::is_void<ReturnType>::value, |
avi | 4ec0dff | 2015-11-24 14:26:24 | [diff] [blame] | 610 | "weak_ptrs can only bind to methods without return values"); |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 611 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 612 | template <typename Functor, typename BoundWeakPtr, typename... RunArgs> |
| 613 | static inline void MakeItSo(Functor&& functor, |
tzik | 33871d8 | 2016-07-14 12:12:06 | [diff] [blame] | 614 | BoundWeakPtr&& weak_ptr, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 615 | RunArgs&&... args) { |
| 616 | if (!weak_ptr) |
| 617 | return; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 618 | using Traits = MakeFunctorTraits<Functor>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 619 | Traits::Invoke(std::forward<Functor>(functor), |
| 620 | std::forward<BoundWeakPtr>(weak_ptr), |
| 621 | std::forward<RunArgs>(args)...); |
| 622 | } |
| 623 | }; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 624 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 625 | // Invoker<> |
| 626 | // |
| 627 | // See description at the top of the file. |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 628 | template <typename StorageType, typename UnboundRunType> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 629 | struct Invoker; |
| 630 | |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 631 | template <typename StorageType, typename R, typename... UnboundArgs> |
| 632 | struct Invoker<StorageType, R(UnboundArgs...)> { |
Vladislav Kuzkokov | 6d208e1 | 2017-11-08 21:31:08 | [diff] [blame] | 633 | static R RunOnce(BindStateBase* base, |
tzik | 9bc6837b | 2018-06-28 20:20:47 | [diff] [blame] | 634 | PassingType<UnboundArgs>... unbound_args) { |
tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 635 | // Local references to make debugger stepping easier. If in a debugger, |
| 636 | // you really want to warp ahead and step through the |
| 637 | // InvokeHelper<>::MakeItSo() call below. |
| 638 | StorageType* storage = static_cast<StorageType*>(base); |
| 639 | static constexpr size_t num_bound_args = |
| 640 | std::tuple_size<decltype(storage->bound_args_)>::value; |
| 641 | return RunImpl(std::move(storage->functor_), |
| 642 | std::move(storage->bound_args_), |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 643 | std::make_index_sequence<num_bound_args>(), |
tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 644 | std::forward<UnboundArgs>(unbound_args)...); |
| 645 | } |
| 646 | |
tzik | 9bc6837b | 2018-06-28 20:20:47 | [diff] [blame] | 647 | static R Run(BindStateBase* base, PassingType<UnboundArgs>... unbound_args) { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 648 | // Local references to make debugger stepping easier. If in a debugger, |
| 649 | // you really want to warp ahead and step through the |
| 650 | // InvokeHelper<>::MakeItSo() call below. |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 651 | const StorageType* storage = static_cast<StorageType*>(base); |
| 652 | static constexpr size_t num_bound_args = |
| 653 | std::tuple_size<decltype(storage->bound_args_)>::value; |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 654 | return RunImpl(storage->functor_, storage->bound_args_, |
| 655 | std::make_index_sequence<num_bound_args>(), |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 656 | std::forward<UnboundArgs>(unbound_args)...); |
| 657 | } |
| 658 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 659 | private: |
| 660 | template <typename Functor, typename BoundArgsTuple, size_t... indices> |
| 661 | static inline R RunImpl(Functor&& functor, |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 662 | BoundArgsTuple&& bound, |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 663 | std::index_sequence<indices...>, |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 664 | UnboundArgs&&... unbound_args) { |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 665 | static constexpr bool is_method = MakeFunctorTraits<Functor>::is_method; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 666 | |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 667 | using DecayedArgsTuple = std::decay_t<BoundArgsTuple>; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 668 | static constexpr bool is_weak_call = |
| 669 | IsWeakMethod<is_method, |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 670 | std::tuple_element_t<indices, DecayedArgsTuple>...>(); |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 671 | |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 672 | return InvokeHelper<is_weak_call, R>::MakeItSo( |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 673 | std::forward<Functor>(functor), |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 674 | Unwrap(std::get<indices>(std::forward<BoundArgsTuple>(bound)))..., |
tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 675 | std::forward<UnboundArgs>(unbound_args)...); |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 676 | } |
| 677 | }; |
| 678 | |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 679 | // Extracts necessary type info from Functor and BoundArgs. |
| 680 | // Used to implement MakeUnboundRunType, BindOnce and BindRepeating. |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 681 | template <typename Functor, typename... BoundArgs> |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 682 | struct BindTypeHelper { |
| 683 | static constexpr size_t num_bounds = sizeof...(BoundArgs); |
| 684 | using FunctorTraits = MakeFunctorTraits<Functor>; |
| 685 | |
| 686 | // Example: |
| 687 | // When Functor is `double (Foo::*)(int, const std::string&)`, and BoundArgs |
| 688 | // is a template pack of `Foo*` and `int16_t`: |
| 689 | // - RunType is `double(Foo*, int, const std::string&)`, |
| 690 | // - ReturnType is `double`, |
| 691 | // - RunParamsList is `TypeList<Foo*, int, const std::string&>`, |
| 692 | // - BoundParamsList is `TypeList<Foo*, int>`, |
| 693 | // - UnboundParamsList is `TypeList<const std::string&>`, |
| 694 | // - BoundArgsList is `TypeList<Foo*, int16_t>`, |
| 695 | // - UnboundRunType is `double(const std::string&)`. |
| 696 | using RunType = typename FunctorTraits::RunType; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 697 | using ReturnType = ExtractReturnType<RunType>; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 698 | |
| 699 | using RunParamsList = ExtractArgs<RunType>; |
| 700 | using BoundParamsList = TakeTypeListItem<num_bounds, RunParamsList>; |
| 701 | using UnboundParamsList = DropTypeListItem<num_bounds, RunParamsList>; |
| 702 | |
| 703 | using BoundArgsList = TypeList<BoundArgs...>; |
| 704 | |
| 705 | using UnboundRunType = MakeFunctionType<ReturnType, UnboundParamsList>; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 706 | }; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 707 | |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 708 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 709 | std::enable_if_t<FunctorTraits<Functor>::is_nullable, bool> IsNull( |
| 710 | const Functor& functor) { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 711 | return !functor; |
| 712 | } |
| 713 | |
| 714 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 715 | std::enable_if_t<!FunctorTraits<Functor>::is_nullable, bool> IsNull( |
| 716 | const Functor&) { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 717 | return false; |
| 718 | } |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 719 | |
tzik | 9697d49e | 2018-08-02 13:35:19 | [diff] [blame] | 720 | // Used by QueryCancellationTraits below. |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 721 | template <typename Functor, typename BoundArgsTuple, size_t... indices> |
tzik | 9697d49e | 2018-08-02 13:35:19 | [diff] [blame] | 722 | bool QueryCancellationTraitsImpl(BindStateBase::CancellationQueryMode mode, |
| 723 | const Functor& functor, |
| 724 | const BoundArgsTuple& bound_args, |
| 725 | std::index_sequence<indices...>) { |
| 726 | switch (mode) { |
| 727 | case BindStateBase::IS_CANCELLED: |
| 728 | return CallbackCancellationTraits<Functor, BoundArgsTuple>::IsCancelled( |
| 729 | functor, std::get<indices>(bound_args)...); |
| 730 | case BindStateBase::MAYBE_VALID: |
| 731 | return CallbackCancellationTraits<Functor, BoundArgsTuple>::MaybeValid( |
| 732 | functor, std::get<indices>(bound_args)...); |
| 733 | } |
| 734 | NOTREACHED(); |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 735 | } |
tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 736 | |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 737 | // Relays |base| to corresponding CallbackCancellationTraits<>::Run(). Returns |
| 738 | // true if the callback |base| represents is canceled. |
| 739 | template <typename BindStateType> |
tzik | 9697d49e | 2018-08-02 13:35:19 | [diff] [blame] | 740 | bool QueryCancellationTraits(const BindStateBase* base, |
| 741 | BindStateBase::CancellationQueryMode mode) { |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 742 | const BindStateType* storage = static_cast<const BindStateType*>(base); |
| 743 | static constexpr size_t num_bound_args = |
| 744 | std::tuple_size<decltype(storage->bound_args_)>::value; |
tzik | 9697d49e | 2018-08-02 13:35:19 | [diff] [blame] | 745 | return QueryCancellationTraitsImpl( |
| 746 | mode, storage->functor_, storage->bound_args_, |
Nicolas Ouellet-payeur | 40f8e9a | 2018-07-30 16:26:48 | [diff] [blame] | 747 | std::make_index_sequence<num_bound_args>()); |
Nicolas Ouellet-payeur | 40f8e9a | 2018-07-30 16:26:48 | [diff] [blame] | 748 | } |
| 749 | |
tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 750 | // The base case of BanUnconstructedRefCountedReceiver that checks nothing. |
| 751 | template <typename Functor, typename Receiver, typename... Unused> |
| 752 | std::enable_if_t< |
| 753 | !(MakeFunctorTraits<Functor>::is_method && |
| 754 | std::is_pointer<std::decay_t<Receiver>>::value && |
| 755 | IsRefCountedType<std::remove_pointer_t<std::decay_t<Receiver>>>::value)> |
| 756 | BanUnconstructedRefCountedReceiver(const Receiver& receiver, Unused&&...) {} |
| 757 | |
| 758 | template <typename Functor> |
| 759 | void BanUnconstructedRefCountedReceiver() {} |
| 760 | |
| 761 | // Asserts that Callback is not the first owner of a ref-counted receiver. |
| 762 | template <typename Functor, typename Receiver, typename... Unused> |
| 763 | std::enable_if_t< |
| 764 | MakeFunctorTraits<Functor>::is_method && |
| 765 | std::is_pointer<std::decay_t<Receiver>>::value && |
| 766 | IsRefCountedType<std::remove_pointer_t<std::decay_t<Receiver>>>::value> |
| 767 | BanUnconstructedRefCountedReceiver(const Receiver& receiver, Unused&&...) { |
| 768 | DCHECK(receiver); |
| 769 | |
| 770 | // It's error prone to make the implicit first reference to ref-counted types. |
| 771 | // In the example below, base::BindOnce() makes the implicit first reference |
| 772 | // to the ref-counted Foo. If PostTask() failed or the posted task ran fast |
| 773 | // enough, the newly created instance can be destroyed before |oo| makes |
| 774 | // another reference. |
| 775 | // Foo::Foo() { |
| 776 | // base::PostTask(FROM_HERE, base::BindOnce(&Foo::Bar, this)); |
| 777 | // } |
| 778 | // |
| 779 | // scoped_refptr<Foo> oo = new Foo(); |
| 780 | // |
| 781 | // Instead of doing like above, please consider adding a static constructor, |
| 782 | // and keep the first reference alive explicitly. |
| 783 | // // static |
| 784 | // scoped_refptr<Foo> Foo::Create() { |
| 785 | // auto foo = base::WrapRefCounted(new Foo()); |
| 786 | // base::PostTask(FROM_HERE, base::BindOnce(&Foo::Bar, foo)); |
| 787 | // return foo; |
| 788 | // } |
| 789 | // |
| 790 | // Foo::Foo() {} |
| 791 | // |
| 792 | // scoped_refptr<Foo> oo = Foo::Create(); |
| 793 | DCHECK(receiver->HasAtLeastOneRef()) |
kylechar | 2255ccc | 2019-09-11 21:47:23 | [diff] [blame] | 794 | << "base::Bind{Once,Repeating}() refuses to create the first reference " |
| 795 | "to ref-counted objects. That typically happens around PostTask() in " |
| 796 | "their constructor, and such objects can be destroyed before `new` " |
| 797 | "returns if the task resolves fast enough."; |
tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 798 | } |
| 799 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 800 | // BindState<> |
| 801 | // |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 802 | // This stores all the state passed into Bind(). |
| 803 | template <typename Functor, typename... BoundArgs> |
| 804 | struct BindState final : BindStateBase { |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 805 | using IsCancellable = std::integral_constant< |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 806 | bool, |
| 807 | CallbackCancellationTraits<Functor, |
| 808 | std::tuple<BoundArgs...>>::is_cancellable>; |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 809 | |
tzik | bfe6612 | 2016-07-08 14:14:01 | [diff] [blame] | 810 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 811 | static BindState* Create(BindStateBase::InvokeFuncStorage invoke_func, |
| 812 | ForwardFunctor&& functor, |
| 813 | ForwardBoundArgs&&... bound_args) { |
| 814 | // Ban ref counted receivers that were not yet fully constructed to avoid |
| 815 | // a common pattern of racy situation. |
| 816 | BanUnconstructedRefCountedReceiver<ForwardFunctor>(bound_args...); |
| 817 | |
| 818 | // IsCancellable is std::false_type if |
| 819 | // CallbackCancellationTraits<>::IsCancelled returns always false. |
| 820 | // Otherwise, it's std::true_type. |
| 821 | return new BindState(IsCancellable{}, invoke_func, |
| 822 | std::forward<ForwardFunctor>(functor), |
| 823 | std::forward<ForwardBoundArgs>(bound_args)...); |
| 824 | } |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 825 | |
| 826 | Functor functor_; |
| 827 | std::tuple<BoundArgs...> bound_args_; |
| 828 | |
| 829 | private: |
| 830 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| 831 | explicit BindState(std::true_type, |
| 832 | BindStateBase::InvokeFuncStorage invoke_func, |
| 833 | ForwardFunctor&& functor, |
| 834 | ForwardBoundArgs&&... bound_args) |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 835 | : BindStateBase(invoke_func, |
| 836 | &Destroy, |
tzik | 9697d49e | 2018-08-02 13:35:19 | [diff] [blame] | 837 | &QueryCancellationTraits<BindState>), |
tzik | ff54a5b15 | 2016-08-31 11:50:41 | [diff] [blame] | 838 | functor_(std::forward<ForwardFunctor>(functor)), |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 839 | bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 840 | DCHECK(!IsNull(functor_)); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 841 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 842 | |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 843 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| 844 | explicit BindState(std::false_type, |
| 845 | BindStateBase::InvokeFuncStorage invoke_func, |
| 846 | ForwardFunctor&& functor, |
| 847 | ForwardBoundArgs&&... bound_args) |
| 848 | : BindStateBase(invoke_func, &Destroy), |
| 849 | functor_(std::forward<ForwardFunctor>(functor)), |
| 850 | bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
| 851 | DCHECK(!IsNull(functor_)); |
| 852 | } |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 853 | |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 854 | ~BindState() = default; |
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 855 | |
tzik | 30e0c31 | 2016-09-21 08:06:54 | [diff] [blame] | 856 | static void Destroy(const BindStateBase* self) { |
| 857 | delete static_cast<const BindState*>(self); |
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 858 | } |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 859 | }; |
| 860 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 861 | // Used to implement MakeBindStateType. |
| 862 | template <bool is_method, typename Functor, typename... BoundArgs> |
| 863 | struct MakeBindStateTypeImpl; |
| 864 | |
| 865 | template <typename Functor, typename... BoundArgs> |
| 866 | struct MakeBindStateTypeImpl<false, Functor, BoundArgs...> { |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 867 | static_assert(!HasRefCountedTypeAsRawPtr<std::decay_t<BoundArgs>...>::value, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 868 | "A parameter is a refcounted type and needs scoped_refptr."); |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 869 | using Type = BindState<std::decay_t<Functor>, std::decay_t<BoundArgs>...>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 870 | }; |
| 871 | |
| 872 | template <typename Functor> |
| 873 | struct MakeBindStateTypeImpl<true, Functor> { |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 874 | using Type = BindState<std::decay_t<Functor>>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 875 | }; |
| 876 | |
| 877 | template <typename Functor, typename Receiver, typename... BoundArgs> |
| 878 | struct MakeBindStateTypeImpl<true, Functor, Receiver, BoundArgs...> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 879 | private: |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 880 | using DecayedReceiver = std::decay_t<Receiver>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 881 | |
tzik | 4625ac61 | 2018-02-28 09:43:55 | [diff] [blame] | 882 | static_assert(!std::is_array<std::remove_reference_t<Receiver>>::value, |
| 883 | "First bound argument to a method cannot be an array."); |
| 884 | static_assert( |
| 885 | !std::is_pointer<DecayedReceiver>::value || |
| 886 | IsRefCountedType<std::remove_pointer_t<DecayedReceiver>>::value, |
| 887 | "Receivers may not be raw pointers. If using a raw pointer here is safe" |
| 888 | " and has no lifetime concerns, use base::Unretained() and document why" |
| 889 | " it's safe."); |
| 890 | static_assert(!HasRefCountedTypeAsRawPtr<std::decay_t<BoundArgs>...>::value, |
| 891 | "A parameter is a refcounted type and needs scoped_refptr."); |
| 892 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 893 | public: |
| 894 | using Type = BindState< |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 895 | std::decay_t<Functor>, |
| 896 | std::conditional_t<std::is_pointer<DecayedReceiver>::value, |
| 897 | scoped_refptr<std::remove_pointer_t<DecayedReceiver>>, |
| 898 | DecayedReceiver>, |
| 899 | std::decay_t<BoundArgs>...>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 900 | }; |
| 901 | |
| 902 | template <typename Functor, typename... BoundArgs> |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 903 | using MakeBindStateType = |
| 904 | typename MakeBindStateTypeImpl<MakeFunctorTraits<Functor>::is_method, |
| 905 | Functor, |
| 906 | BoundArgs...>::Type; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 907 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 908 | } // namespace internal |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 909 | |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 910 | // An injection point to control |this| pointer behavior on a method invocation. |
| 911 | // If IsWeakReceiver<> is true_type for |T| and |T| is used for a receiver of a |
| 912 | // method, base::Bind cancels the method invocation if the receiver is tested as |
| 913 | // false. |
| 914 | // E.g. Foo::bar() is not called: |
| 915 | // struct Foo : base::SupportsWeakPtr<Foo> { |
| 916 | // void bar() {} |
| 917 | // }; |
| 918 | // |
| 919 | // WeakPtr<Foo> oo = nullptr; |
kylechar | 2255ccc | 2019-09-11 21:47:23 | [diff] [blame] | 920 | // base::BindOnce(&Foo::bar, oo).Run(); |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 921 | template <typename T> |
| 922 | struct IsWeakReceiver : std::false_type {}; |
| 923 | |
| 924 | template <typename T> |
jdoerrie | c79c2fa2 | 2019-02-26 12:35:03 | [diff] [blame] | 925 | struct IsWeakReceiver<std::reference_wrapper<T>> : IsWeakReceiver<T> {}; |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 926 | |
| 927 | template <typename T> |
| 928 | struct IsWeakReceiver<WeakPtr<T>> : std::true_type {}; |
| 929 | |
| 930 | // An injection point to control how bound objects passed to the target |
| 931 | // function. BindUnwrapTraits<>::Unwrap() is called for each bound objects right |
| 932 | // before the target function is invoked. |
| 933 | template <typename> |
| 934 | struct BindUnwrapTraits { |
| 935 | template <typename T> |
| 936 | static T&& Unwrap(T&& o) { |
| 937 | return std::forward<T>(o); |
| 938 | } |
| 939 | }; |
| 940 | |
| 941 | template <typename T> |
| 942 | struct BindUnwrapTraits<internal::UnretainedWrapper<T>> { |
| 943 | static T* Unwrap(const internal::UnretainedWrapper<T>& o) { return o.get(); } |
| 944 | }; |
| 945 | |
| 946 | template <typename T> |
jdoerrie | c79c2fa2 | 2019-02-26 12:35:03 | [diff] [blame] | 947 | struct BindUnwrapTraits<std::reference_wrapper<T>> { |
| 948 | static T& Unwrap(std::reference_wrapper<T> o) { return o.get(); } |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 949 | }; |
| 950 | |
| 951 | template <typename T> |
| 952 | struct BindUnwrapTraits<internal::RetainedRefWrapper<T>> { |
| 953 | static T* Unwrap(const internal::RetainedRefWrapper<T>& o) { return o.get(); } |
| 954 | }; |
| 955 | |
Marijn Kruisselbrink | c1e775f | 2019-12-19 01:24:22 | [diff] [blame] | 956 | template <typename T, typename Deleter> |
| 957 | struct BindUnwrapTraits<internal::OwnedWrapper<T, Deleter>> { |
| 958 | static T* Unwrap(const internal::OwnedWrapper<T, Deleter>& o) { |
| 959 | return o.get(); |
| 960 | } |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 961 | }; |
| 962 | |
| 963 | template <typename T> |
| 964 | struct BindUnwrapTraits<internal::PassedWrapper<T>> { |
| 965 | static T Unwrap(const internal::PassedWrapper<T>& o) { return o.Take(); } |
| 966 | }; |
| 967 | |
tzik | c44f810 | 2018-07-24 09:49:19 | [diff] [blame] | 968 | #if defined(OS_WIN) |
| 969 | template <typename T> |
| 970 | struct BindUnwrapTraits<Microsoft::WRL::ComPtr<T>> { |
| 971 | static T* Unwrap(const Microsoft::WRL::ComPtr<T>& ptr) { return ptr.Get(); } |
| 972 | }; |
| 973 | #endif |
| 974 | |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 975 | // CallbackCancellationTraits allows customization of Callback's cancellation |
| 976 | // semantics. By default, callbacks are not cancellable. A specialization should |
| 977 | // set is_cancellable = true and implement an IsCancelled() that returns if the |
| 978 | // callback should be cancelled. |
| 979 | template <typename Functor, typename BoundArgsTuple, typename SFINAE> |
| 980 | struct CallbackCancellationTraits { |
| 981 | static constexpr bool is_cancellable = false; |
| 982 | }; |
| 983 | |
| 984 | // Specialization for method bound to weak pointer receiver. |
| 985 | template <typename Functor, typename... BoundArgs> |
| 986 | struct CallbackCancellationTraits< |
| 987 | Functor, |
| 988 | std::tuple<BoundArgs...>, |
| 989 | std::enable_if_t< |
| 990 | internal::IsWeakMethod<internal::FunctorTraits<Functor>::is_method, |
| 991 | BoundArgs...>::value>> { |
| 992 | static constexpr bool is_cancellable = true; |
| 993 | |
| 994 | template <typename Receiver, typename... Args> |
| 995 | static bool IsCancelled(const Functor&, |
| 996 | const Receiver& receiver, |
| 997 | const Args&...) { |
| 998 | return !receiver; |
| 999 | } |
Nicolas Ouellet-payeur | 40f8e9a | 2018-07-30 16:26:48 | [diff] [blame] | 1000 | |
| 1001 | template <typename Receiver, typename... Args> |
| 1002 | static bool MaybeValid(const Functor&, |
| 1003 | const Receiver& receiver, |
| 1004 | const Args&...) { |
| 1005 | return receiver.MaybeValid(); |
| 1006 | } |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1007 | }; |
| 1008 | |
| 1009 | // Specialization for a nested bind. |
| 1010 | template <typename Signature, typename... BoundArgs> |
| 1011 | struct CallbackCancellationTraits<OnceCallback<Signature>, |
| 1012 | std::tuple<BoundArgs...>> { |
| 1013 | static constexpr bool is_cancellable = true; |
| 1014 | |
| 1015 | template <typename Functor> |
| 1016 | static bool IsCancelled(const Functor& functor, const BoundArgs&...) { |
| 1017 | return functor.IsCancelled(); |
| 1018 | } |
Nicolas Ouellet-payeur | 40f8e9a | 2018-07-30 16:26:48 | [diff] [blame] | 1019 | |
| 1020 | template <typename Functor> |
| 1021 | static bool MaybeValid(const Functor& functor, const BoundArgs&...) { |
| 1022 | return functor.MaybeValid(); |
| 1023 | } |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1024 | }; |
| 1025 | |
| 1026 | template <typename Signature, typename... BoundArgs> |
| 1027 | struct CallbackCancellationTraits<RepeatingCallback<Signature>, |
| 1028 | std::tuple<BoundArgs...>> { |
| 1029 | static constexpr bool is_cancellable = true; |
| 1030 | |
| 1031 | template <typename Functor> |
| 1032 | static bool IsCancelled(const Functor& functor, const BoundArgs&...) { |
| 1033 | return functor.IsCancelled(); |
| 1034 | } |
Nicolas Ouellet-payeur | 40f8e9a | 2018-07-30 16:26:48 | [diff] [blame] | 1035 | |
| 1036 | template <typename Functor> |
| 1037 | static bool MaybeValid(const Functor& functor, const BoundArgs&...) { |
| 1038 | return functor.MaybeValid(); |
| 1039 | } |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1040 | }; |
| 1041 | |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 1042 | // Returns a RunType of bound functor. |
| 1043 | // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). |
| 1044 | template <typename Functor, typename... BoundArgs> |
| 1045 | using MakeUnboundRunType = |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 1046 | typename internal::BindTypeHelper<Functor, BoundArgs...>::UnboundRunType; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 1047 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 1048 | } // namespace base |
| 1049 | |
| 1050 | #endif // BASE_BIND_INTERNAL_H_ |