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 <tuple> | 10 #include <tuple> |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 typename std::enable_if<!FunctorTraits<Functor>::is_nullable, bool>::type | 371 typename std::enable_if<!FunctorTraits<Functor>::is_nullable, bool>::type |
372 IsNull(const Functor&) { | 372 IsNull(const Functor&) { |
373 return false; | 373 return false; |
374 } | 374 } |
375 | 375 |
376 template <typename Functor, typename... BoundArgs> | 376 template <typename Functor, typename... BoundArgs> |
377 struct BindState; | 377 struct BindState; |
378 | 378 |
379 template <typename BindStateType, typename SFINAE = void> | 379 template <typename BindStateType, typename SFINAE = void> |
380 struct CancellationChecker { | 380 struct CancellationChecker { |
381 static constexpr bool is_cancellable = false; | |
381 static bool Run(const BindStateBase*) { | 382 static bool Run(const BindStateBase*) { |
382 return false; | 383 return false; |
383 } | 384 } |
384 }; | 385 }; |
385 | 386 |
386 template <typename Functor, typename... BoundArgs> | 387 template <typename Functor, typename... BoundArgs> |
387 struct CancellationChecker< | 388 struct CancellationChecker< |
388 BindState<Functor, BoundArgs...>, | 389 BindState<Functor, BoundArgs...>, |
389 typename std::enable_if<IsWeakMethod<FunctorTraits<Functor>::is_method, | 390 typename std::enable_if<IsWeakMethod<FunctorTraits<Functor>::is_method, |
390 BoundArgs...>::value>::type> { | 391 BoundArgs...>::value>::type> { |
392 static constexpr bool is_cancellable = true; | |
391 static bool Run(const BindStateBase* base) { | 393 static bool Run(const BindStateBase* base) { |
392 using BindStateType = BindState<Functor, BoundArgs...>; | 394 using BindStateType = BindState<Functor, BoundArgs...>; |
393 const BindStateType* bind_state = static_cast<const BindStateType*>(base); | 395 const BindStateType* bind_state = static_cast<const BindStateType*>(base); |
394 return !base::get<0>(bind_state->bound_args_); | 396 return !base::get<0>(bind_state->bound_args_); |
395 } | 397 } |
396 }; | 398 }; |
397 | 399 |
398 template <typename Signature, typename... BoundArgs> | 400 template <typename Signature, typename... BoundArgs> |
399 struct CancellationChecker<BindState<Callback<Signature>, BoundArgs...>> { | 401 struct CancellationChecker<BindState<Callback<Signature>, BoundArgs...>> { |
402 static constexpr bool is_cancellable = true; | |
400 static bool Run(const BindStateBase* base) { | 403 static bool Run(const BindStateBase* base) { |
401 using Functor = Callback<Signature>; | 404 using Functor = Callback<Signature>; |
402 using BindStateType = BindState<Functor, BoundArgs...>; | 405 using BindStateType = BindState<Functor, BoundArgs...>; |
403 const BindStateType* bind_state = static_cast<const BindStateType*>(base); | 406 const BindStateType* bind_state = static_cast<const BindStateType*>(base); |
404 return bind_state->functor_.IsCancelled(); | 407 return bind_state->functor_.IsCancelled(); |
405 } | 408 } |
406 }; | 409 }; |
407 | 410 |
408 // BindState<> | 411 // BindState<> |
409 // | 412 // |
410 // This stores all the state passed into Bind(). | 413 // This stores all the state passed into Bind(). |
411 template <typename Functor, typename... BoundArgs> | 414 template <typename Functor, typename... BoundArgs> |
412 struct BindState final : BindStateBase { | 415 struct BindState final : BindStateBase { |
416 using IsCancellable = std::integral_constant< | |
417 bool, CancellationChecker<BindState>::is_cancellable>; | |
418 | |
413 template <typename ForwardFunctor, typename... ForwardBoundArgs> | 419 template <typename ForwardFunctor, typename... ForwardBoundArgs> |
414 explicit BindState(BindStateBase::InvokeFuncStorage invoke_func, | 420 explicit BindState(BindStateBase::InvokeFuncStorage invoke_func, |
415 ForwardFunctor&& functor, | 421 ForwardFunctor&& functor, |
416 ForwardBoundArgs&&... bound_args) | 422 ForwardBoundArgs&&... bound_args) |
423 : BindState(IsCancellable(), | |
dcheng
2016/09/09 20:39:01
Is this a C++14ism? How does it work with our tool
dcheng
2016/09/09 20:39:27
Also, see https://github.com/llvm-mirror/libcxx/bl
| |
424 invoke_func, | |
425 std::forward<ForwardFunctor>(functor), | |
426 std::forward<ForwardBoundArgs>(bound_args)...) {} | |
427 | |
428 Functor functor_; | |
429 std::tuple<BoundArgs...> bound_args_; | |
430 | |
431 private: | |
432 template <typename ForwardFunctor, typename... ForwardBoundArgs> | |
433 explicit BindState(std::true_type, | |
434 BindStateBase::InvokeFuncStorage invoke_func, | |
435 ForwardFunctor&& functor, | |
436 ForwardBoundArgs&&... bound_args) | |
417 : BindStateBase(invoke_func, &Destroy, | 437 : BindStateBase(invoke_func, &Destroy, |
418 &CancellationChecker<BindState>::Run), | 438 &CancellationChecker<BindState>::Run), |
419 functor_(std::forward<ForwardFunctor>(functor)), | 439 functor_(std::forward<ForwardFunctor>(functor)), |
420 bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { | 440 bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { |
421 DCHECK(!IsNull(functor_)); | 441 DCHECK(!IsNull(functor_)); |
422 } | 442 } |
423 | 443 |
424 Functor functor_; | 444 template <typename ForwardFunctor, typename... ForwardBoundArgs> |
425 std::tuple<BoundArgs...> bound_args_; | 445 explicit BindState(std::false_type, |
446 BindStateBase::InvokeFuncStorage invoke_func, | |
447 ForwardFunctor&& functor, | |
448 ForwardBoundArgs&&... bound_args) | |
449 : BindStateBase(invoke_func, &Destroy), | |
450 functor_(std::forward<ForwardFunctor>(functor)), | |
451 bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) { | |
452 DCHECK(!IsNull(functor_)); | |
453 } | |
426 | 454 |
427 private: | |
428 ~BindState() {} | 455 ~BindState() {} |
429 | 456 |
430 static void Destroy(BindStateBase* self) { | 457 static void Destroy(BindStateBase* self) { |
431 delete static_cast<BindState*>(self); | 458 delete static_cast<BindState*>(self); |
432 } | 459 } |
433 }; | 460 }; |
434 | 461 |
435 // Used to implement MakeBindStateType. | 462 // Used to implement MakeBindStateType. |
436 template <bool is_method, typename Functor, typename... BoundArgs> | 463 template <bool is_method, typename Functor, typename... BoundArgs> |
437 struct MakeBindStateTypeImpl; | 464 struct MakeBindStateTypeImpl; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
480 | 507 |
481 // Returns a RunType of bound functor. | 508 // Returns a RunType of bound functor. |
482 // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). | 509 // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). |
483 template <typename Functor, typename... BoundArgs> | 510 template <typename Functor, typename... BoundArgs> |
484 using MakeUnboundRunType = | 511 using MakeUnboundRunType = |
485 typename internal::MakeUnboundRunTypeImpl<Functor, BoundArgs...>::Type; | 512 typename internal::MakeUnboundRunTypeImpl<Functor, BoundArgs...>::Type; |
486 | 513 |
487 } // namespace base | 514 } // namespace base |
488 | 515 |
489 #endif // BASE_BIND_INTERNAL_H_ | 516 #endif // BASE_BIND_INTERNAL_H_ |
OLD | NEW |