diff --git a/modules/juce_core/containers/juce_Array.h b/modules/juce_core/containers/juce_Array.h index ca024a9c2c..6228ea9c41 100644 --- a/modules/juce_core/containers/juce_Array.h +++ b/modules/juce_core/containers/juce_Array.h @@ -204,6 +204,15 @@ public: numUsed = 0; } + /** Fills the Array with the provided value. */ + void fill (const ParameterType& newValue) noexcept + { + auto n = size(); + + for (int i = 0; i < n; ++i) + setUnchecked (i, newValue); + } + //============================================================================== /** Returns the current number of elements in the array. */ inline int size() const noexcept @@ -379,7 +388,6 @@ public: //============================================================================== /** Appends a new element at the end of the array. - @param newElement the new object to add to the array @see set, insert, addIfNotAlreadyThere, addSorted, addUsingDefaultSort, addArray */ @@ -391,7 +399,6 @@ public: } /** Appends a new element at the end of the array. - @param newElement the new object to add to the array @see set, insert, addIfNotAlreadyThere, addSorted, addUsingDefaultSort, addArray */ @@ -402,6 +409,24 @@ public: new (data.elements + numUsed++) ElementType (static_cast (newElement)); } + /** Appends multiple new elements at the end of the array. */ + template + void add (const ElementType& firstNewElement, OtherElements... otherElements) + { + const ScopedLockType lock (getLock()); + data.ensureAllocatedSize (numUsed + 1 + (int) sizeof... (otherElements)); + addAssumingCapacityIsReady (firstNewElement, otherElements...); + } + + /** Appends multiple new elements at the end of the array. */ + template + void add (ElementType&& firstNewElement, OtherElements... otherElements) + { + const ScopedLockType lock (getLock()); + data.ensureAllocatedSize (numUsed + 1 + (int) sizeof... (otherElements)); + addAssumingCapacityIsReady (static_cast (firstNewElement), otherElements...); + } + /** Inserts a new element into the array at a given position. If the index is less than 0 or greater than the size of the array, the @@ -1222,4 +1247,21 @@ private: if (data.numAllocated > jmax (minimumAllocatedSize, numUsed * 2)) data.shrinkToNoMoreThan (jmax (numUsed, jmax (minimumAllocatedSize, 64 / (int) sizeof (ElementType)))); } + + void addAssumingCapacityIsReady (const ElementType& e) { new (data.elements + numUsed++) ElementType (e); } + void addAssumingCapacityIsReady (ElementType&& e) { new (data.elements + numUsed++) ElementType (static_cast (e)); } + + template + void addAssumingCapacityIsReady (const ElementType& firstNewElement, OtherElements... otherElements) + { + addAssumingCapacityIsReady (firstNewElement); + addAssumingCapacityIsReady (otherElements...); + } + + template + void addAssumingCapacityIsReady (ElementType&& firstNewElement, OtherElements... otherElements) + { + addAssumingCapacityIsReady (static_cast (firstNewElement)); + addAssumingCapacityIsReady (otherElements...); + } };