Browse Source

Made a couple of OwnedArray methods return the object that they are passed.

tags/2021-05-28
jules 11 years ago
parent
commit
aa8fdc8cba
1 changed files with 42 additions and 46 deletions
  1. +42
    -46
      modules/juce_core/containers/juce_OwnedArray.h

+ 42
- 46
modules/juce_core/containers/juce_OwnedArray.h View File

@@ -231,7 +231,7 @@ public:
@param objectToLookFor the object to look for @param objectToLookFor the object to look for
@returns the index at which the object was found, or -1 if it's not found @returns the index at which the object was found, or -1 if it's not found
*/ */
int indexOf (const ObjectClass* const objectToLookFor) const noexcept
int indexOf (const ObjectClass* objectToLookFor) const noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
ObjectClass* const* e = data.elements.getData(); ObjectClass* const* e = data.elements.getData();
@@ -249,7 +249,7 @@ public:
@param objectToLookFor the object to look for @param objectToLookFor the object to look for
@returns true if the object is in the array @returns true if the object is in the array
*/ */
bool contains (const ObjectClass* const objectToLookFor) const noexcept
bool contains (const ObjectClass* objectToLookFor) const noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
ObjectClass* const* e = data.elements.getData(); ObjectClass* const* e = data.elements.getData();
@@ -271,16 +271,17 @@ public:
Also be careful not to add the same object to the array more than once, Also be careful not to add the same object to the array more than once,
as this will obviously cause deletion of dangling pointers. as this will obviously cause deletion of dangling pointers.
@param newObject the new object to add to the array
@param newObject the new object to add to the array
@returns the new object that was added
@see set, insert, addIfNotAlreadyThere, addSorted @see set, insert, addIfNotAlreadyThere, addSorted
*/ */
ObjectClass* add (ObjectClass* const newObject) noexcept
ObjectClass* add (ObjectClass* newObject) noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
data.ensureAllocatedSize (numUsed + 1); data.ensureAllocatedSize (numUsed + 1);
jassert (data.elements != nullptr); jassert (data.elements != nullptr);
data.elements [numUsed++] = const_cast <ObjectClass*> (newObject);
return const_cast <ObjectClass*> (newObject);
data.elements [numUsed++] = newObject;
return newObject;
} }
/** Inserts a new object into the array at the given index. /** Inserts a new object into the array at the given index.
@@ -298,34 +299,31 @@ public:
@param indexToInsertAt the index at which the new element should be inserted @param indexToInsertAt the index at which the new element should be inserted
@param newObject the new object to add to the array @param newObject the new object to add to the array
@returns the new object that was added
@see add, addSorted, addIfNotAlreadyThere, set @see add, addSorted, addIfNotAlreadyThere, set
*/ */
void insert (int indexToInsertAt,
ObjectClass* const newObject) noexcept
ObjectClass* insert (int indexToInsertAt, ObjectClass* newObject) noexcept
{ {
if (indexToInsertAt >= 0)
{
const ScopedLockType lock (getLock());
if (indexToInsertAt < 0)
return add (newObject);
if (indexToInsertAt > numUsed)
indexToInsertAt = numUsed;
const ScopedLockType lock (getLock());
data.ensureAllocatedSize (numUsed + 1);
jassert (data.elements != nullptr);
if (indexToInsertAt > numUsed)
indexToInsertAt = numUsed;
ObjectClass** const e = data.elements + indexToInsertAt;
const int numToMove = numUsed - indexToInsertAt;
data.ensureAllocatedSize (numUsed + 1);
jassert (data.elements != nullptr);
if (numToMove > 0)
memmove (e + 1, e, sizeof (ObjectClass*) * (size_t) numToMove);
ObjectClass** const e = data.elements + indexToInsertAt;
const int numToMove = numUsed - indexToInsertAt;
*e = const_cast <ObjectClass*> (newObject);
++numUsed;
}
else
{
add (newObject);
}
if (numToMove > 0)
memmove (e + 1, e, sizeof (ObjectClass*) * (size_t) numToMove);
*e = newObject;
++numUsed;
return newObject;
} }
/** Inserts an array of values into this array at a given position. /** Inserts an array of values into this array at a given position.
@@ -374,13 +372,16 @@ public:
If the array already contains a matching object, nothing will be done. If the array already contains a matching object, nothing will be done.
@param newObject the new object to add to the array @param newObject the new object to add to the array
@returns the new object that was added
*/ */
void addIfNotAlreadyThere (ObjectClass* const newObject) noexcept
ObjectClass* addIfNotAlreadyThere (ObjectClass* newObject) noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
if (! contains (newObject)) if (! contains (newObject))
add (newObject); add (newObject);
return newObject;
} }
/** Replaces an object in the array with a different one. /** Replaces an object in the array with a different one.
@@ -396,9 +397,7 @@ public:
@param deleteOldElement whether to delete the object that's being replaced with the new one @param deleteOldElement whether to delete the object that's being replaced with the new one
@see add, insert, remove @see add, insert, remove
*/ */
void set (const int indexToChange,
const ObjectClass* const newObject,
const bool deleteOldElement = true)
ObjectClass* set (int indexToChange, ObjectClass* newObject, bool deleteOldElement = true)
{ {
if (indexToChange >= 0) if (indexToChange >= 0)
{ {
@@ -417,12 +416,12 @@ public:
toDelete.release(); toDelete.release();
} }
data.elements [indexToChange] = const_cast <ObjectClass*> (newObject);
data.elements [indexToChange] = newObject;
} }
else else
{ {
data.ensureAllocatedSize (numUsed + 1); data.ensureAllocatedSize (numUsed + 1);
data.elements [numUsed++] = const_cast <ObjectClass*> (newObject);
data.elements [numUsed++] = newObject;
} }
} }
} }
@@ -431,6 +430,8 @@ public:
jassertfalse; // you're trying to set an object at a negative index, which doesn't have jassertfalse; // you're trying to set an object at a negative index, which doesn't have
// any effect - but since the object is not being added, it may be leaking.. // any effect - but since the object is not being added, it may be leaking..
} }
return newObject;
} }
/** Adds elements from another array to the end of this array. /** Adds elements from another array to the end of this array.
@@ -581,8 +582,7 @@ public:
@param deleteObject whether to delete the object that is removed @param deleteObject whether to delete the object that is removed
@see removeObject, removeRange @see removeObject, removeRange
*/ */
void remove (const int indexToRemove,
const bool deleteObject = true)
void remove (int indexToRemove, bool deleteObject = true)
{ {
ScopedPointer<ObjectClass> toDelete; ScopedPointer<ObjectClass> toDelete;
@@ -617,7 +617,7 @@ public:
@param indexToRemove the index of the element to remove @param indexToRemove the index of the element to remove
@see remove, removeObject, removeRange @see remove, removeObject, removeRange
*/ */
ObjectClass* removeAndReturn (const int indexToRemove)
ObjectClass* removeAndReturn (int indexToRemove)
{ {
ObjectClass* removedItem = nullptr; ObjectClass* removedItem = nullptr;
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
@@ -648,8 +648,7 @@ public:
@param deleteObject whether to delete the object (if it's found) @param deleteObject whether to delete the object (if it's found)
@see remove, removeRange @see remove, removeRange
*/ */
void removeObject (const ObjectClass* const objectToRemove,
const bool deleteObject = true)
void removeObject (const ObjectClass* objectToRemove, bool deleteObject = true)
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
ObjectClass** const e = data.elements.getData(); ObjectClass** const e = data.elements.getData();
@@ -677,9 +676,7 @@ public:
@param deleteObjects whether to delete the objects that get removed @param deleteObjects whether to delete the objects that get removed
@see remove, removeObject @see remove, removeObject
*/ */
void removeRange (int startIndex,
const int numberToRemove,
const bool deleteObjects = true)
void removeRange (int startIndex, int numberToRemove, bool deleteObjects = true)
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
const int endIndex = jlimit (0, numUsed, startIndex + numberToRemove); const int endIndex = jlimit (0, numUsed, startIndex + numberToRemove);
@@ -719,7 +716,7 @@ public:
@see remove, removeObject, removeRange @see remove, removeObject, removeRange
*/ */
void removeLast (int howManyToRemove = 1, void removeLast (int howManyToRemove = 1,
const bool deleteObjects = true)
bool deleteObjects = true)
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
@@ -734,8 +731,8 @@ public:
If either of the indexes passed in is out-of-range, nothing will happen, If either of the indexes passed in is out-of-range, nothing will happen,
otherwise the two objects at these positions will be exchanged. otherwise the two objects at these positions will be exchanged.
*/ */
void swap (const int index1,
const int index2) noexcept
void swap (int index1,
int index2) noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
@@ -760,8 +757,7 @@ public:
@param newIndex the index at which you'd like this object to end up. If this @param newIndex the index at which you'd like this object to end up. If this
is less than zero, it will be moved to the end of the array is less than zero, it will be moved to the end of the array
*/ */
void move (const int currentIndex,
int newIndex) noexcept
void move (int currentIndex, int newIndex) noexcept
{ {
if (currentIndex != newIndex) if (currentIndex != newIndex)
{ {
@@ -859,7 +855,7 @@ public:
*/ */
template <class ElementComparator> template <class ElementComparator>
void sort (ElementComparator& comparator, void sort (ElementComparator& comparator,
const bool retainOrderOfEquivalentItems = false) const noexcept
bool retainOrderOfEquivalentItems = false) const noexcept
{ {
(void) comparator; // if you pass in an object with a static compareElements() method, this (void) comparator; // if you pass in an object with a static compareElements() method, this
// avoids getting warning messages about the parameter being unused // avoids getting warning messages about the parameter being unused


Loading…
Cancel
Save