Files
palemoon27/toolkit/components/places/nsMaybeWeakPtr.h
T
roytam1 5cdc3234f4 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1189369, part 1 - Remove trailing whitespace from some nsNavHistory* files. r=mak (317dd5937)
- Bug 1074416 - Don't AddRef or Release nav history result observers during CC traversal. r=smaug (2ce1e8cc6)
- Bug 1189369, part 2 - Don't implicitly convert nsMaybeWeakPtr to an nsCOMPtr. r=mak (c4c3261e5)
- Bug 1189369, part 3 - Inline nsMaybeWeakPtr_base. r=mak (efdb4fd76)
- Bug 1189369, part 4 - Get rid of some old style QIs from nsMaybeWeakPtrArray. r=mak (fa534f76c)
- Bug 1189369, part 5 - Use the nice cycle collector macro template for nsNavHistoryResult. r=mak (f1fafbdc9)
- Bug 1189369, part 6 - Inline AppendWeakElement and RemoveWeakElement. r=mak (514efe9f8)
- Bug 1189369, part 7 - Use nicer nsTArray functions in nsMaybeWeakPtrArray. r=mak (38384b1a1)
- Bug 1189369, part 8 - Clean up some minor style issues in nsMaybeWeakPtr.h. r=mak (3f37712f9)
- Bug 1189369, part 9 - Use early return in nsMaybeWeakPtr<T>::GetValue(). r=mak (5606a69c5)
- Bug 1180498 - Tolerate a different major libGL.so on NetBSD as well r=jgilbert (4f7c00f01)
- pointer style (44d4789bd)
- Bug 1182428 - Refactor bytecode compilation r=luke (790c13aa2)
- Bug 1163851 - Pass the correct |InHandling| argument when parsing the initializer in a LexicalDeclaration in a for-in loop. r=efaust (e6b891d53)
- reapply Bug 1163851 - Remove the remaining two tests of |pc->parsingForInit| (c59d57225)
- Bug 1180498 - Tolerate a different major libGL.so on NetBSD as well r=jgilbert (9463d2aea)
- Bug 1189282 - Refactor parser BindData class r=efaust (4dd897771)
- Bug 1179063 - Hook up the static scope chain in the Parser and replace SharedContext walking with scope walking. (r=efaust) (70b8fdd1e)
2021-08-26 12:24:33 +08:00

146 lines
3.9 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef nsMaybeWeakPtr_h_
#define nsMaybeWeakPtr_h_
#include "mozilla/Attributes.h"
#include "nsCOMPtr.h"
#include "nsWeakReference.h"
#include "nsTArray.h"
#include "nsCycleCollectionNoteChild.h"
// nsMaybeWeakPtr is a helper object to hold a strong-or-weak reference
// to the template class. It's pretty minimal, but sufficient.
template<class T>
class nsMaybeWeakPtr
{
public:
MOZ_IMPLICIT nsMaybeWeakPtr(nsISupports* aRef) : mPtr(aRef) {}
MOZ_IMPLICIT nsMaybeWeakPtr(const nsCOMPtr<nsIWeakReference>& aRef) : mPtr(aRef) {}
MOZ_IMPLICIT nsMaybeWeakPtr(const nsCOMPtr<T>& aRef) : mPtr(aRef) {}
bool operator==(const nsMaybeWeakPtr<T> &other) const {
return mPtr == other.mPtr;
}
nsISupports* GetRawValue() const { return mPtr.get(); }
const nsCOMPtr<T> GetValue() const;
private:
nsCOMPtr<nsISupports> mPtr;
};
// nsMaybeWeakPtrArray is an array of MaybeWeakPtr objects, that knows how to
// grab a weak reference to a given object if requested. It only allows a
// given object to appear in the array once.
template<class T>
class nsMaybeWeakPtrArray : public nsTArray<nsMaybeWeakPtr<T>>
{
typedef nsTArray<nsMaybeWeakPtr<T>> MaybeWeakArray;
public:
nsresult AppendWeakElement(T* aElement, bool aOwnsWeak)
{
nsCOMPtr<nsISupports> ref;
if (aOwnsWeak) {
ref = do_GetWeakReference(aElement);
} else {
ref = aElement;
}
if (MaybeWeakArray::Contains(ref.get())) {
return NS_ERROR_INVALID_ARG;
}
if (!MaybeWeakArray::AppendElement(ref)) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
nsresult RemoveWeakElement(T* aElement)
{
if (MaybeWeakArray::RemoveElement(aElement)) {
return NS_OK;
}
// Don't use do_GetWeakReference; it should only be called if we know
// the object supports weak references.
nsCOMPtr<nsISupportsWeakReference> supWeakRef = do_QueryInterface(aElement);
NS_ENSURE_TRUE(supWeakRef, NS_ERROR_INVALID_ARG);
nsCOMPtr<nsIWeakReference> weakRef;
nsresult rv = supWeakRef->GetWeakReference(getter_AddRefs(weakRef));
NS_ENSURE_SUCCESS(rv, rv);
if (MaybeWeakArray::RemoveElement(weakRef)) {
return NS_OK;
}
return NS_ERROR_INVALID_ARG;
}
};
template<class T>
const nsCOMPtr<T>
nsMaybeWeakPtr<T>::GetValue() const
{
if (!mPtr) {
return nullptr;
}
nsresult rv;
nsCOMPtr<T> ref = do_QueryInterface(mPtr, &rv);
if (NS_SUCCEEDED(rv)) {
return ref;
}
nsCOMPtr<nsIWeakReference> weakRef = do_QueryInterface(mPtr);
if (weakRef) {
ref = do_QueryReferent(weakRef, &rv);
if (NS_SUCCEEDED(rv)) {
return ref;
}
}
return nullptr;
}
template <typename T>
inline void
ImplCycleCollectionUnlink(nsMaybeWeakPtrArray<T>& aField)
{
aField.Clear();
}
template <typename E>
inline void
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
nsMaybeWeakPtrArray<E>& aField,
const char* aName,
uint32_t aFlags = 0)
{
aFlags |= CycleCollectionEdgeNameArrayFlag;
size_t length = aField.Length();
for (size_t i = 0; i < length; ++i) {
CycleCollectionNoteChild(aCallback, aField[i].GetRawValue(), aName, aFlags);
}
}
// Call a method on each element in the array, but only if the element is
// non-null.
#define ENUMERATE_WEAKARRAY(array, type, method) \
for (uint32_t array_idx = 0; array_idx < array.Length(); ++array_idx) { \
const nsCOMPtr<type> &e = array.ElementAt(array_idx).GetValue(); \
if (e) \
e->method; \
}
#endif