Backed out changeset 5cd8db347c49 (bug 1251559) for tree bustage on a CLOSED TREE
authorCarsten "Tomcat" Book <cbook@mozilla.com>
Fri, 18 Mar 2016 08:03:17 +0100
changeset 342035 20a5b1e769229b9221bccb0735f815a78f409c5f
parent 342034 52029291217539501fced30b968fe639d8d0c011
child 342036 cc6eca49bf0f03986a29c3c6636b6f654ae01a39
push id13342
push userbmo:james@hoppipolla.co.uk
push dateFri, 18 Mar 2016 09:55:58 +0000
bugs1251559
milestone48.0a1
backs out5cd8db347c4970924362eb156c00d19950d99acb
Backed out changeset 5cd8db347c49 (bug 1251559) for tree bustage on a CLOSED TREE
mfbt/WeakPtr.h
--- a/mfbt/WeakPtr.h
+++ b/mfbt/WeakPtr.h
@@ -92,24 +92,16 @@ namespace detail {
 
 // This can live beyond the lifetime of the class derived from
 // SupportsWeakPtr.
 template<class T>
 class WeakReference : public ::mozilla::RefCounted<WeakReference<T> >
 {
 public:
   explicit WeakReference(T* p) : mPtr(p) {}
-  ~WeakReference()
-  {
-    if (mPtr) {
-      // When this WeakReference is going to release, clear the WeakReference
-      // reference to this object in its referent |mPtr|.
-      mPtr->NoticeSelfReferencingDestruction();
-    }
-  }
 
   T* get() const { return mPtr; }
 
 #ifdef MOZ_REFCOUNTED_LEAK_CHECKING
   const char* typeName() const
   {
     // The first time this is called mPtr is null, so don't
     // invoke any methods on mPtr.
@@ -127,59 +119,44 @@ private:
 };
 
 } // namespace detail
 
 template <typename T>
 class SupportsWeakPtr
 {
 protected:
-  SupportsWeakPtr()
-    : mSelfReferencingWeakPtr(nullptr)
-  {
-  }
-
   ~SupportsWeakPtr()
   {
     static_assert(IsBaseOf<SupportsWeakPtr<T>, T>::value,
                   "T must derive from SupportsWeakPtr<T>");
     if (mSelfReferencingWeakPtr) {
-      // When this SupportsWeakPtr is going to release, remove the referent
-      // reference to this object in WeakReference |mSelfReferencingWeakPtr|.
-      mSelfReferencingWeakPtr->detach();
-      mSelfReferencingWeakPtr = nullptr;
+      mSelfReferencingWeakPtr.mRef->detach();
     }
   }
 
 private:
-  detail::WeakReference<T>* SelfReferencingWeakPtr()
+  const WeakPtr<T>& SelfReferencingWeakPtr()
   {
     if (!mSelfReferencingWeakPtr) {
-      mSelfReferencingWeakPtr = new detail::WeakReference<T>(static_cast<T*>(this));
+      mSelfReferencingWeakPtr.mRef = new detail::WeakReference<T>(static_cast<T*>(this));
     }
     return mSelfReferencingWeakPtr;
   }
 
-  detail::WeakReference<const T>* SelfReferencingWeakPtr() const
+  const WeakPtr<const T>& SelfReferencingWeakPtr() const
   {
-    detail::WeakReference<T>* p = const_cast<SupportsWeakPtr*>(this)->SelfReferencingWeakPtr();
-    return reinterpret_cast<detail::WeakReference<const T>*>(p);
-  }
-
-  void NoticeSelfReferencingDestruction() const
-  {
-    mSelfReferencingWeakPtr = nullptr;
+    const WeakPtr<T>& p = const_cast<SupportsWeakPtr*>(this)->SelfReferencingWeakPtr();
+    return reinterpret_cast<const WeakPtr<const T>&>(p);
   }
 
   friend class WeakPtr<T>;
   friend class WeakPtr<const T>;
-  friend class detail::WeakReference<T>;
-  friend class detail::WeakReference<const T>;
 
-  mutable detail::WeakReference<T>* mSelfReferencingWeakPtr;
+  WeakPtr<T> mSelfReferencingWeakPtr;
 };
 
 template <typename T>
 class WeakPtr
 {
   typedef detail::WeakReference<T> WeakReference;
 
 public:
@@ -192,17 +169,17 @@ public:
   WeakPtr(const WeakPtr& aOther)
   {
     *this = aOther;
   }
 
   WeakPtr& operator=(T* aOther)
   {
     if (aOther) {
-      mRef = aOther->SelfReferencingWeakPtr();
+      *this = aOther->SelfReferencingWeakPtr();
     } else if (!mRef || mRef->get()) {
       // Ensure that mRef is dereferenceable in the uninitialized state.
       mRef = new WeakReference(nullptr);
     }
     return *this;
   }
 
   MOZ_IMPLICIT WeakPtr(T* aOther)
@@ -216,14 +193,18 @@ public:
   operator T*() const { return mRef->get(); }
   T& operator*() const { return *mRef->get(); }
 
   T* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN { return mRef->get(); }
 
   T* get() const { return mRef->get(); }
 
 private:
+  friend class SupportsWeakPtr<T>;
+
+  explicit WeakPtr(const RefPtr<WeakReference>& aOther) : mRef(aOther) {}
+
   RefPtr<WeakReference> mRef;
 };
 
 } // namespace mozilla
 
 #endif /* mozilla_WeakPtr_h */