1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "mozilla/Attributes.h"
11 #include "nsWeakReference.h"
14 class nsWeakReference MOZ_FINAL
: public nsIWeakReference
20 // nsIWeakReference...
21 NS_DECL_NSIWEAKREFERENCE
22 virtual size_t SizeOfOnlyThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
25 friend class nsSupportsWeakReference
;
27 explicit nsWeakReference(nsSupportsWeakReference
* aReferent
)
28 : mReferent(aReferent
)
29 // ...I can only be constructed by an |nsSupportsWeakReference|
34 // ...I will only be destroyed by calling |delete| myself.
37 mReferent
->NoticeProxyDestruction();
42 NoticeReferentDestruction()
43 // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
48 nsSupportsWeakReference
* mReferent
;
52 nsQueryReferent::operator()(const nsIID
& aIID
, void** aAnswer
) const
56 if (NS_FAILED(status
= mWeakPtr
->QueryReferent(aIID
, aAnswer
))) {
60 status
= NS_ERROR_NULL_POINTER
;
69 nsIWeakReference
* // or else |already_AddRefed<nsIWeakReference>|
70 NS_GetWeakReference(nsISupports
* aInstancePtr
, nsresult
* aErrorPtr
)
74 nsIWeakReference
* result
= nullptr;
77 nsCOMPtr
<nsISupportsWeakReference
> factoryPtr
=
78 do_QueryInterface(aInstancePtr
, &status
);
80 status
= factoryPtr
->GetWeakReference(&result
);
82 // else, |status| has already been set by |do_QueryInterface|
84 status
= NS_ERROR_NULL_POINTER
;
94 nsSupportsWeakReference::GetWeakReference(nsIWeakReference
** aInstancePtr
)
97 return NS_ERROR_NULL_POINTER
;
101 mProxy
= new nsWeakReference(this);
103 *aInstancePtr
= mProxy
;
106 if (!*aInstancePtr
) {
107 status
= NS_ERROR_OUT_OF_MEMORY
;
109 NS_ADDREF(*aInstancePtr
);
116 NS_IMPL_ISUPPORTS(nsWeakReference
, nsIWeakReference
)
119 nsWeakReference::QueryReferent(const nsIID
& aIID
, void** aInstancePtr
)
121 return mReferent
? mReferent
->QueryInterface(aIID
, aInstancePtr
) :
122 NS_ERROR_NULL_POINTER
;
126 nsWeakReference::SizeOfOnlyThis(mozilla::MallocSizeOf aMallocSizeOf
) const
128 return aMallocSizeOf(this);
132 nsSupportsWeakReference::ClearWeakReferences()
135 mProxy
->NoticeReferentDestruction();