Browse Source

OptionalScopedPointer: Added proper move semantics and convenience constructors

tags/2021-05-28
reuk Tom Poole 5 years ago
parent
commit
4b7043b0cd
3 changed files with 30 additions and 23 deletions
  1. +1
    -1
      modules/juce_core/containers/juce_SortedSet.h
  2. +28
    -20
      modules/juce_core/memory/juce_OptionalScopedPointer.h
  3. +1
    -2
      modules/juce_gui_basics/filebrowser/juce_FileTreeComponent.cpp

+ 1
- 1
modules/juce_core/containers/juce_SortedSet.h View File

@@ -380,7 +380,7 @@ public:
@param valueToRemove the object to try to remove @param valueToRemove the object to try to remove
@see remove, removeRange @see remove, removeRange
*/ */
void removeValue (const ElementType valueToRemove) noexcept
void removeValue (const ElementType& valueToRemove) noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
data.remove (indexOf (valueToRemove)); data.remove (indexOf (valueToRemove));


+ 28
- 20
modules/juce_core/memory/juce_OptionalScopedPointer.h View File

@@ -49,7 +49,8 @@ public:
OptionalScopedPointer just holds a normal pointer to the object, and won't delete it. OptionalScopedPointer just holds a normal pointer to the object, and won't delete it.
*/ */
OptionalScopedPointer (ObjectType* objectToHold, bool takeOwnership) 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 The flag to indicate whether or not to delete the managed object is also
copied from the source object. 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<ObjectType>&& 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 The ownership flag that says whether or not to delete the managed object is also
copied from the source object. 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; return *this;
} }
@@ -91,23 +99,23 @@ public:
takeOwnership flag that was specified when the object was first passed into an takeOwnership flag that was specified when the object was first passed into an
OptionalScopedPointer constructor. OptionalScopedPointer constructor.
*/ */
~OptionalScopedPointer()
~OptionalScopedPointer() noexcept
{ {
reset(); reset();
} }
//============================================================================== //==============================================================================
/** Returns the object that this pointer is managing. */ /** 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. */ /** 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. */ /** 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. */ /** 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. /** 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 /** Resets this pointer to null, possibly deleting the object that it holds, if it has
ownership of it. ownership of it.
*/ */
void reset()
void reset() noexcept
{ {
if (! shouldDelete) if (! shouldDelete)
object.release(); object.release();
@@ -170,8 +178,8 @@ public:
*/ */
void swapWith (OptionalScopedPointer<ObjectType>& other) noexcept void swapWith (OptionalScopedPointer<ObjectType>& other) noexcept
{ {
object.swapWith (other.object);
std::swap (shouldDelete, other.shouldDelete);
std::swap (other.object, object);
std::swap (other.shouldDelete, shouldDelete);
} }
private: private:


+ 1
- 2
modules/juce_gui_basics/filebrowser/juce_FileTreeComponent.cpp View File

@@ -118,8 +118,7 @@ public:
{ {
removeSubContentsList(); removeSubContentsList();
OptionalScopedPointer<DirectoryContentsList> newPointer (newList, canDeleteList);
subContentsList = newPointer;
subContentsList = OptionalScopedPointer<DirectoryContentsList> (newList, canDeleteList);
newList->addChangeListener (this); newList->addChangeListener (this);
} }


Loading…
Cancel
Save