47 template <
class ObjectClass,
48 class TypeOfCriticalSectionToUse = DummyCriticalSection>
69 : values (std::move (other.values))
74 OwnedArray (
const std::initializer_list<ObjectClass*>& items)
84 values = std::move (other.values);
89 template <
class OtherObjectClass,
class OtherCriticalSection>
91 : values (std::move (other.values))
96 template <
class OtherObjectClass,
class OtherCriticalSection>
101 values = std::move (other.values);
107 void clear (
bool deleteObjects =
true)
111 values.setAllocatedSize (0);
130 inline int size() const noexcept
132 return values.size();
152 return values.getValueWithDefault (index);
163 return values[index];
174 return values.getFirst();
185 return values.getLast();
194 return values.begin();
201 inline ObjectClass**
begin() noexcept
203 return values.begin();
209 inline ObjectClass*
const*
begin() const noexcept
211 return values.begin();
217 inline ObjectClass**
end() noexcept
225 inline ObjectClass*
const*
end() const noexcept
233 inline ObjectClass**
data() noexcept
241 inline ObjectClass*
const*
data() const noexcept
252 int indexOf (
const ObjectClass* objectToLookFor)
const noexcept
255 auto* e = values.begin();
257 for (; e != values.end(); ++e)
258 if (objectToLookFor == *e)
259 return static_cast<int> (e - values.begin());
269 bool contains (
const ObjectClass* objectToLookFor)
const noexcept
272 auto* e = values.begin();
274 for (; e != values.end(); ++e)
275 if (objectToLookFor == *e)
294 ObjectClass*
add (ObjectClass* newObject)
297 values.add (newObject);
313 ObjectClass*
add (std::unique_ptr<ObjectClass> newObject)
315 return add (newObject.release());
336 ObjectClass*
insert (
int indexToInsertAt, ObjectClass* newObject)
339 values.insert (indexToInsertAt, newObject, 1);
361 ObjectClass*
insert (
int indexToInsertAt, std::unique_ptr<ObjectClass> newObject)
363 return insert (indexToInsertAt, newObject.release());
379 ObjectClass*
const* newObjects,
380 int numberOfElements)
382 if (numberOfElements > 0)
385 values.insertArray (indexToInsertAt, newObjects, numberOfElements);
402 ObjectClass*
set (
int indexToChange, ObjectClass* newObject,
bool deleteOldElement =
true)
404 if (indexToChange >= 0)
406 std::unique_ptr<ObjectClass> toDelete;
411 if (indexToChange < values.size())
413 if (deleteOldElement)
415 toDelete.reset (values[indexToChange]);
417 if (toDelete.get() == newObject)
421 values[indexToChange] = newObject;
425 values.add (newObject);
451 ObjectClass*
set (
int indexToChange, std::unique_ptr<ObjectClass> newObject,
bool deleteOldElement =
true)
453 return set (indexToChange, newObject.release(), deleteOldElement);
465 template <
class OtherArrayType>
466 void addArray (
const OtherArrayType& arrayToAddFrom,
468 int numElementsToAdd = -1)
470 const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
472 values.addArray (arrayToAddFrom, startIndex, numElementsToAdd);
476 template <
typename OtherArrayType>
477 void addArray (
const std::initializer_list<OtherArrayType>& items)
480 values.addArray (items);
497 template <
class OtherArrayType>
500 int numElementsToAdd = -1)
502 const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
511 if (numElementsToAdd < 0 || startIndex + numElementsToAdd > arrayToAddFrom.size())
512 numElementsToAdd = arrayToAddFrom.size() - startIndex;
514 jassert (numElementsToAdd >= 0);
515 values.ensureAllocatedSize (values.size() + numElementsToAdd);
517 while (--numElementsToAdd >= 0)
518 values.add (createCopyIfNotNull (arrayToAddFrom.getUnchecked (startIndex++)));
533 template <
class ElementComparator>
534 int addSorted (ElementComparator& comparator, ObjectClass* newObject) noexcept
538 ignoreUnused (comparator);
541 auto index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size());
542 insert (index, newObject);
558 template <
typename ElementComparator>
559 int indexOfSorted (ElementComparator& comparator,
const ObjectClass* objectToLookFor)
const noexcept
563 ignoreUnused (comparator);
566 int s = 0, e = values.size();
570 if (comparator.compareElements (objectToLookFor, values[s]) == 0)
573 auto halfway = (s + e) / 2;
578 if (comparator.compareElements (objectToLookFor, values[halfway]) >= 0)
598 void remove (
int indexToRemove,
bool deleteObject =
true)
600 std::unique_ptr<ObjectClass> toDelete;
605 if (isPositiveAndBelow (indexToRemove, values.size()))
607 auto** e = values.begin() + indexToRemove;
612 values.removeElements (indexToRemove, 1);
616 if ((values.size() << 1) < values.capacity())
631 ObjectClass* removedItem =
nullptr;
634 if (isPositiveAndBelow (indexToRemove, values.size()))
636 removedItem = values[indexToRemove];
638 values.removeElements (indexToRemove, 1);
640 if ((values.size() << 1) < values.capacity())
655 void removeObject (
const ObjectClass* objectToRemove,
bool deleteObject =
true)
659 for (
int i = 0; i < values.size(); ++i)
661 if (objectToRemove == values[i])
682 void removeRange (
int startIndex,
int numberToRemove,
bool deleteObjects =
true)
685 auto endIndex = jlimit (0, values.size(), startIndex + numberToRemove);
686 startIndex = jlimit (0, values.size(), startIndex);
687 numberToRemove = endIndex - startIndex;
689 if (numberToRemove > 0)
694 objectsToDelete.
addArray (values.begin() + startIndex, numberToRemove);
696 values.removeElements (startIndex, numberToRemove);
698 for (
auto& o : objectsToDelete)
701 if ((values.size() << 1) < values.capacity())
713 bool deleteObjects =
true)
717 if (howManyToRemove >= values.size())
718 clear (deleteObjects);
720 removeRange (values.size() - howManyToRemove, howManyToRemove, deleteObjects);
728 void swap (
int index1,
int index2) noexcept
731 values.swap (index1, index2);
747 void move (
int currentIndex,
int newIndex) noexcept
749 if (currentIndex != newIndex)
752 values.move (currentIndex, newIndex);
761 template <
class OtherArrayType>
762 void swapWith (OtherArrayType& otherArray) noexcept
765 const typename OtherArrayType::ScopedLockType lock2 (otherArray.getLock());
766 values.swapWith (otherArray.values);
779 values.shrinkToNoMoreThan (values.size());
791 values.ensureAllocatedSize (minNumElements);
820 template <
class ElementComparator>
821 void sort (ElementComparator& comparator,
822 bool retainOrderOfEquivalentItems =
false) noexcept
826 ignoreUnused (comparator);
831 sortArray (comparator, values.begin(), 0,
size() - 1, retainOrderOfEquivalentItems);
839 inline const TypeOfCriticalSectionToUse&
getLock() const noexcept {
return values; }
848 JUCE_DEPRECATED_WITH_BODY (
void swapWithArray (
OwnedArray& other) noexcept, {
swapWith (other); })
855 void deleteAllObjects()
857 auto i = values.size();
862 values.removeElements (i, 1);
867 template <
class OtherObjectClass,
class OtherCriticalSection>
870 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OwnedArray)
void addArray(const Type *elementsToAdd, int numElementsToAdd)
int size() const noexcept
ObjectClass * insert(int indexToInsertAt, std::unique_ptr< ObjectClass > newObject)
void addCopiesOf(const OtherArrayType &arrayToAddFrom, int startIndex=0, int numElementsToAdd=-1)
void addArray(const OtherArrayType &arrayToAddFrom, int startIndex=0, int numElementsToAdd=-1)
bool isEmpty() const noexcept
void swapWith(OtherArrayType &otherArray) noexcept
OwnedArray & operator=(OwnedArray &&other) noexcept
void remove(int indexToRemove, bool deleteObject=true)
ObjectClass ** data() noexcept
typename TypeOfCriticalSectionToUse::ScopedLockType ScopedLockType
void ensureStorageAllocated(int minNumElements) noexcept
ObjectClass *const * begin() const noexcept
void minimiseStorageOverheads() noexcept
void clear(bool deleteObjects=true)
void removeLast(int howManyToRemove=1, bool deleteObjects=true)
int indexOf(const ObjectClass *objectToLookFor) const noexcept
ObjectClass * removeAndReturn(int indexToRemove)
OwnedArray(OwnedArray< OtherObjectClass, OtherCriticalSection > &&other) noexcept
const TypeOfCriticalSectionToUse & getLock() const noexcept
ObjectClass *const * data() const noexcept
void swap(int index1, int index2) noexcept
int indexOfSorted(ElementComparator &comparator, const ObjectClass *objectToLookFor) const noexcept
ObjectClass * getUnchecked(int index) const noexcept
ObjectClass * operator[](int index) const noexcept
void clearQuick(bool deleteObjects)
void removeObject(const ObjectClass *objectToRemove, bool deleteObject=true)
ObjectClass * getLast() const noexcept
ObjectClass * set(int indexToChange, std::unique_ptr< ObjectClass > newObject, bool deleteOldElement=true)
ObjectClass * add(std::unique_ptr< ObjectClass > newObject)
ObjectClass *const * end() const noexcept
ObjectClass * getFirst() const noexcept
ObjectClass ** end() noexcept
OwnedArray(OwnedArray &&other) noexcept
ObjectClass * insert(int indexToInsertAt, ObjectClass *newObject)
void insertArray(int indexToInsertAt, ObjectClass *const *newObjects, int numberOfElements)
void move(int currentIndex, int newIndex) noexcept
int addSorted(ElementComparator &comparator, ObjectClass *newObject) noexcept
void addArray(const std::initializer_list< OtherArrayType > &items)
ObjectClass ** getRawDataPointer() noexcept
void sort(ElementComparator &comparator, bool retainOrderOfEquivalentItems=false) noexcept
OwnedArray(const std::initializer_list< ObjectClass * > &items)
ObjectClass ** begin() noexcept
void removeRange(int startIndex, int numberToRemove, bool deleteObjects=true)
ObjectClass * set(int indexToChange, ObjectClass *newObject, bool deleteOldElement=true)
ObjectClass * add(ObjectClass *newObject)
bool contains(const ObjectClass *objectToLookFor) const noexcept