michael@0: /* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsIWeakReferenceUtils_h__ michael@0: #define nsIWeakReferenceUtils_h__ michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIWeakReference.h" michael@0: michael@0: typedef nsCOMPtr nsWeakPtr; michael@0: michael@0: /** michael@0: * michael@0: */ michael@0: michael@0: // a type-safe shortcut for calling the |QueryReferent()| member function michael@0: // T must inherit from nsIWeakReference, but the cast may be ambiguous. michael@0: template michael@0: inline michael@0: nsresult michael@0: CallQueryReferent( T* aSource, DestinationType** aDestination ) michael@0: { michael@0: NS_PRECONDITION(aSource, "null parameter"); michael@0: NS_PRECONDITION(aDestination, "null parameter"); michael@0: michael@0: return aSource->QueryReferent(NS_GET_TEMPLATE_IID(DestinationType), michael@0: reinterpret_cast(aDestination)); michael@0: } michael@0: michael@0: michael@0: class NS_COM_GLUE nsQueryReferent : public nsCOMPtr_helper michael@0: { michael@0: public: michael@0: nsQueryReferent( nsIWeakReference* aWeakPtr, nsresult* error ) michael@0: : mWeakPtr(aWeakPtr), michael@0: mErrorPtr(error) michael@0: { michael@0: // nothing else to do here michael@0: } michael@0: michael@0: virtual nsresult NS_FASTCALL operator()( const nsIID& aIID, void** ) const; michael@0: michael@0: private: michael@0: nsIWeakReference* mWeakPtr; michael@0: nsresult* mErrorPtr; michael@0: }; michael@0: michael@0: inline michael@0: const nsQueryReferent michael@0: do_QueryReferent( nsIWeakReference* aRawPtr, nsresult* error = 0 ) michael@0: { michael@0: return nsQueryReferent(aRawPtr, error); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Deprecated, use |do_GetWeakReference| instead. michael@0: */ michael@0: extern NS_COM_GLUE michael@0: nsIWeakReference* michael@0: NS_GetWeakReference( nsISupports* , nsresult* aResult=0 ); michael@0: michael@0: /** michael@0: * |do_GetWeakReference| is a convenience function that bundles up all the work needed michael@0: * to get a weak reference to an arbitrary object, i.e., the |QueryInterface|, test, and michael@0: * call through to |GetWeakReference|, and put it into your |nsCOMPtr|. michael@0: * It is specifically designed to cooperate with |nsCOMPtr| (or |nsWeakPtr|) like so: michael@0: * |nsWeakPtr myWeakPtr = do_GetWeakReference(aPtr);|. michael@0: */ michael@0: inline michael@0: already_AddRefed michael@0: do_GetWeakReference( nsISupports* aRawPtr, nsresult* error = 0 ) michael@0: { michael@0: return dont_AddRef(NS_GetWeakReference(aRawPtr, error)); michael@0: } michael@0: michael@0: inline michael@0: void michael@0: do_GetWeakReference( nsIWeakReference* aRawPtr, nsresult* error = 0 ) michael@0: { michael@0: // This signature exists solely to _stop_ you from doing a bad thing. michael@0: // Saying |do_GetWeakReference()| on a weak reference itself, michael@0: // is very likely to be a programmer error. michael@0: } michael@0: michael@0: template michael@0: inline michael@0: void michael@0: do_GetWeakReference( already_AddRefed& ) michael@0: { michael@0: // This signature exists solely to _stop_ you from doing the bad thing. michael@0: // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by michael@0: // someone else is an automatic leak. See . michael@0: } michael@0: michael@0: template michael@0: inline michael@0: void michael@0: do_GetWeakReference( already_AddRefed&, nsresult* ) michael@0: { michael@0: // This signature exists solely to _stop_ you from doing the bad thing. michael@0: // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by michael@0: // someone else is an automatic leak. See . michael@0: } michael@0: michael@0: #endif