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();
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() noexcept
207 return values.begin();
213 inline ObjectClass*
const*
begin() const noexcept
215 return values.begin();
221 inline ObjectClass**
end() noexcept
229 inline ObjectClass*
const*
end() const noexcept
237 inline ObjectClass**
data() noexcept
245 inline ObjectClass*
const*
data() const noexcept
256 int indexOf (
const ObjectClass* objectToLookFor)
const noexcept
259 auto* e = values.begin();
261 for (; e != values.end(); ++e)
262 if (objectToLookFor == *e)
263 return static_cast<int> (e - values.begin());
273 bool contains (
const ObjectClass* objectToLookFor)
const noexcept
276 auto* e = values.begin();
278 for (; e != values.end(); ++e)
279 if (objectToLookFor == *e)
298 ObjectClass*
add (ObjectClass* newObject)
301 values.add (newObject);
317 ObjectClass*
add (std::unique_ptr<ObjectClass> newObject)
319 return add (newObject.release());
340 ObjectClass*
insert (
int indexToInsertAt, ObjectClass* newObject)
343 values.insert (indexToInsertAt, newObject, 1);
365 ObjectClass*
insert (
int indexToInsertAt, std::unique_ptr<ObjectClass> newObject)
367 return insert (indexToInsertAt, newObject.release());
383 ObjectClass*
const* newObjects,
384 int numberOfElements)
386 if (numberOfElements > 0)
389 values.insertArray (indexToInsertAt, newObjects, numberOfElements);
406 ObjectClass*
set (
int indexToChange, ObjectClass* newObject,
bool deleteOldElement =
true)
408 if (indexToChange >= 0)
410 std::unique_ptr<ObjectClass> toDelete;
415 if (indexToChange < values.size())
417 if (deleteOldElement)
419 toDelete.reset (values[indexToChange]);
421 if (toDelete.get() == newObject)
425 values[indexToChange] = newObject;
429 values.add (newObject);
455 ObjectClass*
set (
int indexToChange, std::unique_ptr<ObjectClass> newObject,
bool deleteOldElement =
true)
457 return set (indexToChange, newObject.release(), deleteOldElement);
469 template <
class OtherArrayType>
470 void addArray (
const OtherArrayType& arrayToAddFrom,
472 int numElementsToAdd = -1)
474 const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
476 values.addArray (arrayToAddFrom, startIndex, numElementsToAdd);
480 template <
typename OtherArrayType>
481 void addArray (
const std::initializer_list<OtherArrayType>& items)
484 values.addArray (items);
501 template <
class OtherArrayType>
504 int numElementsToAdd = -1)
506 const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
515 if (numElementsToAdd < 0 || startIndex + numElementsToAdd > arrayToAddFrom.size())
516 numElementsToAdd = arrayToAddFrom.size() - startIndex;
518 jassert (numElementsToAdd >= 0);
519 values.ensureAllocatedSize (values.size() + numElementsToAdd);
521 while (--numElementsToAdd >= 0)
522 values.add (createCopyIfNotNull (arrayToAddFrom.getUnchecked (startIndex++)));
537 template <
class ElementComparator>
538 int addSorted (ElementComparator& comparator, ObjectClass* newObject) noexcept
542 ignoreUnused (comparator);
545 auto index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size());
546 insert (index, newObject);
562 template <
typename ElementComparator>
563 int indexOfSorted (ElementComparator& comparator,
const ObjectClass* objectToLookFor)
const noexcept
567 ignoreUnused (comparator);
570 int s = 0, e = values.size();
574 if (comparator.compareElements (objectToLookFor, values[s]) == 0)
577 auto halfway = (s + e) / 2;
582 if (comparator.compareElements (objectToLookFor, values[halfway]) >= 0)
602 void remove (
int indexToRemove,
bool deleteObject =
true)
604 std::unique_ptr<ObjectClass> toDelete;
609 if (isPositiveAndBelow (indexToRemove, values.size()))
611 auto** e = values.begin() + indexToRemove;
616 values.removeElements (indexToRemove, 1);
620 if ((values.size() << 1) < values.capacity())
635 ObjectClass* removedItem =
nullptr;
638 if (isPositiveAndBelow (indexToRemove, values.size()))
640 removedItem = values[indexToRemove];
642 values.removeElements (indexToRemove, 1);
644 if ((values.size() << 1) < values.capacity())
659 void removeObject (
const ObjectClass* objectToRemove,
bool deleteObject =
true)
663 for (
int i = 0; i < values.size(); ++i)
665 if (objectToRemove == values[i])
686 void removeRange (
int startIndex,
int numberToRemove,
bool deleteObjects =
true)
689 auto endIndex = jlimit (0, values.size(), startIndex + numberToRemove);
690 startIndex = jlimit (0, values.size(), startIndex);
691 numberToRemove = endIndex - startIndex;
693 if (numberToRemove > 0)
698 objectsToDelete.
addArray (values.begin() + startIndex, numberToRemove);
700 values.removeElements (startIndex, numberToRemove);
702 for (
auto& o : objectsToDelete)
705 if ((values.size() << 1) < values.capacity())
717 bool deleteObjects =
true)
721 if (howManyToRemove >= values.size())
722 clear (deleteObjects);
724 removeRange (values.size() - howManyToRemove, howManyToRemove, deleteObjects);
732 void swap (
int index1,
int index2) noexcept
735 values.swap (index1, index2);
751 void move (
int currentIndex,
int newIndex) noexcept
753 if (currentIndex != newIndex)
756 values.move (currentIndex, newIndex);
765 template <
class OtherArrayType>
766 void swapWith (OtherArrayType& otherArray) noexcept
769 const typename OtherArrayType::ScopedLockType lock2 (otherArray.getLock());
770 values.swapWith (otherArray.values);
783 values.shrinkToNoMoreThan (values.size());
795 values.ensureAllocatedSize (minNumElements);
824 template <
class ElementComparator>
825 void sort (ElementComparator& comparator,
826 bool retainOrderOfEquivalentItems =
false) noexcept
830 ignoreUnused (comparator);
835 sortArray (comparator, values.begin(), 0,
size() - 1, retainOrderOfEquivalentItems);
843 inline const TypeOfCriticalSectionToUse&
getLock() const noexcept {
return values; }
852 JUCE_DEPRECATED_WITH_BODY (
void swapWithArray (
OwnedArray& other) noexcept, {
swapWith (other); })
859 void deleteAllObjects()
861 auto i = values.size();
866 values.removeElements (i, 1);
871 template <
class OtherObjectClass,
class OtherCriticalSection>
872 friend class OwnedArray;
874 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OwnedArray)
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.
An array designed for holding objects.
int size() const noexcept
Returns the number of items currently in the array.
ObjectClass * insert(int indexToInsertAt, std::unique_ptr< ObjectClass > newObject)
Inserts a new object into the array at the given index.
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 addArray(const OtherArrayType &arrayToAddFrom, int startIndex=0, int numElementsToAdd=-1)
Adds elements from another array to the end of this array.
bool isEmpty() const noexcept
Returns true if the array is empty, false otherwise.
void swapWith(OtherArrayType &otherArray) noexcept
This swaps the contents of this array with those of another array.
OwnedArray & operator=(OwnedArray &&other) noexcept
Move assignment operator.
void remove(int indexToRemove, bool deleteObject=true)
Removes an object from the array.
ObjectClass ** data() noexcept
Returns a pointer to the first element in the array.
typename TypeOfCriticalSectionToUse::ScopedLockType ScopedLockType
Returns the type of scoped lock to use for locking this array.
void ensureStorageAllocated(int minNumElements) noexcept
Increases the array's internal storage to hold a minimum number of elements.
ObjectClass *const * begin() const noexcept
Returns a pointer to the first element in the array.
void minimiseStorageOverheads() noexcept
Reduces the amount of storage being used by the array.
void clear(bool deleteObjects=true)
Clears the array, optionally deleting the objects inside it first.
void removeLast(int howManyToRemove=1, bool deleteObjects=true)
Removes the last n objects from the array.
int indexOf(const ObjectClass *objectToLookFor) const noexcept
Finds the index of an object which might be in the array.
ObjectClass * removeAndReturn(int indexToRemove)
Removes and returns an object from the array without deleting it.
OwnedArray(OwnedArray< OtherObjectClass, OtherCriticalSection > &&other) noexcept
Converting move constructor.
const TypeOfCriticalSectionToUse & getLock() const noexcept
Returns the CriticalSection that locks this array.
ObjectClass *const * data() const noexcept
Returns a pointer to the first element in the array.
~OwnedArray()
Deletes the array and also deletes any objects inside it.
void swap(int index1, int index2) noexcept
Swaps a pair of objects in the 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.
ObjectClass * getUnchecked(int index) const noexcept
Returns a pointer to the object at this index in the array, without checking whether the index is in-...
ObjectClass * operator[](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.
void removeObject(const ObjectClass *objectToRemove, bool deleteObject=true)
Removes a specified object from the array.
ObjectClass * getLast() const noexcept
Returns a pointer to the last object in the array.
ObjectClass * set(int indexToChange, std::unique_ptr< ObjectClass > newObject, bool deleteOldElement=true)
Replaces an object in the array with a different one.
ObjectClass * add(std::unique_ptr< ObjectClass > newObject)
Appends a new object to the end of the array.
ObjectClass *const * end() const noexcept
Returns a pointer to the element which follows the last element in the array.
OwnedArray()=default
Creates an empty array.
ObjectClass * getFirst() const noexcept
Returns a pointer to the first object in the array.
ObjectClass ** end() noexcept
Returns a pointer to the element which follows the last element in the array.
OwnedArray(OwnedArray &&other) noexcept
Move constructor.
ObjectClass * insert(int indexToInsertAt, ObjectClass *newObject)
Inserts a new object into the array at the given index.
void insertArray(int indexToInsertAt, ObjectClass *const *newObjects, int numberOfElements)
Inserts an array of values into this array at a given position.
void move(int currentIndex, int newIndex) noexcept
Moves one of the objects to a different position.
int addSorted(ElementComparator &comparator, ObjectClass *newObject) noexcept
Inserts a new object into the array assuming that the array is sorted.
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.
void sort(ElementComparator &comparator, bool retainOrderOfEquivalentItems=false) noexcept
Sorts the elements in the array.
OwnedArray(const std::initializer_list< ObjectClass * > &items)
Creates an array from a list of objects.
ObjectClass ** begin() noexcept
Returns a pointer to the first element in the array.
void removeRange(int startIndex, int numberToRemove, bool deleteObjects=true)
Removes a range of objects from the array.
ObjectClass * set(int indexToChange, ObjectClass *newObject, bool deleteOldElement=true)
Replaces an object in the array with a different one.
ObjectClass * add(ObjectClass *newObject)
Appends a new object to the end of the array.
bool contains(const ObjectClass *objectToLookFor) const noexcept
Returns true if the array contains a specified object.
Used by container classes as an indirect way to delete an object of a particular type.