1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/ds/nsArray.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,208 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsArray.h" 1.10 +#include "nsArrayEnumerator.h" 1.11 +#include "nsIWeakReference.h" 1.12 +#include "nsIWeakReferenceUtils.h" 1.13 +#include "nsThreadUtils.h" 1.14 + 1.15 +// used by IndexOf() 1.16 +struct findIndexOfClosure 1.17 +{ 1.18 + nsISupports *targetElement; 1.19 + uint32_t startIndex; 1.20 + uint32_t resultIndex; 1.21 +}; 1.22 + 1.23 +static bool FindElementCallback(void* aElement, void* aClosure); 1.24 + 1.25 +NS_INTERFACE_MAP_BEGIN(nsArray) 1.26 + NS_INTERFACE_MAP_ENTRY(nsIArray) 1.27 + NS_INTERFACE_MAP_ENTRY(nsIMutableArray) 1.28 + NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMutableArray) 1.29 +NS_INTERFACE_MAP_END 1.30 + 1.31 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsArrayCC) 1.32 + NS_INTERFACE_MAP_ENTRY(nsIArray) 1.33 + NS_INTERFACE_MAP_ENTRY(nsIMutableArray) 1.34 + NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMutableArray) 1.35 +NS_INTERFACE_MAP_END 1.36 + 1.37 +nsArray::~nsArray() 1.38 +{ 1.39 + Clear(); 1.40 +} 1.41 + 1.42 + 1.43 +NS_IMPL_ADDREF(nsArray) 1.44 +NS_IMPL_RELEASE(nsArray) 1.45 + 1.46 +NS_IMPL_CYCLE_COLLECTION_CLASS(nsArrayCC) 1.47 + 1.48 +NS_IMPL_CYCLE_COLLECTING_ADDREF(nsArrayCC) 1.49 +NS_IMPL_CYCLE_COLLECTING_RELEASE(nsArrayCC) 1.50 + 1.51 +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsArrayCC) 1.52 + tmp->Clear(); 1.53 +NS_IMPL_CYCLE_COLLECTION_UNLINK_END 1.54 +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsArrayCC) 1.55 + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mArray) 1.56 +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END 1.57 + 1.58 +NS_IMETHODIMP 1.59 +nsArray::GetLength(uint32_t* aLength) 1.60 +{ 1.61 + *aLength = mArray.Count(); 1.62 + return NS_OK; 1.63 +} 1.64 + 1.65 +NS_IMETHODIMP 1.66 +nsArray::QueryElementAt(uint32_t aIndex, 1.67 + const nsIID& aIID, 1.68 + void ** aResult) 1.69 +{ 1.70 + nsISupports * obj = mArray.SafeObjectAt(aIndex); 1.71 + if (!obj) return NS_ERROR_ILLEGAL_VALUE; 1.72 + 1.73 + // no need to worry about a leak here, because SafeObjectAt() 1.74 + // doesn't addref its result 1.75 + return obj->QueryInterface(aIID, aResult); 1.76 +} 1.77 + 1.78 +NS_IMETHODIMP 1.79 +nsArray::IndexOf(uint32_t aStartIndex, nsISupports* aElement, 1.80 + uint32_t* aResult) 1.81 +{ 1.82 + // optimize for the common case by forwarding to mArray 1.83 + if (aStartIndex == 0) { 1.84 + uint32_t idx = mArray.IndexOf(aElement); 1.85 + if (idx == UINT32_MAX) 1.86 + return NS_ERROR_FAILURE; 1.87 + 1.88 + *aResult = idx; 1.89 + return NS_OK; 1.90 + } 1.91 + 1.92 + findIndexOfClosure closure = { aElement, aStartIndex, 0 }; 1.93 + bool notFound = mArray.EnumerateForwards(FindElementCallback, &closure); 1.94 + if (notFound) 1.95 + return NS_ERROR_FAILURE; 1.96 + 1.97 + *aResult = closure.resultIndex; 1.98 + return NS_OK; 1.99 +} 1.100 + 1.101 +NS_IMETHODIMP 1.102 +nsArray::Enumerate(nsISimpleEnumerator **aResult) 1.103 +{ 1.104 + return NS_NewArrayEnumerator(aResult, static_cast<nsIArray*>(this)); 1.105 +} 1.106 + 1.107 +// nsIMutableArray implementation 1.108 + 1.109 +NS_IMETHODIMP 1.110 +nsArray::AppendElement(nsISupports* aElement, bool aWeak) 1.111 +{ 1.112 + bool result; 1.113 + if (aWeak) { 1.114 + nsCOMPtr<nsIWeakReference> elementRef = do_GetWeakReference(aElement); 1.115 + NS_ASSERTION(elementRef, "AppendElement: Trying to use weak references on an object that doesn't support it"); 1.116 + if (!elementRef) 1.117 + return NS_ERROR_FAILURE; 1.118 + result = mArray.AppendObject(elementRef); 1.119 + } 1.120 + 1.121 + else { 1.122 + // add the object directly 1.123 + result = mArray.AppendObject(aElement); 1.124 + } 1.125 + return result ? NS_OK : NS_ERROR_FAILURE; 1.126 +} 1.127 + 1.128 +NS_IMETHODIMP 1.129 +nsArray::RemoveElementAt(uint32_t aIndex) 1.130 +{ 1.131 + bool result = mArray.RemoveObjectAt(aIndex); 1.132 + return result ? NS_OK : NS_ERROR_FAILURE; 1.133 +} 1.134 + 1.135 +NS_IMETHODIMP 1.136 +nsArray::InsertElementAt(nsISupports* aElement, uint32_t aIndex, bool aWeak) 1.137 +{ 1.138 + nsCOMPtr<nsISupports> elementRef; 1.139 + if (aWeak) { 1.140 + elementRef = do_GetWeakReference(aElement); 1.141 + NS_ASSERTION(elementRef, "InsertElementAt: Trying to use weak references on an object that doesn't support it"); 1.142 + if (!elementRef) 1.143 + return NS_ERROR_FAILURE; 1.144 + } else { 1.145 + elementRef = aElement; 1.146 + } 1.147 + bool result = mArray.InsertObjectAt(elementRef, aIndex); 1.148 + return result ? NS_OK : NS_ERROR_FAILURE; 1.149 +} 1.150 + 1.151 +NS_IMETHODIMP 1.152 +nsArray::ReplaceElementAt(nsISupports* aElement, uint32_t aIndex, bool aWeak) 1.153 +{ 1.154 + nsCOMPtr<nsISupports> elementRef; 1.155 + if (aWeak) { 1.156 + elementRef = do_GetWeakReference(aElement); 1.157 + NS_ASSERTION(elementRef, "ReplaceElementAt: Trying to use weak references on an object that doesn't support it"); 1.158 + if (!elementRef) 1.159 + return NS_ERROR_FAILURE; 1.160 + } else { 1.161 + elementRef = aElement; 1.162 + } 1.163 + bool result = mArray.ReplaceObjectAt(elementRef, aIndex); 1.164 + return result ? NS_OK : NS_ERROR_FAILURE; 1.165 +} 1.166 + 1.167 +NS_IMETHODIMP 1.168 +nsArray::Clear() 1.169 +{ 1.170 + mArray.Clear(); 1.171 + return NS_OK; 1.172 +} 1.173 + 1.174 +// 1.175 +// static helper routines 1.176 +// 1.177 +bool 1.178 +FindElementCallback(void *aElement, void* aClosure) 1.179 +{ 1.180 + findIndexOfClosure* closure = 1.181 + static_cast<findIndexOfClosure*>(aClosure); 1.182 + 1.183 + nsISupports* element = 1.184 + static_cast<nsISupports*>(aElement); 1.185 + 1.186 + // don't start searching until we're past the startIndex 1.187 + if (closure->resultIndex >= closure->startIndex && 1.188 + element == closure->targetElement) { 1.189 + return false; // stop! We found it 1.190 + } 1.191 + closure->resultIndex++; 1.192 + 1.193 + return true; 1.194 +} 1.195 + 1.196 +nsresult 1.197 +nsArray::XPCOMConstructor(nsISupports *aOuter, const nsIID& aIID, void **aResult) 1.198 +{ 1.199 + if (aOuter) 1.200 + return NS_ERROR_NO_AGGREGATION; 1.201 + 1.202 + nsCOMPtr<nsIMutableArray> inst = Create(); 1.203 + return inst->QueryInterface(aIID, aResult); 1.204 +} 1.205 + 1.206 +already_AddRefed<nsIMutableArray> 1.207 +nsArray::Create() 1.208 +{ 1.209 + nsCOMPtr<nsIMutableArray> inst = NS_IsMainThread() ? new nsArrayCC : new nsArray; 1.210 + return inst.forget(); 1.211 +}