/src/mozilla-central/xpcom/ds/nsArray.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #include "nsArray.h" |
8 | | #include "nsArrayEnumerator.h" |
9 | | #include "nsThreadUtils.h" |
10 | | #include "xpcjsid.h" |
11 | | |
12 | 0 | NS_INTERFACE_MAP_BEGIN(nsArray) |
13 | 0 | NS_INTERFACE_MAP_ENTRY(nsIArray) |
14 | 0 | NS_INTERFACE_MAP_ENTRY(nsIArrayExtensions) |
15 | 0 | NS_INTERFACE_MAP_ENTRY(nsIMutableArray) |
16 | 0 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMutableArray) |
17 | 0 | NS_INTERFACE_MAP_END |
18 | | |
19 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsArrayCC) |
20 | 0 | NS_INTERFACE_MAP_ENTRY(nsIArray) |
21 | 0 | NS_INTERFACE_MAP_ENTRY(nsIArrayExtensions) |
22 | 0 | NS_INTERFACE_MAP_ENTRY(nsIMutableArray) |
23 | 0 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMutableArray) |
24 | 0 | NS_INTERFACE_MAP_END |
25 | | |
26 | | nsArrayBase::~nsArrayBase() |
27 | 0 | { |
28 | 0 | Clear(); |
29 | 0 | } |
30 | | |
31 | | |
32 | | NS_IMPL_ADDREF(nsArray) |
33 | | NS_IMPL_RELEASE(nsArray) |
34 | | |
35 | | NS_IMPL_CYCLE_COLLECTION_CLASS(nsArrayCC) |
36 | | |
37 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(nsArrayCC) |
38 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(nsArrayCC) |
39 | | |
40 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsArrayCC) |
41 | 0 | tmp->Clear(); |
42 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
43 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsArrayCC) |
44 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mArray) |
45 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
46 | | |
47 | | NS_IMETHODIMP |
48 | | nsArrayBase::GetLength(uint32_t* aLength) |
49 | 0 | { |
50 | 0 | *aLength = mArray.Count(); |
51 | 0 | return NS_OK; |
52 | 0 | } |
53 | | |
54 | | NS_IMETHODIMP |
55 | | nsArrayBase::QueryElementAt(uint32_t aIndex, |
56 | | const nsIID& aIID, |
57 | | void** aResult) |
58 | 0 | { |
59 | 0 | nsISupports* obj = mArray.SafeObjectAt(aIndex); |
60 | 0 | if (!obj) { |
61 | 0 | return NS_ERROR_ILLEGAL_VALUE; |
62 | 0 | } |
63 | 0 | |
64 | 0 | // no need to worry about a leak here, because SafeObjectAt() |
65 | 0 | // doesn't addref its result |
66 | 0 | return obj->QueryInterface(aIID, aResult); |
67 | 0 | } |
68 | | |
69 | | NS_IMETHODIMP |
70 | | nsArrayBase::IndexOf(uint32_t aStartIndex, nsISupports* aElement, |
71 | | uint32_t* aResult) |
72 | 0 | { |
73 | 0 | int32_t idx = mArray.IndexOf(aElement, aStartIndex); |
74 | 0 | if (idx == -1) { |
75 | 0 | return NS_ERROR_FAILURE; |
76 | 0 | } |
77 | 0 | |
78 | 0 | *aResult = static_cast<uint32_t>(idx); |
79 | 0 | return NS_OK; |
80 | 0 | } |
81 | | |
82 | | NS_IMETHODIMP |
83 | | nsArrayBase::ScriptedEnumerate(nsIJSIID* aElemIID, uint8_t aArgc, |
84 | | nsISimpleEnumerator** aResult) |
85 | 0 | { |
86 | 0 | if (aArgc > 0 && aElemIID) { |
87 | 0 | return NS_NewArrayEnumerator(aResult, static_cast<nsIArray*>(this), *aElemIID->GetID()); |
88 | 0 | } |
89 | 0 | return NS_NewArrayEnumerator(aResult, static_cast<nsIArray*>(this)); |
90 | 0 | } |
91 | | |
92 | | |
93 | | NS_IMETHODIMP |
94 | | nsArrayBase::EnumerateImpl(const nsID& aElemIID, nsISimpleEnumerator** aResult) |
95 | 0 | { |
96 | 0 | return NS_NewArrayEnumerator(aResult, static_cast<nsIArray*>(this), aElemIID); |
97 | 0 | } |
98 | | |
99 | | // nsIMutableArray implementation |
100 | | |
101 | | NS_IMETHODIMP |
102 | | nsArrayBase::AppendElement(nsISupports* aElement) |
103 | 0 | { |
104 | 0 | bool result = mArray.AppendObject(aElement); |
105 | 0 | return result ? NS_OK : NS_ERROR_FAILURE; |
106 | 0 | } |
107 | | |
108 | | NS_IMETHODIMP |
109 | | nsArrayBase::RemoveElementAt(uint32_t aIndex) |
110 | 0 | { |
111 | 0 | bool result = mArray.RemoveObjectAt(aIndex); |
112 | 0 | return result ? NS_OK : NS_ERROR_FAILURE; |
113 | 0 | } |
114 | | |
115 | | NS_IMETHODIMP |
116 | | nsArrayBase::InsertElementAt(nsISupports* aElement, uint32_t aIndex) |
117 | 0 | { |
118 | 0 | bool result = mArray.InsertObjectAt(aElement, aIndex); |
119 | 0 | return result ? NS_OK : NS_ERROR_FAILURE; |
120 | 0 | } |
121 | | |
122 | | NS_IMETHODIMP |
123 | | nsArrayBase::ReplaceElementAt(nsISupports* aElement, uint32_t aIndex) |
124 | 0 | { |
125 | 0 | mArray.ReplaceObjectAt(aElement, aIndex); |
126 | 0 | return NS_OK; |
127 | 0 | } |
128 | | |
129 | | NS_IMETHODIMP |
130 | | nsArrayBase::Clear() |
131 | 0 | { |
132 | 0 | mArray.Clear(); |
133 | 0 | return NS_OK; |
134 | 0 | } |
135 | | |
136 | | // nsIArrayExtensions implementation. |
137 | | |
138 | | NS_IMETHODIMP |
139 | | nsArrayBase::Count(uint32_t* aResult) |
140 | 0 | { |
141 | 0 | return GetLength(aResult); |
142 | 0 | } |
143 | | |
144 | | NS_IMETHODIMP |
145 | | nsArrayBase::GetElementAt(uint32_t aIndex, nsISupports** aResult) |
146 | 0 | { |
147 | 0 | nsCOMPtr<nsISupports> obj = mArray.SafeObjectAt(aIndex); |
148 | 0 | obj.forget(aResult); |
149 | 0 | return NS_OK; |
150 | 0 | } |
151 | | |
152 | | nsresult |
153 | | nsArrayBase::XPCOMConstructor(nsISupports* aOuter, const nsIID& aIID, |
154 | | void** aResult) |
155 | 0 | { |
156 | 0 | if (aOuter) { |
157 | 0 | return NS_ERROR_NO_AGGREGATION; |
158 | 0 | } |
159 | 0 | |
160 | 0 | nsCOMPtr<nsIMutableArray> inst = Create(); |
161 | 0 | return inst->QueryInterface(aIID, aResult); |
162 | 0 | } |
163 | | |
164 | | already_AddRefed<nsIMutableArray> |
165 | | nsArrayBase::Create() |
166 | 0 | { |
167 | 0 | nsCOMPtr<nsIMutableArray> inst; |
168 | 0 | if (NS_IsMainThread()) { |
169 | 0 | inst = new nsArrayCC; |
170 | 0 | } else { |
171 | 0 | inst = new nsArray; |
172 | 0 | } |
173 | 0 | return inst.forget(); |
174 | 0 | } |