diff --git a/modules/juce_core/containers/juce_Array.h b/modules/juce_core/containers/juce_Array.h index d3f60442aa..e258b680e4 100644 --- a/modules/juce_core/containers/juce_Array.h +++ b/modules/juce_core/containers/juce_Array.h @@ -279,7 +279,14 @@ public: inline ElementType getFirst() const { const ScopedLockType lock (getLock()); - return numUsed > 0 ? data.elements[0] : ElementType(); + + if (numUsed > 0) + { + jassert (data.elements != nullptr); + return data.elements[0]; + } + + return ElementType(); } /** Returns the last element in the array, or a default value if the array is empty. @@ -289,7 +296,14 @@ public: inline ElementType getLast() const { const ScopedLockType lock (getLock()); - return numUsed > 0 ? data.elements[numUsed - 1] : ElementType(); + + if (numUsed > 0) + { + jassert (data.elements != nullptr); + return data.elements[numUsed - 1]; + } + + return ElementType(); } /** Returns a pointer to the actual array data. diff --git a/modules/juce_core/containers/juce_OwnedArray.h b/modules/juce_core/containers/juce_OwnedArray.h index 7458fe61eb..093ecd5671 100644 --- a/modules/juce_core/containers/juce_OwnedArray.h +++ b/modules/juce_core/containers/juce_OwnedArray.h @@ -166,8 +166,14 @@ public: inline ObjectClass* getFirst() const noexcept { const ScopedLockType lock (getLock()); - return numUsed > 0 ? data.elements [0] - : static_cast (nullptr); + + if (numUsed > 0) + { + jassert (data.elements != nullptr); + return data.elements [0]; + } + + return nullptr; } /** Returns a pointer to the last object in the array. @@ -178,8 +184,14 @@ public: inline ObjectClass* getLast() const noexcept { const ScopedLockType lock (getLock()); - return numUsed > 0 ? data.elements [numUsed - 1] - : static_cast (nullptr); + + if (numUsed > 0) + { + jassert (data.elements != nullptr); + return data.elements [numUsed - 1]; + } + + return nullptr; } /** Returns a pointer to the actual array data. diff --git a/modules/juce_core/containers/juce_ReferenceCountedArray.h b/modules/juce_core/containers/juce_ReferenceCountedArray.h index fba2fa4133..8de5ec9c53 100644 --- a/modules/juce_core/containers/juce_ReferenceCountedArray.h +++ b/modules/juce_core/containers/juce_ReferenceCountedArray.h @@ -180,8 +180,14 @@ public: inline ObjectClass* getObjectPointer (const int index) const noexcept { const ScopedLockType lock (getLock()); - return isPositiveAndBelow (index, numUsed) ? data.elements [index] - : nullptr; + + if (isPositiveAndBelow (index, numUsed)) + { + jassert (data.elements != nullptr); + return data.elements [index]; + } + + return ObjectClassPtr(); } /** Returns a raw pointer to the object at this index in the array, without checking @@ -190,7 +196,7 @@ public: inline ObjectClass* getObjectPointerUnchecked (const int index) const noexcept { const ScopedLockType lock (getLock()); - jassert (isPositiveAndBelow (index, numUsed)); + jassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr); return data.elements [index]; } @@ -202,8 +208,14 @@ public: inline ObjectClassPtr getFirst() const noexcept { const ScopedLockType lock (getLock()); - return numUsed > 0 ? data.elements [0] - : static_cast (nullptr); + + if (numUsed > 0) + { + jassert (data.elements != nullptr); + return data.elements [0]; + } + + return ObjectClassPtr(); } /** Returns a pointer to the last object in the array. @@ -214,8 +226,14 @@ public: inline ObjectClassPtr getLast() const noexcept { const ScopedLockType lock (getLock()); - return numUsed > 0 ? data.elements [numUsed - 1] - : static_cast (nullptr); + + if (numUsed > 0) + { + jassert (data.elements != nullptr); + return data.elements [numUsed - 1]; + } + + return ObjectClassPtr(); } /** Returns a pointer to the actual array data. @@ -325,35 +343,31 @@ public: ObjectClass* insert (int indexToInsertAt, ObjectClass* const newObject) noexcept { - if (indexToInsertAt >= 0) - { - const ScopedLockType lock (getLock()); + if (indexToInsertAt < 0) + return add (newObject); + + const ScopedLockType lock (getLock()); - if (indexToInsertAt > numUsed) - indexToInsertAt = numUsed; + if (indexToInsertAt > numUsed) + indexToInsertAt = numUsed; - data.ensureAllocatedSize (numUsed + 1); - jassert (data.elements != nullptr); + data.ensureAllocatedSize (numUsed + 1); + jassert (data.elements != nullptr); - ObjectClass** const e = data.elements + indexToInsertAt; - const int numToMove = numUsed - indexToInsertAt; + ObjectClass** const e = data.elements + indexToInsertAt; + const int numToMove = numUsed - indexToInsertAt; - if (numToMove > 0) - memmove (e + 1, e, sizeof (ObjectClass*) * (size_t) numToMove); + if (numToMove > 0) + memmove (e + 1, e, sizeof (ObjectClass*) * (size_t) numToMove); - *e = newObject; + *e = newObject; - if (newObject != nullptr) - newObject->incReferenceCount(); + if (newObject != nullptr) + newObject->incReferenceCount(); - ++numUsed; + ++numUsed; - return newObject; - } - else - { - return add (newObject); - } + return newObject; } /** Appends a new object at the end of the array as long as the array doesn't