From 07b73e40712c0db040f95668ebfa7068d9b2af8b Mon Sep 17 00:00:00 2001 From: jules Date: Thu, 29 Sep 2011 14:51:42 +0100 Subject: [PATCH] StringArray fix + additional unit tests. AudioFormatManager tweak for iOS. Minor clean-ups. --- extras/JuceDemo/Source/demos/OpenGLDemo.cpp | 2 +- .../format/juce_AudioFormatManager.cpp | 55 +++++++------------ .../containers/juce_LinkedListPointer.h | 8 +-- modules/juce_core/text/juce_String.cpp | 19 ++++++- modules/juce_core/text/juce_StringArray.cpp | 27 ++++----- modules/juce_core/xml/juce_XmlElement.cpp | 17 +++--- .../broadcasters/juce_AsyncUpdater.cpp | 17 ++---- .../broadcasters/juce_AsyncUpdater.h | 7 +-- .../broadcasters/juce_ListenerList.h | 8 +-- modules/juce_graphics/colour/juce_Colour.cpp | 6 +- .../juce_graphics/colour/juce_FillType.cpp | 12 ++-- .../filebrowser/juce_FileBrowserComponent.h | 4 +- .../juce_gui_basics/menus/juce_PopupMenu.cpp | 8 +-- .../juce_gui_basics/widgets/juce_ComboBox.h | 2 +- 14 files changed, 91 insertions(+), 101 deletions(-) diff --git a/extras/JuceDemo/Source/demos/OpenGLDemo.cpp b/extras/JuceDemo/Source/demos/OpenGLDemo.cpp index fd2e2f46c5..4d8bc3a75e 100644 --- a/extras/JuceDemo/Source/demos/OpenGLDemo.cpp +++ b/extras/JuceDemo/Source/demos/OpenGLDemo.cpp @@ -172,7 +172,7 @@ private: for (int i = 10; --i >= 0;) { Path pp; - pp.addStar (Point (r.nextInt (w), r.nextInt (h)), r.nextInt (8) + 3, 10, 20, 0); + pp.addStar (Point (r.nextFloat() * w, r.nextFloat() * h, r.nextFloat() * 8 + 3, 10.0f, 20.0f, 0.0f); g.setColour (Colours::pink.withAlpha (0.4f)); g.fillPath (pp); } diff --git a/modules/juce_audio_formats/format/juce_AudioFormatManager.cpp b/modules/juce_audio_formats/format/juce_AudioFormatManager.cpp index 6efab88cf0..98e2a11cf4 100644 --- a/modules/juce_audio_formats/format/juce_AudioFormatManager.cpp +++ b/modules/juce_audio_formats/format/juce_AudioFormatManager.cpp @@ -42,7 +42,7 @@ void AudioFormatManager::registerFormat (AudioFormat* newFormat, const bool make if (newFormat != nullptr) { - #if JUCE_DEBUG + #if JUCE_DEBUG for (int i = getNumKnownFormats(); --i >= 0;) { if (getKnownFormat (i)->getFormatName() == newFormat->getFormatName()) @@ -50,7 +50,7 @@ void AudioFormatManager::registerFormat (AudioFormat* newFormat, const bool make jassertfalse; // trying to add the same format twice! } } - #endif + #endif if (makeThisTheDefaultFormat) defaultFormatIndex = getNumKnownFormats(); @@ -61,22 +61,20 @@ void AudioFormatManager::registerFormat (AudioFormat* newFormat, const bool make void AudioFormatManager::registerBasicFormats() { - #if JUCE_MAC - registerFormat (new AiffAudioFormat(), true); - registerFormat (new WavAudioFormat(), false); - registerFormat (new CoreAudioFormat(), false); - #else registerFormat (new WavAudioFormat(), true); registerFormat (new AiffAudioFormat(), false); - #endif - #if JUCE_USE_FLAC + #if JUCE_MAC || JUCE_IOS + registerFormat (new CoreAudioFormat(), false); + #endif + + #if JUCE_USE_FLAC registerFormat (new FlacAudioFormat(), false); - #endif + #endif - #if JUCE_USE_OGGVORBIS + #if JUCE_USE_OGGVORBIS registerFormat (new OggVorbisAudioFormat(), false); - #endif + #endif } void AudioFormatManager::clearFormats() @@ -102,12 +100,11 @@ AudioFormat* AudioFormatManager::getDefaultFormat() const AudioFormat* AudioFormatManager::findFormatForFileExtension (const String& fileExtension) const { - String e (fileExtension); - if (! e.startsWithChar ('.')) - e = "." + e; + if (! fileExtension.startsWithChar ('.')) + return findFormatForFileExtension ("." + fileExtension); for (int i = 0; i < getNumKnownFormats(); ++i) - if (getKnownFormat(i)->getFileExtensions().contains (e, true)) + if (getKnownFormat(i)->getFileExtensions().contains (fileExtension, true)) return getKnownFormat(i); return nullptr; @@ -115,30 +112,20 @@ AudioFormat* AudioFormatManager::findFormatForFileExtension (const String& fileE String AudioFormatManager::getWildcardForAllFormats() const { - StringArray allExtensions; + StringArray extensions; int i; for (i = 0; i < getNumKnownFormats(); ++i) - allExtensions.addArray (getKnownFormat (i)->getFileExtensions()); - - allExtensions.trim(); - allExtensions.removeEmptyStrings(); + extensions.addArray (getKnownFormat(i)->getFileExtensions()); - String s; - for (i = 0; i < allExtensions.size(); ++i) - { - s << '*'; - - if (! allExtensions[i].startsWithChar ('.')) - s << '.'; + extensions.trim(); + extensions.removeEmptyStrings(); - s << allExtensions[i]; - - if (i < allExtensions.size() - 1) - s << ';'; - } + for (i = 0; i < extensions.size(); ++i) + extensions.set (i, (extensions[i].startsWithChar ('.') ? "*" : "*.") + extensions[i]); - return s; + extensions.removeDuplicates (true); + return extensions.joinIntoString (";"); } //============================================================================== diff --git a/modules/juce_core/containers/juce_LinkedListPointer.h b/modules/juce_core/containers/juce_LinkedListPointer.h index a9dfb2648a..f0f7892a0a 100644 --- a/modules/juce_core/containers/juce_LinkedListPointer.h +++ b/modules/juce_core/containers/juce_LinkedListPointer.h @@ -87,12 +87,10 @@ public: LinkedListPointer& operator= (LinkedListPointer&& other) noexcept { - if (this != &other) - { - item = other.item; - other.item = nullptr; - } + jassert (this != &other); // hopefully the compiler should make this situation impossible! + item = other.item; + other.item = nullptr; return *this; } #endif diff --git a/modules/juce_core/text/juce_String.cpp b/modules/juce_core/text/juce_String.cpp index 2f55c15d86..605d638cc7 100644 --- a/modules/juce_core/text/juce_String.cpp +++ b/modules/juce_core/text/juce_String.cpp @@ -2382,8 +2382,8 @@ public: beginTest ("StringArray"); StringArray s; - for (int i = 5; --i >= 0;) - s.add (String (i)); + s.addTokens ("4,3,2,1,0", ";,", "x"); + expectEquals (s.size(), 5); expectEquals (s.joinIntoString ("-"), String ("4-3-2-1-0")); s.remove (2); @@ -2391,6 +2391,21 @@ public: expectEquals (s.joinIntoString (String::empty), String ("4310")); s.clear(); expectEquals (s.joinIntoString ("x"), String::empty); + + StringArray toks; + toks.addTokens ("x,,", ";,", ""); + expectEquals (toks.size(), 3); + expectEquals (toks.joinIntoString ("-"), String ("x--")); + toks.clear(); + + toks.addTokens (",x,", ";,", ""); + expectEquals (toks.size(), 3); + expectEquals (toks.joinIntoString ("-"), String ("-x-")); + toks.clear(); + + toks.addTokens ("x,'y,z',", ";,", "'"); + expectEquals (toks.size(), 3); + expectEquals (toks.joinIntoString ("-"), String ("x-'y,z'-")); } } }; diff --git a/modules/juce_core/text/juce_StringArray.cpp b/modules/juce_core/text/juce_StringArray.cpp index b522329317..7d95b4a3fc 100644 --- a/modules/juce_core/text/juce_StringArray.cpp +++ b/modules/juce_core/text/juce_StringArray.cpp @@ -279,15 +279,13 @@ void StringArray::trim() } //============================================================================== -class InternalStringArrayComparator_CaseSensitive +struct InternalStringArrayComparator_CaseSensitive { -public: static int compareElements (String& first, String& second) { return first.compare (second); } }; -class InternalStringArrayComparator_CaseInsensitive +struct InternalStringArrayComparator_CaseInsensitive { -public: static int compareElements (String& first, String& second) { return first.compareIgnoreCase (second); } }; @@ -363,18 +361,21 @@ int StringArray::addTokens (const String& text, const String& breakCharacters, c int num = 0; String::CharPointerType t (text.getCharPointer()); - while (! t.isEmpty()) + if (! t.isEmpty()) { - String::CharPointerType tokenEnd (CharacterFunctions::findEndOfToken (t, - breakCharacters.getCharPointer(), - quoteCharacters.getCharPointer())); - add (String (t, tokenEnd)); - ++num; + for (;;) + { + String::CharPointerType tokenEnd (CharacterFunctions::findEndOfToken (t, + breakCharacters.getCharPointer(), + quoteCharacters.getCharPointer())); + add (String (t, tokenEnd)); + ++num; - if (tokenEnd.isEmpty()) - break; + if (tokenEnd.isEmpty()) + break; - t = ++tokenEnd; + t = ++tokenEnd; + } } return num; diff --git a/modules/juce_core/xml/juce_XmlElement.cpp b/modules/juce_core/xml/juce_XmlElement.cpp index e6b2980dcf..07ad4a862b 100644 --- a/modules/juce_core/xml/juce_XmlElement.cpp +++ b/modules/juce_core/xml/juce_XmlElement.cpp @@ -95,16 +95,15 @@ XmlElement::XmlElement (XmlElement&& other) noexcept XmlElement& XmlElement::operator= (XmlElement&& other) noexcept { - if (this != &other) - { - removeAllAttributes(); - deleteAllChildElements(); + jassert (this != &other); // hopefully the compiler should make this situation impossible! - nextListItem = static_cast &&> (other.nextListItem); - firstChildElement = static_cast &&> (other.firstChildElement); - attributes = static_cast &&> (other.attributes); - tagName = static_cast (other.tagName); - } + removeAllAttributes(); + deleteAllChildElements(); + + nextListItem = static_cast &&> (other.nextListItem); + firstChildElement = static_cast &&> (other.firstChildElement); + attributes = static_cast &&> (other.attributes); + tagName = static_cast (other.tagName); return *this; } diff --git a/modules/juce_events/broadcasters/juce_AsyncUpdater.cpp b/modules/juce_events/broadcasters/juce_AsyncUpdater.cpp index b28b08f3c0..a21a6d8346 100644 --- a/modules/juce_events/broadcasters/juce_AsyncUpdater.cpp +++ b/modules/juce_events/broadcasters/juce_AsyncUpdater.cpp @@ -26,7 +26,7 @@ BEGIN_JUCE_NAMESPACE //============================================================================== -class AsyncUpdaterMessage : public CallbackMessage +class AsyncUpdater::AsyncUpdaterMessage : public CallbackMessage { public: AsyncUpdaterMessage (AsyncUpdater& owner_) @@ -52,11 +52,6 @@ AsyncUpdater::AsyncUpdater() message = new AsyncUpdaterMessage (*this); } -inline Atomic& AsyncUpdater::getDeliveryFlag() const noexcept -{ - return static_cast (message.getObject())->shouldDeliver; -} - AsyncUpdater::~AsyncUpdater() { // You're deleting this object with a background thread while there's an update @@ -65,18 +60,18 @@ AsyncUpdater::~AsyncUpdater() // deleting this object, or find some other way to avoid such a race condition. jassert ((! isUpdatePending()) || MessageManager::getInstance()->currentThreadHasLockedMessageManager()); - getDeliveryFlag().set (0); + message->shouldDeliver.set (0); } void AsyncUpdater::triggerAsyncUpdate() { - if (getDeliveryFlag().compareAndSetBool (1, 0)) + if (message->shouldDeliver.compareAndSetBool (1, 0)) message->post(); } void AsyncUpdater::cancelPendingUpdate() noexcept { - getDeliveryFlag().set (0); + message->shouldDeliver.set (0); } void AsyncUpdater::handleUpdateNowIfNeeded() @@ -84,13 +79,13 @@ void AsyncUpdater::handleUpdateNowIfNeeded() // This can only be called by the event thread. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager()); - if (getDeliveryFlag().exchange (0) != 0) + if (message->shouldDeliver.exchange (0) != 0) handleAsyncUpdate(); } bool AsyncUpdater::isUpdatePending() const noexcept { - return getDeliveryFlag().value != 0; + return message->shouldDeliver.value != 0; } diff --git a/modules/juce_events/broadcasters/juce_AsyncUpdater.h b/modules/juce_events/broadcasters/juce_AsyncUpdater.h index f7d3db36ac..236e85bf63 100644 --- a/modules/juce_events/broadcasters/juce_AsyncUpdater.h +++ b/modules/juce_events/broadcasters/juce_AsyncUpdater.h @@ -26,8 +26,6 @@ #ifndef __JUCE_ASYNCUPDATER_JUCEHEADER__ #define __JUCE_ASYNCUPDATER_JUCEHEADER__ -#include "../messages/juce_CallbackMessage.h" - //============================================================================== /** @@ -102,8 +100,9 @@ public: private: //============================================================================== - ReferenceCountedObjectPtr message; - Atomic& getDeliveryFlag() const noexcept; + class AsyncUpdaterMessage; + friend class ReferenceCountedObjectPtr; + ReferenceCountedObjectPtr message; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AsyncUpdater); }; diff --git a/modules/juce_events/broadcasters/juce_ListenerList.h b/modules/juce_events/broadcasters/juce_ListenerList.h index 848eb07353..26482a7395 100644 --- a/modules/juce_events/broadcasters/juce_ListenerList.h +++ b/modules/juce_events/broadcasters/juce_ListenerList.h @@ -281,14 +281,14 @@ public: { public: //============================================================================== - Iterator (const ListType& list_) + Iterator (const ListType& list_) noexcept : list (list_), index (list_.size()) {} - ~Iterator() {} + ~Iterator() noexcept {} //============================================================================== - bool next() + bool next() noexcept { if (index <= 0) return false; @@ -302,7 +302,7 @@ public: return index >= 0; } - bool next (const BailOutCheckerType& bailOutChecker) + bool next (const BailOutCheckerType& bailOutChecker) noexcept { return (! bailOutChecker.shouldBailOut()) && next(); } diff --git a/modules/juce_graphics/colour/juce_Colour.cpp b/modules/juce_graphics/colour/juce_Colour.cpp index 5305e2717e..afbf236851 100644 --- a/modules/juce_graphics/colour/juce_Colour.cpp +++ b/modules/juce_graphics/colour/juce_Colour.cpp @@ -97,7 +97,7 @@ struct HSB return Colour (hue, saturation, brightness, original.getAlpha()); } - static PixelARGB convertHSBtoRGB (float h, float s, float v, const uint8 alpha) noexcept + static PixelARGB toRGB (float h, float s, float v, const uint8 alpha) noexcept { v = jlimit (0.0f, 255.0f, v * 255.0f); const uint8 intV = (uint8) roundToInt (v); @@ -178,7 +178,7 @@ Colour Colour::fromRGBAFloat (const uint8 red, const uint8 green, const uint8 bl } Colour::Colour (const float hue, const float saturation, const float brightness, const float alpha) noexcept - : argb (HSB::convertHSBtoRGB (hue, saturation, brightness, ColourHelpers::floatToUInt8 (alpha))) + : argb (HSB::toRGB (hue, saturation, brightness, ColourHelpers::floatToUInt8 (alpha))) { } @@ -188,7 +188,7 @@ Colour Colour::fromHSV (const float hue, const float saturation, const float bri } Colour::Colour (const float hue, const float saturation, const float brightness, const uint8 alpha) noexcept - : argb (HSB::convertHSBtoRGB (hue, saturation, brightness, alpha)) + : argb (HSB::toRGB (hue, saturation, brightness, alpha)) { } diff --git a/modules/juce_graphics/colour/juce_FillType.cpp b/modules/juce_graphics/colour/juce_FillType.cpp index 84c320eee0..2a03c3ed28 100644 --- a/modules/juce_graphics/colour/juce_FillType.cpp +++ b/modules/juce_graphics/colour/juce_FillType.cpp @@ -78,14 +78,12 @@ FillType::FillType (FillType&& other) noexcept FillType& FillType::operator= (FillType&& other) noexcept { - if (this != &other) - { - colour = other.colour; - gradient = other.gradient.release(); - image = static_cast (other.image); - transform = other.transform; - } + jassert (this != &other); // hopefully the compiler should make this situation impossible! + colour = other.colour; + gradient = other.gradient.release(); + image = static_cast (other.image); + transform = other.transform; return *this; } #endif diff --git a/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.h b/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.h index f8c29d7092..590da9a358 100644 --- a/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.h +++ b/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.h @@ -79,11 +79,11 @@ public: or saveMode, and canSelectFiles and/or canSelectDirectories. @param initialFileOrDirectory The file or directory that should be selected when the component begins. If this is File::nonexistent, a default directory will be chosen. - @param fileFilter an optional filter to use to determine which files are shown. + @param fileFilter an optional filter to use to determine which files are shown. If this is nullptr then all files are displayed. Note that a pointer is kept internally to this object, so make sure that it is not deleted before the FileBrowserComponent object is deleted. - @param previewComp an optional preview component that will be used to show previews of + @param previewComp an optional preview component that will be used to show previews of files that the user selects */ FileBrowserComponent (int flags, diff --git a/modules/juce_gui_basics/menus/juce_PopupMenu.cpp b/modules/juce_gui_basics/menus/juce_PopupMenu.cpp index 539d9a329b..6227e64856 100644 --- a/modules/juce_gui_basics/menus/juce_PopupMenu.cpp +++ b/modules/juce_gui_basics/menus/juce_PopupMenu.cpp @@ -1169,12 +1169,10 @@ PopupMenu::PopupMenu (PopupMenu&& other) noexcept PopupMenu& PopupMenu::operator= (PopupMenu&& other) noexcept { - if (this != &other) - { - items.swapWithArray (other.items); - lookAndFeel = other.lookAndFeel; - } + jassert (this != &other); // hopefully the compiler should make this situation impossible! + items.swapWithArray (other.items); + lookAndFeel = other.lookAndFeel; return *this; } #endif diff --git a/modules/juce_gui_basics/widgets/juce_ComboBox.h b/modules/juce_gui_basics/widgets/juce_ComboBox.h index 8ce9d19aed..7ae5b208b0 100644 --- a/modules/juce_gui_basics/widgets/juce_ComboBox.h +++ b/modules/juce_gui_basics/widgets/juce_ComboBox.h @@ -108,7 +108,7 @@ public: The item ID of each item will be its index in the StringArray + firstItemIdOffset. */ void addItemList (const StringArray& items, int firstItemIdOffset); - + /** Adds a separator line to the drop-down list. This is like adding a separator to a popup menu. See PopupMenu::addSeparator().