Bug 1119080: Convert the already_Addrefed Derived->Base conversion operator into a move constructor, r=waldo
authorBrian Smith <brian@briansmith.org>
Wed, 07 Jan 2015 20:32:55 -0800
changeset 223914 9f889f63ea6c1a442b4a31ead6fa5d97b2fecc10
parent 223913 081ec95c09338f945de1b1ce307bd6d0b6019fbb
child 223915 4f852d38c615375bb40d5121f77b64b956441ce4
push id28112
push usercbook@mozilla.com
push dateThu, 15 Jan 2015 13:19:02 +0000
treeherdermozilla-central@206bf1a98cd7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswaldo
bugs1119080
milestone38.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 1119080: Convert the already_Addrefed Derived->Base conversion operator into a move constructor, r=waldo
mfbt/AlreadyAddRefed.h
--- a/mfbt/AlreadyAddRefed.h
+++ b/mfbt/AlreadyAddRefed.h
@@ -66,16 +66,36 @@ struct already_AddRefed
 
   explicit already_AddRefed(T* aRawPtr) : mRawPtr(aRawPtr) {}
 
   // Disallowed. Use move semantics instead.
   already_AddRefed(const already_AddRefed<T>& aOther) = delete;
 
   already_AddRefed(already_AddRefed<T>&& aOther) : mRawPtr(aOther.take()) {}
 
+  /**
+   * This helper is useful in cases like
+   *
+   *  already_AddRefed<BaseClass>
+   *  Foo()
+   *  {
+   *    nsRefPtr<SubClass> x = ...;
+   *    return x.forget();
+   *  }
+   *
+   * The autoconversion allows one to omit the idiom
+   *
+   *    nsRefPtr<BaseClass> y = x.forget();
+   *    return y.forget();
+   *
+   * Note that nsRefPtr is the XPCOM reference counting smart pointer class.
+   */
+  template <typename U>
+  already_AddRefed(already_AddRefed<U>&& aOther) : mRawPtr(aOther.take()) {}
+
   ~already_AddRefed() { MOZ_ASSERT(!mRawPtr); }
 
   // Specialize the unused operator<< for already_AddRefed, to allow
   // nsCOMPtr<nsIFoo> foo;
   // unused << foo.forget();
   // Note that nsCOMPtr is the XPCOM reference counting smart pointer class.
   friend void operator<<(const mozilla::unused_t& aUnused,
                          const already_AddRefed<T>& aRhs)
@@ -87,41 +107,16 @@ struct already_AddRefed
   MOZ_WARN_UNUSED_RESULT T* take()
   {
     T* rawPtr = mRawPtr;
     mRawPtr = nullptr;
     return rawPtr;
   }
 
   /**
-   * This helper is useful in cases like
-   *
-   *  already_AddRefed<BaseClass>
-   *  Foo()
-   *  {
-   *    nsRefPtr<SubClass> x = ...;
-   *    return x.forget();
-   *  }
-   *
-   * The autoconversion allows one to omit the idiom
-   *
-   *    nsRefPtr<BaseClass> y = x.forget();
-   *    return y.forget();
-   *
-   * Note that nsRefPtr is the XPCOM reference counting smart pointer class.
-   */
-  template<class U>
-  operator already_AddRefed<U>()
-  {
-    U* tmp = mRawPtr;
-    mRawPtr = nullptr;
-    return already_AddRefed<U>(tmp);
-  }
-
-  /**
    * This helper provides a static_cast replacement for already_AddRefed, so
    * if you have
    *
    *   already_AddRefed<Parent> F();
    *
    * you can write
    *
    *   already_AddRefed<Child>