1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsStringEnumerator.h"
8 #include "nsISimpleEnumerator.h"
9 #include "nsSupportsPrimitives.h"
10 #include "mozilla/Attributes.h"
17 class nsStringEnumerator MOZ_FINAL
18 : public nsIStringEnumerator
19 , public nsIUTF8StringEnumerator
20 , public nsISimpleEnumerator
23 nsStringEnumerator(const nsTArray
<nsString
>* aArray
, bool aOwnsArray
)
26 , mOwnsArray(aOwnsArray
)
30 nsStringEnumerator(const nsTArray
<nsCString
>* aArray
, bool aOwnsArray
)
33 , mOwnsArray(aOwnsArray
)
37 nsStringEnumerator(const nsTArray
<nsString
>* aArray
, nsISupports
* aOwner
)
45 nsStringEnumerator(const nsTArray
<nsCString
>* aArray
, nsISupports
* aOwner
)
54 NS_DECL_NSIUTF8STRINGENUMERATOR
56 // have to declare nsIStringEnumerator manually, because of
57 // overlapping method names
58 NS_IMETHOD
GetNext(nsAString
& aResult
);
59 NS_DECL_NSISIMPLEENUMERATOR
65 // const-casting is safe here, because the NS_New*
66 // constructors make sure mOwnsArray is consistent with
67 // the constness of the objects
69 delete const_cast<nsTArray
<nsString
>*>(mArray
);
71 delete const_cast<nsTArray
<nsCString
>*>(mCArray
);
78 const nsTArray
<nsString
>* mArray
;
79 const nsTArray
<nsCString
>* mCArray
;
82 inline uint32_t Count()
84 return mIsUnicode
? mArray
->Length() : mCArray
->Length();
89 // the owner allows us to hold a strong reference to the object
90 // that owns the array. Having a non-null value in mOwner implies
91 // that mOwnsArray is false, because we rely on the real owner
92 // to release the array
93 nsCOMPtr
<nsISupports
> mOwner
;
98 NS_IMPL_ISUPPORTS(nsStringEnumerator
,
100 nsIUTF8StringEnumerator
,
104 nsStringEnumerator::HasMore(bool* aResult
)
106 *aResult
= mIndex
< Count();
111 nsStringEnumerator::HasMoreElements(bool* aResult
)
113 return HasMore(aResult
);
117 nsStringEnumerator::GetNext(nsISupports
** aResult
)
120 nsSupportsStringImpl
* stringImpl
= new nsSupportsStringImpl();
122 return NS_ERROR_OUT_OF_MEMORY
;
125 stringImpl
->SetData(mArray
->ElementAt(mIndex
++));
126 *aResult
= stringImpl
;
128 nsSupportsCStringImpl
* cstringImpl
= new nsSupportsCStringImpl();
130 return NS_ERROR_OUT_OF_MEMORY
;
133 cstringImpl
->SetData(mCArray
->ElementAt(mIndex
++));
134 *aResult
= cstringImpl
;
141 nsStringEnumerator::GetNext(nsAString
& aResult
)
143 if (NS_WARN_IF(mIndex
>= Count())) {
144 return NS_ERROR_UNEXPECTED
;
148 aResult
= mArray
->ElementAt(mIndex
++);
150 CopyUTF8toUTF16(mCArray
->ElementAt(mIndex
++), aResult
);
157 nsStringEnumerator::GetNext(nsACString
& aResult
)
159 if (NS_WARN_IF(mIndex
>= Count())) {
160 return NS_ERROR_UNEXPECTED
;
164 CopyUTF16toUTF8(mArray
->ElementAt(mIndex
++), aResult
);
166 aResult
= mCArray
->ElementAt(mIndex
++);
173 static inline nsresult
174 StringEnumeratorTail(T
** aResult
)
177 return NS_ERROR_OUT_OF_MEMORY
;
188 NS_NewStringEnumerator(nsIStringEnumerator
** aResult
,
189 const nsTArray
<nsString
>* aArray
, nsISupports
* aOwner
)
191 if (NS_WARN_IF(!aResult
) || NS_WARN_IF(!aArray
)) {
192 return NS_ERROR_INVALID_ARG
;
195 *aResult
= new nsStringEnumerator(aArray
, aOwner
);
196 return StringEnumeratorTail(aResult
);
201 NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator
** aResult
,
202 const nsTArray
<nsCString
>* aArray
,
205 if (NS_WARN_IF(!aResult
) || NS_WARN_IF(!aArray
)) {
206 return NS_ERROR_INVALID_ARG
;
209 *aResult
= new nsStringEnumerator(aArray
, aOwner
);
210 return StringEnumeratorTail(aResult
);
214 NS_NewAdoptingStringEnumerator(nsIStringEnumerator
** aResult
,
215 nsTArray
<nsString
>* aArray
)
217 if (NS_WARN_IF(!aResult
) || NS_WARN_IF(!aArray
)) {
218 return NS_ERROR_INVALID_ARG
;
221 *aResult
= new nsStringEnumerator(aArray
, true);
222 return StringEnumeratorTail(aResult
);
226 NS_NewAdoptingUTF8StringEnumerator(nsIUTF8StringEnumerator
** aResult
,
227 nsTArray
<nsCString
>* aArray
)
229 if (NS_WARN_IF(!aResult
) || NS_WARN_IF(!aArray
)) {
230 return NS_ERROR_INVALID_ARG
;
233 *aResult
= new nsStringEnumerator(aArray
, true);
234 return StringEnumeratorTail(aResult
);
237 // const ones internally just forward to the non-const equivalents
239 NS_NewStringEnumerator(nsIStringEnumerator
** aResult
,
240 const nsTArray
<nsString
>* aArray
)
242 if (NS_WARN_IF(!aResult
) || NS_WARN_IF(!aArray
)) {
243 return NS_ERROR_INVALID_ARG
;
246 *aResult
= new nsStringEnumerator(aArray
, false);
247 return StringEnumeratorTail(aResult
);
251 NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator
** aResult
,
252 const nsTArray
<nsCString
>* aArray
)
254 if (NS_WARN_IF(!aResult
) || NS_WARN_IF(!aArray
)) {
255 return NS_ERROR_INVALID_ARG
;
258 *aResult
= new nsStringEnumerator(aArray
, false);
259 return StringEnumeratorTail(aResult
);