Bug 1932613 - temporarily disable browser_ml_end_to_end.js for permanent failures...
[gecko.git] / xpcom / base / nsMaybeWeakPtr.h
blob0566624265378d318de82a5072c28a2b26cc2a98
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"
11 #include "nsCOMPtr.h"
12 #include "nsIWeakReferenceUtils.h"
13 #include "nsTArray.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.
19 template <class T>
20 class nsMaybeWeakPtr {
21 public:
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) {
28 mPtr = aRef;
29 mWeak = false;
30 return *this;
33 nsMaybeWeakPtr<T>& operator=(const nsCOMPtr<nsIWeakReference>& aRef) {
34 mPtr = aRef;
35 mWeak = true;
36 return *this;
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;
48 private:
49 nsCOMPtr<nsISupports> mPtr;
50 bool mWeak;
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.
57 template <class T>
58 class nsMaybeWeakPtrArray : public CopyableTArray<nsMaybeWeakPtr<T>> {
59 typedef nsTArray<nsMaybeWeakPtr<T>> MaybeWeakArray;
61 nsresult SetMaybeWeakPtr(nsMaybeWeakPtr<T>& aRef, T* aElement,
62 bool aOwnsWeak) {
63 nsresult rv = NS_OK;
65 if (aOwnsWeak) {
66 aRef = do_GetWeakReference(aElement, &rv);
67 } else {
68 aRef = aElement;
71 return rv;
74 public:
75 nsresult AppendWeakElement(T* aElement, bool aOwnsWeak) {
76 nsMaybeWeakPtr<T> ref;
77 MOZ_TRY(SetMaybeWeakPtr(ref, aElement, aOwnsWeak));
79 MaybeWeakArray::AppendElement(ref);
80 return NS_OK;
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);
92 return NS_OK;
95 nsresult RemoveWeakElement(T* aElement) {
96 if (MaybeWeakArray::RemoveElement(aElement)) {
97 return NS_OK;
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);
103 if (!supWeakRef) {
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)) {
112 return NS_OK;
115 return NS_ERROR_INVALID_ARG;
119 template <class T>
120 const nsCOMPtr<T> nsMaybeWeakPtr<T>::GetValue() const {
121 if (!mPtr) {
122 return nullptr;
125 nsCOMPtr<T> ref;
126 nsresult rv;
128 if (mWeak) {
129 nsCOMPtr<nsIWeakReference> weakRef = do_QueryInterface(mPtr);
130 if (NS_WARN_IF(!weakRef)) {
131 return nullptr;
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");
136 } else {
137 ref = do_QueryInterface(mPtr, &rv);
138 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
139 "QueryInterface failed with non-null pointer");
141 return ref;
144 template <typename T>
145 inline void ImplCycleCollectionUnlink(nsMaybeWeakPtrArray<T>& aField) {
146 aField.Clear();
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
161 // non-null.
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(); \
166 if (e) e->method; \
169 #endif