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
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.
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"
50 class EmptyEnumeratorImpl
: public nsISimpleEnumerator
,
51 public nsIUTF8StringEnumerator
,
52 public nsIStringEnumerator
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
);
70 static const EmptyEnumeratorImpl kInstance
;
73 // nsISupports interface
74 NS_IMETHODIMP_(nsrefcnt
) EmptyEnumeratorImpl::AddRef(void)
79 NS_IMETHODIMP_(nsrefcnt
) EmptyEnumeratorImpl::Release(void)
84 NS_IMPL_QUERY_INTERFACE3(EmptyEnumeratorImpl
, nsISimpleEnumerator
,
85 nsIUTF8StringEnumerator
, nsIStringEnumerator
)
87 // nsISimpleEnumerator interface
88 NS_IMETHODIMP
EmptyEnumeratorImpl::HasMoreElements(PRBool
* aResult
)
94 NS_IMETHODIMP
EmptyEnumeratorImpl::HasMore(PRBool
* aResult
)
100 NS_IMETHODIMP
EmptyEnumeratorImpl::GetNext(nsISupports
** aResult
)
102 return NS_ERROR_UNEXPECTED
;
105 NS_IMETHODIMP
EmptyEnumeratorImpl::GetNext(nsACString
& aResult
)
107 return NS_ERROR_UNEXPECTED
;
110 NS_IMETHODIMP
EmptyEnumeratorImpl::GetNext(nsAString
& aResult
)
112 return NS_ERROR_UNEXPECTED
;
115 const EmptyEnumeratorImpl
EmptyEnumeratorImpl::kInstance
;
118 NS_NewEmptyEnumerator(nsISimpleEnumerator
** aResult
)
120 *aResult
= EmptyEnumeratorImpl::GetInstance();
124 ////////////////////////////////////////////////////////////////////////////////
126 class nsSingletonEnumerator
: public nsISimpleEnumerator
131 // nsISimpleEnumerator methods
132 NS_IMETHOD
HasMoreElements(PRBool
* aResult
);
133 NS_IMETHOD
GetNext(nsISupports
** aResult
);
135 nsSingletonEnumerator(nsISupports
* aValue
);
138 ~nsSingletonEnumerator();
145 nsSingletonEnumerator::nsSingletonEnumerator(nsISupports
* aValue
)
148 NS_IF_ADDREF(mValue
);
149 mConsumed
= (mValue
? PR_FALSE
: PR_TRUE
);
152 nsSingletonEnumerator::~nsSingletonEnumerator()
154 NS_IF_RELEASE(mValue
);
157 NS_IMPL_ISUPPORTS1(nsSingletonEnumerator
, nsISimpleEnumerator
)
160 nsSingletonEnumerator::HasMoreElements(PRBool
* aResult
)
162 NS_PRECONDITION(aResult
!= 0, "null ptr");
164 return NS_ERROR_NULL_POINTER
;
166 *aResult
= !mConsumed
;
172 nsSingletonEnumerator::GetNext(nsISupports
** aResult
)
174 NS_PRECONDITION(aResult
!= 0, "null ptr");
176 return NS_ERROR_NULL_POINTER
;
179 return NS_ERROR_UNEXPECTED
;
189 NS_NewSingletonEnumerator(nsISimpleEnumerator
* *result
,
190 nsISupports
* singleton
)
192 nsSingletonEnumerator
* enumer
= new nsSingletonEnumerator(singleton
);
193 if (enumer
== nsnull
)
194 return NS_ERROR_OUT_OF_MEMORY
;
200 ////////////////////////////////////////////////////////////////////////////////
202 class nsUnionEnumerator
: public nsISimpleEnumerator
207 // nsISimpleEnumerator methods
208 NS_IMETHOD
HasMoreElements(PRBool
* aResult
);
209 NS_IMETHOD
GetNext(nsISupports
** aResult
);
211 nsUnionEnumerator(nsISimpleEnumerator
* firstEnumerator
,
212 nsISimpleEnumerator
* secondEnumerator
);
215 ~nsUnionEnumerator();
218 nsCOMPtr
<nsISimpleEnumerator
> mFirstEnumerator
, mSecondEnumerator
;
223 nsUnionEnumerator::nsUnionEnumerator(nsISimpleEnumerator
* firstEnumerator
,
224 nsISimpleEnumerator
* secondEnumerator
)
225 : mFirstEnumerator(firstEnumerator
),
226 mSecondEnumerator(secondEnumerator
),
227 mConsumed(PR_FALSE
), mAtSecond(PR_FALSE
)
231 nsUnionEnumerator::~nsUnionEnumerator()
235 NS_IMPL_ISUPPORTS1(nsUnionEnumerator
, nsISimpleEnumerator
)
238 nsUnionEnumerator::HasMoreElements(PRBool
* aResult
)
240 NS_PRECONDITION(aResult
!= 0, "null ptr");
242 return NS_ERROR_NULL_POINTER
;
252 rv
= mFirstEnumerator
->HasMoreElements(aResult
);
253 if (NS_FAILED(rv
)) return rv
;
261 rv
= mSecondEnumerator
->HasMoreElements(aResult
);
262 if (NS_FAILED(rv
)) return rv
;
273 nsUnionEnumerator::GetNext(nsISupports
** aResult
)
275 NS_PRECONDITION(aResult
!= 0, "null ptr");
277 return NS_ERROR_NULL_POINTER
;
280 return NS_ERROR_UNEXPECTED
;
283 return mFirstEnumerator
->GetNext(aResult
);
285 return mSecondEnumerator
->GetNext(aResult
);
289 NS_NewUnionEnumerator(nsISimpleEnumerator
* *result
,
290 nsISimpleEnumerator
* firstEnumerator
,
291 nsISimpleEnumerator
* secondEnumerator
)
294 if (! firstEnumerator
) {
295 *result
= secondEnumerator
;
296 } else if (! secondEnumerator
) {
297 *result
= firstEnumerator
;
299 nsUnionEnumerator
* enumer
= new nsUnionEnumerator(firstEnumerator
, secondEnumerator
);
300 if (enumer
== nsnull
)
301 return NS_ERROR_OUT_OF_MEMORY
;