VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsStringEnumerator.cpp@ 104488

最後變更 在這個檔案從104488是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 7.7 KB
 
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 String Enumerator.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corp.
19 * Portions created by the Initial Developer are Copyright (C) 2003
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
40#include "nsStringEnumerator.h"
41#include "prtypes.h"
42#include "nsCRT.h"
43#include "nsString.h"
44#include "nsReadableUtils.h"
45#include "nsISimpleEnumerator.h"
46#include "nsSupportsPrimitives.h"
47
48//
49// nsStringEnumerator
50//
51
52class nsStringEnumerator : public nsIStringEnumerator,
53 public nsIUTF8StringEnumerator,
54 public nsISimpleEnumerator
55{
56public:
57 nsStringEnumerator(const nsStringArray* aArray, PRBool aOwnsArray) :
58 mArray(aArray), mIndex(0), mOwnsArray(aOwnsArray), mIsUnicode(PR_TRUE)
59 {}
60
61 nsStringEnumerator(const nsCStringArray* aArray, PRBool aOwnsArray) :
62 mCArray(aArray), mIndex(0), mOwnsArray(aOwnsArray), mIsUnicode(PR_FALSE)
63 {}
64
65 nsStringEnumerator(const nsStringArray* aArray, nsISupports* aOwner) :
66 mArray(aArray), mIndex(0), mOwner(aOwner), mOwnsArray(PR_FALSE), mIsUnicode(PR_TRUE)
67 {}
68
69 nsStringEnumerator(const nsCStringArray* aArray, nsISupports* aOwner) :
70 mCArray(aArray), mIndex(0), mOwner(aOwner), mOwnsArray(PR_FALSE), mIsUnicode(PR_FALSE)
71 {}
72
73 NS_DECL_ISUPPORTS
74 NS_DECL_NSIUTF8STRINGENUMERATOR
75
76 // have to declare nsIStringEnumerator manually, because of
77 // overlapping method names
78 NS_IMETHOD GetNext(nsAString& aResult);
79 NS_DECL_NSISIMPLEENUMERATOR
80
81private:
82 ~nsStringEnumerator() {
83 if (mOwnsArray) {
84 // const-casting is safe here, because the NS_New*
85 // constructors make sure mOwnsArray is consistent with
86 // the constness of the objects
87 if (mIsUnicode)
88 delete NS_CONST_CAST(nsStringArray*,mArray);
89 else
90 delete NS_CONST_CAST(nsCStringArray*,mCArray);
91 }
92 }
93
94 union {
95 const nsStringArray* mArray;
96 const nsCStringArray* mCArray;
97 };
98
99 inline PRUint32 Count() {
100 return mIsUnicode ? mArray->Count() : mCArray->Count();
101 }
102
103 PRUint32 mIndex;
104
105 // the owner allows us to hold a strong reference to the object
106 // that owns the array. Having a non-null value in mOwner implies
107 // that mOwnsArray is PR_FALSE, because we rely on the real owner
108 // to release the array
109 nsCOMPtr<nsISupports> mOwner;
110 PRPackedBool mOwnsArray;
111 PRPackedBool mIsUnicode;
112};
113
114NS_IMPL_ISUPPORTS3(nsStringEnumerator,
115 nsIStringEnumerator,
116 nsIUTF8StringEnumerator,
117 nsISimpleEnumerator)
118
119NS_IMETHODIMP
120nsStringEnumerator::HasMore(PRBool* aResult)
121{
122 NS_ENSURE_ARG_POINTER(aResult);
123 *aResult = mIndex < Count();
124 return NS_OK;
125}
126
127NS_IMETHODIMP
128nsStringEnumerator::HasMoreElements(PRBool* aResult)
129{
130 return HasMore(aResult);
131}
132
133NS_IMETHODIMP
134nsStringEnumerator::GetNext(nsISupports** aResult)
135{
136 if (mIsUnicode) {
137 nsSupportsStringImpl* stringImpl = new nsSupportsStringImpl();
138 if (!stringImpl) return NS_ERROR_OUT_OF_MEMORY;
139
140 stringImpl->SetData(*mArray->StringAt(mIndex++));
141 *aResult = stringImpl;
142 }
143 else {
144 nsSupportsCStringImpl* cstringImpl = new nsSupportsCStringImpl();
145 if (!cstringImpl) return NS_ERROR_OUT_OF_MEMORY;
146
147 cstringImpl->SetData(*mCArray->CStringAt(mIndex++));
148 *aResult = cstringImpl;
149 }
150 NS_ADDREF(*aResult);
151 return NS_OK;
152}
153
154NS_IMETHODIMP
155nsStringEnumerator::GetNext(nsAString& aResult)
156{
157 NS_ENSURE_TRUE(mIndex < Count(), NS_ERROR_UNEXPECTED);
158
159 if (mIsUnicode)
160 aResult = *mArray->StringAt(mIndex++);
161 else
162 CopyUTF8toUTF16(*mCArray->CStringAt(mIndex++), aResult);
163
164 return NS_OK;
165}
166
167NS_IMETHODIMP
168nsStringEnumerator::GetNext(nsACString& aResult)
169{
170 NS_ENSURE_TRUE(mIndex < Count(), NS_ERROR_UNEXPECTED);
171
172 if (mIsUnicode)
173 CopyUTF16toUTF8(*mArray->StringAt(mIndex++), aResult);
174 else
175 aResult = *mCArray->CStringAt(mIndex++);
176
177 return NS_OK;
178}
179
180template<class T>
181static inline nsresult
182StringEnumeratorTail(T** aResult)
183{
184 if (!*aResult)
185 return NS_ERROR_OUT_OF_MEMORY;
186 NS_ADDREF(*aResult);
187 return NS_OK;
188}
189
190//
191// constructors
192//
193
194NS_COM nsresult
195NS_NewStringEnumerator(nsIStringEnumerator** aResult,
196 const nsStringArray* aArray, nsISupports* aOwner)
197{
198 NS_ENSURE_ARG_POINTER(aResult);
199 NS_ENSURE_ARG_POINTER(aArray);
200
201 *aResult = new nsStringEnumerator(aArray, aOwner);
202 return StringEnumeratorTail(aResult);
203}
204
205
206NS_COM nsresult
207NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
208 const nsCStringArray* aArray, nsISupports* aOwner)
209{
210 NS_ENSURE_ARG_POINTER(aResult);
211 NS_ENSURE_ARG_POINTER(aArray);
212
213 *aResult = new nsStringEnumerator(aArray, aOwner);
214 return StringEnumeratorTail(aResult);
215}
216
217NS_COM nsresult
218NS_NewAdoptingStringEnumerator(nsIStringEnumerator** aResult,
219 nsStringArray* aArray)
220{
221 NS_ENSURE_ARG_POINTER(aResult);
222 NS_ENSURE_ARG_POINTER(aArray);
223
224 *aResult = new nsStringEnumerator(aArray, PR_TRUE);
225 return StringEnumeratorTail(aResult);
226}
227
228NS_COM nsresult
229NS_NewAdoptingUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
230 nsCStringArray* aArray)
231{
232 NS_ENSURE_ARG_POINTER(aResult);
233 NS_ENSURE_ARG_POINTER(aArray);
234
235 *aResult = new nsStringEnumerator(aArray, PR_TRUE);
236 return StringEnumeratorTail(aResult);
237}
238
239// const ones internally just forward to the non-const equivalents
240NS_COM nsresult
241NS_NewStringEnumerator(nsIStringEnumerator** aResult,
242 const nsStringArray* aArray)
243{
244 NS_ENSURE_ARG_POINTER(aResult);
245 NS_ENSURE_ARG_POINTER(aArray);
246
247 *aResult = new nsStringEnumerator(aArray, PR_FALSE);
248 return StringEnumeratorTail(aResult);
249}
250
251NS_COM nsresult
252NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
253 const nsCStringArray* aArray)
254{
255 NS_ENSURE_ARG_POINTER(aResult);
256 NS_ENSURE_ARG_POINTER(aArray);
257
258 *aResult = new nsStringEnumerator(aArray, PR_FALSE);
259 return StringEnumeratorTail(aResult);
260}
261
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette