Browse Source

Changed Array and StringArray to use variadic template constructors instead of initializer_lists, so that we can use them on old platforms

tags/2021-05-28
jules 8 years ago
parent
commit
7ece1b4135
3 changed files with 35 additions and 24 deletions
  1. +31
    -13
      modules/juce_core/containers/juce_Array.h
  2. +0
    -7
      modules/juce_core/text/juce_StringArray.cpp
  3. +4
    -4
      modules/juce_core/text/juce_StringArray.h

+ 31
- 13
modules/juce_core/containers/juce_Array.h View File

@@ -58,7 +58,7 @@ private:
public: public:
//============================================================================== //==============================================================================
/** Creates an empty array. */ /** Creates an empty array. */
Array() noexcept : numUsed (0)
Array() noexcept
{ {
} }
@@ -82,19 +82,17 @@ public:
other.numUsed = 0; 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 @param values the array to copy from
*/ */
template <typename TypeToCreateFrom> template <typename TypeToCreateFrom>
explicit Array (const TypeToCreateFrom* values) : numUsed (0)
explicit Array (const TypeToCreateFrom* values)
{ {
while (*values != TypeToCreateFrom()) while (*values != TypeToCreateFrom())
add (*values++); add (*values++);
} }
/** Initalises from a C array of values.
/** Initalises from a raw array of values.
@param values the array to copy from @param values the array to copy from
@param numValues the number of values in the array @param numValues the number of values in the array
*/ */
@@ -107,13 +105,33 @@ public:
new (data.elements + i) ElementType (values[i]); new (data.elements + i) ElementType (values[i]);
} }
#if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS
template <typename TypeToCreateFrom>
Array (const std::initializer_list<TypeToCreateFrom>& 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<ElementType&&> (singleElementToAdd));
}
/** Initalises an Array from a list of items. */
template <typename... OtherElements>
Array (const ElementType& firstNewElement, OtherElements... otherElements)
{
data.ensureAllocatedSize (1 + (int) sizeof... (otherElements));
addAssumingCapacityIsReady (firstNewElement, otherElements...);
}
/** Initalises an Array from a list of items. */
template <typename... OtherElements>
Array (ElementType&& firstNewElement, OtherElements... otherElements)
{
data.ensureAllocatedSize (1 + (int) sizeof... (otherElements));
addAssumingCapacityIsReady (static_cast<ElementType&&> (firstNewElement), otherElements...);
} }
#endif
/** Destructor. */ /** Destructor. */
~Array() ~Array()
@@ -128,7 +146,7 @@ public:
{ {
if (this != &other) if (this != &other)
{ {
Array<ElementType, TypeOfCriticalSectionToUse> otherCopy (other);
auto otherCopy (other);
swapWith (otherCopy); swapWith (otherCopy);
} }
@@ -1221,7 +1239,7 @@ public:
private: private:
//============================================================================== //==============================================================================
ArrayAllocationBase <ElementType, TypeOfCriticalSectionToUse> data; ArrayAllocationBase <ElementType, TypeOfCriticalSectionToUse> data;
int numUsed;
int numUsed = 0;
void removeInternal (const int indexToRemove) void removeInternal (const int indexToRemove)
{ {


+ 0
- 7
modules/juce_core/text/juce_StringArray.cpp View File

@@ -79,13 +79,6 @@ StringArray& StringArray::operator= (StringArray&& other) noexcept
return *this; return *this;
} }
#if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS
StringArray::StringArray (const std::initializer_list<const char*>& stringList)
{
strings.addArray (stringList);
}
#endif
StringArray::~StringArray() StringArray::~StringArray()
{ {
} }


+ 4
- 4
modules/juce_core/text/juce_StringArray.h View File

@@ -45,6 +45,10 @@ public:
/** Creates an array containing a single string. */ /** Creates an array containing a single string. */
explicit StringArray (const String& firstValue); explicit StringArray (const String& firstValue);
/** Creates an array containing a list of strings. */
template <typename... OtherElements>
StringArray (StringRef firstValue, OtherElements... otherValues) : strings (firstValue, otherValues...) {}
/** Creates an array from a raw array of strings. /** Creates an array from a raw array of strings.
@param strings an array of strings to add @param strings an array of strings to add
@param numberOfStrings how many items there are in the array @param numberOfStrings how many items there are in the array
@@ -78,10 +82,6 @@ public:
*/ */
StringArray (const wchar_t* const* strings, int numberOfStrings); StringArray (const wchar_t* const* strings, int numberOfStrings);
#if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS
StringArray (const std::initializer_list<const char*>& strings);
#endif
/** Destructor. */ /** Destructor. */
~StringArray(); ~StringArray();


Loading…
Cancel
Save