OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_BIND_INTERNAL_H_ | 5 #ifndef BASE_BIND_INTERNAL_H_ |
6 #define BASE_BIND_INTERNAL_H_ | 6 #define BASE_BIND_INTERNAL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <type_traits> | 10 #include <type_traits> |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 // Local references to make debugger stepping easier. If in a debugger, | 348 // Local references to make debugger stepping easier. If in a debugger, |
349 // you really want to warp ahead and step through the | 349 // you really want to warp ahead and step through the |
350 // InvokeHelper<>::MakeItSo() call below. | 350 // InvokeHelper<>::MakeItSo() call below. |
351 return InvokeHelperType::MakeItSo( | 351 return InvokeHelperType::MakeItSo( |
352 storage->runnable_, | 352 storage->runnable_, |
353 Unwrap(get<bound_indices>(storage->bound_args_))..., | 353 Unwrap(get<bound_indices>(storage->bound_args_))..., |
354 CallbackForward(unbound_args)...); | 354 CallbackForward(unbound_args)...); |
355 } | 355 } |
356 }; | 356 }; |
357 | 357 |
| 358 // Used to implement MakeArgsStorage. |
| 359 template <bool is_method, typename... BoundArgs> |
| 360 struct MakeArgsStorageImpl { |
| 361 using Type = std::tuple<BoundArgs...>; |
| 362 }; |
| 363 |
| 364 template <typename Obj, typename... BoundArgs> |
| 365 struct MakeArgsStorageImpl<true, Obj*, BoundArgs...> { |
| 366 using Type = std::tuple<scoped_refptr<Obj>, BoundArgs...>; |
| 367 }; |
| 368 |
| 369 // Constructs a tuple type to store BoundArgs into BindState. |
| 370 // This wraps the first argument into a scoped_refptr if |is_method| is true and |
| 371 // the first argument is a raw pointer. |
| 372 // Other arguments are adjusted for store and packed into a tuple. |
| 373 template <bool is_method, typename... BoundArgs> |
| 374 using MakeArgsStorage = typename MakeArgsStorageImpl< |
| 375 is_method, typename std::decay<BoundArgs>::type...>::Type; |
358 | 376 |
359 // BindState<> | 377 // BindState<> |
360 // | 378 // |
361 // This stores all the state passed into Bind() and is also where most | 379 // This stores all the state passed into Bind() and is also where most |
362 // of the template resolution magic occurs. | 380 // of the template resolution magic occurs. |
363 // | 381 // |
364 // Runnable is the functor we are binding arguments to. | 382 // Runnable is the functor we are binding arguments to. |
365 // RunType is type of the Run() function that the Invoker<> should use. | 383 // RunType is type of the Run() function that the Invoker<> should use. |
366 // Normally, this is the same as the RunType of the Runnable, but it can | 384 // Normally, this is the same as the RunType of the Runnable, but it can |
367 // be different if an adapter like IgnoreResult() has been used. | 385 // be different if an adapter like IgnoreResult() has been used. |
368 // | 386 // |
369 // BoundArgs contains the storage type for all the bound arguments. | 387 // BoundArgs contains the storage type for all the bound arguments. |
370 template <typename Runnable, typename RunType, typename... BoundArgs> | 388 template <typename Runnable, typename RunType, typename... BoundArgs> |
371 struct BindState; | 389 struct BindState; |
372 | 390 |
373 template <typename Runnable, | 391 template <typename Runnable, |
374 typename R, | 392 typename R, |
375 typename... Args, | 393 typename... Args, |
376 typename... BoundArgs> | 394 typename... BoundArgs> |
377 struct BindState<Runnable, R(Args...), BoundArgs...> final | 395 struct BindState<Runnable, R(Args...), BoundArgs...> final |
378 : public BindStateBase { | 396 : public BindStateBase { |
379 private: | 397 private: |
380 using StorageType = BindState<Runnable, R(Args...), BoundArgs...>; | 398 using StorageType = BindState<Runnable, R(Args...), BoundArgs...>; |
381 using RunnableType = Runnable; | 399 using RunnableType = Runnable; |
382 | 400 |
| 401 enum { is_method = HasIsMethodTag<Runnable>::value }; |
| 402 |
383 // true_type if Runnable is a method invocation and the first bound argument | 403 // true_type if Runnable is a method invocation and the first bound argument |
384 // is a WeakPtr. | 404 // is a WeakPtr. |
385 using IsWeakCall = | 405 using IsWeakCall = |
386 IsWeakMethod<HasIsMethodTag<Runnable>::value, BoundArgs...>; | 406 IsWeakMethod<is_method, typename std::decay<BoundArgs>::type...>; |
387 | 407 |
388 using BoundIndices = MakeIndexSequence<sizeof...(BoundArgs)>; | 408 using BoundIndices = MakeIndexSequence<sizeof...(BoundArgs)>; |
389 using UnboundForwardArgs = DropTypeListItem< | 409 using UnboundForwardArgs = DropTypeListItem< |
390 sizeof...(BoundArgs), | 410 sizeof...(BoundArgs), |
391 TypeList<typename CallbackParamTraits<Args>::ForwardType...>>; | 411 TypeList<typename CallbackParamTraits<Args>::ForwardType...>>; |
392 using UnboundForwardRunType = MakeFunctionType<R, UnboundForwardArgs>; | 412 using UnboundForwardRunType = MakeFunctionType<R, UnboundForwardArgs>; |
393 using InvokeHelperType = InvokeHelper<IsWeakCall::value, R, Runnable>; | 413 using InvokeHelperType = InvokeHelper<IsWeakCall::value, R, Runnable>; |
394 | 414 |
395 using UnboundArgs = DropTypeListItem<sizeof...(BoundArgs), TypeList<Args...>>; | 415 using UnboundArgs = DropTypeListItem<sizeof...(BoundArgs), TypeList<Args...>>; |
396 | 416 |
397 public: | 417 public: |
398 using InvokerType = Invoker<BoundIndices, StorageType, | 418 using InvokerType = Invoker<BoundIndices, StorageType, |
399 InvokeHelperType, UnboundForwardRunType>; | 419 InvokeHelperType, UnboundForwardRunType>; |
400 using UnboundRunType = MakeFunctionType<R, UnboundArgs>; | 420 using UnboundRunType = MakeFunctionType<R, UnboundArgs>; |
401 | 421 |
402 template <typename... ForwardArgs> | 422 template <typename... ForwardArgs> |
403 BindState(const Runnable& runnable, ForwardArgs&&... bound_args) | 423 BindState(const Runnable& runnable, ForwardArgs&&... bound_args) |
404 : BindStateBase(&Destroy), | 424 : BindStateBase(&Destroy), |
405 runnable_(runnable), | 425 runnable_(runnable), |
406 ref_(bound_args...), | |
407 bound_args_(std::forward<ForwardArgs>(bound_args)...) {} | 426 bound_args_(std::forward<ForwardArgs>(bound_args)...) {} |
408 | 427 |
409 RunnableType runnable_; | 428 RunnableType runnable_; |
410 MaybeScopedRefPtr<HasIsMethodTag<Runnable>::value, BoundArgs...> ref_; | 429 MakeArgsStorage<is_method, BoundArgs...> bound_args_; |
411 Tuple<BoundArgs...> bound_args_; | |
412 | 430 |
413 private: | 431 private: |
414 ~BindState() {} | 432 ~BindState() {} |
415 | 433 |
416 static void Destroy(BindStateBase* self) { | 434 static void Destroy(BindStateBase* self) { |
417 delete static_cast<BindState*>(self); | 435 delete static_cast<BindState*>(self); |
418 } | 436 } |
419 }; | 437 }; |
420 | 438 |
421 } // namespace internal | 439 } // namespace internal |
422 } // namespace base | 440 } // namespace base |
423 | 441 |
424 #endif // BASE_BIND_INTERNAL_H_ | 442 #endif // BASE_BIND_INTERNAL_H_ |
OLD | NEW |