michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsMaybeWeakPtr.h" michael@0: michael@0: void* michael@0: nsMaybeWeakPtr_base::GetValueAs(const nsIID &iid) const michael@0: { michael@0: nsresult rv; michael@0: void *ref; michael@0: if (mPtr) { michael@0: rv = mPtr->QueryInterface(iid, &ref); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: return ref; michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr weakRef = do_QueryInterface(mPtr); michael@0: if (weakRef) { michael@0: rv = weakRef->QueryReferent(iid, &ref); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: return ref; michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: nsresult michael@0: NS_AppendWeakElementBase(isupports_array_type *aArray, michael@0: nsISupports *aElement, michael@0: bool aOwnsWeak) michael@0: { michael@0: nsCOMPtr ref; michael@0: if (aOwnsWeak) { michael@0: nsCOMPtr weakRef; michael@0: weakRef = do_GetWeakReference(aElement); michael@0: reinterpret_cast*>(&weakRef)->swap(ref); michael@0: } else { michael@0: ref = aElement; michael@0: } michael@0: michael@0: if (aArray->IndexOf(ref) != aArray->NoIndex) { michael@0: return NS_ERROR_INVALID_ARG; // already present michael@0: } michael@0: if (!aArray->AppendElement(ref)) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: NS_RemoveWeakElementBase(isupports_array_type *aArray, michael@0: nsISupports *aElement) michael@0: { michael@0: uint32_t index = aArray->IndexOf(aElement); michael@0: if (index != aArray->NoIndex) { michael@0: aArray->RemoveElementAt(index); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Don't use do_GetWeakReference; it should only be called if we know michael@0: // the object supports weak references. michael@0: nsCOMPtr supWeakRef = do_QueryInterface(aElement); michael@0: NS_ENSURE_TRUE(supWeakRef, NS_ERROR_INVALID_ARG); michael@0: michael@0: nsCOMPtr weakRef; michael@0: nsresult rv = supWeakRef->GetWeakReference(getter_AddRefs(weakRef)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: index = aArray->IndexOf(weakRef); michael@0: if (index == aArray->NoIndex) { michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: aArray->RemoveElementAt(index); michael@0: return NS_OK; michael@0: }