Bug 1116905 - part 2 - add MakeAndAddRef helper function to facilitate constructing TemporaryRef; r=Ms2ger
authorNathan Froyd <froydnj@mozilla.com>
Thu, 30 Apr 2015 15:19:49 -0400
changeset 266209 43684b88e52e138611f58dfb632305681aa564f9
parent 266208 27c4c70a437e92c0d4f1a18ecbfa4081981abdc8
child 266210 f1f569c211024cf1855f5fbedb17e8648a915ae5
push id2231
push usermichael.l.comella@gmail.com
push dateFri, 22 May 2015 20:04:59 +0000
reviewersMs2ger
bugs1116905
milestone41.0a1
Bug 1116905 - part 2 - add MakeAndAddRef helper function to facilitate constructing TemporaryRef; r=Ms2ger With implicit conversion to TemporaryRef going away, one can no longer write: return new T(...); in a function returning TemporaryRef<T>. Instead, provide MakeAndAddRef to prevent people from having to construct boilerplate RefPtrs or similar. It also makes converting from TemporaryRef to already_AddRefed somewhat easier.
mfbt/RefPtr.h
--- a/mfbt/RefPtr.h
+++ b/mfbt/RefPtr.h
@@ -394,11 +394,33 @@ private:
  */
 template<typename T>
 OutParamRef<T>
 byRef(RefPtr<T>& aPtr)
 {
   return OutParamRef<T>(aPtr);
 }
 
+/**
+ * Helper function to be able to conveniently write things like:
+ *
+ *   TemporaryRef<T>
+ *   f(...)
+ *   {
+ *     return MakeAndAddRef<T>(...);
+ *   }
+ *
+ * since explicitly constructing TemporaryRef is unsightly.  Having an
+ * explicit construction of TemporaryRef from T* also inhibits a future
+ * auto-conversion from TemporaryRef to already_AddRefed, since the semantics
+ * of TemporaryRef(T*) differ from already_AddRefed(T*).
+ */
+template<typename T, typename... Args>
+TemporaryRef<T>
+MakeAndAddRef(Args&&... aArgs)
+{
+  RefPtr<T> p(new T(Forward<Args>(aArgs)...));
+  return p.forget();
+}
+
 } // namespace mozilla
 
 #endif /* mozilla_RefPtr_h */