Bug 1287623, part 3 - Add more methods involving StaticRefPtr. r=froydnj
authorAndrew McCreight <continuation@gmail.com>
Thu, 21 Jul 2016 09:54:17 -0700
changeset 331249 aafe24d898d25a20b838a459456d0a1d13e79f9d
parent 331248 cef4132ee60b6e498751c8fabcb153535b5c4cec
child 331250 c3676ef8ac272183a1f2e551f1966700a0790f3b
push id9858
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 14:37:10 +0000
treeherdermozilla-aurora@203106ef6cb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1287623
milestone50.0a1
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