diff --git a/extras/Projucer/Source/Project/jucer_Module.cpp b/extras/Projucer/Source/Project/jucer_Module.cpp index 37044b7c15..6683d4ee17 100644 --- a/extras/Projucer/Source/Project/jucer_Module.cpp +++ b/extras/Projucer/Source/Project/jucer_Module.cpp @@ -341,7 +341,7 @@ void LibraryModule::getConfigFlags (Project& project, OwnedArray config (new Project::ConfigFlag()); + auto config = std::make_unique(); config->sourceModuleID = getID(); config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim(); @@ -361,6 +361,7 @@ void LibraryModule::getConfigFlags (Project& project, OwnedArrayvalue = project.getConfigFlag (config->symbol); i += 2; + if (lines[i].contains ("#define " + config->symbol)) { auto value = lines[i].fromFirstOccurrenceOf ("#define " + config->symbol, false, true).trim(); @@ -372,7 +373,7 @@ void LibraryModule::getConfigFlags (Project& project, OwnedArrayvalue = true; else if (currentValue == "disabled") config->value = false; - flags.add (config.release()); + flags.add (std::move (config)); } } } diff --git a/extras/Projucer/Source/Project/jucer_Project.cpp b/extras/Projucer/Source/Project/jucer_Project.cpp index 0bf27f9acd..26cb07a84c 100644 --- a/extras/Projucer/Source/Project/jucer_Project.cpp +++ b/extras/Projucer/Source/Project/jucer_Project.cpp @@ -1231,15 +1231,17 @@ Project::Item Project::Item::createCopy() { Item i (*this); i.state = i. String Project::Item::getID() const { return state [Ids::ID]; } void Project::Item::setID (const String& newID) { state.setProperty (Ids::ID, newID, nullptr); } -Drawable* Project::Item::loadAsImageFile() const +std::unique_ptr Project::Item::loadAsImageFile() const { const MessageManagerLock mml (ThreadPoolJob::getCurrentThreadPoolJob()); if (! mml.lockWasGained()) return nullptr; - return isValid() ? Drawable::createFromImageFile (getFile()) - : nullptr; + if (isValid()) + return std::unique_ptr (Drawable::createFromImageFile (getFile())); + + return {}; } Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid, bool isModuleCode) diff --git a/extras/Projucer/Source/Project/jucer_Project.h b/extras/Projucer/Source/Project/jucer_Project.h index 75124c05f0..8dc898ee70 100644 --- a/extras/Projucer/Source/Project/jucer_Project.h +++ b/extras/Projucer/Source/Project/jucer_Project.h @@ -261,7 +261,7 @@ public: Item findItemWithID (const String& targetId) const; // (recursive search) String getImageFileID() const; - Drawable* loadAsImageFile() const; + std::unique_ptr loadAsImageFile() const; //============================================================================== Value getNameValue(); diff --git a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Xcode.h b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Xcode.h index 81efee8d70..7605e57c7c 100644 --- a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Xcode.h +++ b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Xcode.h @@ -2385,15 +2385,11 @@ private: void getIconImages (OwnedArray& images) const { - std::unique_ptr bigIcon (getBigIcon()); + if (auto icon = getBigIcon()) + images.add (std::move (icon)); - if (bigIcon != nullptr) - images.add (bigIcon.release()); - - std::unique_ptr smallIcon (getSmallIcon()); - - if (smallIcon != nullptr) - images.add (smallIcon.release()); + if (auto icon = getSmallIcon()) + images.add (std::move (icon)); } void createiOSIconFiles (File appIconSet) const diff --git a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExporter.cpp b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExporter.cpp index e9b56e2f1c..fcb6ba9836 100644 --- a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExporter.cpp +++ b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExporter.cpp @@ -819,12 +819,12 @@ void ProjectExporter::createDefaultConfigs() } } -Drawable* ProjectExporter::getBigIcon() const +std::unique_ptr ProjectExporter::getBigIcon() const { return project.getMainGroup().findItemWithID (settings [Ids::bigIcon]).loadAsImageFile(); } -Drawable* ProjectExporter::getSmallIcon() const +std::unique_ptr ProjectExporter::getSmallIcon() const { return project.getMainGroup().findItemWithID (settings [Ids::smallIcon]).loadAsImageFile(); } @@ -833,8 +833,8 @@ Image ProjectExporter::getBestIconForSize (int size, bool returnNullIfNothingBig { Drawable* im = nullptr; - std::unique_ptr im1 (getSmallIcon()); - std::unique_ptr im2 (getBigIcon()); + auto im1 = getSmallIcon(); + auto im2 = getBigIcon(); if (im1 != nullptr && im2 != nullptr) { diff --git a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExporter.h b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExporter.h index 4fae76432c..f00b2f6f79 100644 --- a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExporter.h +++ b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExporter.h @@ -179,8 +179,8 @@ public: void addProjectPathToBuildPathList (StringArray&, const RelativePath&, int index = -1) const; - Drawable* getBigIcon() const; - Drawable* getSmallIcon() const; + std::unique_ptr getBigIcon() const; + std::unique_ptr getSmallIcon() const; Image getBestIconForSize (int size, bool returnNullIfNothingBigEnough) const; String getExporterIdentifierMacro() const diff --git a/extras/Projucer/Source/ProjectSaving/jucer_ProjectSaver.cpp b/extras/Projucer/Source/ProjectSaving/jucer_ProjectSaver.cpp index 19c922ec0e..cbfb88b9ef 100644 --- a/extras/Projucer/Source/ProjectSaving/jucer_ProjectSaver.cpp +++ b/extras/Projucer/Source/ProjectSaving/jucer_ProjectSaver.cpp @@ -172,7 +172,7 @@ void ProjectSaver::writeProjects (const OwnedArray& modules, cons if (specifiedExporterToSave.isNotEmpty() && exp->getName() != specifiedExporterToSave) continue; - auto* exporter = exporters.add (exp.exporter.release()); + auto exporter = exporters.add (std::move (exp.exporter)); exporter->initialiseDependencyPathValues(); diff --git a/modules/juce_audio_processors/scanning/juce_KnownPluginList.cpp b/modules/juce_audio_processors/scanning/juce_KnownPluginList.cpp index 07cbf268a1..15b70213f1 100644 --- a/modules/juce_audio_processors/scanning/juce_KnownPluginList.cpp +++ b/modules/juce_audio_processors/scanning/juce_KnownPluginList.cpp @@ -419,7 +419,7 @@ struct PluginTreeUtils if (current->plugins.size() + current->subFolders.size() > 0) { current->folder = lastType; - tree.subFolders.add (current.release()); + tree.subFolders.add (std::move (current)); current.reset (new KnownPluginList::PluginTree()); } @@ -432,7 +432,7 @@ struct PluginTreeUtils if (current->plugins.size() + current->subFolders.size() > 0) { current->folder = lastType; - tree.subFolders.add (current.release()); + tree.subFolders.add (std::move (current)); } } diff --git a/modules/juce_core/containers/juce_OwnedArray.h b/modules/juce_core/containers/juce_OwnedArray.h index 3ab95275b7..7a70e567c0 100644 --- a/modules/juce_core/containers/juce_OwnedArray.h +++ b/modules/juce_core/containers/juce_OwnedArray.h @@ -146,7 +146,7 @@ public: @see getUnchecked */ - inline ObjectClass* operator[] (const int index) const noexcept + inline ObjectClass* operator[] (int index) const noexcept { const ScopedLockType lock (getLock()); return values.getValueWithDefault (index); @@ -157,7 +157,7 @@ public: This is a faster and less safe version of operator[] which doesn't check the index passed in, so it can be used when you're sure the index is always going to be legal. */ - inline ObjectClass* getUnchecked (const int index) const noexcept + inline ObjectClass* getUnchecked (int index) const noexcept { const ScopedLockType lock (getLock()); return values[index]; @@ -267,13 +267,30 @@ public: @returns the new object that was added @see set, insert, addIfNotAlreadyThere, addSorted */ - ObjectClass* add (ObjectClass* newObject) noexcept + ObjectClass* add (ObjectClass* newObject) { const ScopedLockType lock (getLock()); values.add (newObject); return newObject; } + /** Appends a new object to the end of the array. + + Note that the this object will be deleted by the OwnedArray when it + is removed, so be careful not to delete it somewhere else. + + Also be careful not to add the same object to the array more than once, + as this will obviously cause deletion of dangling pointers. + + @param newObject the new object to add to the array + @returns the new object that was added + @see set, insert, addIfNotAlreadyThere, addSorted + */ + ObjectClass* add (std::unique_ptr newObject) + { + return add (newObject.release()); + } + /** Inserts a new object into the array at the given index. Note that the this object will be deleted by the OwnedArray when it @@ -292,13 +309,36 @@ public: @returns the new object that was added @see add, addSorted, addIfNotAlreadyThere, set */ - ObjectClass* insert (int indexToInsertAt, ObjectClass* newObject) noexcept + ObjectClass* insert (int indexToInsertAt, ObjectClass* newObject) { const ScopedLockType lock (getLock()); values.insert (indexToInsertAt, newObject, 1); return newObject; } + /** Inserts a new object into the array at the given index. + + Note that the this object will be deleted by the OwnedArray when it + is removed, so be careful not to delete it somewhere else. + + If the index is less than 0 or greater than the size of the array, the + element will be added to the end of the array. + Otherwise, it will be inserted into the array, moving all the later elements + along to make room. + + Be careful not to add the same object to the array more than once, + as this will obviously cause deletion of dangling pointers. + + @param indexToInsertAt the index at which the new element should be inserted + @param newObject the new object to add to the array + @returns the new object that was added + @see add, addSorted, addIfNotAlreadyThere, set + */ + ObjectClass* insert (int indexToInsertAt, std::unique_ptr newObject) + { + return insert (indexToInsertAt, newObject.release()); + } + /** Inserts an array of values into this array at a given position. If the index is less than 0 or greater than the size of the array, the @@ -322,25 +362,6 @@ public: } } - /** Appends a new object at the end of the array as long as the array doesn't - already contain it. - - If the array already contains a matching object, nothing will be done. - - @param newObject the new object to add to the array - @returns true if the new object was added, false otherwise - */ - bool addIfNotAlreadyThere (ObjectClass* newObject) noexcept - { - const ScopedLockType lock (getLock()); - - if (contains (newObject)) - return false; - - add (newObject); - return true; - } - /** Replaces an object in the array with a different one. If the index is less than zero, this method does nothing. @@ -390,6 +411,24 @@ public: return newObject; } + /** Replaces an object in the array with a different one. + + If the index is less than zero, this method does nothing. + If the index is beyond the end of the array, the new object is added to the end of the array. + + Be careful not to add the same object to the array more than once, + as this will obviously cause deletion of dangling pointers. + + @param indexToChange the index whose value you want to change + @param newObject the new value to set for this index. + @param deleteOldElement whether to delete the object that's being replaced with the new one + @see add, insert, remove + */ + ObjectClass* set (int indexToChange, std::unique_ptr newObject, bool deleteOldElement = true) + { + return set (indexToChange, newObject.release(), deleteOldElement); + } + /** Adds elements from another array to the end of this array. @param arrayToAddFrom the array from which to copy the elements @@ -468,14 +507,14 @@ public: @see add, sort, indexOfSorted */ template - int addSorted (ElementComparator& comparator, ObjectClass* const newObject) noexcept + int addSorted (ElementComparator& comparator, ObjectClass* newObject) noexcept { // If you pass in an object with a static compareElements() method, this // avoids getting warning messages about the parameter being unused ignoreUnused (comparator); const ScopedLockType lock (getLock()); - const int index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size()); + auto index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size()); insert (index, newObject); return index; } @@ -493,7 +532,7 @@ public: @see addSorted, sort */ template - int indexOfSorted (ElementComparator& comparator, const ObjectClass* const objectToLookFor) const noexcept + int indexOfSorted (ElementComparator& comparator, const ObjectClass* objectToLookFor) const noexcept { // If you pass in an object with a static compareElements() method, this // avoids getting warning messages about the parameter being unused @@ -722,7 +761,7 @@ public: the array won't have to keep dynamically resizing itself as the elements are added, and it'll therefore be more efficient. */ - void ensureStorageAllocated (const int minNumElements) noexcept + void ensureStorageAllocated (int minNumElements) noexcept { const ScopedLockType lock (getLock()); values.ensureAllocatedSize (minNumElements); diff --git a/modules/juce_core/javascript/juce_Javascript.cpp b/modules/juce_core/javascript/juce_Javascript.cpp index 720f525797..6cfcc90993 100644 --- a/modules/juce_core/javascript/juce_Javascript.cpp +++ b/modules/juce_core/javascript/juce_Javascript.cpp @@ -1183,7 +1183,7 @@ struct JavascriptEngine::RootObject : public DynamicObject if (matchIf (TokenTypes::comma)) { std::unique_ptr block (new BlockStatement (location)); - block->statements.add (s.release()); + block->statements.add (std::move (s)); block->statements.add (parseVar()); return block.release(); } diff --git a/modules/juce_data_structures/undomanager/juce_UndoManager.cpp b/modules/juce_data_structures/undomanager/juce_UndoManager.cpp index 64b546b2d7..d44b1b7fbe 100644 --- a/modules/juce_data_structures/undomanager/juce_UndoManager.cpp +++ b/modules/juce_data_structures/undomanager/juce_UndoManager.cpp @@ -146,7 +146,7 @@ bool UndoManager::perform (UndoableAction* newAction) } totalUnitsStored += action->getSizeInUnits(); - actionSet->actions.add (action.release()); + actionSet->actions.add (std::move (action)); newTransaction = false; moveFutureTransactionsToStash(); diff --git a/modules/juce_gui_basics/filebrowser/juce_DirectoryContentsList.cpp b/modules/juce_gui_basics/filebrowser/juce_DirectoryContentsList.cpp index 4753a18dde..eecdc46b0e 100644 --- a/modules/juce_gui_basics/filebrowser/juce_DirectoryContentsList.cpp +++ b/modules/juce_gui_basics/filebrowser/juce_DirectoryContentsList.cpp @@ -253,7 +253,7 @@ bool DirectoryContentsList::addFile (const File& file, const bool isDir, if (files.getUnchecked(i)->filename == info->filename) return false; - files.add (info.release()); + files.add (std::move (info)); std::sort (files.begin(), files.end(), [] (const FileInfo* a, const FileInfo* b) {