From 5f7ed91c80d2f126e822e3bc35e35c16186545c4 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Thu, 28 May 2015 22:57:57 -0700 Subject: [PATCH] Bug 1169542 - Make MediaPromise more rvalue-friendly. r=gerald,r=jww Again, necessary for passing around nsTArrays. --- dom/media/MediaPromise.h | 58 +++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/dom/media/MediaPromise.h b/dom/media/MediaPromise.h index 835f4fc547ff..b19534bf067e 100644 --- a/dom/media/MediaPromise.h +++ b/dom/media/MediaPromise.h @@ -125,35 +125,40 @@ public: class ResolveOrRejectValue { public: - void SetResolve(const ResolveValueType& aResolveValue) + template + void SetResolve(ResolveValueType_&& aResolveValue) { MOZ_ASSERT(IsNothing()); - mResolveValue.emplace(aResolveValue); + mResolveValue.emplace(Forward(aResolveValue)); } - void SetReject(const RejectValueType& aRejectValue) + template + void SetReject(RejectValueType_&& aRejectValue) { MOZ_ASSERT(IsNothing()); - mRejectValue.emplace(aRejectValue); + mRejectValue.emplace(Forward(aRejectValue)); } - static ResolveOrRejectValue MakeResolve(const ResolveValueType aResolveValue) + template + static ResolveOrRejectValue MakeResolve(ResolveValueType_&& aResolveValue) { ResolveOrRejectValue val; - val.SetResolve(aResolveValue); + val.SetResolve(Forward(aResolveValue)); return val; } - static ResolveOrRejectValue MakeReject(const RejectValueType aRejectValue) + template + static ResolveOrRejectValue MakeReject(RejectValueType_&& aRejectValue) { ResolveOrRejectValue val; - val.SetReject(aRejectValue); + val.SetReject(Forward(aRejectValue)); return val; } bool IsResolve() const { return mResolveValue.isSome(); } bool IsReject() const { return mRejectValue.isSome(); } bool IsNothing() const { return mResolveValue.isNothing() && mRejectValue.isNothing(); } + const ResolveValueType& ResolveValue() const { return mResolveValue.ref(); } const RejectValueType& RejectValue() const { return mRejectValue.ref(); } @@ -183,19 +188,21 @@ public: // NB: We can include the definition of this class inline once B2G ICS is gone. class Private; + template static nsRefPtr - CreateAndResolve(ResolveValueType aResolveValue, const char* aResolveSite) + CreateAndResolve(ResolveValueType_&& aResolveValue, const char* aResolveSite) { nsRefPtr p = new MediaPromise::Private(aResolveSite); - p->Resolve(aResolveValue, aResolveSite); + p->Resolve(Forward(aResolveValue), aResolveSite); return Move(p); } + template static nsRefPtr - CreateAndReject(RejectValueType aRejectValue, const char* aRejectSite) + CreateAndReject(RejectValueType_&& aRejectValue, const char* aRejectSite) { nsRefPtr p = new MediaPromise::Private(aRejectSite); - p->Reject(aRejectValue, aRejectSite); + p->Reject(Forward(aRejectValue), aRejectSite); return Move(p); } @@ -353,18 +360,18 @@ protected: static typename EnableIf>::value && TakesArgument::value, already_AddRefed>::Type - InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType aValue) + InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType&& aValue) { - return ((*aThisVal).*aMethod)(aValue).forget(); + return ((*aThisVal).*aMethod)(Forward(aValue)).forget(); } template static typename EnableIf::value && TakesArgument::value, already_AddRefed>::Type - InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType aValue) + InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType&& aValue) { - ((*aThisVal).*aMethod)(aValue); + ((*aThisVal).*aMethod)(Forward(aValue)); return nullptr; } @@ -372,7 +379,7 @@ protected: static typename EnableIf>::value && !TakesArgument::value, already_AddRefed>::Type - InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType aValue) + InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType&& aValue) { return ((*aThisVal).*aMethod)().forget(); } @@ -381,7 +388,7 @@ protected: static typename EnableIf::value && !TakesArgument::value, already_AddRefed>::Type - InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType aValue) + InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType&& aValue) { ((*aThisVal).*aMethod)(); return nullptr; @@ -602,30 +609,33 @@ class MediaPromise::Private public: explicit Private(const char* aCreationSite) : MediaPromise(aCreationSite) {} - void Resolve(ResolveValueT aResolveValue, const char* aResolveSite) + template + void Resolve(ResolveValueT_&& aResolveValue, const char* aResolveSite) { MutexAutoLock lock(mMutex); MOZ_ASSERT(IsPending()); PROMISE_LOG("%s resolving MediaPromise (%p created at %s)", aResolveSite, this, mCreationSite); - mValue.SetResolve(aResolveValue); + mValue.SetResolve(Forward(aResolveValue)); DispatchAll(); } - void Reject(RejectValueT aRejectValue, const char* aRejectSite) + template + void Reject(RejectValueT_&& aRejectValue, const char* aRejectSite) { MutexAutoLock lock(mMutex); MOZ_ASSERT(IsPending()); PROMISE_LOG("%s rejecting MediaPromise (%p created at %s)", aRejectSite, this, mCreationSite); - mValue.SetReject(aRejectValue); + mValue.SetReject(Forward(aRejectValue)); DispatchAll(); } - void ResolveOrReject(ResolveOrRejectValue aValue, const char* aSite) + template + void ResolveOrReject(ResolveOrRejectValue_&& aValue, const char* aSite) { MutexAutoLock lock(mMutex); MOZ_ASSERT(IsPending()); PROMISE_LOG("%s resolveOrRejecting MediaPromise (%p created at %s)", aSite, this, mCreationSite); - mValue = aValue; + mValue = Forward(aValue); DispatchAll(); } };