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 277470 43684b88e52e138611f58dfb632305681aa564f9
parent 277469 27c4c70a437e92c0d4f1a18ecbfa4081981abdc8
child 277471 f1f569c211024cf1855f5fbedb17e8648a915ae5
push id897
push userjlund@mozilla.com
push dateMon, 14 Sep 2015 18:56:12 +0000
treeherdermozilla-release@9411e2d2b214 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMs2ger
bugs1116905
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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 */