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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/bind_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/bind_internal.h
diff --git a/base/bind_internal.h b/base/bind_internal.h
index 5845a4d031de9c3df658820a4b2f4cc3e545fe61..1c3aa38feea7536e5c9d3fe8fdb50cd62313d39d 100644
--- a/base/bind_internal.h
+++ b/base/bind_internal.h
@@ -422,6 +422,19 @@ struct CancellationChecker<BindState<Callback<Signature>, BoundArgs...>> {
}
};
+// Template helpers to detect using Bind() on a base::Callback without any
+// additional arguments. In that case, the original base::Callback object should
+// just be directly used.
+template <typename Functor, typename... BoundArgs>
+struct BindingCallbackWithNoArgs {
+ static constexpr bool value = false;
+};
+
+template <typename Signature, typename... BoundArgs>
+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
+ static constexpr bool value = sizeof...(BoundArgs) == 0;
+};
+
// BindState<>
//
// This stores all the state passed into Bind().
@@ -439,7 +452,12 @@ struct BindState final : BindStateBase {
: BindState(IsCancellable{},
invoke_func,
std::forward<ForwardFunctor>(functor),
- std::forward<ForwardBoundArgs>(bound_args)...) {}
+ std::forward<ForwardBoundArgs>(bound_args)...) {
+ static_assert(!BindingCallbackWithNoArgs<Functor, BoundArgs...>::value,
+ "Attempting to bind a base::Callback with no additional "
+ "arguments: save a heap allocation and use the original "
+ "base::Callback");
+ }
Functor functor_;
std::tuple<BoundArgs...> bound_args_;
« 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