bug #68872: r=jag, sr=waterson. provide |do_GetWeakReference|.

This commit is contained in:
scc%mozilla.org 2001-03-09 00:40:11 +00:00
parent 5d4c3b09ca
commit 061a83670c
3 changed files with 108 additions and 30 deletions

View File

@ -85,18 +85,6 @@ interface nsISupportsWeakReference : nsISupports
typedef nsCOMPtr<nsIWeakReference> nsWeakPtr;
/**
* |NS_GetWeakReference| is a convenience function that bundles up all the work needed
* to get a weak reference to an arbitrary object, i.e., the |QueryInterface|, test, and
* call through to |GetWeakReference|. It produces an |nsIWeakReference*| as its
* function result specifically to cooperate with |nsCOMPtr| (or |nsWeakPtr|) like so:
* |nsWeakPtr myWeakPtr = getter_AddRefs( NS_GetWeakReference(aPtr) );|.
*/
extern NS_COM
nsIWeakReference*
NS_GetWeakReference( nsISupports* , nsresult* aResult=0 );
/**
*
*/
@ -137,4 +125,74 @@ do_QueryReferent( nsIWeakReference* aRawPtr, nsresult* error = 0 )
}
class NS_COM nsGetWeakReference : public nsCOMPtr_helper
{
public:
nsGetWeakReference( nsISupports* aRawPtr, nsresult* error )
: mRawPtr(aRawPtr),
mErrorPtr(error)
{
// nothing else to do here
}
virtual nsresult operator()( const nsIID&, void** ) const;
private:
nsISupports* mRawPtr;
nsresult* mErrorPtr;
};
/**
* |do_GetWeakReference| is a convenience function that bundles up all the work needed
* to get a weak reference to an arbitrary object, i.e., the |QueryInterface|, test, and
* call through to |GetWeakReference|, and put it into your |nsCOMPtr|.
* It is specifically designed to cooperate with |nsCOMPtr| (or |nsWeakPtr|) like so:
* |nsWeakPtr myWeakPtr = do_GetWeakReference(aPtr);|.
*/
inline
const nsGetWeakReference
do_GetWeakReference( nsISupports* aRawPtr, nsresult* error = 0 )
{
return nsGetWeakReference(aRawPtr, error);
}
inline
void
do_GetWeakReference( nsIWeakReference* aRawPtr, nsresult* error = 0 )
{
// This signature exists soley to _stop_ you from doing a bad thing.
// Saying |do_GetWeakReference()| on a weak reference itself,
// is very likely to be a programmer error.
}
template <class T>
inline
void
do_GetWeakReference( already_AddRefed<T>& )
{
// This signature exists soley to _stop_ you from doing the bad thing.
// Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
// someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
}
template <class T>
inline
void
do_GetWeakReference( already_AddRefed<T>&, nsresult* )
{
// This signature exists soley to _stop_ you from doing the bad thing.
// Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
// someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
}
/**
* Deprecated, use |do_GetWeakReference| instead.
*/
extern NS_COM
nsIWeakReference*
NS_GetWeakReference( nsISupports* , nsresult* aResult=0 );
%}

View File

@ -43,25 +43,35 @@ nsQueryReferent::operator()( const nsIID& aIID, void** answer ) const
return status;
}
NS_COM nsIWeakReference*
NS_GetWeakReference( nsISupports* aInstancePtr, nsresult* aErrorPtr )
nsresult
nsGetWeakReference::operator()( const nsIID&, void** aResult ) const
{
nsresult status;
nsIWeakReference* result = 0;
nsIWeakReference** result = &NS_STATIC_CAST(nsIWeakReference*, *aResult);
*result = 0;
if ( aInstancePtr )
if ( mRawPtr )
{
nsCOMPtr<nsISupportsWeakReference> factoryPtr = do_QueryInterface(aInstancePtr, &status);
NS_ASSERTION(factoryPtr, "Did you know you were calling |NS_GetWeakReference()| on something that doesn't support weak references?");
nsCOMPtr<nsISupportsWeakReference> factoryPtr = do_QueryInterface(mRawPtr, &status);
NS_ASSERTION(factoryPtr, "Oops! You're asking for a weak reference to an object that doesn't support that.");
if ( factoryPtr )
status = factoryPtr->GetWeakReference(&result);
status = factoryPtr->GetWeakReference(result);
// else, |status| has already been set by |do_QueryInterface|
}
else
status = NS_ERROR_NULL_POINTER;
if ( aErrorPtr )
*aErrorPtr = status;
if ( mErrorPtr )
*mErrorPtr = status;
return status;
}
NS_COM nsIWeakReference* // or else |already_AddRefed<nsIWeakReference>|
NS_GetWeakReference( nsISupports* aInstancePtr, nsresult* aErrorPtr )
{
nsIWeakReference* result = 0;
nsGetWeakReference(aInstancePtr, aErrorPtr)(NS_GET_IID(nsIWeakReference), &result);
return result;
}

View File

@ -43,25 +43,35 @@ nsQueryReferent::operator()( const nsIID& aIID, void** answer ) const
return status;
}
NS_COM nsIWeakReference*
NS_GetWeakReference( nsISupports* aInstancePtr, nsresult* aErrorPtr )
nsresult
nsGetWeakReference::operator()( const nsIID&, void** aResult ) const
{
nsresult status;
nsIWeakReference* result = 0;
nsIWeakReference** result = &NS_STATIC_CAST(nsIWeakReference*, *aResult);
*result = 0;
if ( aInstancePtr )
if ( mRawPtr )
{
nsCOMPtr<nsISupportsWeakReference> factoryPtr = do_QueryInterface(aInstancePtr, &status);
NS_ASSERTION(factoryPtr, "Did you know you were calling |NS_GetWeakReference()| on something that doesn't support weak references?");
nsCOMPtr<nsISupportsWeakReference> factoryPtr = do_QueryInterface(mRawPtr, &status);
NS_ASSERTION(factoryPtr, "Oops! You're asking for a weak reference to an object that doesn't support that.");
if ( factoryPtr )
status = factoryPtr->GetWeakReference(&result);
status = factoryPtr->GetWeakReference(result);
// else, |status| has already been set by |do_QueryInterface|
}
else
status = NS_ERROR_NULL_POINTER;
if ( aErrorPtr )
*aErrorPtr = status;
if ( mErrorPtr )
*mErrorPtr = status;
return status;
}
NS_COM nsIWeakReference* // or else |already_AddRefed<nsIWeakReference>|
NS_GetWeakReference( nsISupports* aInstancePtr, nsresult* aErrorPtr )
{
nsIWeakReference* result = 0;
nsGetWeakReference(aInstancePtr, aErrorPtr)(NS_GET_IID(nsIWeakReference), &result);
return result;
}