LCOV - app.info - objdir/xpcom/build/nsWeakReference.cpp

LCOV - code coverage report
| Current view: | directory - objdir/xpcom/build - nsWeakReference.cpp (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 49 | 45 | 91.8 %
| Date: | 2012-07-07 | Functions: | 11 | 11 | 100.0 %

           1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
           2                 :  *
           3                 :  * ***** BEGIN LICENSE BLOCK *****
           4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
           5                 :  *
           6                 :  * The contents of this file are subject to the Mozilla Public License Version
           7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
           8                 :  * the License. You may obtain a copy of the License at
           9                 :  * http://www.mozilla.org/MPL/
          10                 :  *
          11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
          12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
          13                 :  * for the specific language governing rights and limitations under the
          14                 :  * License.
          15                 :  *
          16                 :  * The Original Code is the Mozilla browser.
          17                 :  *
          18                 :  * The Initial Developer of the Original Code is
          19                 :  * Netscape Communications, Inc.
          20                 :  * Portions created by the Initial Developer are Copyright (C) 1999
          21                 :  * the Initial Developer. All Rights Reserved.
          22                 :  *
          23                 :  * Contributor(s):
          24                 :  *   Scott Collins <scc@netscape.com>
          25                 :  *   Pierre Phaneuf <pp@ludusdesign.com>
          26                 :  *
          27                 :  * Alternatively, the contents of this file may be used under the terms of
          28                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
          29                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
          30                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
          31                 :  * of those above. If you wish to allow use of your version of this file only
          32                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
          33                 :  * use your version of this file under the terms of the MPL, indicate your
          34                 :  * decision by deleting the provisions above and replace them with the notice
          35                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
          36                 :  * the provisions above, a recipient may use your version of this file under
          37                 :  * the terms of any one of the MPL, the GPL or the LGPL.
          38                 :  *
          39                 :  * ***** END LICENSE BLOCK ***** */
          40                 :
          41                 : // nsWeakReference.cpp
          42                 :
          43                 : #include "mozilla/Attributes.h"
          44                 :
          45                 : #include "nsWeakReference.h"
          46                 : #include "nsCOMPtr.h"
          47                 :
          48                 : class nsWeakReference MOZ_FINAL : public nsIWeakReference
          49                 :   {
          50                 :     public:
          51                 :     // nsISupports...
          52                 :       NS_DECL_ISUPPORTS
          53                 :
          54                 :     // nsIWeakReference...
          55                 :       NS_DECL_NSIWEAKREFERENCE
          56                 :
          57                 :     private:
          58                 :       friend class nsSupportsWeakReference;
          59                 :
          60           37852 :       nsWeakReference( nsSupportsWeakReference* referent )
          61           37852 :           : mReferent(referent)
          62                 :           // ...I can only be constructed by an |nsSupportsWeakReference|
          63                 :         {
          64                 :           // nothing else to do here
          65           37852 :         }
          66                 :
          67           37839 :       ~nsWeakReference()
          68                 :            // ...I will only be destroyed by calling |delete| myself.
          69           37839 :         {
          70           37839 :           if ( mReferent )
          71           23792 :             mReferent->NoticeProxyDestruction();
          72           37839 :         }
          73                 :
          74                 :       void
          75           14058 :       NoticeReferentDestruction()
          76                 :           // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
          77                 :         {
          78           14058 :           mReferent = 0;
          79           14058 :         }
          80                 :
          81                 :       nsSupportsWeakReference*  mReferent;
          82                 :   };
          83                 :
          84                 : nsresult
          85           61803 : nsQueryReferent::operator()( const nsIID& aIID, void** answer ) const
          86                 :   {
          87                 :     nsresult status;
          88           61803 :     if ( mWeakPtr )
          89                 :       {
          90           41163 :         if ( NS_FAILED(status = mWeakPtr->QueryReferent(aIID, answer)) )
          91             312 :           *answer = 0;
          92                 :       }
          93                 :     else
          94           20640 :       status = NS_ERROR_NULL_POINTER;
          95                 :
          96           61803 :     if ( mErrorPtr )
          97               0 :       *mErrorPtr = status;
          98           61803 :     return status;
          99                 :   }
         100                 :
         101                 : NS_COM_GLUE nsIWeakReference*  // or else |already_AddRefed<nsIWeakReference>|
         102          108279 : NS_GetWeakReference( nsISupports* aInstancePtr, nsresult* aErrorPtr )
         103                 :   {
         104                 :     nsresult status;
         105                 :
         106          108279 :     nsIWeakReference* result = nsnull;
         107                 :
         108          108279 :     if ( aInstancePtr )
         109                 :       {
         110          210014 :         nsCOMPtr<nsISupportsWeakReference> factoryPtr = do_QueryInterface(aInstancePtr, &status);
         111          105007 :         NS_ASSERTION(factoryPtr, "Oops!  You're asking for a weak reference to an object that doesn't support that.");
         112          105007 :         if ( factoryPtr )
         113                 :           {
         114          105007 :             status = factoryPtr->GetWeakReference(&result);
         115                 :           }
         116                 :         // else, |status| has already been set by |do_QueryInterface|
         117                 :       }
         118                 :     else
         119            3272 :       status = NS_ERROR_NULL_POINTER;
         120                 :
         121          108279 :     if ( aErrorPtr )
         122               0 :       *aErrorPtr = status;
         123          108279 :     return result;
         124                 :   }
         125                 :
         126                 : NS_COM_GLUE nsresult
         127          109564 : nsSupportsWeakReference::GetWeakReference( nsIWeakReference** aInstancePtr )
         128                 :   {
         129          109564 :     if ( !aInstancePtr )
         130               0 :       return NS_ERROR_NULL_POINTER;
         131                 :
         132          109564 :     if ( !mProxy )
         133           37852 :       mProxy = new nsWeakReference(this);
         134          109564 :     *aInstancePtr = mProxy;
         135                 :
         136                 :     nsresult status;
         137          109564 :     if ( !*aInstancePtr )
         138               0 :       status = NS_ERROR_OUT_OF_MEMORY;
         139                 :     else
         140                 :       {
         141          109564 :         NS_ADDREF(*aInstancePtr);
         142          109564 :         status = NS_OK;
         143                 :       }
         144                 :
         145          109564 :     return status;
         146                 :   }
         147                 :
         148          909053 : NS_IMPL_ISUPPORTS1(nsWeakReference, nsIWeakReference)
         149                 :
         150                 : NS_IMETHODIMP
         151          573591 : nsWeakReference::QueryReferent( const nsIID& aIID, void** aInstancePtr )
         152                 :   {
         153          573591 :     return mReferent ? mReferent->QueryInterface(aIID, aInstancePtr) : NS_ERROR_NULL_POINTER;
         154                 :   }
         155                 :
         156                 : void
         157          245068 : nsSupportsWeakReference::ClearWeakReferences()
         158                 :         {
         159          245068 :                 if ( mProxy )
         160                 :                         {
         161           14058 :                                 mProxy->NoticeReferentDestruction();
         162           14058 :                                 mProxy = 0;
         163                 :                         }
         164          245068 :         }
         165                 :

Generated by: LCOV version 1.7