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])
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>
806 friend class OwnedArray;
808 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OwnedArray)