53 template <
class ObjectClass,
class TypeOfCriticalSectionToUse = DummyCriticalSection>
69 values.addArray (other.begin(), other.size());
73 o->incReferenceCount();
78 : values (std::move (other.values))
83 template <
class OtherObjectClass,
class OtherCriticalSection>
87 values.addArray (other.begin(), other.size());
91 o->incReferenceCount();
100 auto otherCopy = other;
108 template <
class OtherObjectClass>
111 auto otherCopy = other;
120 values = std::move (other.values);
140 values.setAllocatedSize (0);
154 inline int size() const noexcept
156 return values.size();
200 return values.getValueWithDefault (index);
209 return values[index];
220 return values.getFirst();
231 return values.getLast();
240 return values.begin();
247 inline ObjectClass**
begin() noexcept
249 return values.begin();
255 inline ObjectClass*
const*
begin() const noexcept
257 return values.begin();
263 inline ObjectClass**
end() noexcept
271 inline ObjectClass*
const*
end() const noexcept
279 inline ObjectClass**
data() noexcept
287 inline ObjectClass*
const*
data() const noexcept
298 int indexOf (
const ObjectClass* objectToLookFor)
const noexcept
301 auto* e = values.begin();
302 auto* endPointer = values.end();
304 while (e != endPointer)
306 if (objectToLookFor == *e)
307 return static_cast<int> (e - values.begin());
327 bool contains (
const ObjectClass* objectToLookFor)
const noexcept
330 auto* e = values.begin();
331 auto* endPointer = values.end();
333 while (e != endPointer)
335 if (objectToLookFor == *e)
358 ObjectClass*
add (ObjectClass* newObject)
361 values.add (newObject);
363 if (newObject !=
nullptr)
364 newObject->incReferenceCount();
391 ObjectClass*
insert (
int indexToInsertAt, ObjectClass* newObject)
393 values.insert (indexToInsertAt, newObject, 1);
395 if (newObject !=
nullptr)
396 newObject->incReferenceCount();
457 void set (
int indexToChange, ObjectClass* newObject)
459 if (indexToChange >= 0)
463 if (newObject !=
nullptr)
464 newObject->incReferenceCount();
466 if (indexToChange < values.size())
468 auto* e = values[indexToChange];
469 values[indexToChange] = newObject;
474 values.add (newObject);
504 int numElementsToAdd = -1) noexcept
511 auto numElementsAdded = values.addArray (arrayToAddFrom.values, startIndex, numElementsToAdd);
512 auto** e = values.end();
514 for (
int i = 0; i < numElementsAdded; ++i)
515 (*(--e))->incReferenceCount();
531 template <
class ElementComparator>
532 int addSorted (ElementComparator& comparator, ObjectClass* newObject) noexcept
535 auto index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size());
536 insert (index, newObject);
545 template <
class ElementComparator>
549 auto index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size());
551 if (index > 0 && comparator.compareElements (newObject, values[index - 1]) == 0)
552 set (index - 1, newObject);
554 insert (index, newObject);
569 template <
class ElementComparator>
571 const ObjectClass* objectToLookFor)
const noexcept
573 ignoreUnused (comparator);
575 int s = 0, e = values.size();
579 if (comparator.compareElements (objectToLookFor, values[s]) == 0)
582 auto halfway = (s + e) / 2;
587 if (comparator.compareElements (objectToLookFor, values[halfway]) >= 0)
614 if (isPositiveAndBelow (indexToRemove, values.size()))
616 auto* e = *(values.begin() + indexToRemove);
617 values.removeElements (indexToRemove, 1);
620 if ((values.size() << 1) < values.capacity())
639 if (isPositiveAndBelow (indexToRemove, values.size()))
641 auto* e = *(values.begin() + indexToRemove);
643 values.removeElements (indexToRemove, 1);
646 if ((values.size() << 1) < values.capacity())
696 startIndex = jlimit (0, values.size(), startIndex);
697 auto endIndex = jlimit (0, values.size(), startIndex + numberToRemove);
698 numberToRemove = endIndex - startIndex;
700 if (numberToRemove > 0)
703 objectsToRemove.
addArray (values.begin() + startIndex, numberToRemove);
705 values.removeElements (startIndex, numberToRemove);
707 for (
auto& o : objectsToRemove)
710 if ((values.size() << 1) < values.capacity())
727 if (howManyToRemove > values.size())
728 howManyToRemove = values.size();
730 while (--howManyToRemove >= 0)
731 remove (values.size() - 1);
739 void swap (
int index1,
int index2) noexcept
743 if (isPositiveAndBelow (index1, values.size())
744 && isPositiveAndBelow (index2, values.size()))
746 std::swap (values[index1], values[index2]);
763 void move (
int currentIndex,
int newIndex) noexcept
765 if (currentIndex != newIndex)
768 values.move (currentIndex, newIndex);
778 template <
class OtherArrayType>
779 void swapWith (OtherArrayType& otherArray) noexcept
782 const typename OtherArrayType::ScopedLockType lock2 (otherArray.getLock());
783 values.swapWith (otherArray.values);
795 return values == other.values;
834 template <
class ElementComparator>
835 void sort (ElementComparator& comparator,
836 bool retainOrderOfEquivalentItems =
false) noexcept
840 ignoreUnused (comparator);
843 sortArray (comparator, values.begin(), 0, values.size() - 1, retainOrderOfEquivalentItems);
856 values.shrinkToNoMoreThan (values.size());
868 values.ensureAllocatedSize (minNumElements);
876 inline const TypeOfCriticalSectionToUse&
getLock() const noexcept {
return values; }
892 void releaseAllObjects()
894 auto i = values.size();
899 values.removeElements (i, 1);
904 static void releaseObject (ObjectClass* o)
906 if (o !=
nullptr && o->decReferenceCountWithoutDeleting())
907 ContainerDeletePolicy<ObjectClass>::destroy (o);
A basic object container.
Holds a resizable array of primitive or copy-by-value objects.
void addArray(const Type *elementsToAdd, int numElementsToAdd)
Adds elements from an array to the end of this array.
Holds a list of objects derived from ReferenceCountedObject, or which implement basic reference-count...
ObjectClass ** data() noexcept
Returns a pointer to the first element in the array.
ReferenceCountedArray(ReferenceCountedArray &&other) noexcept
Moves from another array.
ObjectClass *const * end() const noexcept
Returns a pointer to the element which follows the last element in the array.
bool isEmpty() const noexcept
Returns true if the array is empty, false otherwise.
int indexOf(const ObjectClass *objectToLookFor) const noexcept
Finds the index of the first occurrence of an object in the array.
ReferenceCountedArray(const ReferenceCountedArray &other) noexcept
Creates a copy of another array.
int size() const noexcept
Returns the current number of objects in the array.
void removeLast(int howManyToRemove=1)
Removes the last n objects from the array.
void minimiseStorageOverheads() noexcept
Reduces the amount of storage being used by the array.
void set(int indexToChange, ObjectClass *newObject)
Replaces an object in the array with a different one.
ObjectClass *const * begin() const noexcept
Returns a pointer to the first element in the array.
ReferenceCountedArray()=default
Creates an empty array.
ObjectClass * add(ObjectClass *newObject)
Appends a new object to the end of the array.
ObjectClass * add(const ObjectClassPtr &newObject)
Appends a new object to the end of the array.
bool operator!=(const ReferenceCountedArray< ObjectClass, TypeOfCriticalSectionToUse > &other) const noexcept
Compares this array to another one.
const TypeOfCriticalSectionToUse & getLock() const noexcept
Returns the CriticalSection that locks this array.
int indexOfSorted(ElementComparator &comparator, const ObjectClass *objectToLookFor) const noexcept
Finds the index of an object in the array, assuming that the array is sorted.
typename TypeOfCriticalSectionToUse::ScopedLockType ScopedLockType
Returns the type of scoped lock to use for locking this array.
bool addIfNotAlreadyThere(ObjectClass *newObject)
Appends a new object at the end of the array as long as the array doesn't already contain it.
ObjectClassPtr operator[](int index) const noexcept
Returns a pointer to the object at this index in the array.
void remove(int indexToRemove)
Removes an object from the array.
void sort(ElementComparator &comparator, bool retainOrderOfEquivalentItems=false) noexcept
Sorts the elements in the array.
ObjectClass ** begin() noexcept
Returns a pointer to the first element in the array.
ReferenceCountedArray(const ReferenceCountedArray< OtherObjectClass, OtherCriticalSection > &other) noexcept
Creates a copy of another array.
ObjectClass ** getRawDataPointer() const noexcept
Returns a pointer to the actual array data.
bool operator==(const ReferenceCountedArray &other) const noexcept
Compares this array to another one.
void removeObject(ObjectClass *objectToRemove)
Removes the first occurrence of a specified object from the array.
ObjectClass * getObjectPointerUnchecked(int index) const noexcept
Returns a raw pointer to the object at this index in the array, without checking whether the index is...
ObjectClass * getObjectPointer(int index) const noexcept
Returns a raw pointer to the object at this index in the array.
void swap(int index1, int index2) noexcept
Swaps a pair of objects in the array.
ObjectClassPtr getFirst() const noexcept
Returns a pointer to the first object in the array.
void ensureStorageAllocated(const int minNumElements)
Increases the array's internal storage to hold a minimum number of elements.
~ReferenceCountedArray()
Destructor.
void clearQuick()
Removes all objects from the array without freeing the array's allocated storage.
void set(int indexToChange, const ObjectClassPtr &newObject)
Replaces an object in the array with a different one.
ObjectClassPtr getUnchecked(int index) const noexcept
Returns a pointer to the object at this index in the array, without checking whether the index is in-...
void addOrReplaceSorted(ElementComparator &comparator, ObjectClass *newObject) noexcept
Inserts or replaces an object in the array, assuming it is sorted.
bool contains(const ObjectClass *objectToLookFor) const noexcept
Returns true if the array contains a specified object.
void removeRange(int startIndex, int numberToRemove)
Removes a range of objects from the array.
void move(int currentIndex, int newIndex) noexcept
Moves one of the objects to a different position.
ObjectClassPtr removeAndReturn(int indexToRemove)
Removes and returns an object from the array.
ObjectClass ** end() noexcept
Returns a pointer to the element which follows the last element in the array.
void clear()
Removes all objects from the array.
bool contains(const ObjectClassPtr &objectToLookFor) const noexcept
Returns true if the array contains a specified object.
ObjectClass * insert(int indexToInsertAt, ObjectClass *newObject)
Inserts a new object into the array at the given index.
ObjectClass * insert(int indexToInsertAt, const ObjectClassPtr &newObject)
Inserts a new object into the array at the given index.
void swapWith(OtherArrayType &otherArray) noexcept
This swaps the contents of this array with those of another array.
bool addIfNotAlreadyThere(const ObjectClassPtr &newObject)
Appends a new object at the end of the array as long as the array doesn't already contain it.
ReferenceCountedArray & operator=(const ReferenceCountedArray &other) noexcept
Copies another array into this one.
int addSorted(ElementComparator &comparator, ObjectClass *newObject) noexcept
Inserts a new object into the array assuming that the array is sorted.
int indexOf(const ObjectClassPtr &objectToLookFor) const noexcept
Finds the index of the first occurrence of an object in the array.
ObjectClassPtr getLast() const noexcept
Returns a pointer to the last object in the array.
void addArray(const ReferenceCountedArray &arrayToAddFrom, int startIndex=0, int numElementsToAdd=-1) noexcept
Adds elements from another array to the end of this array.
ObjectClass *const * data() const noexcept
Returns a pointer to the first element in the array.
void removeObject(const ObjectClassPtr &objectToRemove)
Removes the first occurrence of a specified object from the array.
A smart-pointer class which points to a reference-counted object.
ReferencedType * get() const noexcept
Returns the object that this pointer references.