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

Unified Diff: third_party/WebKit/Source/wtf/Functional.h

Issue 1760133002: Reland of WTF::bind: Handle movable objects in unbound arguments. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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 | third_party/WebKit/Source/wtf/FunctionalTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/wtf/Functional.h
diff --git a/third_party/WebKit/Source/wtf/Functional.h b/third_party/WebKit/Source/wtf/Functional.h
index d254fd900030641338e13a91a556b3ce19dca36a..d274422347d94e6a684054784cea0e864812c219 100644
--- a/third_party/WebKit/Source/wtf/Functional.h
+++ b/third_party/WebKit/Source/wtf/Functional.h
@@ -45,10 +45,25 @@
// Use threadSafeBind() or createCrossThreadTask() if the function/task is
// called on a (potentially) different thread from the current thread.
-// Bind and rvalue references:
-//
-// For unbound parameters (arguments supplied later on the bound functor), we don't support moving-in and moving-out
-// at this moment, but we are willing to support that soon.
+// WTF::bind() and move semantics
+// ==============================
+//
+// For unbound parameters (arguments supplied later on the bound functor directly), there are two ways to pass movable
+// arguments:
+//
+// 1) Pass by rvalue reference.
+//
+// void yourFunction(Argument&& argument) { ... }
+// OwnPtr<Function<void(Argument&&)>> functor = bind<Argument&&>(yourFunction);
+//
+// 2) Pass by value.
+//
+// void yourFunction(Argument argument) { ... }
+// OwnPtr<Function<void(Argument)>> functor = bind<Argument>(yourFunction);
+//
+// Note that with the latter there will be *two* move constructions happening, because there needs to be at least one
+// intermediary function call taking an argument of type "Argument" (i.e. passed by value). The former case does not
+// require any move constructions inbetween.
//
// For bound parameters (arguments supplied on the creation of a functor), you can move your argument into the internal
// storage of the functor by supplying an rvalue to that argument (this is done in wrap() of ParamStorageTraits).
@@ -214,15 +229,15 @@
{
// What we really want to do is to call m_functionWrapper(m_bound..., unbound...), but to do that we need to
// pass a list of indices to a worker function template.
- return callInternal(unbound..., base::MakeIndexSequence<sizeof...(BoundParameters)>());
+ return callInternal(base::MakeIndexSequence<sizeof...(BoundParameters)>(), std::forward<UnboundParameters>(unbound)...);
}
private:
- template <std::size_t... boundIndices>
- typename FunctionWrapper::ResultType callInternal(UnboundParameters... unbound, const base::IndexSequence<boundIndices...>&)
+ template <std::size_t... boundIndices, typename... IncomingUnboundParameters>
+ typename FunctionWrapper::ResultType callInternal(const base::IndexSequence<boundIndices...>&, IncomingUnboundParameters&&... unbound)
{
// Get each element in m_bound, unwrap them, and call the function with the desired arguments.
- return m_functionWrapper(ParamStorageTraits<typename std::decay<BoundParameters>::type>::unwrap(std::get<boundIndices>(m_bound))..., unbound...);
+ return m_functionWrapper(ParamStorageTraits<typename std::decay<BoundParameters>::type>::unwrap(std::get<boundIndices>(m_bound))..., std::forward<IncomingUnboundParameters>(unbound)...);
}
FunctionWrapper m_functionWrapper;
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/FunctionalTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698