From 221786dfcfe92fa392cbffbdb079c5e54dc14f39 Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 18 Sep 2013 19:10:01 +0100 Subject: [PATCH] New method Array::addNullTerminatedArray(), and misc minor tightening-up of code. --- .../Source/Application/jucer_Application.h | 2 +- .../Source/Application/jucer_MainWindow.cpp | 2 +- modules/juce_core/containers/juce_Array.h | 22 +++++- modules/juce_core/native/juce_mac_Files.mm | 4 +- modules/juce_core/text/juce_StringArray.cpp | 77 ++++--------------- modules/juce_core/text/juce_StringArray.h | 14 ++-- .../native/juce_mac_MouseCursor.mm | 2 +- .../windows/juce_ResizableWindow.h | 15 ++-- 8 files changed, 51 insertions(+), 87 deletions(-) diff --git a/extras/Introjucer/Source/Application/jucer_Application.h b/extras/Introjucer/Source/Application/jucer_Application.h index 87064861bf..b6c5c3e649 100644 --- a/extras/Introjucer/Source/Application/jucer_Application.h +++ b/extras/Introjucer/Source/Application/jucer_Application.h @@ -160,7 +160,7 @@ public: void anotherInstanceStarted (const String& commandLine) override { - openFile (commandLine.unquoted()); + openFile (File (commandLine.unquoted())); } static IntrojucerApp& getApp() diff --git a/extras/Introjucer/Source/Application/jucer_MainWindow.cpp b/extras/Introjucer/Source/Application/jucer_MainWindow.cpp index 352e545301..c6b7b1d3a8 100644 --- a/extras/Introjucer/Source/Application/jucer_MainWindow.cpp +++ b/extras/Introjucer/Source/Application/jucer_MainWindow.cpp @@ -209,7 +209,7 @@ bool MainWindow::openFile (const File& file) bool MainWindow::isInterestedInFileDrag (const StringArray& filenames) { for (int i = filenames.size(); --i >= 0;) - if (canOpenFile (filenames[i])) + if (canOpenFile (File (filenames[i]))) return true; return false; diff --git a/modules/juce_core/containers/juce_Array.h b/modules/juce_core/containers/juce_Array.h index 8adb09c42a..3e96b68d63 100644 --- a/modules/juce_core/containers/juce_Array.h +++ b/modules/juce_core/containers/juce_Array.h @@ -546,11 +546,13 @@ public: /** Adds elements from an array to the end of this array. - @param elementsToAdd the array of elements to add + @param elementsToAdd an array of some kind of object from which elements + can be constructed. @param numElementsToAdd how many elements are in this other array @see add */ - void addArray (const ElementType* elementsToAdd, int numElementsToAdd) + template + void addArray (const Type* elementsToAdd, int numElementsToAdd) { const ScopedLockType lock (getLock()); @@ -566,6 +568,22 @@ public: } } + /** 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 + can be constructed. This array must be terminated by a nullptr + @see addArray + */ + template + void addNullTerminatedArray (const Type* const* elementsToAdd) + { + int num = 0; + for (const Type* const* e = elementsToAdd; *e != nullptr; ++e) + ++num; + + addArray (elementsToAdd, num); + } + /** This swaps the contents of this array with those of another array. If you need to exchange two arrays, this is vastly quicker than using copy-by-value diff --git a/modules/juce_core/native/juce_mac_Files.mm b/modules/juce_core/native/juce_mac_Files.mm index d2be92e35d..7c2bb18389 100644 --- a/modules/juce_core/native/juce_mac_Files.mm +++ b/modules/juce_core/native/juce_mac_Files.mm @@ -205,7 +205,7 @@ File File::getSpecialLocation (const SpecialLocationType type) { File tmp ("~/Library/Caches/" + juce_getExecutableFile().getFileNameWithoutExtension()); tmp.createDirectory(); - return tmp.getFullPathName(); + return File (tmp.getFullPathName()); } #endif case userMusicDirectory: resultPath = "~/Music"; break; @@ -245,7 +245,7 @@ File File::getSpecialLocation (const SpecialLocationType type) buffer.calloc (size + 8); _NSGetExecutablePath (buffer.getData(), &size); - return String::fromUTF8 (buffer, (int) size); + return File (String::fromUTF8 (buffer, (int) size)); } default: diff --git a/modules/juce_core/text/juce_StringArray.cpp b/modules/juce_core/text/juce_StringArray.cpp index 1689c8646a..035381d2c2 100644 --- a/modules/juce_core/text/juce_StringArray.cpp +++ b/modules/juce_core/text/juce_StringArray.cpp @@ -47,47 +47,29 @@ StringArray::StringArray (const String& firstValue) strings.add (firstValue); } -namespace StringArrayHelpers -{ - template - void addArray (Array& dest, const CharType* const* strings) - { - if (strings != nullptr) - while (*strings != nullptr) - dest.add (*strings++); - } - - template - void addArray (Array& dest, const Type* const strings, const int numberOfStrings) - { - for (int i = 0; i < numberOfStrings; ++i) - dest.add (strings [i]); - } -} - StringArray::StringArray (const String* initialStrings, int numberOfStrings) { - StringArrayHelpers::addArray (strings, initialStrings, numberOfStrings); + strings.addArray (initialStrings, numberOfStrings); } -StringArray::StringArray (const char* const* const initialStrings) +StringArray::StringArray (const char* const* initialStrings) { - StringArrayHelpers::addArray (strings, initialStrings); + strings.addNullTerminatedArray (initialStrings); } -StringArray::StringArray (const char* const* const initialStrings, const int numberOfStrings) +StringArray::StringArray (const char* const* initialStrings, int numberOfStrings) { - StringArrayHelpers::addArray (strings, initialStrings, numberOfStrings); + strings.addArray (initialStrings, numberOfStrings); } -StringArray::StringArray (const wchar_t* const* const initialStrings) +StringArray::StringArray (const wchar_t* const* initialStrings) { - StringArrayHelpers::addArray (strings, initialStrings); + strings.addNullTerminatedArray (initialStrings); } -StringArray::StringArray (const wchar_t* const* const initialStrings, const int numberOfStrings) +StringArray::StringArray (const wchar_t* const* initialStrings, int numberOfStrings) { - StringArrayHelpers::addArray (strings, initialStrings, numberOfStrings); + strings.addArray (initialStrings, numberOfStrings); } StringArray& StringArray::operator= (const StringArray& other) @@ -110,14 +92,7 @@ StringArray::~StringArray() bool StringArray::operator== (const StringArray& other) const noexcept { - if (other.size() != size()) - return false; - - for (int i = size(); --i >= 0;) - if (other.strings.getReference(i) != strings.getReference(i)) - return false; - - return true; + return strings == other.strings; } bool StringArray::operator!= (const StringArray& other) const noexcept @@ -150,7 +125,6 @@ const String& StringArray::operator[] (const int index) const noexcept String& StringArray::getReference (const int index) noexcept { - jassert (isPositiveAndBelow (index, strings.size())); return strings.getReference (index); } @@ -192,20 +166,7 @@ void StringArray::set (const int index, const String& newString) bool StringArray::contains (StringRef stringToLookFor, const bool ignoreCase) const { - if (ignoreCase) - { - for (int i = size(); --i >= 0;) - if (strings.getReference(i).equalsIgnoreCase (stringToLookFor)) - return true; - } - else - { - for (int i = size(); --i >= 0;) - if (stringToLookFor == strings.getReference(i)) - return true; - } - - return false; + return indexOf (stringToLookFor, ignoreCase) >= 0; } int StringArray::indexOf (StringRef stringToLookFor, const bool ignoreCase, int i) const @@ -217,23 +178,15 @@ int StringArray::indexOf (StringRef stringToLookFor, const bool ignoreCase, int if (ignoreCase) { - while (i < numElements) - { + for (; i < numElements; ++i) if (strings.getReference(i).equalsIgnoreCase (stringToLookFor)) return i; - - ++i; - } } else { - while (i < numElements) - { + for (; i < numElements; ++i) if (stringToLookFor == strings.getReference (i)) return i; - - ++i; - } } return -1; @@ -453,9 +406,7 @@ void StringArray::removeDuplicates (const bool ignoreCase) { const String s (strings.getReference(i)); - int nextIndex = i + 1; - - for (;;) + for (int nextIndex = i + 1;;) { nextIndex = indexOf (s, ignoreCase, nextIndex); diff --git a/modules/juce_core/text/juce_StringArray.h b/modules/juce_core/text/juce_StringArray.h index 85ceb4c15c..b49296387b 100644 --- a/modules/juce_core/text/juce_StringArray.h +++ b/modules/juce_core/text/juce_StringArray.h @@ -44,10 +44,10 @@ public: StringArray() noexcept; /** Creates a copy of another string array */ - StringArray (const StringArray& other); + StringArray (const StringArray&); #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS - StringArray (StringArray&& other) noexcept; + StringArray (StringArray&&) noexcept; #endif /** Creates an array containing a single string. */ @@ -90,27 +90,27 @@ public: ~StringArray(); /** Copies the contents of another string array into this one */ - StringArray& operator= (const StringArray& other); + StringArray& operator= (const StringArray&); #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS - StringArray& operator= (StringArray&& other) noexcept; + StringArray& operator= (StringArray&&) noexcept; #endif /** Swaps the contents of this and another StringArray. */ - void swapWith (StringArray& other) noexcept; + void swapWith (StringArray&) noexcept; //============================================================================== /** Compares two arrays. Comparisons are case-sensitive. @returns true only if the other array contains exactly the same strings in the same order */ - bool operator== (const StringArray& other) const noexcept; + bool operator== (const StringArray&) const noexcept; /** Compares two arrays. Comparisons are case-sensitive. @returns false if the other array contains exactly the same strings in the same order */ - bool operator!= (const StringArray& other) const noexcept; + bool operator!= (const StringArray&) const noexcept; //============================================================================== /** Returns the number of strings in the array */ diff --git a/modules/juce_gui_basics/native/juce_mac_MouseCursor.mm b/modules/juce_gui_basics/native/juce_mac_MouseCursor.mm index 96270c8024..b685ca8f40 100644 --- a/modules/juce_gui_basics/native/juce_mac_MouseCursor.mm +++ b/modules/juce_gui_basics/native/juce_mac_MouseCursor.mm @@ -52,7 +52,7 @@ namespace MouseCursorHelpers static void* fromWebKitFile (const char* filename, float hx, float hy) { - FileInputStream fileStream (String ("/System/Library/Frameworks/WebKit.framework/Frameworks/WebCore.framework/Resources/") + filename); + FileInputStream fileStream (File ("/System/Library/Frameworks/WebKit.framework/Frameworks/WebCore.framework/Resources").getChildFile (filename)); BufferedInputStream buf (fileStream, 4096); PNGImageFormat pngFormat; diff --git a/modules/juce_gui_basics/windows/juce_ResizableWindow.h b/modules/juce_gui_basics/windows/juce_ResizableWindow.h index a2a765c2f7..e4464119b9 100644 --- a/modules/juce_gui_basics/windows/juce_ResizableWindow.h +++ b/modules/juce_gui_basics/windows/juce_ResizableWindow.h @@ -119,8 +119,7 @@ public: void setResizable (bool shouldBeResizable, bool useBottomRightCornerResizer); - /** True if resizing is enabled. - + /** Returns true if resizing is enabled. @see setResizable */ bool isResizable() const noexcept; @@ -141,7 +140,6 @@ public: int newMaximumHeight) noexcept; /** Returns the bounds constrainer object that this window is using. - You can access this to change its properties. */ ComponentBoundsConstrainer* getConstrainer() noexcept { return constrainer; } @@ -151,13 +149,12 @@ public: A pointer to the object you pass in will be kept, but it won't be deleted by this object, so it's the caller's responsiblity to manage it. - If you pass 0, then no contraints will be placed on the positioning of the window. + If you pass a nullptr, then no contraints will be placed on the positioning of the window. */ void setConstrainer (ComponentBoundsConstrainer* newConstrainer); /** Calls the window's setBounds method, after first checking these bounds with the current constrainer. - @see setConstrainer */ void setBoundsConstrained (const Rectangle& bounds); @@ -165,7 +162,6 @@ public: //============================================================================== /** Returns true if the window is currently in full-screen mode. - @see setFullScreen */ bool isFullScreen() const; @@ -180,7 +176,6 @@ public: void setFullScreen (bool shouldBeFullScreen); /** Returns true if the window is currently minimised. - @see setMinimised */ bool isMinimised() const; @@ -345,14 +340,14 @@ protected: If you know what you're doing and are sure you really want to add a component, specify a base-class method call to Component::addAndMakeVisible(), to side-step this warning. */ - void addChildComponent (Component* child, int zOrder = -1); + void addChildComponent (Component*, int zOrder = -1); /** Overridden to warn people about adding components directly to this component instead of using setContentOwned(). If you know what you're doing and are sure you really want to add a component, specify a base-class method call to Component::addAndMakeVisible(), to side-step this warning. */ - void addAndMakeVisible (Component* child, int zOrder = -1); + void addAndMakeVisible (Component*, int zOrder = -1); #endif ScopedPointer resizableCorner; @@ -360,7 +355,7 @@ protected: private: //============================================================================== - Component::SafePointer contentComponent; + Component::SafePointer contentComponent; bool ownsContentComponent, resizeToFitContent, fullscreen; ComponentDragger dragger; Rectangle lastNonFullScreenPos;