51 template <
class ObjectClass,
52 class TypeOfCriticalSectionToUse = DummyCriticalSection>
73 : values (std::move (other.values))
78 OwnedArray (
const std::initializer_list<ObjectClass*>& items)
88 values = std::move (other.values);
93 template <
class OtherObjectClass,
class OtherCriticalSection>
95 : values (std::move (other.values))
100 template <
class OtherObjectClass,
class OtherCriticalSection>
105 values = std::move (other.values);
111 void clear (
bool deleteObjects =
true)
115 values.setAllocatedSize (0);
134 inline int size() const noexcept
136 return values.size();
153 inline ObjectClass*
operator[] (
const int index)
const noexcept
156 return values.getValueWithDefault (index);
167 return values[index];
178 return values.getFirst();
189 return values.getLast();
198 return values.begin();
205 inline ObjectClass**
begin() const noexcept
207 return values.begin();
213 inline ObjectClass**
end() const noexcept
221 inline ObjectClass**
data() const noexcept
232 int indexOf (
const ObjectClass* objectToLookFor)
const noexcept
235 auto** e = values.begin();
237 for (; e != values.end(); ++e)
238 if (objectToLookFor == *e)
239 return static_cast<int> (e - values.begin());
249 bool contains (
const ObjectClass* objectToLookFor)
const noexcept
252 auto** e = values.begin();
254 for (; e != values.end(); ++e)
255 if (objectToLookFor == *e)
274 ObjectClass*
add (ObjectClass* newObject) noexcept
277 values.add (newObject);
299 ObjectClass*
insert (
int indexToInsertAt, ObjectClass* newObject) noexcept
302 values.insert (indexToInsertAt, newObject, 1);
319 ObjectClass*
const* newObjects,
320 int numberOfElements)
322 if (numberOfElements > 0)
325 values.insertArray (indexToInsertAt, newObjects, numberOfElements);
361 ObjectClass*
set (
int indexToChange, ObjectClass* newObject,
bool deleteOldElement =
true)
363 if (indexToChange >= 0)
365 std::unique_ptr<ObjectClass> toDelete;
370 if (indexToChange < values.size())
372 if (deleteOldElement)
374 toDelete.reset (values[indexToChange]);
376 if (toDelete.get() == newObject)
380 values[indexToChange] = newObject;
384 values.add (newObject);
406 template <
class OtherArrayType>
407 void addArray (
const OtherArrayType& arrayToAddFrom,
409 int numElementsToAdd = -1)
411 const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
413 values.addArray (arrayToAddFrom, startIndex, numElementsToAdd);
417 template <
typename OtherArrayType>
418 void addArray (
const std::initializer_list<OtherArrayType>& items)
421 values.addArray (items);
438 template <
class OtherArrayType>
441 int numElementsToAdd = -1)
443 const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
452 if (numElementsToAdd < 0 || startIndex + numElementsToAdd > arrayToAddFrom.size())
453 numElementsToAdd = arrayToAddFrom.size() - startIndex;
455 jassert (numElementsToAdd >= 0);
456 values.ensureAllocatedSize (values.size() + numElementsToAdd);
458 while (--numElementsToAdd >= 0)
459 values.add (createCopyIfNotNull (arrayToAddFrom.getUnchecked (startIndex++)));
474 template <
class ElementComparator>
475 int addSorted (ElementComparator& comparator, ObjectClass*
const newObject) noexcept
479 ignoreUnused (comparator);
482 const int index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size());
483 insert (index, newObject);
499 template <
typename ElementComparator>
500 int indexOfSorted (ElementComparator& comparator,
const ObjectClass*
const objectToLookFor)
const noexcept
504 ignoreUnused (comparator);
507 int s = 0, e = values.size();
511 if (comparator.compareElements (objectToLookFor, values[s]) == 0)
514 auto halfway = (s + e) / 2;
519 if (comparator.compareElements (objectToLookFor, values[halfway]) >= 0)
539 void remove (
int indexToRemove,
bool deleteObject =
true)
541 std::unique_ptr<ObjectClass> toDelete;
546 if (isPositiveAndBelow (indexToRemove, values.size()))
548 auto** e = values.begin() + indexToRemove;
553 values.removeElements (indexToRemove, 1);
557 if ((values.size() << 1) < values.capacity())
572 ObjectClass* removedItem =
nullptr;
575 if (isPositiveAndBelow (indexToRemove, values.size()))
577 removedItem = values[indexToRemove];
579 values.removeElements (indexToRemove, 1);
581 if ((values.size() << 1) < values.capacity())
596 void removeObject (
const ObjectClass* objectToRemove,
bool deleteObject =
true)
600 for (
int i = 0; i < values.size(); ++i)
602 if (objectToRemove == values[i])
604 remove (i, deleteObject);
623 void removeRange (
int startIndex,
int numberToRemove,
bool deleteObjects =
true)
626 auto endIndex = jlimit (0, values.size(), startIndex + numberToRemove);
627 startIndex = jlimit (0, values.size(), startIndex);
628 numberToRemove = endIndex - startIndex;
630 if (numberToRemove > 0)
634 for (
int i = startIndex; i < endIndex; ++i)
641 values.removeElements (startIndex, numberToRemove);
643 if ((values.size() << 1) < values.capacity())
655 bool deleteObjects =
true)
659 if (howManyToRemove >= values.size())
660 clear (deleteObjects);
662 removeRange (values.size() - howManyToRemove, howManyToRemove, deleteObjects);
670 void swap (
int index1,
int index2) noexcept
673 values.swap (index1, index2);
689 void move (
int currentIndex,
int newIndex) noexcept
691 if (currentIndex != newIndex)
694 values.move (currentIndex, newIndex);
703 template <
class OtherArrayType>
704 void swapWith (OtherArrayType& otherArray) noexcept
707 const typename OtherArrayType::ScopedLockType lock2 (otherArray.getLock());
708 values.swapWith (otherArray.values);
721 values.shrinkToNoMoreThan (values.size());
733 values.ensureAllocatedSize (minNumElements);
762 template <
class ElementComparator>
763 void sort (ElementComparator& comparator,
764 bool retainOrderOfEquivalentItems =
false) const noexcept
768 ignoreUnused (comparator);
773 sortArray (comparator, values.begin(), 0,
size() - 1, retainOrderOfEquivalentItems);
781 inline const TypeOfCriticalSectionToUse&
getLock() const noexcept {
return values; }
790 JUCE_DEPRECATED_WITH_BODY (
void swapWithArray (
OwnedArray& other) noexcept, {
swapWith (other); })
797 void deleteAllObjects()
799 for (
auto& e : values)
805 template <
class OtherObjectClass,
class OtherCriticalSection>
808 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (
OwnedArray)
ObjectClass * getFirst() const noexcept
Returns a pointer to the first object in the array.
void insertArray(int indexToInsertAt, ObjectClass *const *newObjects, int numberOfElements)
Inserts an array of values into this array at a given position.
void minimiseStorageOverheads() noexcept
Reduces the amount of storage being used by the array.
void addCopiesOf(const OtherArrayType &arrayToAddFrom, int startIndex=0, int numElementsToAdd=-1)
Adds copies of the elements in another array to the end of this array.
void clear(bool deleteObjects=true)
Clears the array, optionally deleting the objects inside it first.
ObjectClass * operator[](const int index) const noexcept
Returns a pointer to the object at this index in the array.
void clearQuick(bool deleteObjects)
Clears the array, optionally deleting the objects inside it first.
bool isEmpty() const noexcept
Returns true if the array is empty, false otherwise.
OwnedArray(OwnedArray &&other) noexcept
Move constructor.
void addArray(const OtherArrayType &arrayToAddFrom, int startIndex=0, int numElementsToAdd=-1)
Adds elements from another array to the end of this array.
~OwnedArray()
Deletes the array and also deletes any objects inside it.
int indexOfSorted(ElementComparator &comparator, const ObjectClass *const objectToLookFor) const noexcept
Finds the index of an object in the array, assuming that the array is sorted.
int indexOf(const ObjectClass *objectToLookFor) const noexcept
Finds the index of an object which might be in the array.
const TypeOfCriticalSectionToUse & getLock() const noexcept
Returns the CriticalSection that locks this array.
void sort(ElementComparator &comparator, bool retainOrderOfEquivalentItems=false) const noexcept
Sorts the elements in the array.
ObjectClass * getUnchecked(const int index) const noexcept
Returns a pointer to the object at this index in the array, without checking whether the index is in-...
void removeLast(int howManyToRemove=1, bool deleteObjects=true)
Removes the last n objects from the array.
void removeRange(int startIndex, int numberToRemove, bool deleteObjects=true)
Removes a range of objects from the array.
typename DummyCriticalSection ::ScopedLockType ScopedLockType
Returns the type of scoped lock to use for locking this array.
ObjectClass ** end() const noexcept
Returns a pointer to the element which follows the last element in the array.
void addArray(const std::initializer_list< OtherArrayType > &items)
Adds elements from another array to the end of this array.
ObjectClass ** getRawDataPointer() noexcept
Returns a pointer to the actual array data.
ObjectClass ** data() const noexcept
Returns a pointer to the first element in the array.
void removeObject(const ObjectClass *objectToRemove, bool deleteObject=true)
Removes a specified object from the array.
OwnedArray & operator=(OwnedArray &&other) noexcept
Move assignment operator.
bool addIfNotAlreadyThere(ObjectClass *newObject) noexcept
Appends a new object at the end of the array as long as the array doesn't already contain it...
ObjectClass * insert(int indexToInsertAt, ObjectClass *newObject) noexcept
Inserts a new object into the array at the given index.
int size() const noexcept
Returns the number of items currently in the array.
Used by container classes as an indirect way to delete an object of a particular type.
int addSorted(ElementComparator &comparator, ObjectClass *const newObject) noexcept
Inserts a new object into the array assuming that the array is sorted.
OwnedArray(const std::initializer_list< ObjectClass *> &items)
Creates an array from a list of objects.
ObjectClass ** begin() const noexcept
Returns a pointer to the first element in the array.
ObjectClass * add(ObjectClass *newObject) noexcept
Appends a new object to the end of the array.
void swap(int index1, int index2) noexcept
Swaps a pair of objects in the array.
void ensureStorageAllocated(const int minNumElements) noexcept
Increases the array's internal storage to hold a minimum number of elements.
An array designed for holding objects.
OwnedArray(OwnedArray< OtherObjectClass, OtherCriticalSection > &&other) noexcept
Converting move constructor.
OwnedArray()=default
Creates an empty array.
ObjectClass * getLast() const noexcept
Returns a pointer to the last object in the array.
bool contains(const ObjectClass *objectToLookFor) const noexcept
Returns true if the array contains a specified object.
ObjectClass * removeAndReturn(int indexToRemove)
Removes and returns an object from the array without deleting it.
void swapWith(OtherArrayType &otherArray) noexcept
This swaps the contents of this array with those of another array.
void move(int currentIndex, int newIndex) noexcept
Moves one of the objects to a different position.