diff --git a/mozilla/xpcom/ds/nsArray.cpp b/mozilla/xpcom/ds/nsArray.cpp new file mode 100644 index 00000000000..53b8236cab1 --- /dev/null +++ b/mozilla/xpcom/ds/nsArray.cpp @@ -0,0 +1,142 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsArray.h" + +// used by IndexOf() +struct findIndexOfClosure +{ + nsISupports *targetElement; + PRUint32 startIndex; + PRUint32 resultIndex; +}; + +NS_IMPL_ISUPPORTS2(nsArray, nsIArray, nsIMutableArray) + +nsArray::~nsArray() +{ + Clear(); +} + +NS_IMETHODIMP +nsArray::GetLength(PRUint32* aLength) +{ + *aLength = mArray.Count(); + return NS_OK; +} + +NS_IMETHODIMP +nsArray::QueryElementAt(PRUint32 aIndex, + const nsIID& aIID, + void ** aResult) +{ + nsISupports * obj = mArray.ObjectAt(aIndex); + if (!obj) return NS_ERROR_UNEXPECTED; + + return obj->QueryInterface(aIID, aResult); +} + +NS_IMETHODIMP +nsArray::IndexOf(PRUint32 aStartIndex, nsISupports* aElement, + PRUint32* aResult) +{ + if (aStartIndex == 0) { + *aResult = mArray.IndexOf(aElement); + if (*aResult == -1) + return NS_ERROR_FAILURE; + return NS_OK; + } + + findIndexOfClosure closure = { aElement, aStartIndex, 0 }; + PRBool notFound = mArray.EnumerateForwards(FindElementCallback, &closure); + if (notFound) + return NS_ERROR_FAILURE; + + *aResult = closure.resultIndex; + return NS_OK; +} + +NS_IMETHODIMP +nsArray::Enumerate(nsISimpleEnumerator **aResult) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +// nsIMutableArray implementation + +NS_IMETHODIMP +nsArray::AppendElement(nsISupports* aElement) +{ + PRBool result = mArray.AppendObject(aElement); + return result ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +nsArray::RemoveElementAt(PRUint32 aIndex) +{ + PRBool result = mArray.RemoveObjectAt(aIndex); + return result ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +nsArray::InsertElementAt(nsISupports* element, PRUint32 aIndex) +{ + PRBool result = mArray.InsertObjectAt(element, aIndex); + return result ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +nsArray::Clear() +{ + mArray.Clear(); + return NS_OK; +} + +nsArray::FindElementCallback(void *aElement, void* aClosure) +{ + findIndexOfClosure* closure = + NS_STATIC_CAST(findIndexOfClosure*, aClosure); + + // don't start searching until we're past the startIndex + if (closure->resultIndex >= closure->startIndex && + aElement == closure->targetElement) { + return PR_FALSE; // stop! We found it + } + closure->resultIndex++; + + return PR_TRUE; +} diff --git a/mozilla/xpcom/ds/nsArray.h b/mozilla/xpcom/ds/nsArray.h new file mode 100644 index 00000000000..bc4c2f98bb7 --- /dev/null +++ b/mozilla/xpcom/ds/nsArray.h @@ -0,0 +1,75 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsArray_h__ +#define nsArray_h__ + +#include "nsIArray.h" +#include "nsCOMArray.h" + +#define NS_ARRAY_CLASSNAME \ + "nsIArray implementation" + +// {35C66FD1-95E9-4e0a-80C5-C3BD2B375481} +#define NS_ARRAY_CID \ +{ 0x35c66fd1, 0x95e9, 0x4e0a, \ + { 0x80, 0xc5, 0xc3, 0xbd, 0x2b, 0x37, 0x54, 0x81 } } + +#define NS_ARRAY_CONTRACTID \ + "@mozilla.org/array;1" + +// adapter class to map nsIArray->nsCOMArray +// do NOT declare this as a stack or member variable, use +// nsCOMArray instead +class nsArray : public nsIMutableArray +{ +public: + nsArray() { NS_INIT_ISUPPORTS(); } + virtual ~nsArray(); + + NS_DECL_ISUPPORTS + NS_DECL_NSIARRAY + NS_DECL_NSIMUTABLEARRAY + +private: + static PRBool FindElementCallback(void* aElement, void* aClosure); + nsCOMArray mArray; +}; + + + +#endif diff --git a/mozilla/xpcom/ds/nsCOMArray.h b/mozilla/xpcom/ds/nsCOMArray.h new file mode 100644 index 00000000000..c0dcef9f466 --- /dev/null +++ b/mozilla/xpcom/ds/nsCOMArray.h @@ -0,0 +1,140 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsCOMArray_h__ +#define nsCOMArray_h__ + +#include "nsVoidArray.h" +#include "nsISupports.h" + +// a non-XPCOM, refcounting array of XPCOM objects +// used as a member variable or stack variable - this object is NOT +// refcounted, but the objects that it holds are +template +class NS_COM nsCOMArray +{ + public: + nsCOMArray() {} + nsCOMArray(PRInt32 aCount) : mArray(aCount) {} + + ~nsCOMArray() {} + + // these do NOT refcount on the way out, for speed + T* ObjectAt(PRInt32 aIndex) const { + return NS_STATIC_CAST(T*,mArray.ElementAt(aIndex)); + } + + T* operator[](PRInt32 aIndex) const { + return ObjectAt(aIndex); + } + + PRInt32 IndexOf(T* aObject) { + return mArray.IndexOf(aObject); + } + + PRBool InsertObjectAt(T* aObject, PRInt32 aIndex) { + PRBool result = mArray.InsertElementAt(aObject, aIndex); + if (result) + NS_ADDREF(aObject); + return result; + } + + PRBool ReplaceObjectAt(T* aObject, PRInt32 aIndex) { + T *oldObject = ObjectAt(aIndex); + NS_IF_RELEASE(oldObject); + PRBool result = mArray.ReplaceElementAt(aObject, aIndex); + if (result) + NS_ADDREF(aObject); + return result; + } + + // override nsVoidArray stuff so that they can be accessed by + // other methods + PRInt32 Count() const { + return mArray.Count(); + } + + void Clear() { + mArray.EnumerateForwards(ClearObjectsCallback, nsnull); + mArray.Clear(); + } + + PRBool EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData) { + return mArray.EnumerateForwards(aFunc, aData); + } + + PRBool AppendObject(T *aObject) { + PRBool result = InsertObjectAt(aObject, Count()); + if (result) + NS_ADDREF(aObject); + return result; + } + + PRBool RemoveObject(T *aObject) { + PRBool result = mArray.RemoveElement(aObject); + if (result) + NS_RELEASE(aObject); + return result; + } + + PRBool RemoveObjectAt(PRInt32 aIndex) { + T* element = ObjectAt(aIndex); + if (element) { + PRBool result = mArray.RemoveElementAt(aIndex); + if (result) + NS_RELEASE(element); + return result; + } + return PR_FALSE; + } + + + private: + + static PRBool ClearObjectsCallback(void *aElement, void*) { + T* element = NS_STATIC_CAST(nsISupports*, aElement); + NS_RELEASE(element); + return PR_TRUE; + } + + nsCOMArray(const nsCOMArray& other); + nsCOMArray& operator=(const nsCOMArray& other); + nsVoidArray mArray; +}; + + +#endif diff --git a/mozilla/xpcom/ds/nsIArray.idl b/mozilla/xpcom/ds/nsIArray.idl index 20012a8d8fd..fbc2eee635a 100644 --- a/mozilla/xpcom/ds/nsIArray.idl +++ b/mozilla/xpcom/ds/nsIArray.idl @@ -63,7 +63,7 @@ interface nsIArray : nsISupports /** * number of elements in the array */ - attribute unsigned long length; + readonly attribute unsigned long length; /** * Retrieve a specific element of the array @@ -113,7 +113,7 @@ interface nsIMutableArray : nsIArray /** * */ - void insertElementAt(in unsigned long index, in nsISupports element); + void insertElementAt(in nsISupports element, in unsigned long index); /** * clear the entire array