1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsArrayEnumerator.h"
9 #include "nsThreadUtils.h"
11 NS_INTERFACE_MAP_BEGIN(nsArray
)
12 NS_INTERFACE_MAP_ENTRY(nsIArray
)
13 NS_INTERFACE_MAP_ENTRY(nsIArrayExtensions
)
14 NS_INTERFACE_MAP_ENTRY(nsIMutableArray
)
15 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIMutableArray
)
18 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsArrayCC
)
19 NS_INTERFACE_MAP_ENTRY(nsIArray
)
20 NS_INTERFACE_MAP_ENTRY(nsIArrayExtensions
)
21 NS_INTERFACE_MAP_ENTRY(nsIMutableArray
)
22 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIMutableArray
)
25 nsArrayBase::~nsArrayBase() { Clear(); }
27 NS_IMPL_ADDREF(nsArray
)
28 NS_IMPL_RELEASE(nsArray
)
30 NS_IMPL_CYCLE_COLLECTION_CLASS(nsArrayCC
)
32 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsArrayCC
)
33 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsArrayCC
)
35 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsArrayCC
)
37 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
38 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsArrayCC
)
39 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mArray
)
40 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
43 nsArrayBase::GetLength(uint32_t* aLength
) {
44 *aLength
= mArray
.Count();
49 nsArrayBase::QueryElementAt(uint32_t aIndex
, const nsIID
& aIID
,
51 nsISupports
* obj
= mArray
.SafeObjectAt(aIndex
);
53 return NS_ERROR_ILLEGAL_VALUE
;
56 // no need to worry about a leak here, because SafeObjectAt()
57 // doesn't addref its result
58 return obj
->QueryInterface(aIID
, aResult
);
62 nsArrayBase::IndexOf(uint32_t aStartIndex
, nsISupports
* aElement
,
64 int32_t idx
= mArray
.IndexOf(aElement
, aStartIndex
);
66 return NS_ERROR_FAILURE
;
69 *aResult
= static_cast<uint32_t>(idx
);
74 nsArrayBase::ScriptedEnumerate(const nsIID
& aElemIID
, uint8_t aArgc
,
75 nsISimpleEnumerator
** aResult
) {
77 return NS_NewArrayEnumerator(aResult
, static_cast<nsIArray
*>(this),
80 return NS_NewArrayEnumerator(aResult
, static_cast<nsIArray
*>(this));
84 nsArrayBase::EnumerateImpl(const nsID
& aElemIID
,
85 nsISimpleEnumerator
** aResult
) {
86 return NS_NewArrayEnumerator(aResult
, static_cast<nsIArray
*>(this), aElemIID
);
89 // nsIMutableArray implementation
92 nsArrayBase::AppendElement(nsISupports
* aElement
) {
93 bool result
= mArray
.AppendObject(aElement
);
94 return result
? NS_OK
: NS_ERROR_FAILURE
;
98 nsArrayBase::RemoveElementAt(uint32_t aIndex
) {
99 bool result
= mArray
.RemoveObjectAt(aIndex
);
100 return result
? NS_OK
: NS_ERROR_FAILURE
;
104 nsArrayBase::InsertElementAt(nsISupports
* aElement
, uint32_t aIndex
) {
105 bool result
= mArray
.InsertObjectAt(aElement
, aIndex
);
106 return result
? NS_OK
: NS_ERROR_FAILURE
;
110 nsArrayBase::ReplaceElementAt(nsISupports
* aElement
, uint32_t aIndex
) {
111 mArray
.ReplaceObjectAt(aElement
, aIndex
);
116 nsArrayBase::Clear() {
121 // nsIArrayExtensions implementation.
124 nsArrayBase::Count(uint32_t* aResult
) { return GetLength(aResult
); }
127 nsArrayBase::GetElementAt(uint32_t aIndex
, nsISupports
** aResult
) {
128 nsCOMPtr
<nsISupports
> obj
= mArray
.SafeObjectAt(aIndex
);
133 nsresult
nsArrayBase::XPCOMConstructor(nsISupports
* aOuter
, const nsIID
& aIID
,
136 return NS_ERROR_NO_AGGREGATION
;
139 nsCOMPtr
<nsIMutableArray
> inst
= Create();
140 return inst
->QueryInterface(aIID
, aResult
);
143 already_AddRefed
<nsIMutableArray
> nsArrayBase::Create() {
144 nsCOMPtr
<nsIMutableArray
> inst
;
145 if (NS_IsMainThread()) {
146 inst
= new nsArrayCC
;
150 return inst
.forget();