1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is the Mozilla browser.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications, Inc.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
24 * Scott Collins <scc@netscape.com>
25 * Pierre Phaneuf <pp@ludusdesign.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 // nsWeakReference.cpp
43 #include "nsWeakReference.h"
46 class nsWeakReference
: public nsIWeakReference
52 // nsIWeakReference...
53 NS_DECL_NSIWEAKREFERENCE
56 friend class nsSupportsWeakReference
;
58 nsWeakReference( nsSupportsWeakReference
* referent
)
60 // ...I can only be constructed by an |nsSupportsWeakReference|
62 // nothing else to do here
66 // ...I will only be destroyed by calling |delete| myself.
69 mReferent
->NoticeProxyDestruction();
73 NoticeReferentDestruction()
74 // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
79 nsSupportsWeakReference
* mReferent
;
83 nsQueryReferent::operator()( const nsIID
& aIID
, void** answer
) const
88 if ( NS_FAILED(status
= mWeakPtr
->QueryReferent(aIID
, answer
)) )
92 status
= NS_ERROR_NULL_POINTER
;
99 NS_COM_GLUE nsIWeakReference
* // or else |already_AddRefed<nsIWeakReference>|
100 NS_GetWeakReference( nsISupports
* aInstancePtr
, nsresult
* aErrorPtr
)
104 nsIWeakReference
* result
= nsnull
;
108 nsCOMPtr
<nsISupportsWeakReference
> factoryPtr
= do_QueryInterface(aInstancePtr
, &status
);
109 NS_ASSERTION(factoryPtr
, "Oops! You're asking for a weak reference to an object that doesn't support that.");
112 status
= factoryPtr
->GetWeakReference(&result
);
114 // else, |status| has already been set by |do_QueryInterface|
117 status
= NS_ERROR_NULL_POINTER
;
125 nsSupportsWeakReference::GetWeakReference( nsIWeakReference
** aInstancePtr
)
128 return NS_ERROR_NULL_POINTER
;
131 mProxy
= new nsWeakReference(this);
132 *aInstancePtr
= mProxy
;
135 if ( !*aInstancePtr
)
136 status
= NS_ERROR_OUT_OF_MEMORY
;
139 NS_ADDREF(*aInstancePtr
);
146 NS_IMPL_ISUPPORTS1(nsWeakReference
, nsIWeakReference
)
149 nsWeakReference::QueryReferent( const nsIID
& aIID
, void** aInstancePtr
)
151 return mReferent
? mReferent
->QueryInterface(aIID
, aInstancePtr
) : NS_ERROR_NULL_POINTER
;
155 nsSupportsWeakReference::ClearWeakReferences()
159 mProxy
->NoticeReferentDestruction();