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.
Also it allows movable-only function objects.

Note that any reference is removed from the type to be stored, so that the
runnable always contain a concrete function object.

MozReview-Commit-ID: 9EZK84ZhMvR

--HG--
extra : rebase_source : e1f87c3284fda4df6d13839ea6a0b0c2ce196833
This commit is contained in:
Gerald Squelart 2016-06-02 01:21:36 +02:00
parent 42ba4360c9
commit e8513ef5ee

View File

@ -20,6 +20,7 @@
#include "mozilla/Atomics.h"
#include "mozilla/IndexSequence.h"
#include "mozilla/Likely.h"
#include "mozilla/Move.h"
#include "mozilla/Tuple.h"
#include "mozilla/TypeTraits.h"
@ -262,12 +263,13 @@ private:
// 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>
template<typename StoredFunction>
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() {
@ -277,13 +279,18 @@ public:
return NS_OK;
}
private:
Function mFunction;
StoredFunction mFunction;
};
template<typename Function>
nsRunnableFunction<Function>* NS_NewRunnableFunction(const Function& aFunction)
nsRunnableFunction<typename mozilla::RemoveReference<Function>::Type>*
NS_NewRunnableFunction(Function&& aFunction)
{
return new nsRunnableFunction<Function>(aFunction);
return new nsRunnableFunction
// Make sure we store a non-reference in nsRunnableFunction.
<typename mozilla::RemoveReference<Function>::Type>
// But still forward aFunction to move if possible.
(mozilla::Forward<Function>(aFunction));
}
// An event that can be used to call a method on a class. The class type must