Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: base/bind_internal.h

Issue 2352853002: Disallow redundant Bind calls. (Closed)
Patch Set: Another BindToLoop Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/bind_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 struct CancellationChecker<BindState<Callback<Signature>, BoundArgs...>> { 415 struct CancellationChecker<BindState<Callback<Signature>, BoundArgs...>> {
416 static constexpr bool is_cancellable = true; 416 static constexpr bool is_cancellable = true;
417 static bool Run(const BindStateBase* base) { 417 static bool Run(const BindStateBase* base) {
418 using Functor = Callback<Signature>; 418 using Functor = Callback<Signature>;
419 using BindStateType = BindState<Functor, BoundArgs...>; 419 using BindStateType = BindState<Functor, BoundArgs...>;
420 const BindStateType* bind_state = static_cast<const BindStateType*>(base); 420 const BindStateType* bind_state = static_cast<const BindStateType*>(base);
421 return bind_state->functor_.IsCancelled(); 421 return bind_state->functor_.IsCancelled();
422 } 422 }
423 }; 423 };
424 424
425 // Template helpers to detect using Bind() on a base::Callback without any
426 // additional arguments. In that case, the original base::Callback object should
427 // just be directly used.
428 template <typename Functor, typename... BoundArgs>
429 struct BindingCallbackWithNoArgs {
430 static constexpr bool value = false;
431 };
432
433 template <typename Signature, typename... BoundArgs>
434 struct BindingCallbackWithNoArgs<Callback<Signature>, BoundArgs...> {
tzik 2016/09/20 07:33:04 Could you add a specialization for OnceCallback to
dcheng 2016/09/20 08:22:39 How come things like CancellationChecker don't cur
dcheng 2016/09/24 00:49:17 Done (though I did this by templatizing on the add
435 static constexpr bool value = sizeof...(BoundArgs) == 0;
436 };
437
425 // BindState<> 438 // BindState<>
426 // 439 //
427 // This stores all the state passed into Bind(). 440 // This stores all the state passed into Bind().
428 template <typename Functor, typename... BoundArgs> 441 template <typename Functor, typename... BoundArgs>
429 struct BindState final : BindStateBase { 442 struct BindState final : BindStateBase {
430 using IsCancellable = std::integral_constant< 443 using IsCancellable = std::integral_constant<
431 bool, CancellationChecker<BindState>::is_cancellable>; 444 bool, CancellationChecker<BindState>::is_cancellable>;
432 445
433 template <typename ForwardFunctor, typename... ForwardBoundArgs> 446 template <typename ForwardFunctor, typename... ForwardBoundArgs>
434 explicit BindState(BindStateBase::InvokeFuncStorage invoke_func, 447 explicit BindState(BindStateBase::InvokeFuncStorage invoke_func,
435 ForwardFunctor&& functor, 448 ForwardFunctor&& functor,
436 ForwardBoundArgs&&... bound_args) 449 ForwardBoundArgs&&... bound_args)
437 // IsCancellable is std::false_type if the CancellationChecker<>::Run 450 // IsCancellable is std::false_type if the CancellationChecker<>::Run
438 // returns always false. Otherwise, it's std::true_type. 451 // returns always false. Otherwise, it's std::true_type.
439 : BindState(IsCancellable{}, 452 : BindState(IsCancellable{},
440 invoke_func, 453 invoke_func,
441 std::forward<ForwardFunctor>(functor), 454 std::forward<ForwardFunctor>(functor),
442 std::forward<ForwardBoundArgs>(bound_args)...) {} 455 std::forward<ForwardBoundArgs>(bound_args)...) {
456 static_assert(!BindingCallbackWithNoArgs<Functor, BoundArgs...>::value,
457 "Attempting to bind a base::Callback with no additional "
458 "arguments: save a heap allocation and use the original "
459 "base::Callback");
460 }
443 461
444 Functor functor_; 462 Functor functor_;
445 std::tuple<BoundArgs...> bound_args_; 463 std::tuple<BoundArgs...> bound_args_;
446 464
447 private: 465 private:
448 template <typename ForwardFunctor, typename... ForwardBoundArgs> 466 template <typename ForwardFunctor, typename... ForwardBoundArgs>
449 explicit BindState(std::true_type, 467 explicit BindState(std::true_type,
450 BindStateBase::InvokeFuncStorage invoke_func, 468 BindStateBase::InvokeFuncStorage invoke_func,
451 ForwardFunctor&& functor, 469 ForwardFunctor&& functor,
452 ForwardBoundArgs&&... bound_args) 470 ForwardBoundArgs&&... bound_args)
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 541
524 // Returns a RunType of bound functor. 542 // Returns a RunType of bound functor.
525 // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C). 543 // E.g. MakeUnboundRunType<R(A, B, C), A, B> is evaluated to R(C).
526 template <typename Functor, typename... BoundArgs> 544 template <typename Functor, typename... BoundArgs>
527 using MakeUnboundRunType = 545 using MakeUnboundRunType =
528 typename internal::MakeUnboundRunTypeImpl<Functor, BoundArgs...>::Type; 546 typename internal::MakeUnboundRunTypeImpl<Functor, BoundArgs...>::Type;
529 547
530 } // namespace base 548 } // namespace base
531 549
532 #endif // BASE_BIND_INTERNAL_H_ 550 #endif // BASE_BIND_INTERNAL_H_
OLDNEW
« no previous file with comments | « no previous file | base/bind_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698