From 8c66a5e767cd7cbf6f1cc149453af7d4ea8f0cd5 Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 23 Dec 2014 15:01:10 +0000 Subject: [PATCH] Added flag JUCE_COMPILER_SUPPORTS_INITIALIZER_LIST, and implemented some constructors for StringArray and Array that use C+++11 initialiser lists. --- modules/juce_core/containers/juce_Array.h | 29 ++++++++++++++++--- .../juce_core/system/juce_CompilerSupport.h | 10 +++++++ modules/juce_core/text/juce_StringArray.cpp | 7 +++++ modules/juce_core/text/juce_StringArray.h | 4 +++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/modules/juce_core/containers/juce_Array.h b/modules/juce_core/containers/juce_Array.h index 87a136e6bc..b729c99d2a 100644 --- a/modules/juce_core/containers/juce_Array.h +++ b/modules/juce_core/containers/juce_Array.h @@ -96,8 +96,7 @@ public: @param values the array to copy from */ template - explicit Array (const TypeToCreateFrom* values) - : numUsed (0) + explicit Array (const TypeToCreateFrom* values) : numUsed (0) { while (*values != TypeToCreateFrom()) add (*values++); @@ -109,8 +108,7 @@ public: @param numValues the number of values in the array */ template - Array (const TypeToCreateFrom* values, int numValues) - : numUsed (numValues) + Array (const TypeToCreateFrom* values, int numValues) : numUsed (numValues) { data.setAllocatedSize (numValues); @@ -118,6 +116,14 @@ public: new (data.elements + i) ElementType (values[i]); } + #if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS + template + Array (const std::initializer_list& items) : numUsed (0) + { + addArray (items); + } + #endif + /** Destructor. */ ~Array() { @@ -604,6 +610,21 @@ public: } } + #if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS + template + void addArray (const std::initializer_list& items) + { + const ScopedLockType lock (getLock()); + data.ensureAllocatedSize (numUsed + (int) items.size()); + + for (auto& item : items) + { + new (data.elements + numUsed) ElementType (item); + ++numUsed; + } + } + #endif + /** Adds elements from a null-terminated array of pointers to the end of this array. @param elementsToAdd an array of pointers to some kind of object from which elements diff --git a/modules/juce_core/system/juce_CompilerSupport.h b/modules/juce_core/system/juce_CompilerSupport.h index d09095645e..5376a0c6ea 100644 --- a/modules/juce_core/system/juce_CompilerSupport.h +++ b/modules/juce_core/system/juce_CompilerSupport.h @@ -40,6 +40,7 @@ #define JUCE_COMPILER_SUPPORTS_NOEXCEPT 1 #define JUCE_COMPILER_SUPPORTS_NULLPTR 1 #define JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS 1 + #define JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS 1 #if (__GNUC__ * 100 + __GNUC_MINOR__) >= 407 && ! defined (JUCE_COMPILER_SUPPORTS_OVERRIDE_AND_FINAL) #define JUCE_COMPILER_SUPPORTS_OVERRIDE_AND_FINAL 1 @@ -87,6 +88,11 @@ #ifndef JUCE_COMPILER_SUPPORTS_ARC #define JUCE_COMPILER_SUPPORTS_ARC 1 #endif + +#if __has_feature (cxx_generalized_initializers) + #define JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS 1 +#endif + #endif //============================================================================== @@ -102,6 +108,10 @@ #define JUCE_COMPILER_SUPPORTS_LAMBDAS 1 #endif + #if _MSC_VER >= 1800 + #define JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS 1 + #endif + #if _MSC_VER >= 1900 #define JUCE_COMPILER_SUPPORTS_NOEXCEPT 1 #define JUCE_DELETED_FUNCTION = delete diff --git a/modules/juce_core/text/juce_StringArray.cpp b/modules/juce_core/text/juce_StringArray.cpp index 6c49138623..05311ff242 100644 --- a/modules/juce_core/text/juce_StringArray.cpp +++ b/modules/juce_core/text/juce_StringArray.cpp @@ -86,6 +86,13 @@ StringArray& StringArray::operator= (StringArray&& other) noexcept } #endif +#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 52335e1b45..c3a1a17b6e 100644 --- a/modules/juce_core/text/juce_StringArray.h +++ b/modules/juce_core/text/juce_StringArray.h @@ -86,6 +86,10 @@ public: */ StringArray (const wchar_t* const* strings, int numberOfStrings); + #if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS + StringArray (const std::initializer_list& strings); + #endif + /** Destructor. */ ~StringArray();