From 7ece1b413599b4bb112f76f36b46866358e1c065 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 16 Oct 2017 12:08:06 +0100 Subject: [PATCH] Changed Array and StringArray to use variadic template constructors instead of initializer_lists, so that we can use them on old platforms --- modules/juce_core/containers/juce_Array.h | 44 +++++++++++++++------ modules/juce_core/text/juce_StringArray.cpp | 7 ---- modules/juce_core/text/juce_StringArray.h | 8 ++-- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/modules/juce_core/containers/juce_Array.h b/modules/juce_core/containers/juce_Array.h index 8f7380b1ea..fc30e0acb9 100644 --- a/modules/juce_core/containers/juce_Array.h +++ b/modules/juce_core/containers/juce_Array.h @@ -58,7 +58,7 @@ private: public: //============================================================================== /** Creates an empty array. */ - Array() noexcept : numUsed (0) + Array() noexcept { } @@ -82,19 +82,17 @@ public: other.numUsed = 0; } - /** Initalises from a null-terminated C array of values. - + /** Initalises from a null-terminated raw array of values. @param values the array to copy from */ template - explicit Array (const TypeToCreateFrom* values) : numUsed (0) + explicit Array (const TypeToCreateFrom* values) { while (*values != TypeToCreateFrom()) add (*values++); } - /** Initalises from a C array of values. - + /** Initalises from a raw array of values. @param values the array to copy from @param numValues the number of values in the array */ @@ -107,13 +105,33 @@ public: new (data.elements + i) ElementType (values[i]); } - #if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS - template - Array (const std::initializer_list& items) : numUsed (0) + /** Initalises an Array of size 1 containing a single element. */ + explicit Array (const ElementType& singleElementToAdd) { - addArray (items); + add (singleElementToAdd); + } + + /** Initalises an Array of size 1 containing a single element. */ + explicit Array (ElementType&& singleElementToAdd) + { + add (static_cast (singleElementToAdd)); + } + + /** Initalises an Array from a list of items. */ + template + Array (const ElementType& firstNewElement, OtherElements... otherElements) + { + data.ensureAllocatedSize (1 + (int) sizeof... (otherElements)); + addAssumingCapacityIsReady (firstNewElement, otherElements...); + } + + /** Initalises an Array from a list of items. */ + template + Array (ElementType&& firstNewElement, OtherElements... otherElements) + { + data.ensureAllocatedSize (1 + (int) sizeof... (otherElements)); + addAssumingCapacityIsReady (static_cast (firstNewElement), otherElements...); } - #endif /** Destructor. */ ~Array() @@ -128,7 +146,7 @@ public: { if (this != &other) { - Array otherCopy (other); + auto otherCopy (other); swapWith (otherCopy); } @@ -1221,7 +1239,7 @@ public: private: //============================================================================== ArrayAllocationBase data; - int numUsed; + int numUsed = 0; void removeInternal (const int indexToRemove) { diff --git a/modules/juce_core/text/juce_StringArray.cpp b/modules/juce_core/text/juce_StringArray.cpp index 5e737edc5e..26bfbae062 100644 --- a/modules/juce_core/text/juce_StringArray.cpp +++ b/modules/juce_core/text/juce_StringArray.cpp @@ -79,13 +79,6 @@ StringArray& StringArray::operator= (StringArray&& other) noexcept return *this; } -#if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS -StringArray::StringArray (const std::initializer_list& stringList) -{ - strings.addArray (stringList); -} -#endif - StringArray::~StringArray() { } diff --git a/modules/juce_core/text/juce_StringArray.h b/modules/juce_core/text/juce_StringArray.h index 49d8662753..6c5e7eba3c 100644 --- a/modules/juce_core/text/juce_StringArray.h +++ b/modules/juce_core/text/juce_StringArray.h @@ -45,6 +45,10 @@ public: /** Creates an array containing a single string. */ explicit StringArray (const String& firstValue); + /** Creates an array containing a list of strings. */ + template + StringArray (StringRef firstValue, OtherElements... otherValues) : strings (firstValue, otherValues...) {} + /** Creates an array from a raw array of strings. @param strings an array of strings to add @param numberOfStrings how many items there are in the array @@ -78,10 +82,6 @@ public: */ StringArray (const wchar_t* const* strings, int numberOfStrings); - #if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS - StringArray (const std::initializer_list& strings); - #endif - /** Destructor. */ ~StringArray();