2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsMaybeWeakPtr.h"
|
|
|
|
|
|
|
|
void*
|
|
|
|
nsMaybeWeakPtr_base::GetValueAs(const nsIID &iid) const
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
void *ref;
|
|
|
|
if (mPtr) {
|
|
|
|
rv = mPtr->QueryInterface(iid, &ref);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWeakReference> weakRef = do_QueryInterface(mPtr);
|
|
|
|
if (weakRef) {
|
|
|
|
rv = weakRef->QueryReferent(iid, &ref);
|
2008-03-12 15:51:14 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return ref;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2008-03-12 15:51:14 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 18:48:59 -08:00
|
|
|
nsresult
|
|
|
|
NS_AppendWeakElementBase(isupports_array_type *aArray,
|
|
|
|
nsISupports *aElement,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aOwnsWeak)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> ref;
|
|
|
|
if (aOwnsWeak) {
|
|
|
|
nsCOMPtr<nsIWeakReference> weakRef;
|
|
|
|
weakRef = do_GetWeakReference(aElement);
|
2007-07-08 00:08:04 -07:00
|
|
|
reinterpret_cast<nsCOMPtr<nsISupports>*>(&weakRef)->swap(ref);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
ref = aElement;
|
|
|
|
}
|
|
|
|
|
2010-11-08 18:48:59 -08:00
|
|
|
if (aArray->IndexOf(ref) != aArray->NoIndex) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_INVALID_ARG; // already present
|
|
|
|
}
|
2010-11-08 18:48:59 -08:00
|
|
|
if (!aArray->AppendElement(ref)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-08 18:48:59 -08:00
|
|
|
nsresult
|
|
|
|
NS_RemoveWeakElementBase(isupports_array_type *aArray,
|
|
|
|
nsISupports *aElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t index = aArray->IndexOf(aElement);
|
2010-11-08 18:48:59 -08:00
|
|
|
if (index != aArray->NoIndex) {
|
|
|
|
aArray->RemoveElementAt(index);
|
2007-03-22 10:30:00 -07: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);
|
|
|
|
|
2010-11-08 18:48:59 -08:00
|
|
|
index = aArray->IndexOf(weakRef);
|
|
|
|
if (index == aArray->NoIndex) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2010-11-08 18:48:59 -08:00
|
|
|
aArray->RemoveElementAt(index);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|