This commit is contained in:
scc%netscape.com 1999-08-21 20:07:27 +00:00
Родитель c092767679
Коммит 9d8652d710
5 изменённых файлов: 334 добавлений и 224 удалений

Просмотреть файл

@ -1,3 +1,25 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications, Inc. Portions created by Netscape are
* Copyright (C) 1999, Mozilla. All Rights Reserved.
*
* Contributor(s):
* Scott Collins <scc@netscape.com>
*/
#ifndef nsWeakPtr_h__ #ifndef nsWeakPtr_h__
#define nsWeakPtr_h__ #define nsWeakPtr_h__

Просмотреть файл

@ -1,3 +1,25 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications, Inc. Portions created by Netscape are
* Copyright (C) 1999, Mozilla. All Rights Reserved.
*
* Contributor(s):
* Scott Collins <scc@netscape.com>
*/
// nsWeakReference.cpp // nsWeakReference.cpp
#include "nsWeakReference.h" #include "nsWeakReference.h"
@ -5,83 +27,83 @@
NS_COM nsIWeakReference * NS_COM nsIWeakReference *
NS_GetWeakReference( nsISupports* aInstance, nsresult* aResult ) NS_GetWeakReference( nsISupports* aInstance, nsresult* aResult )
{ {
nsresult status; nsresult status;
if ( !aResult ) if ( !aResult )
aResult = &status; aResult = &status;
nsCOMPtr<nsISupportsWeakReference> factoryP = do_QueryInterface(aInstance, aResult); nsCOMPtr<nsISupportsWeakReference> factoryP = do_QueryInterface(aInstance, aResult);
nsIWeakReference* weakP = 0; nsIWeakReference* weakP = 0;
if ( factoryP ) if ( factoryP )
status = factoryP->GetWeakReference(&weakP); status = factoryP->GetWeakReference(&weakP);
return weakP; return weakP;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsSupportsWeakReference::GetWeakReference( nsIWeakReference** aInstancePtr ) nsSupportsWeakReference::GetWeakReference( nsIWeakReference** aInstancePtr )
{ {
if ( !aInstancePtr ) if ( !aInstancePtr )
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
if ( !mProxy ) if ( !mProxy )
mProxy = new nsWeakReference(this); mProxy = new nsWeakReference(this);
*aInstancePtr = mProxy; *aInstancePtr = mProxy;
nsresult status; nsresult status;
if ( !*aInstancePtr ) if ( !*aInstancePtr )
status = NS_NOINTERFACE; status = NS_NOINTERFACE;
else else
{ {
NS_ADDREF(*aInstancePtr); NS_ADDREF(*aInstancePtr);
status = NS_OK; status = NS_OK;
} }
return status; return status;
} }
NS_IMETHODIMP_(nsrefcnt) NS_IMETHODIMP_(nsrefcnt)
nsWeakReference::AddRef() nsWeakReference::AddRef()
{ {
return ++mRefCount; return ++mRefCount;
} }
NS_IMETHODIMP_(nsrefcnt) NS_IMETHODIMP_(nsrefcnt)
nsWeakReference::Release() nsWeakReference::Release()
{ {
nsrefcnt temp = --mRefCount; nsrefcnt temp = --mRefCount;
if ( !mRefCount ) if ( !mRefCount )
delete this; delete this;
return temp; return temp;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsWeakReference::QueryInterface( const nsIID& aIID, void** aInstancePtr ) nsWeakReference::QueryInterface( const nsIID& aIID, void** aInstancePtr )
{ {
if ( !aInstancePtr ) if ( !aInstancePtr )
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
if ( aIID.Equals(nsCOMTypeInfo<nsIWeakReference>::GetIID()) ) if ( aIID.Equals(nsCOMTypeInfo<nsIWeakReference>::GetIID()) )
*aInstancePtr = NS_STATIC_CAST(nsIWeakReference*, this); *aInstancePtr = NS_STATIC_CAST(nsIWeakReference*, this);
else if ( aIID.Equals(nsCOMTypeInfo<nsISupports>::GetIID()) ) else if ( aIID.Equals(nsCOMTypeInfo<nsISupports>::GetIID()) )
*aInstancePtr = NS_STATIC_CAST(nsISupports*, this); *aInstancePtr = NS_STATIC_CAST(nsISupports*, this);
else else
*aInstancePtr = 0; *aInstancePtr = 0;
nsresult status; nsresult status;
if ( !*aInstancePtr ) if ( !*aInstancePtr )
status = NS_NOINTERFACE; status = NS_NOINTERFACE;
else else
{ {
NS_ADDREF( NS_REINTERPRET_CAST(nsISupports*, *aInstancePtr) ); NS_ADDREF( NS_REINTERPRET_CAST(nsISupports*, *aInstancePtr) );
status = NS_OK; status = NS_OK;
} }
return status; return status;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsWeakReference::QueryReferent( const nsIID& aIID, void** aInstancePtr ) nsWeakReference::QueryReferent( const nsIID& aIID, void** aInstancePtr )
{ {
return mReferent ? mReferent->QueryInterface(aIID, aInstancePtr) : NS_ERROR_NULL_POINTER; return mReferent ? mReferent->QueryInterface(aIID, aInstancePtr) : NS_ERROR_NULL_POINTER;
} }

Просмотреть файл

@ -1,3 +1,25 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications, Inc. Portions created by Netscape are
* Copyright (C) 1999, Mozilla. All Rights Reserved.
*
* Contributor(s):
* Scott Collins <scc@netscape.com>
*/
#ifndef nsWeakReference_h__ #ifndef nsWeakReference_h__
#define nsWeakReference_h__ #define nsWeakReference_h__
@ -6,76 +28,76 @@
#include "nsIWeakReference.h" #include "nsIWeakReference.h"
class NS_COM nsSupportsWeakReference : public nsISupportsWeakReference class NS_COM nsSupportsWeakReference : public nsISupportsWeakReference
{ {
public: public:
nsSupportsWeakReference() nsSupportsWeakReference()
: mProxy(0) : mProxy(0)
{ {
// nothing else to do here // nothing else to do here
} }
inline virtual ~nsSupportsWeakReference(); inline virtual ~nsSupportsWeakReference();
NS_IMETHOD GetWeakReference( nsIWeakReference** ); NS_IMETHOD GetWeakReference( nsIWeakReference** );
private: private:
friend class nsWeakReference; friend class nsWeakReference;
void void
NoticeProxyDestruction() NoticeProxyDestruction()
// ...called (only) by an |nsWeakReference| from _its_ dtor. // ...called (only) by an |nsWeakReference| from _its_ dtor.
{ {
mProxy = 0; mProxy = 0;
} }
nsWeakReference* mProxy; nsWeakReference* mProxy;
}; };
class NS_COM nsWeakReference : public nsIWeakReference class NS_COM nsWeakReference : public nsIWeakReference
{ {
public: public:
// nsISupports... // nsISupports...
NS_IMETHOD_(nsrefcnt) AddRef(); NS_IMETHOD_(nsrefcnt) AddRef();
NS_IMETHOD_(nsrefcnt) Release(); NS_IMETHOD_(nsrefcnt) Release();
NS_IMETHOD QueryInterface( const nsIID&, void** ); NS_IMETHOD QueryInterface( const nsIID&, void** );
// nsIWeakReference... // nsIWeakReference...
NS_IMETHOD QueryReferent( const nsIID&, void** ); NS_IMETHOD QueryReferent( const nsIID&, void** );
private: private:
friend class nsSupportsWeakReference; friend class nsSupportsWeakReference;
nsWeakReference( nsSupportsWeakReference* referent ) nsWeakReference( nsSupportsWeakReference* referent )
: mRefCount(0), : mRefCount(0),
mReferent(referent) mReferent(referent)
// ...I can only be constructed by an |nsSupportsWeakReference| // ...I can only be constructed by an |nsSupportsWeakReference|
{ {
// nothing else to do here // nothing else to do here
} }
virtual ~nsWeakReference() virtual ~nsWeakReference()
// ...I will only be destroyed by calling |delete| myself. // ...I will only be destroyed by calling |delete| myself.
{ {
if ( mReferent ) if ( mReferent )
mReferent->NoticeProxyDestruction(); mReferent->NoticeProxyDestruction();
} }
void void
NoticeReferentDestruction() NoticeReferentDestruction()
// ...called (only) by an |nsSupportsWeakReference| from _its_ dtor. // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
{ {
mReferent = 0; mReferent = 0;
} }
nsrefcnt mRefCount; nsrefcnt mRefCount;
nsSupportsWeakReference* mReferent; nsSupportsWeakReference* mReferent;
}; };
inline nsSupportsWeakReference::~nsSupportsWeakReference() inline nsSupportsWeakReference::~nsSupportsWeakReference()
{ {
if ( mProxy ) if ( mProxy )
mProxy->NoticeReferentDestruction(); mProxy->NoticeReferentDestruction();
} }
#endif #endif

Просмотреть файл

@ -1,3 +1,25 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications, Inc. Portions created by Netscape are
* Copyright (C) 1999, Mozilla. All Rights Reserved.
*
* Contributor(s):
* Scott Collins <scc@netscape.com>
*/
// nsWeakReference.cpp // nsWeakReference.cpp
#include "nsWeakReference.h" #include "nsWeakReference.h"
@ -5,83 +27,83 @@
NS_COM nsIWeakReference * NS_COM nsIWeakReference *
NS_GetWeakReference( nsISupports* aInstance, nsresult* aResult ) NS_GetWeakReference( nsISupports* aInstance, nsresult* aResult )
{ {
nsresult status; nsresult status;
if ( !aResult ) if ( !aResult )
aResult = &status; aResult = &status;
nsCOMPtr<nsISupportsWeakReference> factoryP = do_QueryInterface(aInstance, aResult); nsCOMPtr<nsISupportsWeakReference> factoryP = do_QueryInterface(aInstance, aResult);
nsIWeakReference* weakP = 0; nsIWeakReference* weakP = 0;
if ( factoryP ) if ( factoryP )
status = factoryP->GetWeakReference(&weakP); status = factoryP->GetWeakReference(&weakP);
return weakP; return weakP;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsSupportsWeakReference::GetWeakReference( nsIWeakReference** aInstancePtr ) nsSupportsWeakReference::GetWeakReference( nsIWeakReference** aInstancePtr )
{ {
if ( !aInstancePtr ) if ( !aInstancePtr )
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
if ( !mProxy ) if ( !mProxy )
mProxy = new nsWeakReference(this); mProxy = new nsWeakReference(this);
*aInstancePtr = mProxy; *aInstancePtr = mProxy;
nsresult status; nsresult status;
if ( !*aInstancePtr ) if ( !*aInstancePtr )
status = NS_NOINTERFACE; status = NS_NOINTERFACE;
else else
{ {
NS_ADDREF(*aInstancePtr); NS_ADDREF(*aInstancePtr);
status = NS_OK; status = NS_OK;
} }
return status; return status;
} }
NS_IMETHODIMP_(nsrefcnt) NS_IMETHODIMP_(nsrefcnt)
nsWeakReference::AddRef() nsWeakReference::AddRef()
{ {
return ++mRefCount; return ++mRefCount;
} }
NS_IMETHODIMP_(nsrefcnt) NS_IMETHODIMP_(nsrefcnt)
nsWeakReference::Release() nsWeakReference::Release()
{ {
nsrefcnt temp = --mRefCount; nsrefcnt temp = --mRefCount;
if ( !mRefCount ) if ( !mRefCount )
delete this; delete this;
return temp; return temp;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsWeakReference::QueryInterface( const nsIID& aIID, void** aInstancePtr ) nsWeakReference::QueryInterface( const nsIID& aIID, void** aInstancePtr )
{ {
if ( !aInstancePtr ) if ( !aInstancePtr )
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
if ( aIID.Equals(nsCOMTypeInfo<nsIWeakReference>::GetIID()) ) if ( aIID.Equals(nsCOMTypeInfo<nsIWeakReference>::GetIID()) )
*aInstancePtr = NS_STATIC_CAST(nsIWeakReference*, this); *aInstancePtr = NS_STATIC_CAST(nsIWeakReference*, this);
else if ( aIID.Equals(nsCOMTypeInfo<nsISupports>::GetIID()) ) else if ( aIID.Equals(nsCOMTypeInfo<nsISupports>::GetIID()) )
*aInstancePtr = NS_STATIC_CAST(nsISupports*, this); *aInstancePtr = NS_STATIC_CAST(nsISupports*, this);
else else
*aInstancePtr = 0; *aInstancePtr = 0;
nsresult status; nsresult status;
if ( !*aInstancePtr ) if ( !*aInstancePtr )
status = NS_NOINTERFACE; status = NS_NOINTERFACE;
else else
{ {
NS_ADDREF( NS_REINTERPRET_CAST(nsISupports*, *aInstancePtr) ); NS_ADDREF( NS_REINTERPRET_CAST(nsISupports*, *aInstancePtr) );
status = NS_OK; status = NS_OK;
} }
return status; return status;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsWeakReference::QueryReferent( const nsIID& aIID, void** aInstancePtr ) nsWeakReference::QueryReferent( const nsIID& aIID, void** aInstancePtr )
{ {
return mReferent ? mReferent->QueryInterface(aIID, aInstancePtr) : NS_ERROR_NULL_POINTER; return mReferent ? mReferent->QueryInterface(aIID, aInstancePtr) : NS_ERROR_NULL_POINTER;
} }

Просмотреть файл

@ -1,3 +1,25 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications, Inc. Portions created by Netscape are
* Copyright (C) 1999, Mozilla. All Rights Reserved.
*
* Contributor(s):
* Scott Collins <scc@netscape.com>
*/
#ifndef nsWeakReference_h__ #ifndef nsWeakReference_h__
#define nsWeakReference_h__ #define nsWeakReference_h__
@ -6,76 +28,76 @@
#include "nsIWeakReference.h" #include "nsIWeakReference.h"
class NS_COM nsSupportsWeakReference : public nsISupportsWeakReference class NS_COM nsSupportsWeakReference : public nsISupportsWeakReference
{ {
public: public:
nsSupportsWeakReference() nsSupportsWeakReference()
: mProxy(0) : mProxy(0)
{ {
// nothing else to do here // nothing else to do here
} }
inline virtual ~nsSupportsWeakReference(); inline virtual ~nsSupportsWeakReference();
NS_IMETHOD GetWeakReference( nsIWeakReference** ); NS_IMETHOD GetWeakReference( nsIWeakReference** );
private: private:
friend class nsWeakReference; friend class nsWeakReference;
void void
NoticeProxyDestruction() NoticeProxyDestruction()
// ...called (only) by an |nsWeakReference| from _its_ dtor. // ...called (only) by an |nsWeakReference| from _its_ dtor.
{ {
mProxy = 0; mProxy = 0;
} }
nsWeakReference* mProxy; nsWeakReference* mProxy;
}; };
class NS_COM nsWeakReference : public nsIWeakReference class NS_COM nsWeakReference : public nsIWeakReference
{ {
public: public:
// nsISupports... // nsISupports...
NS_IMETHOD_(nsrefcnt) AddRef(); NS_IMETHOD_(nsrefcnt) AddRef();
NS_IMETHOD_(nsrefcnt) Release(); NS_IMETHOD_(nsrefcnt) Release();
NS_IMETHOD QueryInterface( const nsIID&, void** ); NS_IMETHOD QueryInterface( const nsIID&, void** );
// nsIWeakReference... // nsIWeakReference...
NS_IMETHOD QueryReferent( const nsIID&, void** ); NS_IMETHOD QueryReferent( const nsIID&, void** );
private: private:
friend class nsSupportsWeakReference; friend class nsSupportsWeakReference;
nsWeakReference( nsSupportsWeakReference* referent ) nsWeakReference( nsSupportsWeakReference* referent )
: mRefCount(0), : mRefCount(0),
mReferent(referent) mReferent(referent)
// ...I can only be constructed by an |nsSupportsWeakReference| // ...I can only be constructed by an |nsSupportsWeakReference|
{ {
// nothing else to do here // nothing else to do here
} }
virtual ~nsWeakReference() virtual ~nsWeakReference()
// ...I will only be destroyed by calling |delete| myself. // ...I will only be destroyed by calling |delete| myself.
{ {
if ( mReferent ) if ( mReferent )
mReferent->NoticeProxyDestruction(); mReferent->NoticeProxyDestruction();
} }
void void
NoticeReferentDestruction() NoticeReferentDestruction()
// ...called (only) by an |nsSupportsWeakReference| from _its_ dtor. // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
{ {
mReferent = 0; mReferent = 0;
} }
nsrefcnt mRefCount; nsrefcnt mRefCount;
nsSupportsWeakReference* mReferent; nsSupportsWeakReference* mReferent;
}; };
inline nsSupportsWeakReference::~nsSupportsWeakReference() inline nsSupportsWeakReference::~nsSupportsWeakReference()
{ {
if ( mProxy ) if ( mProxy )
mProxy->NoticeReferentDestruction(); mProxy->NoticeReferentDestruction();
} }
#endif #endif