Bug 1367679. P1 - refactor InvokeCallbackMethod() to deal with one concern at a time. draft
authorJW Wang <jwwang@mozilla.com>
Fri, 26 May 2017 00:12:29 +0800
changeset 588053 cbe886b7c1bcdc258f6610c13e6532f17b09d419
parent 588052 aeb3d0ca558f034cbef1c5a68bd07dd738611494
child 588054 ff817a8034e47a6f9ebd619991877a1e93cd5a13
child 588113 cde4ba9d0dd75c0e0cf924b29b643cbbb6ffe104
push id61889
push userjwwang@mozilla.com
push dateFri, 02 Jun 2017 02:39:38 +0000
bugs1367679
milestone55.0a1
Bug 1367679. P1 - refactor InvokeCallbackMethod() to deal with one concern at a time. InvokeMethod() handles optional arguments. InvokeCallbackMethod() handles optional return value. MozReview-Commit-ID: AyT6TEKRqbs
xpcom/threads/MozPromise.h
--- a/xpcom/threads/MozPromise.h
+++ b/xpcom/threads/MozPromise.h
@@ -498,52 +498,52 @@ protected:
     uint32_t mMagic2 = sMagic;
 #endif
   };
 
   /*
    * We create two overloads for invoking Resolve/Reject Methods so as to
    * make the resolve/reject value argument "optional".
    */
-
   template<typename ThisType, typename MethodType, typename ValueType>
-  static typename EnableIf<ReturnTypeIs<MethodType, RefPtr<MozPromise>>::value &&
-                           TakesArgument<MethodType>::value,
-                           already_AddRefed<MozPromise>>::Type
-  InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType&& aValue)
+  static typename EnableIf<
+    TakesArgument<MethodType>::value,
+    typename detail::MethodTrait<MethodType>::ReturnType>::Type
+  InvokeMethod(ThisType* aThisVal, MethodType aMethod, ValueType&& aValue)
   {
-    return ((*aThisVal).*aMethod)(Forward<ValueType>(aValue)).forget();
+    return (aThisVal->*aMethod)(Forward<ValueType>(aValue));
   }
 
   template<typename ThisType, typename MethodType, typename ValueType>
-  static typename EnableIf<ReturnTypeIs<MethodType, void>::value &&
-                           TakesArgument<MethodType>::value,
-                           already_AddRefed<MozPromise>>::Type
-  InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType&& aValue)
+  static typename EnableIf<
+    !TakesArgument<MethodType>::value,
+    typename detail::MethodTrait<MethodType>::ReturnType>::Type
+  InvokeMethod(ThisType* aThisVal, MethodType aMethod, ValueType&& aValue)
   {
-    ((*aThisVal).*aMethod)(Forward<ValueType>(aValue));
-    return nullptr;
+    return (aThisVal->*aMethod)();
   }
 
   template<typename ThisType, typename MethodType, typename ValueType>
-  static typename EnableIf<ReturnTypeIs<MethodType, RefPtr<MozPromise>>::value &&
-                           !TakesArgument<MethodType>::value,
+  static typename EnableIf<ReturnTypeIs<MethodType, RefPtr<MozPromise>>::value,
                            already_AddRefed<MozPromise>>::Type
-  InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType&& aValue)
+  InvokeCallbackMethod(ThisType* aThisVal,
+                       MethodType aMethod,
+                       ValueType&& aValue)
   {
-    return ((*aThisVal).*aMethod)().forget();
+    return InvokeMethod(aThisVal, aMethod, Forward<ValueType>(aValue)).forget();
   }
 
   template<typename ThisType, typename MethodType, typename ValueType>
-  static typename EnableIf<ReturnTypeIs<MethodType, void>::value &&
-                           !TakesArgument<MethodType>::value,
+  static typename EnableIf<ReturnTypeIs<MethodType, void>::value,
                            already_AddRefed<MozPromise>>::Type
-  InvokeCallbackMethod(ThisType* aThisVal, MethodType aMethod, ValueType&& aValue)
+  InvokeCallbackMethod(ThisType* aThisVal,
+                       MethodType aMethod,
+                       ValueType&& aValue)
   {
-    ((*aThisVal).*aMethod)();
+    InvokeMethod(aThisVal, aMethod, Forward<ValueType>(aValue));
     return nullptr;
   }
 
   template<typename>
   class ThenCommand;
 
   template<typename...>
   class ThenValue;