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