1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-18 00:54:19 +00:00

Added variadic add methods to Array

This commit is contained in:
jules 2017-07-21 16:19:58 +01:00
parent ebce454514
commit 86deea87ce

View file

@ -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<ElementType&&> (newElement));
}
/** Appends multiple new elements at the end of the array. */
template <typename... OtherElements>
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 <typename... OtherElements>
void add (ElementType&& firstNewElement, OtherElements... otherElements)
{
const ScopedLockType lock (getLock());
data.ensureAllocatedSize (numUsed + 1 + (int) sizeof... (otherElements));
addAssumingCapacityIsReady (static_cast<ElementType&&> (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<ElementType&&> (e)); }
template <typename... OtherElements>
void addAssumingCapacityIsReady (const ElementType& firstNewElement, OtherElements... otherElements)
{
addAssumingCapacityIsReady (firstNewElement);
addAssumingCapacityIsReady (otherElements...);
}
template <typename... OtherElements>
void addAssumingCapacityIsReady (ElementType&& firstNewElement, OtherElements... otherElements)
{
addAssumingCapacityIsReady (static_cast<ElementType&&> (firstNewElement));
addAssumingCapacityIsReady (otherElements...);
}
};