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 mozilla.org code.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
26 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "nsEnumeratorUtils.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | nsArrayEnumerator::nsArrayEnumerator(nsISupportsArray* aValueArray)
|
---|
42 | : mValueArray(aValueArray),
|
---|
43 | mIndex(0)
|
---|
44 | {
|
---|
45 | NS_IF_ADDREF(mValueArray);
|
---|
46 | }
|
---|
47 |
|
---|
48 | nsArrayEnumerator::~nsArrayEnumerator(void)
|
---|
49 | {
|
---|
50 | NS_IF_RELEASE(mValueArray);
|
---|
51 | }
|
---|
52 |
|
---|
53 | NS_IMPL_ISUPPORTS1(nsArrayEnumerator, nsISimpleEnumerator)
|
---|
54 |
|
---|
55 | NS_IMETHODIMP
|
---|
56 | nsArrayEnumerator::HasMoreElements(PRBool* aResult)
|
---|
57 | {
|
---|
58 | NS_PRECONDITION(aResult != 0, "null ptr");
|
---|
59 | if (! aResult)
|
---|
60 | return NS_ERROR_NULL_POINTER;
|
---|
61 |
|
---|
62 | if (!mValueArray) {
|
---|
63 | *aResult = PR_FALSE;
|
---|
64 | return NS_OK;
|
---|
65 | }
|
---|
66 |
|
---|
67 | PRUint32 cnt;
|
---|
68 | nsresult rv = mValueArray->Count(&cnt);
|
---|
69 | if (NS_FAILED(rv)) return rv;
|
---|
70 | *aResult = (mIndex < (PRInt32) cnt);
|
---|
71 | return NS_OK;
|
---|
72 | }
|
---|
73 |
|
---|
74 | NS_IMETHODIMP
|
---|
75 | nsArrayEnumerator::GetNext(nsISupports** aResult)
|
---|
76 | {
|
---|
77 | NS_PRECONDITION(aResult != 0, "null ptr");
|
---|
78 | if (! aResult)
|
---|
79 | return NS_ERROR_NULL_POINTER;
|
---|
80 |
|
---|
81 | if (!mValueArray) {
|
---|
82 | *aResult = nsnull;
|
---|
83 | return NS_OK;
|
---|
84 | }
|
---|
85 |
|
---|
86 | PRUint32 cnt;
|
---|
87 | nsresult rv = mValueArray->Count(&cnt);
|
---|
88 | if (NS_FAILED(rv)) return rv;
|
---|
89 | if (mIndex >= (PRInt32) cnt)
|
---|
90 | return NS_ERROR_UNEXPECTED;
|
---|
91 |
|
---|
92 | *aResult = mValueArray->ElementAt(mIndex++);
|
---|
93 | return NS_OK;
|
---|
94 | }
|
---|
95 |
|
---|
96 | extern NS_COM nsresult
|
---|
97 | NS_NewArrayEnumerator(nsISimpleEnumerator* *result,
|
---|
98 | nsISupportsArray* array)
|
---|
99 | {
|
---|
100 | nsArrayEnumerator* enumer = new nsArrayEnumerator(array);
|
---|
101 | if (enumer == nsnull)
|
---|
102 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
103 | *result = enumer;
|
---|
104 | NS_ADDREF(*result);
|
---|
105 | return NS_OK;
|
---|
106 | }
|
---|
107 |
|
---|
108 | ////////////////////////////////////////////////////////////////////////////////
|
---|
109 |
|
---|
110 | nsSingletonEnumerator::nsSingletonEnumerator(nsISupports* aValue)
|
---|
111 | : mValue(aValue)
|
---|
112 | {
|
---|
113 | NS_IF_ADDREF(mValue);
|
---|
114 | mConsumed = (mValue ? PR_FALSE : PR_TRUE);
|
---|
115 | }
|
---|
116 |
|
---|
117 | nsSingletonEnumerator::~nsSingletonEnumerator()
|
---|
118 | {
|
---|
119 | NS_IF_RELEASE(mValue);
|
---|
120 | }
|
---|
121 |
|
---|
122 | NS_IMPL_ISUPPORTS1(nsSingletonEnumerator, nsISimpleEnumerator)
|
---|
123 |
|
---|
124 | NS_IMETHODIMP
|
---|
125 | nsSingletonEnumerator::HasMoreElements(PRBool* aResult)
|
---|
126 | {
|
---|
127 | NS_PRECONDITION(aResult != 0, "null ptr");
|
---|
128 | if (! aResult)
|
---|
129 | return NS_ERROR_NULL_POINTER;
|
---|
130 |
|
---|
131 | *aResult = !mConsumed;
|
---|
132 | return NS_OK;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | NS_IMETHODIMP
|
---|
137 | nsSingletonEnumerator::GetNext(nsISupports** aResult)
|
---|
138 | {
|
---|
139 | NS_PRECONDITION(aResult != 0, "null ptr");
|
---|
140 | if (! aResult)
|
---|
141 | return NS_ERROR_NULL_POINTER;
|
---|
142 |
|
---|
143 | if (mConsumed)
|
---|
144 | return NS_ERROR_UNEXPECTED;
|
---|
145 |
|
---|
146 | mConsumed = PR_TRUE;
|
---|
147 |
|
---|
148 | *aResult = mValue;
|
---|
149 | NS_ADDREF(*aResult);
|
---|
150 | return NS_OK;
|
---|
151 | }
|
---|
152 |
|
---|
153 | extern "C" NS_COM nsresult
|
---|
154 | NS_NewSingletonEnumerator(nsISimpleEnumerator* *result,
|
---|
155 | nsISupports* singleton)
|
---|
156 | {
|
---|
157 | nsSingletonEnumerator* enumer = new nsSingletonEnumerator(singleton);
|
---|
158 | if (enumer == nsnull)
|
---|
159 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
160 | *result = enumer;
|
---|
161 | NS_ADDREF(*result);
|
---|
162 | return NS_OK;
|
---|
163 | }
|
---|
164 |
|
---|
165 | ////////////////////////////////////////////////////////////////////////////////
|
---|
166 |
|
---|
167 | nsUnionEnumerator::nsUnionEnumerator(nsISimpleEnumerator* firstEnumerator,
|
---|
168 | nsISimpleEnumerator* secondEnumerator)
|
---|
169 | : mFirstEnumerator(firstEnumerator),
|
---|
170 | mSecondEnumerator(secondEnumerator),
|
---|
171 | mConsumed(PR_FALSE), mAtSecond(PR_FALSE)
|
---|
172 | {
|
---|
173 | }
|
---|
174 |
|
---|
175 | nsUnionEnumerator::~nsUnionEnumerator()
|
---|
176 | {
|
---|
177 | }
|
---|
178 |
|
---|
179 | NS_IMPL_ISUPPORTS1(nsUnionEnumerator, nsISimpleEnumerator)
|
---|
180 |
|
---|
181 | NS_IMETHODIMP
|
---|
182 | nsUnionEnumerator::HasMoreElements(PRBool* aResult)
|
---|
183 | {
|
---|
184 | NS_PRECONDITION(aResult != 0, "null ptr");
|
---|
185 | if (! aResult)
|
---|
186 | return NS_ERROR_NULL_POINTER;
|
---|
187 |
|
---|
188 | nsresult rv;
|
---|
189 |
|
---|
190 | if (mConsumed) {
|
---|
191 | *aResult = PR_FALSE;
|
---|
192 | return NS_OK;
|
---|
193 | }
|
---|
194 |
|
---|
195 | if (! mAtSecond) {
|
---|
196 | rv = mFirstEnumerator->HasMoreElements(aResult);
|
---|
197 | if (NS_FAILED(rv)) return rv;
|
---|
198 |
|
---|
199 | if (*aResult)
|
---|
200 | return NS_OK;
|
---|
201 |
|
---|
202 | mAtSecond = PR_TRUE;
|
---|
203 | }
|
---|
204 |
|
---|
205 | rv = mSecondEnumerator->HasMoreElements(aResult);
|
---|
206 | if (NS_FAILED(rv)) return rv;
|
---|
207 |
|
---|
208 | if (*aResult)
|
---|
209 | return NS_OK;
|
---|
210 |
|
---|
211 | *aResult = PR_FALSE;
|
---|
212 | mConsumed = PR_TRUE;
|
---|
213 | return NS_OK;
|
---|
214 | }
|
---|
215 |
|
---|
216 | NS_IMETHODIMP
|
---|
217 | nsUnionEnumerator::GetNext(nsISupports** aResult)
|
---|
218 | {
|
---|
219 | NS_PRECONDITION(aResult != 0, "null ptr");
|
---|
220 | if (! aResult)
|
---|
221 | return NS_ERROR_NULL_POINTER;
|
---|
222 |
|
---|
223 | if (mConsumed)
|
---|
224 | return NS_ERROR_UNEXPECTED;
|
---|
225 |
|
---|
226 | if (! mAtSecond)
|
---|
227 | return mFirstEnumerator->GetNext(aResult);
|
---|
228 |
|
---|
229 | return mSecondEnumerator->GetNext(aResult);
|
---|
230 | }
|
---|
231 |
|
---|
232 | extern "C" NS_COM nsresult
|
---|
233 | NS_NewUnionEnumerator(nsISimpleEnumerator* *result,
|
---|
234 | nsISimpleEnumerator* firstEnumerator,
|
---|
235 | nsISimpleEnumerator* secondEnumerator)
|
---|
236 | {
|
---|
237 | *result = nsnull;
|
---|
238 | if (! firstEnumerator) {
|
---|
239 | *result = secondEnumerator;
|
---|
240 | } else if (! secondEnumerator) {
|
---|
241 | *result = firstEnumerator;
|
---|
242 | } else {
|
---|
243 | nsUnionEnumerator* enumer = new nsUnionEnumerator(firstEnumerator, secondEnumerator);
|
---|
244 | if (enumer == nsnull)
|
---|
245 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
246 | *result = enumer;
|
---|
247 | }
|
---|
248 | NS_ADDREF(*result);
|
---|
249 | return NS_OK;
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | ////////////////////////////////////////////////////////////////////////////////
|
---|