Return object in OwnedArray add routines

This commit is contained in:
Vinnie Falco
2013-06-29 09:13:55 -07:00
parent 989549fac5
commit a458afc111

View File

@@ -242,14 +242,23 @@ public:
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 object that was added
@see set, insert, addIfNotAlreadyThere, addSorted @see set, insert, addIfNotAlreadyThere, addSorted
*/ */
void add (const ObjectClass* const newObject) noexcept ObjectClass const& add (ObjectClass const* const newObject) noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
data.ensureAllocatedSize (numUsed + 1); data.ensureAllocatedSize (numUsed + 1);
bassert (data.elements != nullptr); bassert (data.elements != nullptr);
data.elements [numUsed++] = const_cast <ObjectClass*> (newObject); data.elements [numUsed++] = const_cast <ObjectClass*> (newObject);
return *newObject;
}
ObjectClass& add (ObjectClass* const newObject) noexcept
{
add (const_cast <ObjectClass const*> (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.
@@ -267,10 +276,12 @@ 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 object that was added
@see add, addSorted, addIfNotAlreadyThere, set @see add, addSorted, addIfNotAlreadyThere, set
*/ */
void insert (int indexToInsertAt, ObjectClass const& insert (int indexToInsertAt,
const ObjectClass* const newObject) noexcept ObjectClass const* const newObject) noexcept
{ {
if (indexToInsertAt >= 0) if (indexToInsertAt >= 0)
{ {
@@ -295,6 +306,15 @@ public:
{ {
add (newObject); add (newObject);
} }
return *newObject;
}
ObjectClass& insert (int indexToInsertAt,
ObjectClass* const newObject) noexcept
{
insert (indexToInsertAt, const_cast <ObjectClass const*> (newObject));
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.
@@ -343,13 +363,22 @@ 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 object
*/ */
void addIfNotAlreadyThere (const ObjectClass* const newObject) noexcept ObjectClass const& addIfNotAlreadyThere (ObjectClass const* const newObject) noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
if (! contains (newObject)) if (! contains (newObject))
add (newObject); add (newObject);
return *newObject;
}
ObjectClass& addIfNotAlreadyThere (ObjectClass* const newObject) noexcept
{
addIfNotAlreadyThere (const_cast <ObjectClass const*> (newObject));
return *newObject;
} }
/** Replaces an object in the array with a different one. /** Replaces an object in the array with a different one.
@@ -363,11 +392,13 @@ public:
@param indexToChange the index whose value you want to change @param indexToChange the index whose value you want to change
@param newObject the new value to set for this index. @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 @param deleteOldElement whether to delete the object that's being replaced with the new one
@returns the object that was set
@see add, insert, remove @see add, insert, remove
*/ */
void set (const int indexToChange, ObjectClass const& set (int const indexToChange,
const ObjectClass* const newObject, ObjectClass const* const newObject,
const bool deleteOldElement = true) bool const deleteOldElement = true)
{ {
if (indexToChange >= 0) if (indexToChange >= 0)
{ {
@@ -404,6 +435,16 @@ public:
bassertfalse; // you're trying to set an object at a negative index, which doesn't have bassertfalse; // 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;
}
ObjectClass& set (int const indexToChange,
ObjectClass* const newObject,
bool const deleteOldElement = true)
{
set (indexToChange, const_cast <ObjectClass const*> (newObject), deleteOldElement);
return *newObject;
} }
/** Adds elements from another array to the end of this array. /** Adds elements from another array to the end of this array.