Bug 1368382. P1 - rename and make MethodThenValue/FunctionValue specializations of ThenValue<>. draft
authorJW Wang <jwwang@mozilla.com>
Wed, 31 May 2017 07:44:53 +0800
changeset 587506 3fc785baf6ba5794f02f8375704767349f7a6638
parent 587432 a8f378825e81daff1279a7d6e940b610912ee6dc
child 587507 daee28588ca865f4141244edc1759657bdf83ea0
push id61734
push userjwwang@mozilla.com
push dateThu, 01 Jun 2017 06:57:58 +0000
bugs1368382
milestone55.0a1
Bug 1368382. P1 - rename and make MethodThenValue/FunctionValue specializations of ThenValue<>. This allows us to remove 2 overloads of MozPromise::Then() using variadic template. MozReview-Commit-ID: 5LHwDhIhh8e
xpcom/threads/MozPromise.h
--- a/xpcom/threads/MozPromise.h
+++ b/xpcom/threads/MozPromise.h
@@ -538,27 +538,37 @@ protected:
                            !TakesArgument<MethodType>::value,
                            already_AddRefed<MozPromise>>::Type
   InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType&& aValue)
   {
     ((*aThisVal).*aMethod)();
     return nullptr;
   }
 
-  template<typename ThisType, typename ResolveMethodType, typename RejectMethodType>
-  class MethodThenValue : public ThenValueBase
+  template<typename...>
+  class ThenValue;
+
+  template<typename ThisType,
+           typename ResolveMethodType,
+           typename RejectMethodType>
+  class ThenValue<ThisType*, ResolveMethodType, RejectMethodType>
+    : public ThenValueBase
   {
   public:
-    MethodThenValue(AbstractThread* aResponseTarget, ThisType* aThisVal,
-                    ResolveMethodType aResolveMethod, RejectMethodType aRejectMethod,
-                    const char* aCallSite)
+    ThenValue(AbstractThread* aResponseTarget,
+              ThisType* aThisVal,
+              ResolveMethodType aResolveMethod,
+              RejectMethodType aRejectMethod,
+              const char* aCallSite)
       : ThenValueBase(aResponseTarget, aCallSite)
       , mThisVal(aThisVal)
       , mResolveMethod(aResolveMethod)
-      , mRejectMethod(aRejectMethod) {}
+      , mRejectMethod(aRejectMethod)
+    {
+    }
 
     void Disconnect() override
     {
       ThenValueBase::Disconnect();
 
       // If a Request has been disconnected, we don't guarantee that the
       // resolve/reject runnable will be dispatched. Null out our refcounted
       // this-value now so that it's released predictably on the dispatch thread.
@@ -587,25 +597,24 @@ protected:
     }
 
   private:
     RefPtr<ThisType> mThisVal; // Only accessed and refcounted on dispatch thread.
     ResolveMethodType mResolveMethod;
     RejectMethodType mRejectMethod;
   };
 
-  // Specialization of MethodThenValue (with 3rd template arg being 'void')
-  // that only takes one method, to be called with a ResolveOrRejectValue.
   template<typename ThisType, typename ResolveRejectMethodType>
