From 383528ec6e69deb2e52e8aff89926dd4b4ba622c Mon Sep 17 00:00:00 2001 From: jules Date: Fri, 15 Jun 2018 10:02:25 +0100 Subject: [PATCH] Added some helpers to StringArray to allow creation from Arrays of string-convertible objects --- modules/juce_core/text/juce_StringArray.cpp | 5 ++++ modules/juce_core/text/juce_StringArray.h | 30 +++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/modules/juce_core/text/juce_StringArray.cpp b/modules/juce_core/text/juce_StringArray.cpp index 4029231dec..b2e4fa1510 100644 --- a/modules/juce_core/text/juce_StringArray.cpp +++ b/modules/juce_core/text/juce_StringArray.cpp @@ -37,6 +37,11 @@ StringArray::StringArray (StringArray&& other) noexcept { } +StringArray::StringArray (Array&& other) noexcept + : strings (static_cast&&> (other)) +{ +} + StringArray::StringArray (const String& firstValue) { strings.add (firstValue); diff --git a/modules/juce_core/text/juce_StringArray.h b/modules/juce_core/text/juce_StringArray.h index 51fdd2478b..904182910d 100644 --- a/modules/juce_core/text/juce_StringArray.h +++ b/modules/juce_core/text/juce_StringArray.h @@ -54,6 +54,16 @@ public: /** Creates an array containing a list of strings. */ StringArray (const std::initializer_list& strings); + /** Creates a StringArray by moving from an Array */ + StringArray (Array&&) noexcept; + + /** Creates a StringArray from an array of objects which can be implicitly converted to Strings. */ + template + StringArray (const Array& stringArray) + { + addArray (stringArray.begin(), stringArray.end()); + } + /** 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 @@ -96,6 +106,14 @@ public: /** Move assignment operator */ StringArray& operator= (StringArray&&) noexcept; + /** Copies a StringArray from an array of objects which can be implicitly converted to Strings. */ + template + StringArray& operator= (const Array& stringArray) + { + addArray (stringArray.begin(), stringArray.end()); + return *this; + } + /** Swaps the contents of this and another StringArray. */ void swapWith (StringArray&) noexcept; @@ -208,6 +226,18 @@ public: int startIndex = 0, int numElementsToAdd = -1); + /** Adds items from a range of start/end iterators of some kind of objects which + can be implicitly converted to Strings. + */ + template + void addArray (Iterator&& start, Iterator&& end) + { + ensureStorageAllocated (size() + (int) static_cast (end - start)); + + while (start != end) + strings.add (*start++); + } + /** Merges the strings from another array into this one. This will not add a string that already exists.