Bug 1287623, part 3 - Add more methods involving StaticRefPtr. r=froydnj
☠☠ backed out by d2c17679b215 ☠ ☠
authorAndrew McCreight <continuation@gmail.com>
Wed, 20 Jul 2016 15:19:00 -0700
changeset 345973 8aa409c9b1ce3ebd68d5e6f970da64cf91d9d011
parent 345972 602d4d88e80608ad9e5af42ef4d7c7b3ffa99cf9
child 345974 248153344e15de4cb10a5d5829a24059a861fe98
push id6389
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:38:22 +0000
treeherdermozilla-beta@01d67bfe6c81 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1287623
milestone50.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 1287623, part 3 - Add more methods involving StaticRefPtr. r=froydnj This patch adds a number of standard conversions to and from RefPtr<> and already_AddRefed<>.
mfbt/RefPtr.h
xpcom/base/StaticPtr.h
--- a/mfbt/RefPtr.h
+++ b/mfbt/RefPtr.h
@@ -14,16 +14,17 @@
 /*****************************************************************************/
 
 // template <class T> class RefPtrGetterAddRefs;
 
 class nsCOMPtr_helper;
 
 namespace mozilla {
 template<class T> class OwningNonNull;
+template<class T> class StaticRefPtr;
 
 // Traditionally, RefPtr supports automatic refcounting of any pointer type
 // with AddRef() and Release() methods that follow the traditional semantics.
 //
 // This traits class can be specialized to operate on other pointer types. For
 // example, we specialize this trait for opaque FFI types that represent
 // refcounted objects in Rust.
 //
@@ -143,16 +144,20 @@ public:
   }
 
   MOZ_IMPLICIT RefPtr(const nsCOMPtr_helper& aHelper);
 
   // Defined in OwningNonNull.h
   template<class U>
   MOZ_IMPLICIT RefPtr(const mozilla::OwningNonNull<U>& aOther);
 
+  // Defined in StaticPtr.h
+  template<class U>
+  MOZ_IMPLICIT RefPtr(const mozilla::StaticRefPtr<U>& aOther);
+
   // Assignment operators
 
   RefPtr<T>&
   operator=(const RefPtr<T>& aRhs)
   // copy assignment operator
   {
     assign_with_AddRef(aRhs.mRawPtr);
     return *this;
@@ -203,16 +208,21 @@ public:
     return *this;
   }
 
   // Defined in OwningNonNull.h
   template<class U>
   RefPtr<T>&
   operator=(const mozilla::OwningNonNull<U>& aOther);
 
+  // Defined in StaticPtr.h
+  template<class U>
+  RefPtr<T>&
+  operator=(const mozilla::StaticRefPtr<U>& aOther);
+
   // Other pointer operators
 
   void
   swap(RefPtr<T>& aRhs)
   // ...exchange ownership with |aRhs|; can save a pair of refcount operations
   {
     T* temp = aRhs.mRawPtr;
     aRhs.mRawPtr = mRawPtr;
--- a/xpcom/base/StaticPtr.h
+++ b/xpcom/base/StaticPtr.h
@@ -2,18 +2,20 @@
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_StaticPtr_h
 #define mozilla_StaticPtr_h
 
+#include "mozilla/AlreadyAddRefed.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/Attributes.h"
+#include "mozilla/RefPtr.h"
 
 namespace mozilla {
 
 /**
  * StaticAutoPtr and StaticRefPtr are like nsAutoPtr and nsRefPtr, except they
  * are suitable for use as global variables.
  *
  * In particular, a global instance of Static{Auto,Ref}Ptr doesn't cause the
@@ -105,16 +107,36 @@ public:
     return *this;
   }
 
   StaticRefPtr<T>& operator=(const StaticRefPtr<T>& aRhs)
   {
     return (this = aRhs.mRawPtr);
   }
 
+  StaticRefPtr<T>& operator=(already_AddRefed<T>& aRhs)
+  {
+    AssignAssumingAddRef(aRhs.take());
+    return *this;
+  }
+
+  StaticRefPtr<T>& operator=(already_AddRefed<T>&& aRhs)
+  {
+    AssignAssumingAddRef(aRhs.take());
+    return *this;
+  }
+
+  already_AddRefed<T>
+  forget()
+  {
+    T* temp = mRawPtr;
+    mRawPtr = nullptr;
+    return already_AddRefed<T>(temp);
+  }
+
   T* get() const { return mRawPtr; }
 
   operator T*() const { return get(); }
 
   T* operator->() const
   {
     MOZ_ASSERT(mRawPtr);
     return get();
@@ -227,9 +249,22 @@ REFLEXIVE_EQUALITY_OPERATORS(const Stati
 // Let us compare StaticRefPtr to 0.
 REFLEXIVE_EQUALITY_OPERATORS(const StaticRefPtr<T>&, StaticPtr_internal::Zero*,
                              lhs.get() == nullptr, class T)
 
 #undef REFLEXIVE_EQUALITY_OPERATORS
 
 } // namespace mozilla
 
+// Declared in mozilla/RefPtr.h
+template<class T> template<class U>
+RefPtr<T>::RefPtr(const mozilla::StaticRefPtr<U>& aOther)
+  : RefPtr(aOther.get())
+{}
+
+template<class T> template<class U>
+RefPtr<T>&
+RefPtr<T>::operator=(const mozilla::StaticRefPtr<U>& aOther)
+{
+  return operator=(aOther.get());
+}
+
 #endif