On x86 compilers without fastcall, simulate it when invoking traces and un-simulate...
[wine-gecko.git] / xpcom / glue / nsEnumeratorUtils.cpp
blob6b4fcfe9ab790950b79d91561ad48aaa3990f7d4
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
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/
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.
15 * The Original Code is mozilla.org code.
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.
22 * Contributor(s):
23 * Benjamin Smedberg <benjamin@smedbergs.us>
25 * Code moved from nsEmptyEnumerator.cpp:
26 * L. David Baron <dbaron@dbaron.org>
27 * Pierre Phaneuf <pp@ludusdesign.com>
29 * Alternatively, the contents of this file may be used under the terms of
30 * either of the GNU General Public License Version 2 or later (the "GPL"),
31 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32 * in which case the provisions of the GPL or the LGPL are applicable instead
33 * of those above. If you wish to allow use of your version of this file only
34 * under the terms of either the GPL or the LGPL, and not to allow others to
35 * use your version of this file under the terms of the MPL, indicate your
36 * decision by deleting the provisions above and replace them with the notice
37 * and other provisions required by the GPL or the LGPL. If you do not delete
38 * the provisions above, a recipient may use your version of this file under
39 * the terms of any one of the MPL, the GPL or the LGPL.
41 * ***** END LICENSE BLOCK ***** */
43 #include "nsEnumeratorUtils.h"
45 #include "nsISimpleEnumerator.h"
46 #include "nsIStringEnumerator.h"
48 #include "nsCOMPtr.h"
50 class EmptyEnumeratorImpl : public nsISimpleEnumerator,
51 public nsIUTF8StringEnumerator,
52 public nsIStringEnumerator
54 public:
55 // nsISupports interface
56 NS_DECL_ISUPPORTS_INHERITED // not really inherited, but no mRefCnt
58 // nsISimpleEnumerator
59 NS_DECL_NSISIMPLEENUMERATOR
60 NS_DECL_NSIUTF8STRINGENUMERATOR
61 // can't use NS_DECL_NSISTRINGENUMERATOR because they share the
62 // HasMore() signature
63 NS_IMETHOD GetNext(nsAString& aResult);
65 static EmptyEnumeratorImpl* GetInstance() {
66 return const_cast<EmptyEnumeratorImpl*>(&kInstance);
69 private:
70 static const EmptyEnumeratorImpl kInstance;
73 // nsISupports interface
74 NS_IMETHODIMP_(nsrefcnt) EmptyEnumeratorImpl::AddRef(void)
76 return 2;
79 NS_IMETHODIMP_(nsrefcnt) EmptyEnumeratorImpl::Release(void)
81 return 1;
84 NS_IMPL_QUERY_INTERFACE1(EmptyEnumeratorImpl, nsISimpleEnumerator)
86 // nsISimpleEnumerator interface
87 NS_IMETHODIMP EmptyEnumeratorImpl::HasMoreElements(PRBool* aResult)
89 *aResult = PR_FALSE;
90 return NS_OK;
93 NS_IMETHODIMP EmptyEnumeratorImpl::HasMore(PRBool* aResult)
95 *aResult = PR_FALSE;
96 return NS_OK;
99 NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsISupports** aResult)
101 return NS_ERROR_UNEXPECTED;
104 NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsACString& aResult)
106 return NS_ERROR_UNEXPECTED;
109 NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsAString& aResult)
111 return NS_ERROR_UNEXPECTED;
114 const EmptyEnumeratorImpl EmptyEnumeratorImpl::kInstance;
116 nsresult
117 NS_NewEmptyEnumerator(nsISimpleEnumerator** aResult)
119 *aResult = EmptyEnumeratorImpl::GetInstance();
120 return NS_OK;
123 ////////////////////////////////////////////////////////////////////////////////
125 class nsSingletonEnumerator : public nsISimpleEnumerator
127 public:
128 NS_DECL_ISUPPORTS
130 // nsISimpleEnumerator methods
131 NS_IMETHOD HasMoreElements(PRBool* aResult);
132 NS_IMETHOD GetNext(nsISupports** aResult);
134 nsSingletonEnumerator(nsISupports* aValue);
136 private:
137 ~nsSingletonEnumerator();
139 protected:
140 nsISupports* mValue;
141 PRBool mConsumed;
144 nsSingletonEnumerator::nsSingletonEnumerator(nsISupports* aValue)
145 : mValue(aValue)
147 NS_IF_ADDREF(mValue);
148 mConsumed = (mValue ? PR_FALSE : PR_TRUE);
151 nsSingletonEnumerator::~nsSingletonEnumerator()
153 NS_IF_RELEASE(mValue);
156 NS_IMPL_ISUPPORTS1(nsSingletonEnumerator, nsISimpleEnumerator)
158 NS_IMETHODIMP
159 nsSingletonEnumerator::HasMoreElements(PRBool* aResult)
161 NS_PRECONDITION(aResult != 0, "null ptr");
162 if (! aResult)
163 return NS_ERROR_NULL_POINTER;
165 *aResult = !mConsumed;
166 return NS_OK;
170 NS_IMETHODIMP
171 nsSingletonEnumerator::GetNext(nsISupports** aResult)
173 NS_PRECONDITION(aResult != 0, "null ptr");
174 if (! aResult)
175 return NS_ERROR_NULL_POINTER;
177 if (mConsumed)
178 return NS_ERROR_UNEXPECTED;
180 mConsumed = PR_TRUE;
182 *aResult = mValue;
183 NS_ADDREF(*aResult);
184 return NS_OK;
187 nsresult
188 NS_NewSingletonEnumerator(nsISimpleEnumerator* *result,
189 nsISupports* singleton)
191 nsSingletonEnumerator* enumer = new nsSingletonEnumerator(singleton);
192 if (enumer == nsnull)
193 return NS_ERROR_OUT_OF_MEMORY;
194 *result = enumer;
195 NS_ADDREF(*result);
196 return NS_OK;
199 ////////////////////////////////////////////////////////////////////////////////
201 class nsUnionEnumerator : public nsISimpleEnumerator
203 public:
204 NS_DECL_ISUPPORTS
206 // nsISimpleEnumerator methods
207 NS_IMETHOD HasMoreElements(PRBool* aResult);
208 NS_IMETHOD GetNext(nsISupports** aResult);
210 nsUnionEnumerator(nsISimpleEnumerator* firstEnumerator,
211 nsISimpleEnumerator* secondEnumerator);
213 private:
214 ~nsUnionEnumerator();
216 protected:
217 nsCOMPtr<nsISimpleEnumerator> mFirstEnumerator, mSecondEnumerator;
218 PRBool mConsumed;
219 PRBool mAtSecond;
222 nsUnionEnumerator::nsUnionEnumerator(nsISimpleEnumerator* firstEnumerator,
223 nsISimpleEnumerator* secondEnumerator)
224 : mFirstEnumerator(firstEnumerator),
225 mSecondEnumerator(secondEnumerator),
226 mConsumed(PR_FALSE), mAtSecond(PR_FALSE)
230 nsUnionEnumerator::~nsUnionEnumerator()
234 NS_IMPL_ISUPPORTS1(nsUnionEnumerator, nsISimpleEnumerator)
236 NS_IMETHODIMP
237 nsUnionEnumerator::HasMoreElements(PRBool* aResult)
239 NS_PRECONDITION(aResult != 0, "null ptr");
240 if (! aResult)
241 return NS_ERROR_NULL_POINTER;
243 nsresult rv;
245 if (mConsumed) {
246 *aResult = PR_FALSE;
247 return NS_OK;
250 if (! mAtSecond) {
251 rv = mFirstEnumerator->HasMoreElements(aResult);
252 if (NS_FAILED(rv)) return rv;
254 if (*aResult)
255 return NS_OK;
257 mAtSecond = PR_TRUE;
260 rv = mSecondEnumerator->HasMoreElements(aResult);
261 if (NS_FAILED(rv)) return rv;
263 if (*aResult)
264 return NS_OK;
266 *aResult = PR_FALSE;
267 mConsumed = PR_TRUE;
268 return NS_OK;
271 NS_IMETHODIMP
272 nsUnionEnumerator::GetNext(nsISupports** aResult)
274 NS_PRECONDITION(aResult != 0, "null ptr");
275 if (! aResult)
276 return NS_ERROR_NULL_POINTER;
278 if (mConsumed)
279 return NS_ERROR_UNEXPECTED;
281 if (! mAtSecond)
282 return mFirstEnumerator->GetNext(aResult);
284 return mSecondEnumerator->GetNext(aResult);
287 nsresult
288 NS_NewUnionEnumerator(nsISimpleEnumerator* *result,
289 nsISimpleEnumerator* firstEnumerator,
290 nsISimpleEnumerator* secondEnumerator)
292 *result = nsnull;
293 if (! firstEnumerator) {
294 *result = secondEnumerator;
295 } else if (! secondEnumerator) {
296 *result = firstEnumerator;
297 } else {
298 nsUnionEnumerator* enumer = new nsUnionEnumerator(firstEnumerator, secondEnumerator);
299 if (enumer == nsnull)
300 return NS_ERROR_OUT_OF_MEMORY;
301 *result = enumer;
303 NS_ADDREF(*result);
304 return NS_OK;