1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsMaybeWeakPtr_h_
7 #define nsMaybeWeakPtr_h_
9 #include "mozilla/Attributes.h"
10 #include "mozilla/Try.h"
12 #include "nsIWeakReferenceUtils.h"
14 #include "nsCycleCollectionNoteChild.h"
16 // nsMaybeWeakPtr is a helper object to hold a strong-or-weak reference
17 // to the template class. It's pretty minimal, but sufficient.
20 class nsMaybeWeakPtr
{
22 nsMaybeWeakPtr() = default;
23 MOZ_IMPLICIT
nsMaybeWeakPtr(T
* aRef
) : mPtr(aRef
), mWeak(false) {}
24 MOZ_IMPLICIT
nsMaybeWeakPtr(const nsCOMPtr
<nsIWeakReference
>& aRef
)
25 : mPtr(aRef
), mWeak(true) {}
27 nsMaybeWeakPtr
<T
>& operator=(T
* aRef
) {
33 nsMaybeWeakPtr
<T
>& operator=(const nsCOMPtr
<nsIWeakReference
>& aRef
) {
39 bool operator==(const nsMaybeWeakPtr
<T
>& other
) const {
40 return mPtr
== other
.mPtr
;
43 nsISupports
* GetRawValue() const { return mPtr
.get(); }
44 bool IsWeak() const { return mWeak
; }
46 const nsCOMPtr
<T
> GetValue() const;
49 nsCOMPtr
<nsISupports
> mPtr
;
53 // nsMaybeWeakPtrArray is an array of MaybeWeakPtr objects, that knows how to
54 // grab a weak reference to a given object if requested. It only allows a
55 // given object to appear in the array once.
58 class nsMaybeWeakPtrArray
: public CopyableTArray
<nsMaybeWeakPtr
<T
>> {
59 typedef nsTArray
<nsMaybeWeakPtr
<T
>> MaybeWeakArray
;
61 nsresult
SetMaybeWeakPtr(nsMaybeWeakPtr
<T
>& aRef
, T
* aElement
,
66 aRef
= do_GetWeakReference(aElement
, &rv
);
75 nsresult
AppendWeakElement(T
* aElement
, bool aOwnsWeak
) {
76 nsMaybeWeakPtr
<T
> ref
;
77 MOZ_TRY(SetMaybeWeakPtr(ref
, aElement
, aOwnsWeak
));
79 MaybeWeakArray::AppendElement(ref
);
83 nsresult
AppendWeakElementUnlessExists(T
* aElement
, bool aOwnsWeak
) {
84 nsMaybeWeakPtr
<T
> ref
;
85 MOZ_TRY(SetMaybeWeakPtr(ref
, aElement
, aOwnsWeak
));
87 if (MaybeWeakArray::Contains(ref
)) {
88 return NS_ERROR_INVALID_ARG
;
91 MaybeWeakArray::AppendElement(ref
);
95 nsresult
RemoveWeakElement(T
* aElement
) {
96 if (MaybeWeakArray::RemoveElement(aElement
)) {
100 // Don't use do_GetWeakReference; it should only be called if we know
101 // the object supports weak references.
102 nsCOMPtr
<nsISupportsWeakReference
> supWeakRef
= do_QueryInterface(aElement
);
104 return NS_ERROR_INVALID_ARG
;
107 nsCOMPtr
<nsIWeakReference
> weakRef
;
108 nsresult rv
= supWeakRef
->GetWeakReference(getter_AddRefs(weakRef
));
109 NS_ENSURE_SUCCESS(rv
, rv
);
111 if (MaybeWeakArray::RemoveElement(weakRef
)) {
115 return NS_ERROR_INVALID_ARG
;
120 const nsCOMPtr
<T
> nsMaybeWeakPtr
<T
>::GetValue() const {
129 nsCOMPtr
<nsIWeakReference
> weakRef
= do_QueryInterface(mPtr
);
130 if (NS_WARN_IF(!weakRef
)) {
133 ref
= do_QueryReferent(weakRef
, &rv
);
134 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv
) || rv
== NS_ERROR_NULL_POINTER
,
135 "QueryReferent failed with non-null pointer");
137 ref
= do_QueryInterface(mPtr
, &rv
);
138 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv
),
139 "QueryInterface failed with non-null pointer");
144 template <typename T
>
145 inline void ImplCycleCollectionUnlink(nsMaybeWeakPtrArray
<T
>& aField
) {
149 template <typename E
>
150 inline void ImplCycleCollectionTraverse(
151 nsCycleCollectionTraversalCallback
& aCallback
,
152 nsMaybeWeakPtrArray
<E
>& aField
, const char* aName
, uint32_t aFlags
= 0) {
153 aFlags
|= CycleCollectionEdgeNameArrayFlag
;
154 size_t length
= aField
.Length();
155 for (size_t i
= 0; i
< length
; ++i
) {
156 CycleCollectionNoteChild(aCallback
, aField
[i
].GetRawValue(), aName
, aFlags
);
160 // Call a method on each element in the array, but only if the element is
163 #define ENUMERATE_WEAKARRAY(array, type, method) \
164 for (uint32_t array_idx = 0; array_idx < array.Length(); ++array_idx) { \
165 const nsCOMPtr<type>& e = array.ElementAt(array_idx).GetValue(); \