[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 | |
vmpstr | c52317f | 2015-11-18 08:43:26 | [diff] [blame] | 10 | #include <type_traits> |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 11 | #include <utility> |
vmpstr | c52317f | 2015-11-18 08:43:26 | [diff] [blame] | 12 | |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 13 | #include "base/callback_internal.h" |
[email protected] | 8217d454 | 2011-10-01 06:31:41 | [diff] [blame] | 14 | #include "base/memory/raw_scoped_refptr_mismatch_checker.h" |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 16 | #include "base/template_util.h" |
[email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 17 | #include "build/build_config.h" |
| 18 | |
[email protected] | 2429264 | 2012-07-12 20:06:40 | [diff] [blame] | 19 | // See base/callback.h for user documentation. |
| 20 | // |
| 21 | // |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 22 | // CONCEPTS: |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 23 | // Functor -- A movable type representing something that should be called. |
| 24 | // All function pointers and Callback<> are functors even if the |
| 25 | // invocation syntax differs. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 26 | // RunType -- A function type (as opposed to function _pointer_ type) for |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 27 | // a Callback<>::Run(). Usually just a convenience typedef. |
tzik | ce3ecf8 | 2015-12-15 06:41:49 | [diff] [blame] | 28 | // (Bound)Args -- A set of types that stores the arguments. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 29 | // |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 30 | // Types: |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 31 | // ForceVoidReturn<> -- Helper class for translating function signatures to |
| 32 | // equivalent forms with a "void" return type. |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 33 | // FunctorTraits<> -- Type traits used to determine the correct RunType and |
| 34 | // invocation manner for a Functor. This is where function |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 35 | // signature adapters are applied. |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 36 | // InvokeHelper<> -- Take a Functor + arguments and actully invokes it. |
tzik | 8ce6570 | 2015-02-05 19:11:26 | [diff] [blame] | 37 | // Handle the differing syntaxes needed for WeakPtr<> |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 38 | // support. This is separate from Invoker to avoid creating |
| 39 | // multiple version of Invoker<>. |
| 40 | // Invoker<> -- Unwraps the curried parameters and executes the Functor. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 41 | // BindState<> -- Stores the curried parameters, and is the main entry point |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 42 | // into the Bind() system. |
[email protected] | 4346ef91 | 2011-02-19 00:52:15 | [diff] [blame] | 43 | |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 44 | namespace base { |
| 45 | |
| 46 | template <typename T> |
| 47 | struct IsWeakReceiver; |
| 48 | |
| 49 | template <typename> |
| 50 | struct BindUnwrapTraits; |
| 51 | |
| 52 | template <typename Functor, typename BoundArgsTuple, typename SFINAE = void> |
| 53 | struct CallbackCancellationTraits; |
| 54 | |
| 55 | namespace internal { |
| 56 | |
| 57 | template <typename Functor, typename SFINAE = void> |
| 58 | struct FunctorTraits; |
| 59 | |
| 60 | template <typename T> |
| 61 | class UnretainedWrapper { |
| 62 | public: |
| 63 | explicit UnretainedWrapper(T* o) : ptr_(o) {} |
| 64 | T* get() const { return ptr_; } |
| 65 | |
| 66 | private: |
| 67 | T* ptr_; |
| 68 | }; |
| 69 | |
| 70 | template <typename T> |
| 71 | class ConstRefWrapper { |
| 72 | public: |
| 73 | explicit ConstRefWrapper(const T& o) : ptr_(&o) {} |
| 74 | const T& get() const { return *ptr_; } |
| 75 | |
| 76 | private: |
| 77 | const T* ptr_; |
| 78 | }; |
| 79 | |
| 80 | template <typename T> |
| 81 | class RetainedRefWrapper { |
| 82 | public: |
| 83 | explicit RetainedRefWrapper(T* o) : ptr_(o) {} |
| 84 | explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {} |
| 85 | T* get() const { return ptr_.get(); } |
| 86 | |
| 87 | private: |
| 88 | scoped_refptr<T> ptr_; |
| 89 | }; |
| 90 | |
| 91 | template <typename T> |
| 92 | struct IgnoreResultHelper { |
| 93 | explicit IgnoreResultHelper(T functor) : functor_(std::move(functor)) {} |
| 94 | explicit operator bool() const { return !!functor_; } |
| 95 | |
| 96 | T functor_; |
| 97 | }; |
| 98 | |
| 99 | // An alternate implementation is to avoid the destructive copy, and instead |
| 100 | // specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to |
| 101 | // a class that is essentially a std::unique_ptr<>. |
| 102 | // |
| 103 | // The current implementation has the benefit though of leaving ParamTraits<> |
| 104 | // fully in callback_internal.h as well as avoiding type conversions during |
| 105 | // storage. |
| 106 | template <typename T> |
| 107 | class OwnedWrapper { |
| 108 | public: |
| 109 | explicit OwnedWrapper(T* o) : ptr_(o) {} |
| 110 | ~OwnedWrapper() { delete ptr_; } |
| 111 | T* get() const { return ptr_; } |
| 112 | OwnedWrapper(OwnedWrapper&& other) { |
| 113 | ptr_ = other.ptr_; |
| 114 | other.ptr_ = NULL; |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | mutable T* ptr_; |
| 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> |
| 166 | auto Unwrap(T&& o) -> decltype(Unwrapper<T>::Unwrap(std::forward<T>(o))) { |
| 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 | |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 326 | // HasRefCountedTypeAsRawPtr selects true_type when any of the |Args| is a raw |
| 327 | // pointer to a RefCounted type. |
| 328 | // Implementation note: This non-specialized case handles zero-arity case only. |
| 329 | // Non-zero-arity cases should be handled by the specialization below. |
| 330 | template <typename... Args> |
tzik | 403cb6c | 2016-03-10 07:17:25 | [diff] [blame] | 331 | struct HasRefCountedTypeAsRawPtr : std::false_type {}; |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 332 | |
| 333 | // Implementation note: Select true_type if the first parameter is a raw pointer |
| 334 | // to a RefCounted type. Otherwise, skip the first parameter and check rest of |
| 335 | // parameters recursively. |
| 336 | template <typename T, typename... Args> |
| 337 | struct HasRefCountedTypeAsRawPtr<T, Args...> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 338 | : std::conditional_t<NeedsScopedRefptrButGetsRawPtr<T>::value, |
| 339 | std::true_type, |
| 340 | HasRefCountedTypeAsRawPtr<Args...>> {}; |
tzik | 401dd367 | 2014-11-26 07:54:58 | [diff] [blame] | 341 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 342 | // ForceVoidReturn<> |
| 343 | // |
| 344 | // Set of templates that support forcing the function return type to void. |
| 345 | template <typename Sig> |
| 346 | struct ForceVoidReturn; |
| 347 | |
tzik | c8214992 | 2014-11-20 10:09:45 | [diff] [blame] | 348 | template <typename R, typename... Args> |
| 349 | struct ForceVoidReturn<R(Args...)> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 350 | using RunType = void(Args...); |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 351 | }; |
| 352 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 353 | // FunctorTraits<> |
| 354 | // |
| 355 | // See description at top of file. |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 356 | template <typename Functor, typename SFINAE> |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 357 | struct FunctorTraits; |
| 358 | |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 359 | // For empty callable types. |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 360 | // This specialization is intended to allow binding captureless lambdas by |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 361 | // base::Bind(), based on the fact that captureless lambdas are empty while |
| 362 | // capturing lambdas are not. This also allows any functors as far as it's an |
| 363 | // empty class. |
| 364 | // Example: |
| 365 | // |
| 366 | // // Captureless lambdas are allowed. |
| 367 | // []() {return 42;}; |
| 368 | // |
| 369 | // // Capturing lambdas are *not* allowed. |
| 370 | // int x; |
| 371 | // [x]() {return x;}; |
| 372 | // |
| 373 | // // Any empty class with operator() is allowed. |
| 374 | // struct Foo { |
| 375 | // void operator()() const {} |
| 376 | // // No non-static member variable and no virtual functions. |
| 377 | // }; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 378 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 379 | struct FunctorTraits<Functor, |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 380 | std::enable_if_t<IsCallableObject<Functor>::value && |
| 381 | std::is_empty<Functor>::value>> { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 382 | using RunType = ExtractCallableRunType<Functor>; |
| 383 | static constexpr bool is_method = false; |
| 384 | static constexpr bool is_nullable = false; |
| 385 | |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 386 | template <typename RunFunctor, typename... RunArgs> |
| 387 | static ExtractReturnType<RunType> Invoke(RunFunctor&& functor, |
| 388 | RunArgs&&... args) { |
| 389 | return std::forward<RunFunctor>(functor)(std::forward<RunArgs>(args)...); |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 390 | } |
| 391 | }; |
| 392 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 393 | // For functions. |
| 394 | template <typename R, typename... Args> |
| 395 | struct FunctorTraits<R (*)(Args...)> { |
| 396 | using RunType = R(Args...); |
| 397 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 398 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 399 | |
| 400 | template <typename... RunArgs> |
| 401 | static R Invoke(R (*function)(Args...), RunArgs&&... args) { |
| 402 | return function(std::forward<RunArgs>(args)...); |
| 403 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 404 | }; |
| 405 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 406 | #if defined(OS_WIN) && !defined(ARCH_CPU_X86_64) |
| 407 | |
| 408 | // For functions. |
| 409 | template <typename R, typename... Args> |
| 410 | struct FunctorTraits<R(__stdcall*)(Args...)> { |
| 411 | using RunType = R(Args...); |
| 412 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 413 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 414 | |
| 415 | template <typename... RunArgs> |
| 416 | static R Invoke(R(__stdcall* function)(Args...), RunArgs&&... args) { |
| 417 | return function(std::forward<RunArgs>(args)...); |
| 418 | } |
| 419 | }; |
| 420 | |
| 421 | // For functions. |
| 422 | template <typename R, typename... Args> |
| 423 | struct FunctorTraits<R(__fastcall*)(Args...)> { |
| 424 | using RunType = R(Args...); |
| 425 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 426 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 427 | |
| 428 | template <typename... RunArgs> |
| 429 | static R Invoke(R(__fastcall* function)(Args...), RunArgs&&... args) { |
| 430 | return function(std::forward<RunArgs>(args)...); |
| 431 | } |
| 432 | }; |
| 433 | |
| 434 | #endif // defined(OS_WIN) && !defined(ARCH_CPU_X86_64) |
| 435 | |
| 436 | // For methods. |
| 437 | template <typename R, typename Receiver, typename... Args> |
| 438 | struct FunctorTraits<R (Receiver::*)(Args...)> { |
| 439 | using RunType = R(Receiver*, Args...); |
| 440 | static constexpr bool is_method = true; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 441 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 442 | |
| 443 | template <typename ReceiverPtr, typename... RunArgs> |
| 444 | static R Invoke(R (Receiver::*method)(Args...), |
| 445 | ReceiverPtr&& receiver_ptr, |
| 446 | RunArgs&&... args) { |
tzik | 75851f4 | 2017-06-14 06:57:01 | [diff] [blame] | 447 | return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 448 | } |
| 449 | }; |
| 450 | |
| 451 | // For const methods. |
| 452 | template <typename R, typename Receiver, typename... Args> |
| 453 | struct FunctorTraits<R (Receiver::*)(Args...) const> { |
| 454 | using RunType = R(const Receiver*, Args...); |
| 455 | static constexpr bool is_method = true; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 456 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 457 | |
| 458 | template <typename ReceiverPtr, typename... RunArgs> |
| 459 | static R Invoke(R (Receiver::*method)(Args...) const, |
| 460 | ReceiverPtr&& receiver_ptr, |
| 461 | RunArgs&&... args) { |
tzik | 75851f4 | 2017-06-14 06:57:01 | [diff] [blame] | 462 | return ((*receiver_ptr).*method)(std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 463 | } |
| 464 | }; |
| 465 | |
| 466 | // For IgnoreResults. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 467 | template <typename T> |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 468 | struct FunctorTraits<IgnoreResultHelper<T>> : FunctorTraits<T> { |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 469 | using RunType = |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 470 | typename ForceVoidReturn<typename FunctorTraits<T>::RunType>::RunType; |
| 471 | |
| 472 | template <typename IgnoreResultType, typename... RunArgs> |
| 473 | static void Invoke(IgnoreResultType&& ignore_result_helper, |
| 474 | RunArgs&&... args) { |
tzik | ff54a5b15 | 2016-08-31 11:50:41 | [diff] [blame] | 475 | FunctorTraits<T>::Invoke( |
| 476 | std::forward<IgnoreResultType>(ignore_result_helper).functor_, |
| 477 | std::forward<RunArgs>(args)...); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 478 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 479 | }; |
| 480 | |
tzik | d4bb5b7d | 2017-08-28 19:08:52 | [diff] [blame] | 481 | // For OnceCallbacks. |
| 482 | template <typename R, typename... Args> |
| 483 | struct FunctorTraits<OnceCallback<R(Args...)>> { |
| 484 | using RunType = R(Args...); |
| 485 | static constexpr bool is_method = false; |
| 486 | static constexpr bool is_nullable = true; |
| 487 | |
| 488 | template <typename CallbackType, typename... RunArgs> |
| 489 | static R Invoke(CallbackType&& callback, RunArgs&&... args) { |
| 490 | DCHECK(!callback.is_null()); |
| 491 | return std::forward<CallbackType>(callback).Run( |
| 492 | std::forward<RunArgs>(args)...); |
| 493 | } |
| 494 | }; |
| 495 | |
| 496 | // For RepeatingCallbacks. |
| 497 | template <typename R, typename... Args> |
| 498 | struct FunctorTraits<RepeatingCallback<R(Args...)>> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 499 | using RunType = R(Args...); |
| 500 | static constexpr bool is_method = false; |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 501 | static constexpr bool is_nullable = true; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 502 | |
| 503 | template <typename CallbackType, typename... RunArgs> |
| 504 | static R Invoke(CallbackType&& callback, RunArgs&&... args) { |
| 505 | DCHECK(!callback.is_null()); |
| 506 | return std::forward<CallbackType>(callback).Run( |
| 507 | std::forward<RunArgs>(args)...); |
| 508 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 509 | }; |
| 510 | |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 511 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 512 | using MakeFunctorTraits = FunctorTraits<std::decay_t<Functor>>; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 513 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 514 | // InvokeHelper<> |
| 515 | // |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 516 | // There are 2 logical InvokeHelper<> specializations: normal, WeakCalls. |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 517 | // |
| 518 | // The normal type just calls the underlying runnable. |
| 519 | // |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 520 | // WeakCalls need special syntax that is applied to the first argument to check |
| 521 | // if they should no-op themselves. |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 522 | template <bool is_weak_call, typename ReturnType> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 523 | struct InvokeHelper; |
| 524 | |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 525 | template <typename ReturnType> |
| 526 | struct InvokeHelper<false, ReturnType> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 527 | template <typename Functor, typename... RunArgs> |
| 528 | static inline ReturnType MakeItSo(Functor&& functor, RunArgs&&... args) { |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 529 | using Traits = MakeFunctorTraits<Functor>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 530 | return Traits::Invoke(std::forward<Functor>(functor), |
| 531 | std::forward<RunArgs>(args)...); |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 532 | } |
| 533 | }; |
| 534 | |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 535 | template <typename ReturnType> |
| 536 | struct InvokeHelper<true, ReturnType> { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 537 | // WeakCalls are only supported for functions with a void return type. |
| 538 | // Otherwise, the function result would be undefined if the the WeakPtr<> |
| 539 | // is invalidated. |
tzik | 403cb6c | 2016-03-10 07:17:25 | [diff] [blame] | 540 | static_assert(std::is_void<ReturnType>::value, |
avi | 4ec0dff | 2015-11-24 14:26:24 | [diff] [blame] | 541 | "weak_ptrs can only bind to methods without return values"); |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 542 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 543 | template <typename Functor, typename BoundWeakPtr, typename... RunArgs> |
| 544 | static inline void MakeItSo(Functor&& functor, |
tzik | 33871d8 | 2016-07-14 12:12:06 | [diff] [blame] | 545 | BoundWeakPtr&& weak_ptr, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 546 | RunArgs&&... args) { |
| 547 | if (!weak_ptr) |
| 548 | return; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 549 | using Traits = MakeFunctorTraits<Functor>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 550 | Traits::Invoke(std::forward<Functor>(functor), |
| 551 | std::forward<BoundWeakPtr>(weak_ptr), |
| 552 | std::forward<RunArgs>(args)...); |
| 553 | } |
| 554 | }; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 555 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 556 | // Invoker<> |
| 557 | // |
| 558 | // See description at the top of the file. |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 559 | template <typename StorageType, typename UnboundRunType> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 560 | struct Invoker; |
| 561 | |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 562 | template <typename StorageType, typename R, typename... UnboundArgs> |
| 563 | struct Invoker<StorageType, R(UnboundArgs...)> { |
Vladislav Kuzkokov | 6d208e1 | 2017-11-08 21:31:08 | [diff] [blame] | 564 | static R RunOnce(BindStateBase* base, |
| 565 | PassingTraitsType<UnboundArgs>... unbound_args) { |
tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 566 | // Local references to make debugger stepping easier. If in a debugger, |
| 567 | // you really want to warp ahead and step through the |
| 568 | // InvokeHelper<>::MakeItSo() call below. |
| 569 | StorageType* storage = static_cast<StorageType*>(base); |
| 570 | static constexpr size_t num_bound_args = |
| 571 | std::tuple_size<decltype(storage->bound_args_)>::value; |
| 572 | return RunImpl(std::move(storage->functor_), |
| 573 | std::move(storage->bound_args_), |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 574 | std::make_index_sequence<num_bound_args>(), |
tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 575 | std::forward<UnboundArgs>(unbound_args)...); |
| 576 | } |
| 577 | |
Vladislav Kuzkokov | 6d208e1 | 2017-11-08 21:31:08 | [diff] [blame] | 578 | static R Run(BindStateBase* base, |
| 579 | PassingTraitsType<UnboundArgs>... unbound_args) { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 580 | // Local references to make debugger stepping easier. If in a debugger, |
| 581 | // you really want to warp ahead and step through the |
| 582 | // InvokeHelper<>::MakeItSo() call below. |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 583 | const StorageType* storage = static_cast<StorageType*>(base); |
| 584 | static constexpr size_t num_bound_args = |
| 585 | std::tuple_size<decltype(storage->bound_args_)>::value; |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 586 | return RunImpl(storage->functor_, storage->bound_args_, |
| 587 | std::make_index_sequence<num_bound_args>(), |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 588 | std::forward<UnboundArgs>(unbound_args)...); |
| 589 | } |
| 590 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 591 | private: |
| 592 | template <typename Functor, typename BoundArgsTuple, size_t... indices> |
| 593 | static inline R RunImpl(Functor&& functor, |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 594 | BoundArgsTuple&& bound, |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 595 | std::index_sequence<indices...>, |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 596 | UnboundArgs&&... unbound_args) { |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 597 | static constexpr bool is_method = MakeFunctorTraits<Functor>::is_method; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 598 | |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 599 | using DecayedArgsTuple = std::decay_t<BoundArgsTuple>; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 600 | static constexpr bool is_weak_call = |
| 601 | IsWeakMethod<is_method, |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 602 | std::tuple_element_t<indices, DecayedArgsTuple>...>(); |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 603 | |
tzik | ee24872 | 2016-06-01 08:22:51 | [diff] [blame] | 604 | return InvokeHelper<is_weak_call, R>::MakeItSo( |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 605 | std::forward<Functor>(functor), |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 606 | Unwrap(std::get<indices>(std::forward<BoundArgsTuple>(bound)))..., |
tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 607 | std::forward<UnboundArgs>(unbound_args)...); |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 608 | } |
| 609 | }; |
| 610 | |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 611 | // Extracts necessary type info from Functor and BoundArgs. |
| 612 | // Used to implement MakeUnboundRunType, BindOnce and BindRepeating. |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 613 | template <typename Functor, typename... BoundArgs> |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 614 | struct BindTypeHelper { |
| 615 | static constexpr size_t num_bounds = sizeof...(BoundArgs); |
| 616 | using FunctorTraits = MakeFunctorTraits<Functor>; |
| 617 | |
| 618 | // Example: |
| 619 | // When Functor is `double (Foo::*)(int, const std::string&)`, and BoundArgs |
| 620 | // is a template pack of `Foo*` and `int16_t`: |
| 621 | // - RunType is `double(Foo*, int, const std::string&)`, |
| 622 | // - ReturnType is `double`, |
| 623 | // - RunParamsList is `TypeList<Foo*, int, const std::string&>`, |
| 624 | // - BoundParamsList is `TypeList<Foo*, int>`, |
| 625 | // - UnboundParamsList is `TypeList<const std::string&>`, |
| 626 | // - BoundArgsList is `TypeList<Foo*, int16_t>`, |
| 627 | // - UnboundRunType is `double(const std::string&)`. |
| 628 | using RunType = typename FunctorTraits::RunType; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 629 | using ReturnType = ExtractReturnType<RunType>; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 630 | |
| 631 | using RunParamsList = ExtractArgs<RunType>; |
| 632 | using BoundParamsList = TakeTypeListItem<num_bounds, RunParamsList>; |
| 633 | using UnboundParamsList = DropTypeListItem<num_bounds, RunParamsList>; |
| 634 | |
| 635 | using BoundArgsList = TypeList<BoundArgs...>; |
| 636 | |
| 637 | using UnboundRunType = MakeFunctionType<ReturnType, UnboundParamsList>; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 638 | }; |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 639 | |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 640 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 641 | std::enable_if_t<FunctorTraits<Functor>::is_nullable, bool> IsNull( |
| 642 | const Functor& functor) { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 643 | return !functor; |
| 644 | } |
| 645 | |
| 646 | template <typename Functor> |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 647 | std::enable_if_t<!FunctorTraits<Functor>::is_nullable, bool> IsNull( |
| 648 | const Functor&) { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 649 | return false; |
| 650 | } |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 651 | |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 652 | // Used by ApplyCancellationTraits below. |
| 653 | template <typename Functor, typename BoundArgsTuple, size_t... indices> |
| 654 | bool ApplyCancellationTraitsImpl(const Functor& functor, |
| 655 | const BoundArgsTuple& bound_args, |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 656 | std::index_sequence<indices...>) { |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 657 | return CallbackCancellationTraits<Functor, BoundArgsTuple>::IsCancelled( |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 658 | functor, std::get<indices>(bound_args)...); |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 659 | } |
tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 660 | |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 661 | // Relays |base| to corresponding CallbackCancellationTraits<>::Run(). Returns |
| 662 | // true if the callback |base| represents is canceled. |
| 663 | template <typename BindStateType> |
| 664 | bool ApplyCancellationTraits(const BindStateBase* base) { |
| 665 | const BindStateType* storage = static_cast<const BindStateType*>(base); |
| 666 | static constexpr size_t num_bound_args = |
| 667 | std::tuple_size<decltype(storage->bound_args_)>::value; |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 668 | return ApplyCancellationTraitsImpl( |
| 669 | storage->functor_, storage->bound_args_, |
| 670 | std::make_index_sequence<num_bound_args>()); |
tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 671 | }; |
| 672 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 673 | // BindState<> |
| 674 | // |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 675 | // This stores all the state passed into Bind(). |
| 676 | template <typename Functor, typename... BoundArgs> |
| 677 | struct BindState final : BindStateBase { |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 678 | using IsCancellable = std::integral_constant< |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 679 | bool, |
| 680 | CallbackCancellationTraits<Functor, |
| 681 | std::tuple<BoundArgs...>>::is_cancellable>; |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 682 | |
tzik | bfe6612 | 2016-07-08 14:14:01 | [diff] [blame] | 683 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
tzik | 1886c27 | 2016-09-08 05:45:38 | [diff] [blame] | 684 | explicit BindState(BindStateBase::InvokeFuncStorage invoke_func, |
| 685 | ForwardFunctor&& functor, |
| 686 | ForwardBoundArgs&&... bound_args) |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 687 | // IsCancellable is std::false_type if |
| 688 | // CallbackCancellationTraits<>::IsCancelled returns always false. |
| 689 | // Otherwise, it's std::true_type. |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 690 | : BindState(IsCancellable{}, |
| 691 | invoke_func, |
| 692 | std::forward<ForwardFunctor>(functor), |
tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 693 | std::forward<ForwardBoundArgs>(bound_args)...) {} |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 694 | |
| 695 | Functor functor_; |
| 696 | std::tuple<BoundArgs...> bound_args_; |
| 697 | |
| 698 | private: |
| 699 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| 700 | explicit BindState(std::true_type, |
| 701 | BindStateBase::InvokeFuncStorage invoke_func, |
| 702 | ForwardFunctor&& functor, |
| 703 | ForwardBoundArgs&&... bound_args) |
tzik | 6c92eab | 2016-11-25 15:56:36 | [diff] [blame] | 704 | : BindStateBase(invoke_func, |
| 705 | &Destroy, |
| 706 | &ApplyCancellationTraits<BindState>), |
tzik | ff54a5b15 | 2016-08-31 11:50:41 | [diff] [blame] | 707 | functor_(std::forward<ForwardFunctor>(functor)), |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 708 | bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 709 | DCHECK(!IsNull(functor_)); |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 710 | } |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 711 | |
tzik | 1fdcca3 | 2016-09-14 07:15:00 | [diff] [blame] | 712 | template <typename ForwardFunctor, typename... ForwardBoundArgs> |
| 713 | explicit BindState(std::false_type, |
| 714 | BindStateBase::InvokeFuncStorage invoke_func, |
| 715 | ForwardFunctor&& functor, |
| 716 | ForwardBoundArgs&&... bound_args) |
| 717 | : BindStateBase(invoke_func, &Destroy), |
| 718 | functor_(std::forward<ForwardFunctor>(functor)), |
| 719 | bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
| 720 | DCHECK(!IsNull(functor_)); |
| 721 | } |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 722 | |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 723 | ~BindState() = default; |
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 724 | |
tzik | 30e0c31 | 2016-09-21 08:06:54 | [diff] [blame] | 725 | static void Destroy(const BindStateBase* self) { |
| 726 | delete static_cast<const BindState*>(self); |
tapted | e7e804c | 2015-05-14 08:03:32 | [diff] [blame] | 727 | } |
[email protected] | fccef155 | 2011-11-28 22:13:54 | [diff] [blame] | 728 | }; |
| 729 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 730 | // Used to implement MakeBindStateType. |
| 731 | template <bool is_method, typename Functor, typename... BoundArgs> |
| 732 | struct MakeBindStateTypeImpl; |
| 733 | |
| 734 | template <typename Functor, typename... BoundArgs> |
| 735 | struct MakeBindStateTypeImpl<false, Functor, BoundArgs...> { |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 736 | static_assert(!HasRefCountedTypeAsRawPtr<std::decay_t<BoundArgs>...>::value, |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 737 | "A parameter is a refcounted type and needs scoped_refptr."); |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 738 | using Type = BindState<std::decay_t<Functor>, std::decay_t<BoundArgs>...>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 739 | }; |
| 740 | |
| 741 | template <typename Functor> |
| 742 | struct MakeBindStateTypeImpl<true, Functor> { |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 743 | using Type = BindState<std::decay_t<Functor>>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 744 | }; |
| 745 | |
| 746 | template <typename Functor, typename Receiver, typename... BoundArgs> |
| 747 | struct MakeBindStateTypeImpl<true, Functor, Receiver, BoundArgs...> { |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 748 | private: |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 749 | using DecayedReceiver = std::decay_t<Receiver>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 750 | |
tzik | 4625ac61 | 2018-02-28 09:43:55 | [diff] [blame^] | 751 | static_assert(!std::is_array<std::remove_reference_t<Receiver>>::value, |
| 752 | "First bound argument to a method cannot be an array."); |
| 753 | static_assert( |
| 754 | !std::is_pointer<DecayedReceiver>::value || |
| 755 | IsRefCountedType<std::remove_pointer_t<DecayedReceiver>>::value, |
| 756 | "Receivers may not be raw pointers. If using a raw pointer here is safe" |
| 757 | " and has no lifetime concerns, use base::Unretained() and document why" |
| 758 | " it's safe."); |
| 759 | static_assert(!HasRefCountedTypeAsRawPtr<std::decay_t<BoundArgs>...>::value, |
| 760 | "A parameter is a refcounted type and needs scoped_refptr."); |
| 761 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 762 | public: |
| 763 | using Type = BindState< |
Jeremy Roman | 35a31743 | 2017-08-16 22:20:53 | [diff] [blame] | 764 | std::decay_t<Functor>, |
| 765 | std::conditional_t<std::is_pointer<DecayedReceiver>::value, |
| 766 | scoped_refptr<std::remove_pointer_t<DecayedReceiver>>, |
| 767 | DecayedReceiver>, |
| 768 | std::decay_t<BoundArgs>...>; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 769 | }; |
| 770 | |
| 771 | template <typename Functor, typename... BoundArgs> |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 772 | using MakeBindStateType = |
| 773 | typename MakeBindStateTypeImpl<MakeFunctorTraits<Functor>::is_method, |
| 774 | Functor, |
| 775 | BoundArgs...>::Type; |
tzik | 99de02b | 2016-07-01 05:54:12 | [diff] [blame] | 776 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 777 | } // namespace internal |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 778 | |
Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 779 | // An injection point to control |this| pointer behavior on a method invocation. |
| 780 | // If IsWeakReceiver<> is true_type for |T| and |T| is used for a receiver of a |
| 781 | // method, base::Bind cancels the method invocation if the receiver is tested as |
| 782 | // false. |
| 783 | // E.g. Foo::bar() is not called: |
| 784 | // struct Foo : base::SupportsWeakPtr<Foo> { |
| 785 | // void bar() {} |
| 786 | // }; |
| 787 | // |
| 788 | // WeakPtr<Foo> oo = nullptr; |
| 789 | // base::Bind(&Foo::bar, oo).Run(); |
| 790 | template <typename T> |
| 791 | struct IsWeakReceiver : std::false_type {}; |
| 792 | |
| 793 | template <typename T> |
| 794 | struct IsWeakReceiver<internal::ConstRefWrapper<T>> : IsWeakReceiver<T> {}; |
| 795 | |
| 796 | template <typename T> |
| 797 | struct IsWeakReceiver<WeakPtr<T>> : std::true_type {}; |
| 798 | |
| 799 | // An injection point to control how bound objects passed to the target |
| 800 | // function. BindUnwrapTraits<>::Unwrap() is called for each bound objects right |
| 801 | // before the target function is invoked. |
| 802 | template <typename> |
| 803 | struct BindUnwrapTraits { |
| 804 | template <typename T> |
| 805 | static T&& Unwrap(T&& o) { |
| 806 | return std::forward<T>(o); |
| 807 | } |
| 808 | }; |
| 809 | |
| 810 | template <typename T> |
| 811 | struct BindUnwrapTraits<internal::UnretainedWrapper<T>> { |
| 812 | static T* Unwrap(const internal::UnretainedWrapper<T>& o) { return o.get(); } |
| 813 | }; |
| 814 | |
| 815 | template <typename T> |
| 816 | struct BindUnwrapTraits<internal::ConstRefWrapper<T>> { |
| 817 | static const T& Unwrap(const internal::ConstRefWrapper<T>& o) { |
| 818 | return o.get(); |
| 819 | } |
| 820 | }; |
| 821 | |
| 822 | template <typename T> |
| 823 | struct BindUnwrapTraits<internal::RetainedRefWrapper<T>> { |
| 824 | static T* Unwrap(const internal::RetainedRefWrapper<T>& o) { return o.get(); } |
| 825 | }; |
| 826 | |
| 827 | template <typename T> |
| 828 | struct BindUnwrapTraits<internal::OwnedWrapper<T>> { |
| 829 | static T* Unwrap(const internal::OwnedWrapper<T>& o) { return o.get(); } |
| 830 | }; |
| 831 | |
| 832 | template <typename T> |
| 833 | struct BindUnwrapTraits<internal::PassedWrapper<T>> { |
| 834 | static T Unwrap(const internal::PassedWrapper<T>& o) { return o.Take(); } |
| 835 | }; |
| 836 | |
| 837 | // CallbackCancellationTraits allows customization of Callback's cancellation |
| 838 | // semantics. By default, callbacks are not cancellable. A specialization should |
| 839 | // set is_cancellable = true and implement an IsCancelled() that returns if the |
| 840 | // callback should be cancelled. |
| 841 | template <typename Functor, typename BoundArgsTuple, typename SFINAE> |
| 842 | struct CallbackCancellationTraits { |
| 843 | static constexpr bool is_cancellable = false; |
| 844 | }; |
| 845 | |
| 846 | // Specialization for method bound to weak pointer receiver. |
| 847 | template <typename Functor, typename... BoundArgs> |
| 848 | struct CallbackCancellationTraits< |
| 849 | Functor, |
| 850 | std::tuple<BoundArgs...>, |
| 851 | std::enable_if_t< |
| 852 | internal::IsWeakMethod<internal::FunctorTraits<Functor>::is_method, |
| 853 | BoundArgs...>::value>> { |
| 854 | static constexpr bool is_cancellable = true; |
| 855 | |
| 856 | template <typename Receiver, typename... Args> |
| 857 | static bool IsCancelled(const Functor&, |
| 858 | const Receiver& receiver, |
| 859 | const Args&...) { |
| 860 | return !receiver; |
| 861 | } |
| 862 | }; |
| 863 | |
| 864 | // Specialization for a nested bind. |
| 865 | template <typename Signature, typename... BoundArgs> |
| 866 | struct CallbackCancellationTraits<OnceCallback<Signature>, |
| 867 | std::tuple<BoundArgs...>> { |
| 868 | static constexpr bool is_cancellable = true; |
| 869 | |
| 870 | template <typename Functor> |
| 871 | static bool IsCancelled(const Functor& functor, const BoundArgs&...) { |
| 872 | return functor.IsCancelled(); |
| 873 | } |
| 874 | }; |
| 875 | |
| 876 | template <typename Signature, typename... BoundArgs> |
| 877 | struct CallbackCancellationTraits<RepeatingCallback<Signature>, |
| 878 | std::tuple<BoundArgs...>> { |
| 879 | static constexpr bool is_cancellable = true; |
| 880 | |
| 881 | template <typename Functor> |
| 882 | static bool IsCancelled(const Functor& functor, const BoundArgs&...) { |
| 883 | return functor.IsCancelled(); |
| 884 | } |
| 885 | }; |
| 886 | |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 887 | // Returns a RunType of bound functor. |
| 888 | // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). |
| 889 | template <typename Functor, typename... BoundArgs> |
| 890 | using MakeUnboundRunType = |
tzik | ae4202e | 2017-07-31 10:41:54 | [diff] [blame] | 891 | typename internal::BindTypeHelper<Functor, BoundArgs...>::UnboundRunType; |
tzik | caf1d84b | 2016-06-28 12:22:21 | [diff] [blame] | 892 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 893 | } // namespace base |
| 894 | |
| 895 | #endif // BASE_BIND_INTERNAL_H_ |