1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is XPCOM Array implementation.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corp.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 2002
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | * Alec Flett <[email protected]>
|
---|
24 | *
|
---|
25 | * Alternatively, the contents of this file may be used under the terms of
|
---|
26 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
28 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
29 | * of those above. If you wish to allow use of your version of this file only
|
---|
30 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
31 | * use your version of this file under the terms of the MPL, indicate your
|
---|
32 | * decision by deleting the provisions above and replace them with the notice
|
---|
33 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
34 | * the provisions above, a recipient may use your version of this file under
|
---|
35 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
36 | *
|
---|
37 | * ***** END LICENSE BLOCK ***** */
|
---|
38 |
|
---|
39 | #include "nsArray.h"
|
---|
40 | #include "nsArrayEnumerator.h"
|
---|
41 | #include "nsWeakReference.h"
|
---|
42 |
|
---|
43 | // used by IndexOf()
|
---|
44 | struct findIndexOfClosure
|
---|
45 | {
|
---|
46 | nsISupports *targetElement;
|
---|
47 | PRUint32 startIndex;
|
---|
48 | PRUint32 resultIndex;
|
---|
49 | };
|
---|
50 |
|
---|
51 | PR_STATIC_CALLBACK(PRBool) FindElementCallback(void* aElement, void* aClosure);
|
---|
52 |
|
---|
53 |
|
---|
54 | NS_IMPL_ISUPPORTS2(nsArray, nsIArray, nsIMutableArray)
|
---|
55 |
|
---|
56 | nsArray::~nsArray()
|
---|
57 | {
|
---|
58 | Clear();
|
---|
59 | }
|
---|
60 |
|
---|
61 | NS_IMETHODIMP
|
---|
62 | nsArray::GetLength(PRUint32* aLength)
|
---|
63 | {
|
---|
64 | *aLength = mArray.Count();
|
---|
65 | return NS_OK;
|
---|
66 | }
|
---|
67 |
|
---|
68 | NS_IMETHODIMP
|
---|
69 | nsArray::QueryElementAt(PRUint32 aIndex,
|
---|
70 | const nsIID& aIID,
|
---|
71 | void ** aResult)
|
---|
72 | {
|
---|
73 | nsISupports * obj = mArray.ObjectAt(aIndex);
|
---|
74 | if (!obj) return NS_ERROR_UNEXPECTED;
|
---|
75 |
|
---|
76 | // no need to worry about a leak here, because ObjectAt() doesn't
|
---|
77 | // addref its result
|
---|
78 | return obj->QueryInterface(aIID, aResult);
|
---|
79 | }
|
---|
80 |
|
---|
81 | NS_IMETHODIMP
|
---|
82 | nsArray::IndexOf(PRUint32 aStartIndex, nsISupports* aElement,
|
---|
83 | PRUint32* aResult)
|
---|
84 | {
|
---|
85 | // optimize for the common case by forwarding to mArray
|
---|
86 | if (aStartIndex == 0) {
|
---|
87 | *aResult = mArray.IndexOf(aElement);
|
---|
88 | if (*aResult == -1)
|
---|
89 | return NS_ERROR_FAILURE;
|
---|
90 | return NS_OK;
|
---|
91 | }
|
---|
92 |
|
---|
93 | findIndexOfClosure closure = { aElement, aStartIndex, 0 };
|
---|
94 | PRBool notFound = mArray.EnumerateForwards(FindElementCallback, &closure);
|
---|
95 | if (notFound)
|
---|
96 | return NS_ERROR_FAILURE;
|
---|
97 |
|
---|
98 | *aResult = closure.resultIndex;
|
---|
99 | return NS_OK;
|
---|
100 | }
|
---|
101 |
|
---|
102 | NS_IMETHODIMP
|
---|
103 | nsArray::Enumerate(nsISimpleEnumerator **aResult)
|
---|
104 | {
|
---|
105 | return NS_NewArrayEnumerator(aResult, NS_STATIC_CAST(nsIArray*, this));
|
---|
106 | }
|
---|
107 |
|
---|
108 | // nsIMutableArray implementation
|
---|
109 |
|
---|
110 | NS_IMETHODIMP
|
---|
111 | nsArray::AppendElement(nsISupports* aElement, PRBool aWeak)
|
---|
112 | {
|
---|
113 | PRBool result;
|
---|
114 | if (aWeak) {
|
---|
115 | nsCOMPtr<nsISupports> elementRef =
|
---|
116 | getter_AddRefs(NS_STATIC_CAST(nsISupports*,
|
---|
117 | NS_GetWeakReference(aElement)));
|
---|
118 | NS_ASSERTION(elementRef, "AppendElement: Trying to use weak references on an object that doesn't support it");
|
---|
119 | if (!elementRef)
|
---|
120 | return NS_ERROR_FAILURE;
|
---|
121 | result = mArray.AppendObject(elementRef);
|
---|
122 | }
|
---|
123 |
|
---|
124 | else {
|
---|
125 | // add the object directly
|
---|
126 | result = mArray.AppendObject(aElement);
|
---|
127 | }
|
---|
128 | return result ? NS_OK : NS_ERROR_FAILURE;
|
---|
129 | }
|
---|
130 |
|
---|
131 | NS_IMETHODIMP
|
---|
132 | nsArray::RemoveElementAt(PRUint32 aIndex)
|
---|
133 | {
|
---|
134 | PRBool result = mArray.RemoveObjectAt(aIndex);
|
---|
135 | return result ? NS_OK : NS_ERROR_FAILURE;
|
---|
136 | }
|
---|
137 |
|
---|
138 | NS_IMETHODIMP
|
---|
139 | nsArray::InsertElementAt(nsISupports* aElement, PRUint32 aIndex, PRBool aWeak)
|
---|
140 | {
|
---|
141 | nsCOMPtr<nsISupports> elementRef;
|
---|
142 | if (aWeak) {
|
---|
143 | elementRef =
|
---|
144 | getter_AddRefs(NS_STATIC_CAST(nsISupports*,
|
---|
145 | NS_GetWeakReference(aElement)));
|
---|
146 | NS_ASSERTION(elementRef, "InsertElementAt: Trying to use weak references on an object that doesn't support it");
|
---|
147 | if (!elementRef)
|
---|
148 | return NS_ERROR_FAILURE;
|
---|
149 | } else {
|
---|
150 | elementRef = aElement;
|
---|
151 | }
|
---|
152 | PRBool result = mArray.InsertObjectAt(elementRef, aIndex);
|
---|
153 | return result ? NS_OK : NS_ERROR_FAILURE;
|
---|
154 | }
|
---|
155 |
|
---|
156 | NS_IMETHODIMP
|
---|
157 | nsArray::Clear()
|
---|
158 | {
|
---|
159 | mArray.Clear();
|
---|
160 | return NS_OK;
|
---|
161 | }
|
---|
162 |
|
---|
163 | //
|
---|
164 | // static helper routines
|
---|
165 | //
|
---|
166 | PRBool
|
---|
167 | FindElementCallback(void *aElement, void* aClosure)
|
---|
168 | {
|
---|
169 | findIndexOfClosure* closure =
|
---|
170 | NS_STATIC_CAST(findIndexOfClosure*, aClosure);
|
---|
171 |
|
---|
172 | nsISupports* element =
|
---|
173 | NS_STATIC_CAST(nsISupports*, aElement);
|
---|
174 |
|
---|
175 | // don't start searching until we're past the startIndex
|
---|
176 | if (closure->resultIndex >= closure->startIndex &&
|
---|
177 | element == closure->targetElement) {
|
---|
178 | return PR_FALSE; // stop! We found it
|
---|
179 | }
|
---|
180 | closure->resultIndex++;
|
---|
181 |
|
---|
182 | return PR_TRUE;
|
---|
183 | }
|
---|
184 |
|
---|
185 | //
|
---|
186 | // do_QueryElementAt helper stuff
|
---|
187 | //
|
---|
188 | nsresult
|
---|
189 | nsQueryArrayElementAt::operator()(const nsIID& aIID, void** aResult) const
|
---|
190 | {
|
---|
191 | nsresult status = mArray
|
---|
192 | ? mArray->QueryElementAt(mIndex, aIID, aResult)
|
---|
193 | : NS_ERROR_NULL_POINTER;
|
---|
194 |
|
---|
195 | if (mErrorPtr)
|
---|
196 | *mErrorPtr = status;
|
---|
197 |
|
---|
198 | return status;
|
---|
199 | }
|
---|
200 |
|
---|
201 | //
|
---|
202 | // exported constructor routines
|
---|
203 | //
|
---|
204 | nsresult
|
---|
205 | NS_NewArray(nsIMutableArray** aResult)
|
---|
206 | {
|
---|
207 | nsArray* arr = new nsArray;
|
---|
208 | if (!arr) return NS_ERROR_OUT_OF_MEMORY;
|
---|
209 |
|
---|
210 | *aResult = NS_STATIC_CAST(nsIMutableArray*,arr);
|
---|
211 | NS_ADDREF(*aResult);
|
---|
212 |
|
---|
213 | return NS_OK;
|
---|
214 | }
|
---|
215 |
|
---|
216 | nsresult
|
---|
217 | NS_NewArray(nsIMutableArray** aResult, const nsCOMArray_base& aBaseArray)
|
---|
218 | {
|
---|
219 | nsArray* arr = new nsArray(aBaseArray);
|
---|
220 | if (!arr) return NS_ERROR_OUT_OF_MEMORY;
|
---|
221 |
|
---|
222 | *aResult = NS_STATIC_CAST(nsIMutableArray*, arr);
|
---|
223 | NS_ADDREF(*aResult);
|
---|
224 |
|
---|
225 | return NS_OK;
|
---|
226 | }
|
---|