From a458afc1117c24c1032c2c83a76d6c55e7b65ec7 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 29 Jun 2013 09:13:55 -0700 Subject: [PATCH] Return object in OwnedArray add routines --- .../beast_core/containers/beast_OwnedArray.h | 55 ++++++++++++++++--- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h b/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h index 773064f6f1..bb134cc36e 100644 --- a/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h +++ b/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h @@ -242,14 +242,23 @@ public: as this will obviously cause deletion of dangling pointers. @param newObject the new object to add to the array + @returns the object that was added + @see set, insert, addIfNotAlreadyThere, addSorted */ - void add (const ObjectClass* const newObject) noexcept + ObjectClass const& add (ObjectClass const* const newObject) noexcept { const ScopedLockType lock (getLock()); data.ensureAllocatedSize (numUsed + 1); bassert (data.elements != nullptr); data.elements [numUsed++] = const_cast (newObject); + return *newObject; + } + + ObjectClass& add (ObjectClass* const newObject) noexcept + { + add (const_cast (newObject)); + return *newObject; } /** 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 newObject the new object to add to the array + @returns the object that was added + @see add, addSorted, addIfNotAlreadyThere, set */ - void insert (int indexToInsertAt, - const ObjectClass* const newObject) noexcept + ObjectClass const& insert (int indexToInsertAt, + ObjectClass const* const newObject) noexcept { if (indexToInsertAt >= 0) { @@ -295,6 +306,15 @@ public: { add (newObject); } + + return *newObject; + } + + ObjectClass& insert (int indexToInsertAt, + ObjectClass* const newObject) noexcept + { + insert (indexToInsertAt, const_cast (newObject)); + return *newObject; } /** 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. @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()); if (! contains (newObject)) add (newObject); + + return *newObject; + } + + ObjectClass& addIfNotAlreadyThere (ObjectClass* const newObject) noexcept + { + addIfNotAlreadyThere (const_cast (newObject)); + return *newObject; } /** 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 newObject the new value to set for this index. @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 */ - void set (const int indexToChange, - const ObjectClass* const newObject, - const bool deleteOldElement = true) + ObjectClass const& set (int const indexToChange, + ObjectClass const* const newObject, + bool const deleteOldElement = true) { if (indexToChange >= 0) { @@ -404,6 +435,16 @@ public: 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.. } + + return *newObject; + } + + ObjectClass& set (int const indexToChange, + ObjectClass* const newObject, + bool const deleteOldElement = true) + { + set (indexToChange, const_cast (newObject), deleteOldElement); + return *newObject; } /** Adds elements from another array to the end of this array.