diff --git a/modules/juce_core/containers/juce_SortedSet.h b/modules/juce_core/containers/juce_SortedSet.h index b3f7905ff9..69d7319d46 100644 --- a/modules/juce_core/containers/juce_SortedSet.h +++ b/modules/juce_core/containers/juce_SortedSet.h @@ -380,7 +380,7 @@ public: @param valueToRemove the object to try to remove @see remove, removeRange */ - void removeValue (const ElementType valueToRemove) noexcept + void removeValue (const ElementType& valueToRemove) noexcept { const ScopedLockType lock (getLock()); data.remove (indexOf (valueToRemove)); diff --git a/modules/juce_core/memory/juce_OptionalScopedPointer.h b/modules/juce_core/memory/juce_OptionalScopedPointer.h index 1c21885e4f..325bb0258a 100644 --- a/modules/juce_core/memory/juce_OptionalScopedPointer.h +++ b/modules/juce_core/memory/juce_OptionalScopedPointer.h @@ -49,7 +49,8 @@ public: OptionalScopedPointer just holds a normal pointer to the object, and won't delete it. */ OptionalScopedPointer (ObjectType* objectToHold, bool takeOwnership) - : object (objectToHold), shouldDelete (takeOwnership) + : object (objectToHold), + shouldDelete (takeOwnership) { } @@ -61,9 +62,21 @@ public: The flag to indicate whether or not to delete the managed object is also copied from the source object. */ - OptionalScopedPointer (OptionalScopedPointer& objectToTransferFrom) - : object (objectToTransferFrom.release()), - shouldDelete (objectToTransferFrom.shouldDelete) + OptionalScopedPointer (OptionalScopedPointer&& other) noexcept + : object (std::move (other.object)), + shouldDelete (std::move (other.shouldDelete)) + { + } + + /** Takes ownership of the object owned by `ptr`. */ + explicit OptionalScopedPointer (std::unique_ptr&& ptr) noexcept + : OptionalScopedPointer (ptr.release(), true) + { + } + + /** Points to the same object as `ref`, but does not take ownership. */ + explicit OptionalScopedPointer (ObjectType& ref) noexcept + : OptionalScopedPointer (std::addressof (ref), false) { } @@ -75,15 +88,10 @@ public: The ownership flag that says whether or not to delete the managed object is also copied from the source object. */ - OptionalScopedPointer& operator= (OptionalScopedPointer& objectToTransferFrom) + OptionalScopedPointer& operator= (OptionalScopedPointer&& other) noexcept { - if (object != objectToTransferFrom.object) - { - reset(); - object.reset (objectToTransferFrom.object.release()); - } - - shouldDelete = objectToTransferFrom.shouldDelete; + swapWith (other); + other.reset(); return *this; } @@ -91,23 +99,23 @@ public: takeOwnership flag that was specified when the object was first passed into an OptionalScopedPointer constructor. */ - ~OptionalScopedPointer() + ~OptionalScopedPointer() noexcept { reset(); } //============================================================================== /** Returns the object that this pointer is managing. */ - inline operator ObjectType*() const noexcept { return object.get(); } + operator ObjectType*() const noexcept { return object.get(); } /** Returns the object that this pointer is managing. */ - inline ObjectType* get() const noexcept { return object.get(); } + ObjectType* get() const noexcept { return object.get(); } /** Returns the object that this pointer is managing. */ - inline ObjectType& operator*() const noexcept { return *object; } + ObjectType& operator*() const noexcept { return *object; } /** Lets you access methods and properties of the object that this pointer is holding. */ - inline ObjectType* operator->() const noexcept { return object.get(); } + ObjectType* operator->() const noexcept { return object.get(); } //============================================================================== /** Removes the current object from this OptionalScopedPointer without deleting it. @@ -118,7 +126,7 @@ public: /** Resets this pointer to null, possibly deleting the object that it holds, if it has ownership of it. */ - void reset() + void reset() noexcept { if (! shouldDelete) object.release(); @@ -170,8 +178,8 @@ public: */ void swapWith (OptionalScopedPointer& other) noexcept { - object.swapWith (other.object); - std::swap (shouldDelete, other.shouldDelete); + std::swap (other.object, object); + std::swap (other.shouldDelete, shouldDelete); } private: diff --git a/modules/juce_gui_basics/filebrowser/juce_FileTreeComponent.cpp b/modules/juce_gui_basics/filebrowser/juce_FileTreeComponent.cpp index f1a3c300cb..63d7ec44e2 100644 --- a/modules/juce_gui_basics/filebrowser/juce_FileTreeComponent.cpp +++ b/modules/juce_gui_basics/filebrowser/juce_FileTreeComponent.cpp @@ -118,8 +118,7 @@ public: { removeSubContentsList(); - OptionalScopedPointer newPointer (newList, canDeleteList); - subContentsList = newPointer; + subContentsList = OptionalScopedPointer (newList, canDeleteList); newList->addChangeListener (this); }