2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-06-28 21:36:56 +04:00
|
|
|
|
2006-07-02 11:23:10 +04:00
|
|
|
#ifndef nsTObserverArray_h___
|
|
|
|
#define nsTObserverArray_h___
|
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2007-12-20 10:30:04 +03:00
|
|
|
#include "nsTArray.h"
|
2012-11-15 11:32:39 +04:00
|
|
|
#include "nsCycleCollectionNoteChild.h"
|
2006-06-28 21:36:56 +04:00
|
|
|
|
2016-11-28 19:03:53 +03:00
|
|
|
#include <functional>
|
|
|
|
|
2011-01-14 20:15:27 +03:00
|
|
|
/**
|
|
|
|
* An array of observers. Like a normal array, but supports iterators that are
|
|
|
|
* stable even if the array is modified during iteration.
|
|
|
|
* The template parameter T is the observer type the array will hold;
|
|
|
|
* N is the number of built-in storage slots that come with the array.
|
|
|
|
* NOTE: You probably want to use nsTObserverArray, unless you specifically
|
|
|
|
* want built-in storage. See below.
|
|
|
|
* @see nsTObserverArray, nsTArray
|
|
|
|
*/
|
|
|
|
|
2014-08-28 02:47:27 +04:00
|
|
|
class nsTObserverArray_base
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef size_t index_type;
|
|
|
|
typedef size_t size_type;
|
|
|
|
typedef ptrdiff_t diff_type;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
class Iterator_base
|
|
|
|
{
|
2007-12-22 04:06:43 +03:00
|
|
|
protected:
|
2014-06-27 05:35:39 +04:00
|
|
|
friend class nsTObserverArray_base;
|
2006-06-28 21:36:56 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
Iterator_base(index_type aPosition, Iterator_base* aNext)
|
|
|
|
: mPosition(aPosition)
|
|
|
|
, mNext(aNext)
|
|
|
|
{
|
2006-06-28 21:36:56 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// The current position of the iterator. Its exact meaning differs
|
|
|
|
// depending on iterator. See nsTObserverArray<T>::ForwardIterator.
|
|
|
|
index_type mPosition;
|
2007-12-21 14:22:31 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// The next iterator currently iterating the same array
|
|
|
|
Iterator_base* mNext;
|
|
|
|
};
|
2006-06-28 21:36:56 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsTObserverArray_base() : mIterators(nullptr) {}
|
2007-12-14 04:41:48 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
~nsTObserverArray_base()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mIterators == nullptr, "iterators outlasting array");
|
|
|
|
}
|
2006-06-28 21:36:56 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
/**
|
|
|
|
* Adjusts iterators after an element has been inserted or removed
|
|
|
|
* from the array.
|
|
|
|
* @param aModPos Position where elements were added or removed.
|
|
|
|
* @param aAdjustment -1 if an element was removed, 1 if an element was
|
|
|
|
* added.
|
|
|
|
*/
|
|
|
|
void AdjustIterators(index_type aModPos, diff_type aAdjustment);
|
2007-12-20 10:30:04 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
/**
|
|
|
|
* Clears iterators when the array is destroyed.
|
|
|
|
*/
|
|
|
|
void ClearIterators();
|
2006-07-02 11:23:10 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
mutable Iterator_base* mIterators;
|
|
|
|
};
|
2006-07-02 11:23:10 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class T, size_t N>
|
|
|
|
class nsAutoTObserverArray : protected nsTObserverArray_base
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T elem_type;
|
|
|
|
typedef nsTArray<T> array_type;
|
|
|
|
|
|
|
|
nsAutoTObserverArray() {}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Accessor methods
|
|
|
|
//
|
|
|
|
|
|
|
|
// @return The number of elements in the array.
|
|
|
|
size_type Length() const { return mArray.Length(); }
|
|
|
|
|
|
|
|
// @return True if the array is empty or false otherwise.
|
|
|
|
bool IsEmpty() const { return mArray.IsEmpty(); }
|
|
|
|
|
2017-06-18 09:43:55 +03:00
|
|
|
// This method provides direct, readonly access to the array elements.
|
|
|
|
// @return A pointer to the first element of the array. If the array is
|
|
|
|
// empty, then this pointer must not be dereferenced.
|
|
|
|
const elem_type* Elements() const
|
|
|
|
{
|
|
|
|
return mArray.Elements();
|
|
|
|
}
|
|
|
|
elem_type* Elements()
|
|
|
|
{
|
|
|
|
return mArray.Elements();
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// This method provides direct access to an element of the array. The given
|
|
|
|
// index must be within the array bounds. If the underlying array may change
|
|
|
|
// during iteration, use an iterator instead of this function.
|
|
|
|
// @param aIndex The index of an element in the array.
|
|
|
|
// @return A reference to the i'th element of the array.
|
|
|
|
elem_type& ElementAt(index_type aIndex)
|
|
|
|
{
|
|
|
|
return mArray.ElementAt(aIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same as above, but readonly.
|
|
|
|
const elem_type& ElementAt(index_type aIndex) const
|
|
|
|
{
|
|
|
|
return mArray.ElementAt(aIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method provides direct access to an element of the array in a bounds
|
|
|
|
// safe manner. If the requested index is out of bounds the provided default
|
|
|
|
// value is returned.
|
|
|
|
// @param aIndex The index of an element in the array.
|
|
|
|
// @param aDef The value to return if the index is out of bounds.
|
|
|
|
elem_type& SafeElementAt(index_type aIndex, elem_type& aDef)
|
|
|
|
{
|
|
|
|
return mArray.SafeElementAt(aIndex, aDef);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same as above, but readonly.
|
|
|
|
const elem_type& SafeElementAt(index_type aIndex, const elem_type& aDef) const
|
|
|
|
{
|
|
|
|
return mArray.SafeElementAt(aIndex, aDef);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No operator[] is provided because the point of this class is to support
|
|
|
|
// allow modifying the array during iteration, and ElementAt() is not safe
|
|
|
|
// in those conditions.
|
|
|
|
|
|
|
|
//
|
|
|
|
// Search methods
|
|
|
|
//
|
|
|
|
|
|
|
|
// This method searches, starting from the beginning of the array,
|
|
|
|
// for the first element in this array that is equal to the given element.
|
|
|
|
// 'operator==' must be defined for elem_type.
|
|
|
|
// @param aItem The item to search for.
|
|
|
|
// @return true if the element was found.
|
|
|
|
template<class Item>
|
|
|
|
bool Contains(const Item& aItem) const
|
|
|
|
{
|
|
|
|
return IndexOf(aItem) != array_type::NoIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method searches for the offset of the first element in this
|
|
|
|
// array that is equal to the given element.
|
|
|
|
// 'operator==' must be defined for elem_type.
|
|
|
|
// @param aItem The item to search for.
|
|
|
|
// @param aStart The index to start from.
|
|
|
|
// @return The index of the found element or NoIndex if not found.
|
|
|
|
template<class Item>
|
|
|
|
index_type IndexOf(const Item& aItem, index_type aStart = 0) const
|
|
|
|
{
|
|
|
|
return mArray.IndexOf(aItem, aStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Mutation methods
|
|
|
|
//
|
|
|
|
|
|
|
|
// Insert a given element at the given index.
|
|
|
|
// @param aIndex The index at which to insert item.
|
|
|
|
// @param aItem The item to insert,
|
|
|
|
// @return A pointer to the newly inserted element, or a null on DOM
|
|
|
|
template<class Item>
|
|
|
|
elem_type* InsertElementAt(index_type aIndex, const Item& aItem)
|
|
|
|
{
|
|
|
|
elem_type* item = mArray.InsertElementAt(aIndex, aItem);
|
|
|
|
AdjustIterators(aIndex, 1);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same as above but without copy constructing.
|
|
|
|
// This is useful to avoid temporaries.
|
|
|
|
elem_type* InsertElementAt(index_type aIndex)
|
|
|
|
{
|
|
|
|
elem_type* item = mArray.InsertElementAt(aIndex);
|
|
|
|
AdjustIterators(aIndex, 1);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend an element to the array unless it already exists in the array.
|
|
|
|
// 'operator==' must be defined for elem_type.
|
|
|
|
// @param aItem The item to prepend.
|
|
|
|
// @return true if the element was found, or inserted successfully.
|
|
|
|
template<class Item>
|
|
|
|
bool PrependElementUnlessExists(const Item& aItem)
|
|
|
|
{
|
|
|
|
if (Contains(aItem)) {
|
|
|
|
return true;
|
2007-12-14 04:41:48 +03:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
bool inserted = mArray.InsertElementAt(0, aItem) != nullptr;
|
|
|
|
AdjustIterators(0, 1);
|
|
|
|
return inserted;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append an element to the array.
|
|
|
|
// @param aItem The item to append.
|
|
|
|
// @return A pointer to the newly appended element, or null on OOM.
|
|
|
|
template<class Item>
|
|
|
|
elem_type* AppendElement(const Item& aItem)
|
|
|
|
{
|
|
|
|
return mArray.AppendElement(aItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same as above, but without copy-constructing. This is useful to avoid
|
|
|
|
// temporaries.
|
|
|
|
elem_type* AppendElement()
|
|
|
|
{
|
|
|
|
return mArray.AppendElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append an element to the array unless it already exists in the array.
|
|
|
|
// 'operator==' must be defined for elem_type.
|
|
|
|
// @param aItem The item to append.
|
|
|
|
// @return true if the element was found, or inserted successfully.
|
|
|
|
template<class Item>
|
|
|
|
bool AppendElementUnlessExists(const Item& aItem)
|
|
|
|
{
|
|
|
|
return Contains(aItem) || AppendElement(aItem) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an element from the array.
|
|
|
|
// @param aIndex The index of the item to remove.
|
|
|
|
void RemoveElementAt(index_type aIndex)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aIndex < mArray.Length(), "invalid index");
|
|
|
|
mArray.RemoveElementAt(aIndex);
|
|
|
|
AdjustIterators(aIndex, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This helper function combines IndexOf with RemoveElementAt to "search
|
|
|
|
// and destroy" the first element that is equal to the given element.
|
|
|
|
// 'operator==' must be defined for elem_type.
|
|
|
|
// @param aItem The item to search for.
|
|
|
|
// @return true if the element was found and removed.
|
|
|
|
template<class Item>
|
|
|
|
bool RemoveElement(const Item& aItem)
|
|
|
|
{
|
|
|
|
index_type index = mArray.IndexOf(aItem, 0);
|
|
|
|
if (index == array_type::NoIndex) {
|
|
|
|
return false;
|
2007-12-20 10:30:04 +03:00
|
|
|
}
|
2006-06-28 21:36:56 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
mArray.RemoveElementAt(index);
|
|
|
|
AdjustIterators(index, -1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-28 05:00:06 +03:00
|
|
|
// See nsTArray::RemoveElementsBy.
|
2017-06-18 01:03:47 +03:00
|
|
|
template <typename Predicate>
|
|
|
|
void RemoveElementsBy(Predicate aPredicate)
|
2016-07-28 05:00:06 +03:00
|
|
|
{
|
|
|
|
index_type i = 0;
|
|
|
|
mArray.RemoveElementsBy([&](const elem_type& aItem) {
|
|
|
|
if (aPredicate(aItem)) {
|
|
|
|
// This element is going to be removed.
|
|
|
|
AdjustIterators(i, -1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// Removes all observers and collapses all iterators to the beginning of
|
|
|
|
// the array. The result is that forward iterators will see all elements
|
|
|
|
// in the array.
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
mArray.Clear();
|
|
|
|
ClearIterators();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compact the array to minimize the memory it uses
|
|
|
|
void Compact() { mArray.Compact(); }
|
|
|
|
|
|
|
|
// Returns the number of bytes on the heap taken up by this object, not
|
2015-07-29 09:24:24 +03:00
|
|
|
// including sizeof(*this). If you want to measure anything hanging off the
|
|
|
|
// array, you must iterate over the elements and measure them individually;
|
|
|
|
// hence the "Shallow" prefix.
|
|
|
|
size_t ShallowSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2015-07-29 09:24:24 +03:00
|
|
|
return mArray.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Iterators
|
|
|
|
//
|
|
|
|
|
|
|
|
// Base class for iterators. Do not use this directly.
|
|
|
|
class Iterator : public Iterator_base
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
friend class nsAutoTObserverArray;
|
|
|
|
typedef nsAutoTObserverArray<T, N> array_type;
|
|
|
|
|
|
|
|
Iterator(index_type aPosition, const array_type& aArray)
|
|
|
|
: Iterator_base(aPosition, aArray.mIterators)
|
|
|
|
, mArray(const_cast<array_type&>(aArray))
|
|
|
|
{
|
|
|
|
aArray.mIterators = this;
|
2007-12-20 10:30:04 +03:00
|
|
|
}
|
2007-12-19 04:06:34 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
~Iterator()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mArray.mIterators == this,
|
|
|
|
"Iterators must currently be destroyed in opposite order "
|
|
|
|
"from the construction order. It is suggested that you "
|
|
|
|
"simply put them on the stack");
|
|
|
|
mArray.mIterators = mNext;
|
2007-12-19 04:06:34 +03:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// The array we're iterating
|
|
|
|
array_type& mArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Iterates the array forward from beginning to end. mPosition points
|
|
|
|
// to the element that will be returned on next call to GetNext.
|
|
|
|
// Elements:
|
|
|
|
// - prepended to the array during iteration *will not* be traversed
|
|
|
|
// - appended during iteration *will* be traversed
|
|
|
|
// - removed during iteration *will not* be traversed.
|
|
|
|
// @see EndLimitedIterator
|
|
|
|
class ForwardIterator : protected Iterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef nsAutoTObserverArray<T, N> array_type;
|
|
|
|
typedef Iterator base_type;
|
2007-12-19 04:06:34 +03:00
|
|
|
|
2014-07-30 04:43:56 +04:00
|
|
|
explicit ForwardIterator(const array_type& aArray)
|
2014-06-27 05:35:39 +04:00
|
|
|
: Iterator(0, aArray)
|
|
|
|
{
|
2007-12-19 09:03:05 +03:00
|
|
|
}
|
2007-12-19 04:06:34 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
ForwardIterator(const array_type& aArray, index_type aPos)
|
|
|
|
: Iterator(aPos, aArray)
|
|
|
|
{
|
2012-06-29 12:32:21 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
bool operator<(const ForwardIterator& aOther) const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(&this->mArray == &aOther.mArray,
|
|
|
|
"not iterating the same array");
|
|
|
|
return base_type::mPosition < aOther.mPosition;
|
2012-06-29 12:32:21 +04:00
|
|
|
}
|
2007-12-20 10:30:04 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// Returns true if there are more elements to iterate.
|
|
|
|
// This must precede a call to GetNext(). If false is
|
|
|
|
// returned, GetNext() must not be called.
|
|
|
|
bool HasMore() const
|
|
|
|
{
|
|
|
|
return base_type::mPosition < base_type::mArray.Length();
|
2007-12-19 09:03:05 +03:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// Returns the next element and steps one step. This must
|
|
|
|
// be preceded by a call to HasMore().
|
|
|
|
// @return The next observer.
|
|
|
|
elem_type& GetNext()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(HasMore(), "iterating beyond end of array");
|
2017-06-18 09:43:55 +03:00
|
|
|
return base_type::mArray.Elements()[base_type::mPosition++];
|
2007-12-19 09:03:05 +03:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
};
|
2007-12-19 09:03:05 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// EndLimitedIterator works like ForwardIterator, but will not iterate new
|
|
|
|
// observers appended to the array after the iterator was created.
|
|
|
|
class EndLimitedIterator : protected ForwardIterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef nsAutoTObserverArray<T, N> array_type;
|
|
|
|
typedef Iterator base_type;
|
2007-12-19 09:03:05 +03:00
|
|
|
|
2014-07-30 04:43:56 +04:00
|
|
|
explicit EndLimitedIterator(const array_type& aArray)
|
2014-06-27 05:35:39 +04:00
|
|
|
: ForwardIterator(aArray)
|
|
|
|
, mEnd(aArray, aArray.Length())
|
|
|
|
{
|
2007-12-20 10:30:04 +03:00
|
|
|
}
|
2007-12-19 09:03:05 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// Returns true if there are more elements to iterate.
|
|
|
|
// This must precede a call to GetNext(). If false is
|
|
|
|
// returned, GetNext() must not be called.
|
|
|
|
bool HasMore() const { return *this < mEnd; }
|
|
|
|
|
|
|
|
// Returns the next element and steps one step. This must
|
|
|
|
// be preceded by a call to HasMore().
|
|
|
|
// @return The next observer.
|
|
|
|
elem_type& GetNext()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(HasMore(), "iterating beyond end of array");
|
2017-06-18 09:43:55 +03:00
|
|
|
return base_type::mArray.Elements()[base_type::mPosition++];
|
2007-12-20 10:30:04 +03:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
private:
|
|
|
|
ForwardIterator mEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Iterates the array backward from end to start. mPosition points
|
|
|
|
// to the element that was returned last.
|
|
|
|
// Elements:
|
|
|
|
// - prepended to the array during iteration *will* be traversed,
|
|
|
|
// unless the iteration already arrived at the first element
|
|
|
|
// - appended during iteration *will not* be traversed
|
|
|
|
// - removed during iteration *will not* be traversed.
|
|
|
|
class BackwardIterator : protected Iterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef nsAutoTObserverArray<T, N> array_type;
|
|
|
|
typedef Iterator base_type;
|
2007-12-20 10:30:04 +03:00
|
|
|
|
2014-07-30 04:43:56 +04:00
|
|
|
explicit BackwardIterator(const array_type& aArray)
|
2014-06-27 05:35:39 +04:00
|
|
|
: Iterator(aArray.Length(), aArray)
|
|
|
|
{
|
2007-12-20 10:30:04 +03:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// Returns true if there are more elements to iterate.
|
|
|
|
// This must precede a call to GetNext(). If false is
|
|
|
|
// returned, GetNext() must not be called.
|
|
|
|
bool HasMore() const { return base_type::mPosition > 0; }
|
|
|
|
|
|
|
|
// Returns the next element and steps one step. This must
|
|
|
|
// be preceded by a call to HasMore().
|
|
|
|
// @return The next observer.
|
|
|
|
elem_type& GetNext()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(HasMore(), "iterating beyond start of array");
|
2017-06-18 09:43:55 +03:00
|
|
|
return base_type::mArray.Elements()[--base_type::mPosition];
|
2013-12-10 16:28:31 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// Removes the element at the current iterator position.
|
|
|
|
// (the last element returned from |GetNext()|)
|
|
|
|
// This will not affect the next call to |GetNext()|
|
|
|
|
void Remove()
|
|
|
|
{
|
|
|
|
return base_type::mArray.RemoveElementAt(base_type::mPosition);
|
2011-09-15 04:37:45 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
};
|
2011-09-15 04:37:45 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
protected:
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<T, N> mArray;
|
2007-12-20 10:30:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
2014-06-27 05:35:39 +04:00
|
|
|
class nsTObserverArray : public nsAutoTObserverArray<T, 0>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef nsAutoTObserverArray<T, 0> base_type;
|
|
|
|
typedef nsTObserverArray_base::size_type size_type;
|
2007-12-20 10:30:04 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
//
|
|
|
|
// Initialization methods
|
|
|
|
//
|
2007-12-20 10:30:04 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsTObserverArray() {}
|
2007-12-20 10:30:04 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// Initialize this array and pre-allocate some number of elements.
|
|
|
|
explicit nsTObserverArray(size_type aCapacity)
|
|
|
|
{
|
|
|
|
base_type::mArray.SetCapacity(aCapacity);
|
|
|
|
}
|
2006-06-28 21:36:56 +04:00
|
|
|
};
|
2006-07-02 11:23:10 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<typename T, size_t N>
|
2012-11-15 11:32:39 +04:00
|
|
|
inline void
|
2012-11-23 23:13:39 +04:00
|
|
|
ImplCycleCollectionUnlink(nsAutoTObserverArray<T, N>& aField)
|
2012-11-15 11:32:39 +04:00
|
|
|
{
|
|
|
|
aField.Clear();
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<typename T, size_t N>
|
2012-11-15 11:32:39 +04:00
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
2012-11-23 23:13:39 +04:00
|
|
|
nsAutoTObserverArray<T, N>& aField,
|
2012-11-15 11:32:39 +04:00
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
aFlags |= CycleCollectionEdgeNameArrayFlag;
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t length = aField.Length();
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2012-11-23 22:50:05 +04:00
|
|
|
ImplCycleCollectionTraverse(aCallback, aField.ElementAt(i), aName, aFlags);
|
2012-11-15 11:32:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-16 02:41:58 +04:00
|
|
|
// XXXbz I wish I didn't have to pass in the observer type, but I
|
|
|
|
// don't see a way to get it out of array_.
|
2007-12-20 10:30:04 +03:00
|
|
|
// Note that this macro only works if the array holds pointers to XPCOM objects.
|
2010-07-21 19:33:31 +04:00
|
|
|
#define NS_OBSERVER_ARRAY_NOTIFY_XPCOM_OBSERVERS(array_, obstype_, func_, params_) \
|
2017-04-06 04:06:44 +03:00
|
|
|
do { \
|
2007-12-20 10:30:04 +03:00
|
|
|
nsTObserverArray<obstype_ *>::ForwardIterator iter_(array_); \
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<obstype_> obs_; \
|
2007-12-20 10:30:04 +03:00
|
|
|
while (iter_.HasMore()) { \
|
|
|
|
obs_ = iter_.GetNext(); \
|
2006-08-16 02:41:58 +04:00
|
|
|
obs_ -> func_ params_ ; \
|
|
|
|
} \
|
2017-04-06 04:06:44 +03:00
|
|
|
} while(0)
|
2006-08-16 02:41:58 +04:00
|
|
|
|
2010-07-21 19:33:31 +04:00
|
|
|
// Note that this macro only works if the array holds pointers to XPCOM objects.
|
|
|
|
#define NS_OBSERVER_ARRAY_NOTIFY_OBSERVERS(array_, obstype_, func_, params_) \
|
2017-04-06 04:06:44 +03:00
|
|
|
do { \
|
2010-07-21 19:33:31 +04:00
|
|
|
nsTObserverArray<obstype_ *>::ForwardIterator iter_(array_); \
|
|
|
|
obstype_* obs_; \
|
|
|
|
while (iter_.HasMore()) { \
|
|
|
|
obs_ = iter_.GetNext(); \
|
|
|
|
obs_ -> func_ params_ ; \
|
|
|
|
} \
|
2017-04-06 04:06:44 +03:00
|
|
|
} while(0)
|
2015-03-14 08:34:40 +03:00
|
|
|
|
|
|
|
#define NS_OBSERVER_ARRAY_NOTIFY_OBSERVERS_WITH_QI(array_, basetype_, obstype_, func_, params_) \
|
2017-04-06 04:06:44 +03:00
|
|
|
do { \
|
2015-03-14 08:34:40 +03:00
|
|
|
nsTObserverArray<basetype_ *>::ForwardIterator iter_(array_); \
|
|
|
|
basetype_* obsbase_; \
|
|
|
|
while (iter_.HasMore()) { \
|
|
|
|
obsbase_ = iter_.GetNext(); \
|
|
|
|
nsCOMPtr<obstype_> obs_ = do_QueryInterface(obsbase_); \
|
|
|
|
if (obs_) { \
|
|
|
|
obs_ -> func_ params_ ; \
|
|
|
|
} \
|
|
|
|
} \
|
2017-04-06 04:06:44 +03:00
|
|
|
} while(0)
|
2006-07-02 11:23:10 +04:00
|
|
|
#endif // nsTObserverArray_h___
|