xpcom/glue/nsWeakReference.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/glue/nsWeakReference.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,130 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + *
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// nsWeakReference.cpp
    1.11 +
    1.12 +#include "mozilla/Attributes.h"
    1.13 +
    1.14 +#include "nsWeakReference.h"
    1.15 +#include "nsCOMPtr.h"
    1.16 +
    1.17 +class nsWeakReference MOZ_FINAL : public nsIWeakReference
    1.18 +  {
    1.19 +    public:
    1.20 +    // nsISupports...
    1.21 +      NS_DECL_ISUPPORTS
    1.22 +
    1.23 +    // nsIWeakReference...
    1.24 +      NS_DECL_NSIWEAKREFERENCE
    1.25 +
    1.26 +    private:
    1.27 +      friend class nsSupportsWeakReference;
    1.28 +
    1.29 +      nsWeakReference( nsSupportsWeakReference* referent )
    1.30 +          : mReferent(referent)
    1.31 +          // ...I can only be constructed by an |nsSupportsWeakReference|
    1.32 +        {
    1.33 +          // nothing else to do here
    1.34 +        }
    1.35 +
    1.36 +      ~nsWeakReference()
    1.37 +           // ...I will only be destroyed by calling |delete| myself.
    1.38 +        {
    1.39 +          if ( mReferent )
    1.40 +            mReferent->NoticeProxyDestruction();
    1.41 +        }
    1.42 +
    1.43 +      void
    1.44 +      NoticeReferentDestruction()
    1.45 +          // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
    1.46 +        {
    1.47 +          mReferent = 0;
    1.48 +        }
    1.49 +
    1.50 +      nsSupportsWeakReference*  mReferent;
    1.51 +  };
    1.52 +
    1.53 +nsresult
    1.54 +nsQueryReferent::operator()( const nsIID& aIID, void** answer ) const
    1.55 +  {
    1.56 +    nsresult status;
    1.57 +    if ( mWeakPtr )
    1.58 +      {
    1.59 +        if ( NS_FAILED(status = mWeakPtr->QueryReferent(aIID, answer)) )
    1.60 +          *answer = 0;
    1.61 +      }
    1.62 +    else
    1.63 +      status = NS_ERROR_NULL_POINTER;
    1.64 +
    1.65 +    if ( mErrorPtr )
    1.66 +      *mErrorPtr = status;
    1.67 +    return status;
    1.68 +  }
    1.69 +
    1.70 +NS_COM_GLUE nsIWeakReference*  // or else |already_AddRefed<nsIWeakReference>|
    1.71 +NS_GetWeakReference( nsISupports* aInstancePtr, nsresult* aErrorPtr )
    1.72 +  {
    1.73 +    nsresult status;
    1.74 +
    1.75 +    nsIWeakReference* result = nullptr;
    1.76 +
    1.77 +    if ( aInstancePtr )
    1.78 +      {
    1.79 +        nsCOMPtr<nsISupportsWeakReference> factoryPtr = do_QueryInterface(aInstancePtr, &status);
    1.80 +        if ( factoryPtr )
    1.81 +          {
    1.82 +            status = factoryPtr->GetWeakReference(&result);
    1.83 +          }
    1.84 +        // else, |status| has already been set by |do_QueryInterface|
    1.85 +      }
    1.86 +    else
    1.87 +      status = NS_ERROR_NULL_POINTER;
    1.88 +
    1.89 +    if ( aErrorPtr )
    1.90 +      *aErrorPtr = status;
    1.91 +    return result;
    1.92 +  }
    1.93 +
    1.94 +NS_COM_GLUE nsresult
    1.95 +nsSupportsWeakReference::GetWeakReference( nsIWeakReference** aInstancePtr )
    1.96 +  {
    1.97 +    if ( !aInstancePtr )
    1.98 +      return NS_ERROR_NULL_POINTER;
    1.99 +
   1.100 +    if ( !mProxy )
   1.101 +      mProxy = new nsWeakReference(this);
   1.102 +    *aInstancePtr = mProxy;
   1.103 +
   1.104 +    nsresult status;
   1.105 +    if ( !*aInstancePtr )
   1.106 +      status = NS_ERROR_OUT_OF_MEMORY;
   1.107 +    else
   1.108 +      {
   1.109 +        NS_ADDREF(*aInstancePtr);
   1.110 +        status = NS_OK;
   1.111 +      }
   1.112 +
   1.113 +    return status;
   1.114 +  }
   1.115 +
   1.116 +NS_IMPL_ISUPPORTS(nsWeakReference, nsIWeakReference)
   1.117 +
   1.118 +NS_IMETHODIMP
   1.119 +nsWeakReference::QueryReferent( const nsIID& aIID, void** aInstancePtr )
   1.120 +  {
   1.121 +    return mReferent ? mReferent->QueryInterface(aIID, aInstancePtr) : NS_ERROR_NULL_POINTER;
   1.122 +  }
   1.123 +
   1.124 +void
   1.125 +nsSupportsWeakReference::ClearWeakReferences()
   1.126 +	{
   1.127 +		if ( mProxy )
   1.128 +			{
   1.129 +				mProxy->NoticeReferentDestruction();
   1.130 +				mProxy = 0;
   1.131 +			}
   1.132 +	}
   1.133 +

mercurial