From e9d380a7cb3eed38d92860ac8494212f83a2a383 Mon Sep 17 00:00:00 2001 From: "dbaron%fas.harvard.edu" Date: Sat, 9 Dec 2000 04:45:43 +0000 Subject: [PATCH] Work in progress for bug 59414. Prepare to make |nsCOMPtr|'s |operator&| private to prevent many cases of accidental usage, and provide |address_of| for clients who really want a pointer to an nsCOMPtr. r=scc@mozilla.org sr=waterson@netcape.com git-svn-id: svn://10.0.0.236/trunk@83445 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/xpcom/base/nsCOMPtr.h | 101 ++++++++++++++++++++++++++++++++-- mozilla/xpcom/glue/nsCOMPtr.h | 101 ++++++++++++++++++++++++++++++++-- 2 files changed, 192 insertions(+), 10 deletions(-) diff --git a/mozilla/xpcom/base/nsCOMPtr.h b/mozilla/xpcom/base/nsCOMPtr.h index d463cf022d9..68b811b9dfb 100644 --- a/mozilla/xpcom/base/nsCOMPtr.h +++ b/mozilla/xpcom/base/nsCOMPtr.h @@ -17,10 +17,9 @@ * Copyright (C) 1998 Netscape Communications Corporation. All * Rights Reserved. * - * Original Author: - * Scott Collins - * * Contributor(s): + * Scott Collins (original author) + * L. David Baron */ #ifndef nsCOMPtr_h___ @@ -650,6 +649,44 @@ class nsCOMPtr return get(); } + nsCOMPtr* + get_address() + { + return this; + } + + const nsCOMPtr* + get_address() const + { + return this; + } + + // This is going to become private soon, once all users of + // nsCOMPtr stop using it. It may even become: + // void operator&() const {} +// private: + const nsCOMPtr* + operator&() const + // This is private to prevent accidental use of |operator&| + // instead of |getter_AddRefs|, which can happen if the result + // is cast. + + // To pass an nsCOMPtr as an out parameter to a function, either + // (preferably) pass by reference or use |address_of|. + { + return this; + } + + nsCOMPtr* + operator&() + // This version is also needed so things will compile before + // all the uses are removed and we make it private. After it's + // private, we won't need two anymore. + { + return this; + } + +// public: nsDerivedSafe& operator*() const { @@ -814,6 +851,45 @@ class nsCOMPtr return get(); } + nsCOMPtr* + get_address() + { + return this; + } + + const nsCOMPtr* + get_address() const + { + return this; + } + + // This is going to become private soon, once all users of + // nsCOMPtr stop using it. It may even become: + // void operator&() const {} +// private: + const nsCOMPtr* + operator&() const + // This is private to prevent accidental use of |operator&| + // instead of |getter_AddRefs|, which can happen if the result + // is cast. + + // To pass an nsCOMPtr as an out parameter to a function, either + // (preferably) pass by reference or use |address_of|. + { + return this; + } + + nsCOMPtr* + operator&() + // This version is also needed so things will compile before + // all the uses are removed and we make it private. After it's + // private, we won't need two anymore. + { + return this; + } + +// public: + nsDerivedSafe& operator*() const { @@ -867,6 +943,21 @@ nsCOMPtr::begin_assignment() } #endif +template +inline +nsCOMPtr* +address_of( nsCOMPtr& aPtr ) + { + return aPtr.get_address(); + } + +template +inline +const nsCOMPtr* +address_of( const nsCOMPtr& aPtr ) + { + return aPtr.get_address(); + } template class nsGetterAddRefs @@ -900,7 +991,7 @@ class nsGetterAddRefs ~nsGetterAddRefs() { #ifdef NSCAP_LOG_EXTERNAL_ASSIGNMENT - NSCAP_LOG_ASSIGNMENT(&mTargetSmartPtr, mTargetSmartPtr.get()); + NSCAP_LOG_ASSIGNMENT(NS_REINTERPRET_CAST(void *, address_of(mTargetSmartPtr)), mTargetSmartPtr.get()); #endif #ifdef NSCAP_FEATURE_TEST_DONTQUERY_CASES @@ -949,7 +1040,7 @@ class nsGetterAddRefs #ifdef NSCAP_LOG_EXTERNAL_ASSIGNMENT ~nsGetterAddRefs() { - NSCAP_LOG_ASSIGNMENT(&mTargetSmartPtr, mTargetSmartPtr.get()); + NSCAP_LOG_ASSIGNMENT(NS_REINTERPRET_CAST(void *, address_of(mTargetSmartPtr)), mTargetSmartPtr.get()); } #endif diff --git a/mozilla/xpcom/glue/nsCOMPtr.h b/mozilla/xpcom/glue/nsCOMPtr.h index d463cf022d9..68b811b9dfb 100644 --- a/mozilla/xpcom/glue/nsCOMPtr.h +++ b/mozilla/xpcom/glue/nsCOMPtr.h @@ -17,10 +17,9 @@ * Copyright (C) 1998 Netscape Communications Corporation. All * Rights Reserved. * - * Original Author: - * Scott Collins - * * Contributor(s): + * Scott Collins (original author) + * L. David Baron */ #ifndef nsCOMPtr_h___ @@ -650,6 +649,44 @@ class nsCOMPtr return get(); } + nsCOMPtr* + get_address() + { + return this; + } + + const nsCOMPtr* + get_address() const + { + return this; + } + + // This is going to become private soon, once all users of + // nsCOMPtr stop using it. It may even become: + // void operator&() const {} +// private: + const nsCOMPtr* + operator&() const + // This is private to prevent accidental use of |operator&| + // instead of |getter_AddRefs|, which can happen if the result + // is cast. + + // To pass an nsCOMPtr as an out parameter to a function, either + // (preferably) pass by reference or use |address_of|. + { + return this; + } + + nsCOMPtr* + operator&() + // This version is also needed so things will compile before + // all the uses are removed and we make it private. After it's + // private, we won't need two anymore. + { + return this; + } + +// public: nsDerivedSafe& operator*() const { @@ -814,6 +851,45 @@ class nsCOMPtr return get(); } + nsCOMPtr* + get_address() + { + return this; + } + + const nsCOMPtr* + get_address() const + { + return this; + } + + // This is going to become private soon, once all users of + // nsCOMPtr stop using it. It may even become: + // void operator&() const {} +// private: + const nsCOMPtr* + operator&() const + // This is private to prevent accidental use of |operator&| + // instead of |getter_AddRefs|, which can happen if the result + // is cast. + + // To pass an nsCOMPtr as an out parameter to a function, either + // (preferably) pass by reference or use |address_of|. + { + return this; + } + + nsCOMPtr* + operator&() + // This version is also needed so things will compile before + // all the uses are removed and we make it private. After it's + // private, we won't need two anymore. + { + return this; + } + +// public: + nsDerivedSafe& operator*() const { @@ -867,6 +943,21 @@ nsCOMPtr::begin_assignment() } #endif +template +inline +nsCOMPtr* +address_of( nsCOMPtr& aPtr ) + { + return aPtr.get_address(); + } + +template +inline +const nsCOMPtr* +address_of( const nsCOMPtr& aPtr ) + { + return aPtr.get_address(); + } template class nsGetterAddRefs @@ -900,7 +991,7 @@ class nsGetterAddRefs ~nsGetterAddRefs() { #ifdef NSCAP_LOG_EXTERNAL_ASSIGNMENT - NSCAP_LOG_ASSIGNMENT(&mTargetSmartPtr, mTargetSmartPtr.get()); + NSCAP_LOG_ASSIGNMENT(NS_REINTERPRET_CAST(void *, address_of(mTargetSmartPtr)), mTargetSmartPtr.get()); #endif #ifdef NSCAP_FEATURE_TEST_DONTQUERY_CASES @@ -949,7 +1040,7 @@ class nsGetterAddRefs #ifdef NSCAP_LOG_EXTERNAL_ASSIGNMENT ~nsGetterAddRefs() { - NSCAP_LOG_ASSIGNMENT(&mTargetSmartPtr, mTargetSmartPtr.get()); + NSCAP_LOG_ASSIGNMENT(NS_REINTERPRET_CAST(void *, address_of(mTargetSmartPtr)), mTargetSmartPtr.get()); } #endif