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 XPCOM Array implementation.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corp.
19 * Portions created by the Initial Developer are Copyright (C) 2002
20 * the Initial Developer. All Rights Reserved.
23 * Alec Flett <alecf@netscape.com>
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.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsArrayEnumerator.h"
42 #include "nsISimpleEnumerator.h"
44 #include "nsCOMArray.h"
47 class nsSimpleArrayEnumerator
: public nsISimpleEnumerator
50 // nsISupports interface
53 // nsISimpleEnumerator interface
54 NS_DECL_NSISIMPLEENUMERATOR
56 // nsSimpleArrayEnumerator methods
57 nsSimpleArrayEnumerator(nsIArray
* aValueArray
) :
58 mValueArray(aValueArray
), mIndex(0) {
62 ~nsSimpleArrayEnumerator() {}
65 nsCOMPtr
<nsIArray
> mValueArray
;
69 NS_IMPL_ISUPPORTS1(nsSimpleArrayEnumerator
, nsISimpleEnumerator
)
72 nsSimpleArrayEnumerator::HasMoreElements(PRBool
* aResult
)
74 NS_PRECONDITION(aResult
!= 0, "null ptr");
76 return NS_ERROR_NULL_POINTER
;
84 nsresult rv
= mValueArray
->GetLength(&cnt
);
85 if (NS_FAILED(rv
)) return rv
;
86 *aResult
= (mIndex
< cnt
);
91 nsSimpleArrayEnumerator::GetNext(nsISupports
** aResult
)
93 NS_PRECONDITION(aResult
!= 0, "null ptr");
95 return NS_ERROR_NULL_POINTER
;
103 nsresult rv
= mValueArray
->GetLength(&cnt
);
104 if (NS_FAILED(rv
)) return rv
;
106 return NS_ERROR_UNEXPECTED
;
108 return mValueArray
->QueryElementAt(mIndex
++, NS_GET_IID(nsISupports
), (void**)aResult
);
112 NS_NewArrayEnumerator(nsISimpleEnumerator
* *result
,
115 nsSimpleArrayEnumerator
* enumer
= new nsSimpleArrayEnumerator(array
);
116 if (enumer
== nsnull
)
117 return NS_ERROR_OUT_OF_MEMORY
;
119 NS_ADDREF(*result
= enumer
);
123 ////////////////////////////////////////////////////////////////////////////////
125 // enumerator implementation for nsCOMArray
126 // creates a snapshot of the array in question
127 // you MUST use NS_NewArrayEnumerator to create this, so that
128 // allocation is done correctly
129 class nsCOMArrayEnumerator
: public nsISimpleEnumerator
132 // nsISupports interface
135 // nsISimpleEnumerator interface
136 NS_DECL_NSISIMPLEENUMERATOR
138 // nsSimpleArrayEnumerator methods
139 nsCOMArrayEnumerator() : mIndex(0) {
142 // specialized operator to make sure we make room for mValues
143 void* operator new (size_t size
, const nsCOMArray_base
& aArray
) CPP_THROW_NEW
;
144 void operator delete(void* ptr
) {
145 ::operator delete(ptr
);
149 ~nsCOMArrayEnumerator(void);
152 PRUint32 mIndex
; // current position
153 PRUint32 mArraySize
; // size of the array
155 // this is actually bigger
156 nsISupports
* mValueArray
[1];
159 NS_IMPL_ISUPPORTS1(nsCOMArrayEnumerator
, nsISimpleEnumerator
)
161 nsCOMArrayEnumerator::~nsCOMArrayEnumerator()
163 // only release the entries that we haven't visited yet
164 for (; mIndex
< mArraySize
; ++mIndex
) {
165 NS_IF_RELEASE(mValueArray
[mIndex
]);
170 nsCOMArrayEnumerator::HasMoreElements(PRBool
* aResult
)
172 NS_PRECONDITION(aResult
!= 0, "null ptr");
174 return NS_ERROR_NULL_POINTER
;
176 *aResult
= (mIndex
< mArraySize
);
181 nsCOMArrayEnumerator::GetNext(nsISupports
** aResult
)
183 NS_PRECONDITION(aResult
!= 0, "null ptr");
185 return NS_ERROR_NULL_POINTER
;
187 if (mIndex
>= mArraySize
)
188 return NS_ERROR_UNEXPECTED
;
190 // pass the ownership of the reference to the caller. Since
191 // we AddRef'ed during creation of |this|, there is no need
193 *aResult
= mValueArray
[mIndex
++];
195 // this really isn't necessary. just pretend this happens, since
196 // we'll never visit this value again!
197 // mValueArray[(mIndex-1)] = nsnull;
203 nsCOMArrayEnumerator::operator new (size_t size
, const nsCOMArray_base
& aArray
)
206 // create enough space such that mValueArray points to a large
207 // enough value. Note that the initial value of size gives us
208 // space for mValueArray[0], so we must subtract
209 size
+= (aArray
.Count() - 1) * sizeof(aArray
[0]);
211 // do the actual allocation
212 nsCOMArrayEnumerator
* result
=
213 static_cast<nsCOMArrayEnumerator
*>(::operator new(size
));
214 NS_ENSURE_TRUE(result
, nsnull
);
216 // now need to copy over the values, and addref each one
217 // now this might seem like a lot of work, but we're actually just
218 // doing all our AddRef's ahead of time since GetNext() doesn't
219 // need to AddRef() on the way out
221 PRUint32 max
= result
->mArraySize
= aArray
.Count();
222 for (i
= 0; i
<max
; i
++) {
223 result
->mValueArray
[i
] = aArray
[i
];
224 NS_IF_ADDREF(result
->mValueArray
[i
]);
231 NS_NewArrayEnumerator(nsISimpleEnumerator
* *aResult
,
232 const nsCOMArray_base
& aArray
)
234 nsCOMArrayEnumerator
*enumerator
= new (aArray
) nsCOMArrayEnumerator();
235 if (!enumerator
) return NS_ERROR_OUT_OF_MEMORY
;
237 NS_ADDREF(*aResult
= enumerator
);