Backed out changeset 5f819082a015 (bug 1936189) for causing reftest failures @ native...
[gecko.git] / xpcom / ds / nsCOMArray.h
blob9b2abb3cbfe526b034eec1f4b25975b4579f57e2
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/. */
7 #ifndef nsCOMArray_h__
8 #define nsCOMArray_h__
10 #include "mozilla/Attributes.h"
11 #include "mozilla/ArrayIterator.h"
12 #include "mozilla/MemoryReporting.h"
14 #include "nsCycleCollectionNoteChild.h"
15 #include "nsTArray.h"
16 #include "nsISupports.h"
18 #include <iterator>
20 // See below for the definition of nsCOMArray<T>
22 // a class that's nsISupports-specific, so that we can contain the
23 // work of this class in the XPCOM dll
24 class nsCOMArray_base {
25 friend class nsArrayBase;
27 protected:
28 nsCOMArray_base() = default;
29 explicit nsCOMArray_base(int32_t aCount) : mArray(aCount) {}
30 nsCOMArray_base(const nsCOMArray_base& aOther);
31 nsCOMArray_base(nsCOMArray_base&& aOther) = default;
32 nsCOMArray_base& operator=(nsCOMArray_base&& aOther) = default;
33 ~nsCOMArray_base();
35 int32_t IndexOf(nsISupports* aObject, uint32_t aStartIndex = 0) const;
36 bool Contains(nsISupports* aObject) const { return IndexOf(aObject) != -1; }
38 int32_t IndexOfObject(nsISupports* aObject) const;
39 bool ContainsObject(nsISupports* aObject) const {
40 return IndexOfObject(aObject) != -1;
43 typedef bool (*nsBaseArrayEnumFunc)(void* aElement, void* aData);
45 // enumerate through the array with a callback.
46 bool EnumerateForwards(nsBaseArrayEnumFunc aFunc, void* aData) const;
48 bool EnumerateBackwards(nsBaseArrayEnumFunc aFunc, void* aData) const;
50 bool InsertObjectAt(nsISupports* aObject, int32_t aIndex);
51 void InsertElementAt(uint32_t aIndex, nsISupports* aElement);
52 void InsertElementAt(uint32_t aIndex, already_AddRefed<nsISupports> aElement);
53 bool InsertObjectsAt(const nsCOMArray_base& aObjects, int32_t aIndex);
54 void InsertElementsAt(uint32_t aIndex, const nsCOMArray_base& aElements);
55 void InsertElementsAt(uint32_t aIndex, nsISupports* const* aElements,
56 uint32_t aCount);
57 void ReplaceObjectAt(nsISupports* aObject, int32_t aIndex);
58 void ReplaceElementAt(uint32_t aIndex, nsISupports* aElement) {
59 nsISupports* oldElement = mArray[aIndex];
60 NS_IF_ADDREF(mArray[aIndex] = aElement);
61 NS_IF_RELEASE(oldElement);
63 bool AppendObject(nsISupports* aObject) {
64 return InsertObjectAt(aObject, Count());
66 void AppendElement(nsISupports* aElement) {
67 InsertElementAt(Length(), aElement);
69 void AppendElement(already_AddRefed<nsISupports> aElement) {
70 InsertElementAt(Length(), std::move(aElement));
73 bool AppendObjects(const nsCOMArray_base& aObjects) {
74 return InsertObjectsAt(aObjects, Count());
76 void AppendElements(const nsCOMArray_base& aElements) {
77 return InsertElementsAt(Length(), aElements);
79 void AppendElements(nsISupports* const* aElements, uint32_t aCount) {
80 return InsertElementsAt(Length(), aElements, aCount);
82 bool RemoveObject(nsISupports* aObject);
83 nsISupports** Elements() { return mArray.Elements(); }
84 void SwapElements(nsCOMArray_base& aOther) {
85 mArray.SwapElements(aOther.mArray);
88 public:
89 // elements in the array (including null elements!)
90 int32_t Count() const { return mArray.Length(); }
91 // nsTArray-compatible version
92 uint32_t Length() const { return mArray.Length(); }
93 bool IsEmpty() const { return mArray.IsEmpty(); }
95 // If the array grows, the newly created entries will all be null;
96 // if the array shrinks, the excess entries will all be released.
97 bool SetCount(int32_t aNewCount);
98 // nsTArray-compatible version
99 void TruncateLength(uint32_t aNewLength) {
100 if (mArray.Length() > aNewLength) {
101 RemoveElementsAt(aNewLength, mArray.Length() - aNewLength);
105 // remove all elements in the array, and call NS_RELEASE on each one
106 void Clear();
108 nsISupports* ObjectAt(int32_t aIndex) const { return mArray[aIndex]; }
109 // nsTArray-compatible version
110 nsISupports* ElementAt(uint32_t aIndex) const { return mArray[aIndex]; }
112 nsISupports* SafeObjectAt(int32_t aIndex) const {
113 return mArray.SafeElementAt(aIndex, nullptr);
115 // nsTArray-compatible version
116 nsISupports* SafeElementAt(uint32_t aIndex) const {
117 return mArray.SafeElementAt(aIndex, nullptr);
120 nsISupports* operator[](int32_t aIndex) const { return mArray[aIndex]; }
122 // remove an element at a specific position, shrinking the array
123 // as necessary
124 bool RemoveObjectAt(int32_t aIndex);
125 // nsTArray-compatible version
126 void RemoveElementAt(uint32_t aIndex);
128 // remove a range of elements at a specific position, shrinking the array
129 // as necessary
130 bool RemoveObjectsAt(int32_t aIndex, int32_t aCount);
131 // nsTArray-compatible version
132 void RemoveElementsAt(uint32_t aIndex, uint32_t aCount);
134 void SwapElementsAt(uint32_t aIndex1, uint32_t aIndex2) {
135 nsISupports* tmp = mArray[aIndex1];
136 mArray[aIndex1] = mArray[aIndex2];
137 mArray[aIndex2] = tmp;
140 // Ensures there is enough space to store a total of aCapacity objects.
141 // This method never deletes any objects.
142 void SetCapacity(uint32_t aCapacity) { mArray.SetCapacity(aCapacity); }
143 uint32_t Capacity() { return mArray.Capacity(); }
145 // Measures the size of the array's element storage. If you want to measure
146 // anything hanging off the array, you must iterate over the elements and
147 // measure them individually; hence the "Shallow" prefix. Note that because
148 // each element in an nsCOMArray<T> is actually a T* any such iteration
149 // should use a SizeOfIncludingThis() function on each element rather than a
150 // SizeOfExcludingThis() function, so that the memory taken by the T itself
151 // is included as well as anything it points to.
152 size_t ShallowSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
153 return mArray.ShallowSizeOfExcludingThis(aMallocSizeOf);
156 protected:
157 // the actual storage
158 nsTArray<nsISupports*> mArray;
160 private:
161 // don't implement these, defaults will muck with refcounts!
162 nsCOMArray_base& operator=(const nsCOMArray_base& aOther) = delete;
165 inline void ImplCycleCollectionUnlink(nsCOMArray_base& aField) {
166 aField.Clear();
169 inline void ImplCycleCollectionTraverse(
170 nsCycleCollectionTraversalCallback& aCallback, nsCOMArray_base& aField,
171 const char* aName, uint32_t aFlags = 0) {
172 aFlags |= CycleCollectionEdgeNameArrayFlag;
173 int32_t length = aField.Count();
174 for (int32_t i = 0; i < length; ++i) {
175 CycleCollectionNoteChild(aCallback, aField[i], aName, aFlags);
179 // a non-XPCOM, refcounting array of XPCOM objects
180 // used as a member variable or stack variable - this object is NOT
181 // refcounted, but the objects that it holds are
183 // most of the read-only accessors like ObjectAt()/etc do NOT refcount
184 // on the way out. This means that you can do one of two things:
186 // * does an addref, but holds onto a reference
187 // nsCOMPtr<T> foo = array[i];
189 // * avoids the refcount, but foo might go stale if array[i] is ever
190 // * modified/removed. Be careful not to NS_RELEASE(foo)!
191 // T* foo = array[i];
193 // This array will accept null as an argument for any object, and will store
194 // null in the array. But that also means that methods like ObjectAt() may
195 // return null when referring to an existing, but null entry in the array.
196 template <class T>
197 class nsCOMArray : public nsCOMArray_base {
198 public:
199 typedef int32_t index_type;
200 typedef mozilla::ArrayIterator<T*, nsCOMArray> iterator;
201 typedef mozilla::ArrayIterator<const T*, nsCOMArray> const_iterator;
202 typedef std::reverse_iterator<iterator> reverse_iterator;
203 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
205 nsCOMArray() = default;
206 explicit nsCOMArray(int32_t aCount) : nsCOMArray_base(aCount) {}
207 explicit nsCOMArray(const nsCOMArray<T>& aOther) : nsCOMArray_base(aOther) {}
208 nsCOMArray(nsCOMArray<T>&& aOther) = default;
209 ~nsCOMArray() = default;
211 // We have a move assignment operator, but no copy assignment operator.
212 nsCOMArray<T>& operator=(nsCOMArray<T>&& aOther) = default;
214 // these do NOT refcount on the way out, for speed
215 T* ObjectAt(int32_t aIndex) const {
216 return static_cast<T*>(nsCOMArray_base::ObjectAt(aIndex));
218 // nsTArray-compatible version
219 T* ElementAt(uint32_t aIndex) const {
220 return static_cast<T*>(nsCOMArray_base::ElementAt(aIndex));
223 // these do NOT refcount on the way out, for speed
224 T* SafeObjectAt(int32_t aIndex) const {
225 return static_cast<T*>(nsCOMArray_base::SafeObjectAt(aIndex));
227 // nsTArray-compatible version
228 T* SafeElementAt(uint32_t aIndex) const {
229 return static_cast<T*>(nsCOMArray_base::SafeElementAt(aIndex));
232 // indexing operator for syntactic sugar
233 T* operator[](int32_t aIndex) const { return ObjectAt(aIndex); }
235 // index of the element in question.. does NOT refcount
236 // note: this does not check COM object identity. Use
237 // IndexOfObject() for that purpose
238 int32_t IndexOf(T* aObject, uint32_t aStartIndex = 0) const {
239 return nsCOMArray_base::IndexOf(aObject, aStartIndex);
241 bool Contains(T* aObject) const { return nsCOMArray_base::Contains(aObject); }
243 // index of the element in question.. be careful!
244 // this is much slower than IndexOf() because it uses
245 // QueryInterface to determine actual COM identity of the object
246 // if you need to do this frequently then consider enforcing
247 // COM object identity before adding/comparing elements
248 int32_t IndexOfObject(T* aObject) const {
249 return nsCOMArray_base::IndexOfObject(aObject);
251 bool ContainsObject(nsISupports* aObject) const {
252 return nsCOMArray_base::ContainsObject(aObject);
255 // inserts aObject at aIndex, shifting the objects at aIndex and
256 // later to make space
257 bool InsertObjectAt(T* aObject, int32_t aIndex) {
258 return nsCOMArray_base::InsertObjectAt(aObject, aIndex);
260 // nsTArray-compatible version
261 void InsertElementAt(uint32_t aIndex, T* aElement) {
262 nsCOMArray_base::InsertElementAt(aIndex, aElement);
265 // inserts the objects from aObject at aIndex, shifting the
266 // objects at aIndex and later to make space
267 bool InsertObjectsAt(const nsCOMArray<T>& aObjects, int32_t aIndex) {
268 return nsCOMArray_base::InsertObjectsAt(aObjects, aIndex);
270 // nsTArray-compatible version
271 void InsertElementsAt(uint32_t aIndex, const nsCOMArray<T>& aElements) {
272 nsCOMArray_base::InsertElementsAt(aIndex, aElements);
274 void InsertElementsAt(uint32_t aIndex, T* const* aElements, uint32_t aCount) {
275 nsCOMArray_base::InsertElementsAt(
276 aIndex, reinterpret_cast<nsISupports* const*>(aElements), aCount);
279 // replaces an existing element. Warning: if the array grows,
280 // the newly created entries will all be null
281 void ReplaceObjectAt(T* aObject, int32_t aIndex) {
282 nsCOMArray_base::ReplaceObjectAt(aObject, aIndex);
284 // nsTArray-compatible version
285 void ReplaceElementAt(uint32_t aIndex, T* aElement) {
286 nsCOMArray_base::ReplaceElementAt(aIndex, aElement);
289 using TComparatorFunc = int (*)(T*, T*);
291 // The default sort function uses nsTArray::Sort.
292 // Note that the order of equal items is unstable with this.
293 void Sort(TComparatorFunc aFunc) {
294 mArray.Sort(
295 [aFunc](nsISupports* const& aLeft, nsISupports* const& aRight) -> int {
296 return aFunc(static_cast<T*>(aLeft), static_cast<T*>(aRight));
300 // Sort with a stable algorithm, uses nsTArray::StableSort.
301 void StableSort(TComparatorFunc aFunc) {
302 mArray.StableSort(
303 [aFunc](nsISupports* const& aLeft, nsISupports* const& aRight) -> int {
304 return aFunc(static_cast<T*>(aLeft), static_cast<T*>(aRight));
308 // append an object, growing the array as necessary
309 bool AppendObject(T* aObject) {
310 return nsCOMArray_base::AppendObject(aObject);
312 // nsTArray-compatible version
313 void AppendElement(T* aElement) { nsCOMArray_base::AppendElement(aElement); }
314 void AppendElement(already_AddRefed<T> aElement) {
315 nsCOMArray_base::AppendElement(std::move(aElement));
318 // append objects, growing the array as necessary
319 bool AppendObjects(const nsCOMArray<T>& aObjects) {
320 return nsCOMArray_base::AppendObjects(aObjects);
322 // nsTArray-compatible version
323 void AppendElements(const nsCOMArray<T>& aElements) {
324 return nsCOMArray_base::AppendElements(aElements);
326 void AppendElements(T* const* aElements, uint32_t aCount) {
327 InsertElementsAt(Length(), aElements, aCount);
330 // remove the first instance of the given object and shrink the
331 // array as necessary
332 // Warning: if you pass null here, it will remove the first null element
333 bool RemoveObject(T* aObject) {
334 return nsCOMArray_base::RemoveObject(aObject);
336 // nsTArray-compatible version
337 bool RemoveElement(T* aElement) {
338 return nsCOMArray_base::RemoveObject(aElement);
341 T** Elements() { return reinterpret_cast<T**>(nsCOMArray_base::Elements()); }
342 void SwapElements(nsCOMArray<T>& aOther) {
343 nsCOMArray_base::SwapElements(aOther);
346 // Methods for range-based for loops.
347 iterator begin() { return iterator(*this, 0); }
348 const_iterator begin() const { return const_iterator(*this, 0); }
349 const_iterator cbegin() const { return begin(); }
350 iterator end() { return iterator(*this, Length()); }
351 const_iterator end() const { return const_iterator(*this, Length()); }
352 const_iterator cend() const { return end(); }
354 // Methods for reverse iterating.
355 reverse_iterator rbegin() { return reverse_iterator(end()); }
356 const_reverse_iterator rbegin() const {
357 return const_reverse_iterator(end());
359 const_reverse_iterator crbegin() const { return rbegin(); }
360 reverse_iterator rend() { return reverse_iterator(begin()); }
361 const_reverse_iterator rend() const {
362 return const_reverse_iterator(begin());
364 const_reverse_iterator crend() const { return rend(); }
366 private:
367 // don't implement these!
368 nsCOMArray<T>& operator=(const nsCOMArray<T>& aOther) = delete;
371 template <typename T>
372 inline void ImplCycleCollectionUnlink(nsCOMArray<T>& aField) {
373 aField.Clear();
376 template <typename E>
377 inline void ImplCycleCollectionTraverse(
378 nsCycleCollectionTraversalCallback& aCallback, nsCOMArray<E>& aField,
379 const char* aName, uint32_t aFlags = 0) {
380 aFlags |= CycleCollectionEdgeNameArrayFlag;
381 int32_t length = aField.Count();
382 for (int32_t i = 0; i < length; ++i) {
383 CycleCollectionNoteChild(aCallback, aField[i], aName, aFlags);
387 #endif