Bug 392493 - "Add forget() method to nsCOMPtr and nsRefPtr". r=dbaron, a=bzbarsky.
authorbent.mozilla@gmail.com
Tue, 28 Aug 2007 16:37:22 -0700
changeset 5401 2bfe36ffbfbe737af68f1a771a9b2812aa4a9cd3
parent 5400 26d162e8be46bbb26e7a625bd9e9d458fce78abe
child 5402 80131b88a3cc34269594073092d8e5ca15bca062
push idunknown
push userunknown
push dateunknown
reviewersdbaron, bzbarsky
bugs392493
milestone1.9a8pre
Bug 392493 - "Add forget() method to nsCOMPtr and nsRefPtr". r=dbaron, a=bzbarsky.
xpcom/base/nsAutoPtr.h
xpcom/glue/nsCOMPtr.h
--- a/xpcom/base/nsAutoPtr.h
+++ b/xpcom/base/nsAutoPtr.h
@@ -1027,16 +1027,26 @@ class nsRefPtr
       swap( T*& rhs )
           // ...exchange ownership with |rhs|; can save a pair of refcount operations
         {
           T* temp = rhs;
           rhs = mRawPtr;
           mRawPtr = temp;
         }
 
+      already_AddRefed<T>
+      forget()
+          // return the value of mRawPtr and null out mRawPtr. Useful for
+          // already_AddRefed return values.
+        {
+          T* temp = 0;
+          swap(temp);
+          return temp;
+        }
+
       T*
       get() const
           /*
             Prefer the implicit conversion provided automatically by |operator nsDerivedSafe<T>*() const|.
              Use |get()| to resolve ambiguity or to get a castable pointer.
 
             Returns a |nsDerivedSafe<T>*| to deny clients the use of |AddRef| and |Release|.
           */
--- a/xpcom/glue/nsCOMPtr.h
+++ b/xpcom/glue/nsCOMPtr.h
@@ -814,16 +814,26 @@ class nsCOMPtr
           rhs = reinterpret_cast<T*>(mRawPtr);
           mRawPtr = temp;
           NSCAP_ASSERT_NO_QUERY_NEEDED();
         }
 
 
         // Other pointer operators
 
+      already_AddRefed<T>
+      forget()
+          // return the value of mRawPtr and null out mRawPtr. Useful for
+          // already_AddRefed return values.
+        {
+          T* temp = 0;
+          swap(temp);
+          return temp;
+        }
+
       T*
       get() const
           /*
             Prefer the implicit conversion provided automatically by |operator nsDerivedSafe<T>*() const|.
              Use |get()| to resolve ambiguity or to get a castable pointer.
           */
         {
           return reinterpret_cast<T*>(mRawPtr);