51template <
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);
115 values.setAllocatedSize (0);
136 return values.
size();
153 inline ObjectClass*
operator[] (
const int index)
const noexcept
156 return values.getValueWithDefault (index);
167 return values[index];
198 return values.
begin();
207 return values.
begin();
235 auto**
e = values.
begin();
237 for (;
e != values.
end(); ++
e)
239 return static_cast<int> (
e - values.
begin());
252 auto**
e = values.
begin();
254 for (;
e != values.
end(); ++
e)
365 std::unique_ptr<ObjectClass>
toDelete;
406 template <
class OtherArrayType>
417 template <
typename OtherArrayType>
418 void addArray (
const std::initializer_list<OtherArrayType>& items)
438 template <
class OtherArrayType>
474 template <
class ElementComparator>
479 ignoreUnused (comparator);
482 const int index = findInsertIndexInSortedArray (comparator, values.
begin(),
newObject, 0, values.
size());
499 template <
typename ElementComparator>
504 ignoreUnused (comparator);
507 int s = 0,
e = values.
size();
541 std::unique_ptr<ObjectClass>
toDelete;
557 if ((values.
size() << 1) < values.capacity())
581 if ((values.
size() << 1) < values.capacity())
600 for (
int i = 0; i < values.
size(); ++i)
627 startIndex = jlimit (0, values.
size(), startIndex);
634 for (
int i = startIndex; i < endIndex; ++i)
643 if ((values.
size() << 1) < values.capacity())
703 template <
class OtherArrayType>
721 values.shrinkToNoMoreThan (values.
size());
762 template <
class ElementComparator>
768 ignoreUnused (comparator);
795 ArrayBase <ObjectClass*, TypeOfCriticalSectionToUse> values;
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)
A basic object container.
Holds a resizable array of primitive or copy-by-value objects.
void swapWith(OtherArrayType &otherArray) noexcept
This swaps the contents of this array with those of another array.
ElementType getUnchecked(int index) const
Returns one of the elements in the array, without checking the index passed in.
void insertArray(int indexToInsertAt, const ElementType *newElements, int numberOfElements)
Inserts an array of values into this array at a given position.
const TypeOfCriticalSectionToUse & getLock() const noexcept
Returns the CriticalSection that locks this array.
void addArray(const Type *elementsToAdd, int numElementsToAdd)
Adds elements from an array to the end of this array.
int size() const noexcept
Returns the current number of elements in the array.
ElementType * begin() const noexcept
Returns a pointer to the first element in the array.
void insert(int indexToInsertAt, ParameterType newElement)
Inserts a new element into the array at a given position.
ElementType getFirst() const noexcept
Returns the first element in the array, or a default value if the array is empty.
void add(const ElementType &newElement)
Appends a new element at the end of the array.
void clear()
Removes all elements from the array.
void move(int currentIndex, int newIndex) noexcept
Moves one of the values to a different position.
void swap(int index1, int index2)
Swaps over two elements in the array.
ElementType getLast() const noexcept
Returns the last element in the array, or a default value if the array is empty.
ElementType * end() const noexcept
Returns a pointer to the element which follows the last element in the array.
An array designed for holding objects.
int size() const noexcept
Returns the number of items currently in the array.
ObjectClass * set(int indexToChange, ObjectClass *newObject, bool deleteOldElement=true)
Replaces an object in the array with a different one.
ObjectClass * removeAndReturn(int indexToRemove)
Removes and returns an object from the array without deleting it.
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-...
int addSorted(ElementComparator &comparator, ObjectClass *const newObject) noexcept
Inserts a new object into the array assuming that the array is sorted.
void sort(ElementComparator &comparator, bool retainOrderOfEquivalentItems=false) const noexcept
Sorts the elements in 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 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.
void remove(int indexToRemove, bool deleteObject=true)
Removes an object from the array.
typename TypeOfCriticalSectionToUse::ScopedLockType ScopedLockType
Returns the type of scoped lock to use for locking this array.
void ensureStorageAllocated(const int minNumElements) noexcept
Increases the array's internal storage to hold a minimum number of elements.
ObjectClass * getFirst() const noexcept
Returns a pointer to the first object 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.
OwnedArray(OwnedArray< OtherObjectClass, OtherCriticalSection > &&other) noexcept
Converting move constructor.
~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.
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 * insert(int indexToInsertAt, ObjectClass *newObject) noexcept
Inserts a new object into the array at the given index.
OwnedArray()=default
Creates an empty array.
ObjectClass * add(ObjectClass *newObject) noexcept
Appends a new object to the end of the array.
OwnedArray & operator=(OwnedArray &&other) noexcept
Move assignment operator.
OwnedArray(OwnedArray &&other) noexcept
Move constructor.
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.
const TypeOfCriticalSectionToUse & getLock() const noexcept
Returns the CriticalSection that locks this array.
void addArray(const std::initializer_list< OtherArrayType > &items)
Adds elements from another array to the end of this array.
ObjectClass ** data() const noexcept
Returns a pointer to the first element in the array.
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.
ObjectClass ** getRawDataPointer() noexcept
Returns a pointer to the actual array data.
ObjectClass * operator[](const int index) const noexcept
Returns a pointer to the object at this index in the array.
OwnedArray(const std::initializer_list< ObjectClass * > &items)
Creates an array from a list of objects.
void removeRange(int startIndex, int numberToRemove, bool deleteObjects=true)
Removes a range of objects from the array.
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 ** begin() const noexcept
Returns a pointer to the first element in the array.
ObjectClass ** end() const noexcept
Returns a pointer to the element which follows the last element in the 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.
Used by container classes as an indirect way to delete an object of a particular type.