2006-07-18 19:45:43 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2006-07-18 19:45:43 +04:00
|
|
|
|
|
|
|
#ifndef nsMaybeWeakPtr_h_
|
|
|
|
#define nsMaybeWeakPtr_h_
|
|
|
|
|
2014-09-04 02:25:36 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2006-07-18 19:45:43 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsWeakReference.h"
|
|
|
|
#include "nsTArray.h"
|
2012-11-15 11:32:39 +04:00
|
|
|
#include "nsCycleCollectionNoteChild.h"
|
2006-07-18 19:45:43 +04:00
|
|
|
|
|
|
|
// 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>
|
2015-08-04 23:55:01 +03:00
|
|
|
class nsMaybeWeakPtr
|
2006-07-18 19:45:43 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-08-04 23:55:01 +03:00
|
|
|
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) {}
|
2006-07-18 19:45:43 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool operator==(const nsMaybeWeakPtr<T> &other) const {
|
2006-07-18 19:45:43 +04:00
|
|
|
return mPtr == other.mPtr;
|
|
|
|
}
|
|
|
|
|
2015-07-30 03:36:00 +03:00
|
|
|
nsISupports* GetRawValue() const { return mPtr.get(); }
|
2015-08-04 23:55:01 +03:00
|
|
|
|
2015-08-04 23:55:01 +03:00
|
|
|
const nsCOMPtr<T> GetValue() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsISupports> mPtr;
|
2006-07-18 19:45:43 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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>
|
2015-08-04 23:55:01 +03:00
|
|
|
class nsMaybeWeakPtrArray : public nsTArray<nsMaybeWeakPtr<T>>
|
2006-07-18 19:45:43 +04:00
|
|
|
{
|
2015-08-04 23:55:01 +03:00
|
|
|
typedef nsTArray<nsMaybeWeakPtr<T>> MaybeWeakArray;
|
|
|
|
|
2006-07-18 19:45:43 +04:00
|
|
|
public:
|
2015-08-04 23:55:01 +03:00
|
|
|
nsresult AppendWeakElement(T* aElement, bool aOwnsWeak)
|
2006-07-18 19:45:43 +04:00
|
|
|
{
|
2015-08-04 23:55:01 +03:00
|
|
|
nsCOMPtr<nsISupports> ref;
|
|
|
|
if (aOwnsWeak) {
|
|
|
|
ref = do_GetWeakReference(aElement);
|
|
|
|
} else {
|
|
|
|
ref = aElement;
|
|
|
|
}
|
|
|
|
|
2015-08-04 23:55:01 +03:00
|
|
|
if (MaybeWeakArray::Contains(ref.get())) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2015-08-04 23:55:01 +03:00
|
|
|
}
|
|
|
|
if (!MaybeWeakArray::AppendElement(ref)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2006-07-18 19:45:43 +04:00
|
|
|
}
|
|
|
|
|
2015-08-04 23:55:01 +03:00
|
|
|
nsresult RemoveWeakElement(T* aElement)
|
2006-07-18 19:45:43 +04:00
|
|
|
{
|
2015-08-04 23:55:01 +03:00
|
|
|
if (MaybeWeakArray::RemoveElement(aElement)) {
|
2015-08-04 23:55:01 +03:00
|
|
|
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);
|
|
|
|
|
2015-08-04 23:55:01 +03:00
|
|
|
if (MaybeWeakArray::RemoveElement(weakRef)) {
|
|
|
|
return NS_OK;
|
2015-08-04 23:55:01 +03:00
|
|
|
}
|
|
|
|
|
2015-08-04 23:55:01 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2006-07-18 19:45:43 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-04 23:55:01 +03:00
|
|
|
template<class T>
|
|
|
|
const nsCOMPtr<T>
|
|
|
|
nsMaybeWeakPtr<T>::GetValue() const
|
|
|
|
{
|
2015-08-04 23:55:01 +03:00
|
|
|
if (!mPtr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-04 23:55:01 +03:00
|
|
|
nsresult rv;
|
2015-08-04 23:55:01 +03:00
|
|
|
nsCOMPtr<T> ref = do_QueryInterface(mPtr, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return ref;
|
2015-08-04 23:55:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWeakReference> weakRef = do_QueryInterface(mPtr);
|
|
|
|
if (weakRef) {
|
2015-08-04 23:55:01 +03:00
|
|
|
ref = do_QueryReferent(weakRef, &rv);
|
2015-08-04 23:55:01 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-15 11:32:39 +04:00
|
|
|
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) {
|
2015-08-04 23:55:01 +03:00
|
|
|
CycleCollectionNoteChild(aCallback, aField[i].GetRawValue(), aName, aFlags);
|
2012-11-15 11:32:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-18 19:45:43 +04:00
|
|
|
// Call a method on each element in the array, but only if the element is
|
|
|
|
// non-null.
|
|
|
|
|
|
|
|
#define ENUMERATE_WEAKARRAY(array, type, method) \
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t array_idx = 0; array_idx < array.Length(); ++array_idx) { \
|
2015-08-04 23:55:01 +03:00
|
|
|
const nsCOMPtr<type> &e = array.ElementAt(array_idx).GetValue(); \
|
2006-07-18 19:45:43 +04:00
|
|
|
if (e) \
|
|
|
|
e->method; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|