1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/nsIWeakReferenceUtils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 +/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsIWeakReferenceUtils_h__ 1.10 +#define nsIWeakReferenceUtils_h__ 1.11 + 1.12 +#include "nsCOMPtr.h" 1.13 +#include "nsIWeakReference.h" 1.14 + 1.15 +typedef nsCOMPtr<nsIWeakReference> nsWeakPtr; 1.16 + 1.17 +/** 1.18 + * 1.19 + */ 1.20 + 1.21 +// a type-safe shortcut for calling the |QueryReferent()| member function 1.22 +// T must inherit from nsIWeakReference, but the cast may be ambiguous. 1.23 +template <class T, class DestinationType> 1.24 +inline 1.25 +nsresult 1.26 +CallQueryReferent( T* aSource, DestinationType** aDestination ) 1.27 + { 1.28 + NS_PRECONDITION(aSource, "null parameter"); 1.29 + NS_PRECONDITION(aDestination, "null parameter"); 1.30 + 1.31 + return aSource->QueryReferent(NS_GET_TEMPLATE_IID(DestinationType), 1.32 + reinterpret_cast<void**>(aDestination)); 1.33 + } 1.34 + 1.35 + 1.36 +class NS_COM_GLUE nsQueryReferent : public nsCOMPtr_helper 1.37 + { 1.38 + public: 1.39 + nsQueryReferent( nsIWeakReference* aWeakPtr, nsresult* error ) 1.40 + : mWeakPtr(aWeakPtr), 1.41 + mErrorPtr(error) 1.42 + { 1.43 + // nothing else to do here 1.44 + } 1.45 + 1.46 + virtual nsresult NS_FASTCALL operator()( const nsIID& aIID, void** ) const; 1.47 + 1.48 + private: 1.49 + nsIWeakReference* mWeakPtr; 1.50 + nsresult* mErrorPtr; 1.51 + }; 1.52 + 1.53 +inline 1.54 +const nsQueryReferent 1.55 +do_QueryReferent( nsIWeakReference* aRawPtr, nsresult* error = 0 ) 1.56 + { 1.57 + return nsQueryReferent(aRawPtr, error); 1.58 + } 1.59 + 1.60 + 1.61 + /** 1.62 + * Deprecated, use |do_GetWeakReference| instead. 1.63 + */ 1.64 +extern NS_COM_GLUE 1.65 +nsIWeakReference* 1.66 +NS_GetWeakReference( nsISupports* , nsresult* aResult=0 ); 1.67 + 1.68 + /** 1.69 + * |do_GetWeakReference| is a convenience function that bundles up all the work needed 1.70 + * to get a weak reference to an arbitrary object, i.e., the |QueryInterface|, test, and 1.71 + * call through to |GetWeakReference|, and put it into your |nsCOMPtr|. 1.72 + * It is specifically designed to cooperate with |nsCOMPtr| (or |nsWeakPtr|) like so: 1.73 + * |nsWeakPtr myWeakPtr = do_GetWeakReference(aPtr);|. 1.74 + */ 1.75 +inline 1.76 +already_AddRefed<nsIWeakReference> 1.77 +do_GetWeakReference( nsISupports* aRawPtr, nsresult* error = 0 ) 1.78 + { 1.79 + return dont_AddRef(NS_GetWeakReference(aRawPtr, error)); 1.80 + } 1.81 + 1.82 +inline 1.83 +void 1.84 +do_GetWeakReference( nsIWeakReference* aRawPtr, nsresult* error = 0 ) 1.85 + { 1.86 + // This signature exists solely to _stop_ you from doing a bad thing. 1.87 + // Saying |do_GetWeakReference()| on a weak reference itself, 1.88 + // is very likely to be a programmer error. 1.89 + } 1.90 + 1.91 +template <class T> 1.92 +inline 1.93 +void 1.94 +do_GetWeakReference( already_AddRefed<T>& ) 1.95 + { 1.96 + // This signature exists solely to _stop_ you from doing the bad thing. 1.97 + // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by 1.98 + // someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>. 1.99 + } 1.100 + 1.101 +template <class T> 1.102 +inline 1.103 +void 1.104 +do_GetWeakReference( already_AddRefed<T>&, nsresult* ) 1.105 + { 1.106 + // This signature exists solely to _stop_ you from doing the bad thing. 1.107 + // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by 1.108 + // someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>. 1.109 + } 1.110 + 1.111 +#endif