Bug 1271593 - NS_NewRunnableFunction should forward its arg - r?froydnj
By perfect-forwarding its argument, we can automatically gain move semantics
optimization when storing the given function object into nsRunnableFunction.
MozReview-Commit-ID: 9EZK84ZhMvR
--- a/xpcom/glue/nsThreadUtils.h
+++ b/xpcom/glue/nsThreadUtils.h
@@ -15,16 +15,17 @@
#include "nsIRunnable.h"
#include "nsICancelableRunnable.h"
#include "nsStringGlue.h"
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "mozilla/Atomics.h"
#include "mozilla/IndexSequence.h"
#include "mozilla/Likely.h"
+#include "mozilla/Move.h"
#include "mozilla/Tuple.h"
#include "mozilla/TypeTraits.h"
//-----------------------------------------------------------------------------
// These methods are alternatives to the methods on nsIThreadManager, provided
// for convenience.
/**
@@ -253,34 +254,35 @@ protected:
// An event that can be used to call a C++11 functions or function objects,
// including lambdas. The function must have no required arguments, and must
// return void.
template<typename Function>
class nsRunnableFunction : public mozilla::Runnable
{
public:
- explicit nsRunnableFunction(const Function& aFunction)
- : mFunction(aFunction)
+ template <typename F>
+ explicit nsRunnableFunction(F&& aFunction)
+ : mFunction(mozilla::Forward<F>(aFunction))
{ }
NS_IMETHOD Run() {
static_assert(mozilla::IsVoid<decltype(mFunction())>::value,
"The lambda must return void!");
mFunction();
return NS_OK;
}
private:
Function mFunction;
};
template<typename Function>
-nsRunnableFunction<Function>* NS_NewRunnableFunction(const Function& aFunction)
+nsRunnableFunction<Function>* NS_NewRunnableFunction(Function&& aFunction)
{
- return new nsRunnableFunction<Function>(aFunction);
+ return new nsRunnableFunction<Function>(mozilla::Forward<Function>(aFunction));
}
// An event that can be used to call a method on a class. The class type must
// support reference counting. This event supports Revoke for use
// with nsRevocableEventPtr.
template<class ClassType,
typename ReturnType = void,
bool Owning = true,