-  class MethodThenValue<ThisType, ResolveRejectMethodType, void> : public ThenValueBase
+  class ThenValue<ThisType*, ResolveRejectMethodType> : public ThenValueBase
   {
   public:
-    MethodThenValue(AbstractThread* aResponseTarget, ThisType* aThisVal,
-                    ResolveRejectMethodType aResolveRejectMethod,
-                    const char* aCallSite)
+    ThenValue(AbstractThread* aResponseTarget,
+              ThisType* aThisVal,
+              ResolveRejectMethodType aResolveRejectMethod,
+              const char* aCallSite)
       : ThenValueBase(aResponseTarget, aCallSite)
       , mThisVal(aThisVal)
       , mResolveRejectMethod(aResolveRejectMethod)
     {}
 
     void Disconnect() override
     {
       ThenValueBase::Disconnect();
@@ -633,23 +642,23 @@ protected:
 
   private:
     RefPtr<ThisType> mThisVal; // Only accessed and refcounted on dispatch thread.
     ResolveRejectMethodType mResolveRejectMethod;
   };
 
   // NB: We could use std::function here instead of a template if it were supported. :-(
   template<typename ResolveFunction, typename RejectFunction>
-  class FunctionThenValue : public ThenValueBase
+  class ThenValue<ResolveFunction, RejectFunction> : public ThenValueBase
   {
   public:
-    FunctionThenValue(AbstractThread* aResponseTarget,
-                      ResolveFunction&& aResolveFunction,
-                      RejectFunction&& aRejectFunction,
-                      const char* aCallSite)
+    ThenValue(AbstractThread* aResponseTarget,
+              ResolveFunction&& aResolveFunction,
+              RejectFunction&& aRejectFunction,
+              const char* aCallSite)
       : ThenValueBase(aResponseTarget, aCallSite)
     {
       mResolveFunction.emplace(Move(aResolveFunction));
       mRejectFunction.emplace(Move(aRejectFunction));
     }
 
     void Disconnect() override
     {
@@ -690,25 +699,23 @@ protected:
       return completion.forget();
     }
 
   private:
     Maybe<ResolveFunction> mResolveFunction; // Only accessed and deleted on dispatch thread.
     Maybe<RejectFunction> mRejectFunction; // Only accessed and deleted on dispatch thread.
   };
 
-  // Specialization of FunctionThenValue (with 2nd template arg being 'void')
-  // that only takes one function, to be called with a ResolveOrRejectValue.
   template<typename ResolveRejectFunction>
-  class FunctionThenValue<ResolveRejectFunction, void> : public ThenValueBase
+  class ThenValue<ResolveRejectFunction> : public ThenValueBase
   {
   public:
-    FunctionThenValue(AbstractThread* aResponseTarget,
-                      ResolveRejectFunction&& aResolveRejectFunction,
-                      const char* aCallSite)
+    ThenValue(AbstractThread* aResponseTarget,
+              ResolveRejectFunction&& aResolveRejectFunction,
+              const char* aCallSite)
       : ThenValueBase(aResponseTarget, aCallSite)
     {
       mResolveRejectFunction.emplace(Move(aResolveRejectFunction));
     }
 
     void Disconnect() override
     {
       ThenValueBase::Disconnect();
@@ -890,53 +897,53 @@ private:
   };
 
 public:
   template<typename ThisType, typename ResolveMethodType, typename RejectMethodType>
   typename MethodThenCommand<ResolveMethodType, RejectMethodType>::type
   Then(AbstractThread* aResponseThread, const char* aCallSite,
     ThisType* aThisVal, ResolveMethodType aResolveMethod, RejectMethodType aRejectMethod)
   {
-    using ThenType = MethodThenValue<ThisType, ResolveMethodType, RejectMethodType>;
+    using ThenType = ThenValue<ThisType*, ResolveMethodType, RejectMethodType>;
     RefPtr<ThenValueBase> thenValue = new ThenType(aResponseThread,
        aThisVal, aResolveMethod, aRejectMethod, aCallSite);
     return typename MethodThenCommand<ResolveMethodType, RejectMethodType>::type(
       aResponseThread, aCallSite, thenValue.forget(), this);
   }
 
   template<typename ThisType, typename ResolveRejectMethodType>
   typename MethodThenCommand<ResolveRejectMethodType>::type
   Then(AbstractThread* aResponseThread, const char* aCallSite,
     ThisType* aThisVal, ResolveRejectMethodType aResolveRejectMethod)
   {
-    using ThenType = MethodThenValue<ThisType, ResolveRejectMethodType, void>;
+    using ThenType = ThenValue<ThisType*, ResolveRejectMethodType>;
     RefPtr<ThenValueBase> thenValue = new ThenType(aResponseThread,
        aThisVal, aResolveRejectMethod, aCallSite);
     return typename MethodThenCommand<ResolveRejectMethodType>::type(
       aResponseThread, aCallSite, thenValue.forget(), this);
   }
 
   template<typename ResolveFunction, typename RejectFunction>
   typename FunctionThenCommand<ResolveFunction, RejectFunction>::type
   Then(AbstractThread* aResponseThread, const char* aCallSite,
     ResolveFunction&& aResolveFunction, RejectFunction&& aRejectFunction)
   {
-    using ThenType = FunctionThenValue<ResolveFunction, RejectFunction>;
+    using ThenType = ThenValue<ResolveFunction, RejectFunction>;
     RefPtr<ThenValueBase> thenValue = new ThenType(aResponseThread,
       Move(aResolveFunction), Move(aRejectFunction), aCallSite);
     return typename FunctionThenCommand<ResolveFunction, RejectFunction>::type(
       aResponseThread, aCallSite, thenValue.forget(), this);
   }
 
   template<typename ResolveRejectFunction>
   typename FunctionThenCommand<ResolveRejectFunction>::type
   Then(AbstractThread* aResponseThread, const char* aCallSite,
                    ResolveRejectFunction&& aResolveRejectFunction)
   {
-    using ThenType = FunctionThenValue<ResolveRejectFunction, void>;
+    using ThenType = ThenValue<ResolveRejectFunction>;
     RefPtr<ThenValueBase> thenValue = new ThenType(aResponseThread,
       Move(aResolveRejectFunction), aCallSite);
     return typename FunctionThenCommand<ResolveRejectFunction>::type(
       aResponseThread, aCallSite, thenValue.forget(), this);
   }
 
   void ChainTo(already_AddRefed<Private> aChainedPromise, const char* aCallSite)
   {