diff --git a/modules/juce_core/containers/juce_ArrayBase.h b/modules/juce_core/containers/juce_ArrayBase.h index d706858600..39acf48884 100644 --- a/modules/juce_core/containers/juce_ArrayBase.h +++ b/modules/juce_core/containers/juce_ArrayBase.h @@ -230,7 +230,7 @@ public: } template - typename std::enable_if::value, void>::type + typename std::enable_if::value, int>::type addArray (const OtherArrayType& arrayToAddFrom, int startIndex, int numElementsToAdd = -1) { @@ -244,6 +244,8 @@ public: numElementsToAdd = (int) arrayToAddFrom.size() - startIndex; addArray (arrayToAddFrom.data() + startIndex, numElementsToAdd); + + return numElementsToAdd; } //============================================================================== diff --git a/modules/juce_core/containers/juce_ReferenceCountedArray.cpp b/modules/juce_core/containers/juce_ReferenceCountedArray.cpp new file mode 100644 index 0000000000..7660045747 --- /dev/null +++ b/modules/juce_core/containers/juce_ReferenceCountedArray.cpp @@ -0,0 +1,76 @@ +/* + ============================================================================== + + This file is part of the JUCE library. + Copyright (c) 2018 - ROLI Ltd. + + JUCE is an open source library subject to commercial or open-source + licensing. + + The code included in this file is provided under the terms of the ISC license + http://www.isc.org/downloads/software-support-policy/isc-license. Permission + To use, copy, modify, and/or distribute this software for any purpose with or + without fee is hereby granted provided that the above copyright notice and + this permission notice appear in all copies. + + JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE + DISCLAIMED. + + ============================================================================== +*/ + +namespace juce +{ + +#if JUCE_UNIT_TESTS + +class ReferenceCountedArrayTests : public UnitTest +{ +public: + ReferenceCountedArrayTests() : UnitTest ("ReferenceCountedArray", "Containers") {} + + //============================================================================== + void runTest() override + { + beginTest ("Add derived objects"); + { + derivedObjects.add (static_cast (new TestBaseObj())); + expectEquals (derivedObjects.size(), 1); + expectEquals (derivedObjects.getObjectPointer (0)->getReferenceCount(), 1); + expectEquals (derivedObjects[0]->getReferenceCount(), 2); + + for (auto o : derivedObjects) + expectEquals (o->getReferenceCount(), 1); + + for (auto o : getObjects()) + expectEquals (o->getReferenceCount(), 2); + } + } + +private: + struct TestBaseObj : public ReferenceCountedObject + { + TestBaseObj() = default; + }; + + struct TestDerivedObj : public TestBaseObj + { + TestDerivedObj() = default; + }; + + ReferenceCountedArray derivedObjects; + + ReferenceCountedArray getObjects() const + { + ReferenceCountedArray objects; + objects.addArray (derivedObjects); + return objects; + } +}; + +static ReferenceCountedArrayTests referenceCountedArrayTests; + +#endif + +} // namespace juce diff --git a/modules/juce_core/containers/juce_ReferenceCountedArray.h b/modules/juce_core/containers/juce_ReferenceCountedArray.h index 02d2ba152d..3b2a886077 100644 --- a/modules/juce_core/containers/juce_ReferenceCountedArray.h +++ b/modules/juce_core/containers/juce_ReferenceCountedArray.h @@ -62,7 +62,7 @@ public: ReferenceCountedArray (const ReferenceCountedArray& other) noexcept { const ScopedLockType lock (other.getLock()); - values.addArray (other.values.begin(), other.values.size()); + values.addArray (other.begin(), other.size()); for (auto* o : *this) if (o != nullptr) @@ -80,7 +80,6 @@ public: ReferenceCountedArray (const ReferenceCountedArray& other) noexcept { const typename ReferenceCountedArray::ScopedLockType lock (other.getLock()); - values.addArray (other.begin(), other.size()); for (auto* o : *this) @@ -466,11 +465,11 @@ public: { const ScopedLockType lock2 (getLock()); - values.addArray (arrayToAddFrom.values, startIndex, numElementsToAdd); - auto newSize = values.size(); + auto numElementsAdded = values.addArray (arrayToAddFrom.values, startIndex, numElementsToAdd); + auto** e = values.end(); - for (int i = newSize - numElementsToAdd; i < newSize; ++i) - values[i]->incReferenceCount(); + for (int i = 0; i < numElementsAdded; ++i) + (*(--e))->incReferenceCount(); } } diff --git a/modules/juce_core/juce_core.cpp b/modules/juce_core/juce_core.cpp index 1a174b1020..9cf231dc34 100644 --- a/modules/juce_core/juce_core.cpp +++ b/modules/juce_core/juce_core.cpp @@ -119,6 +119,7 @@ //============================================================================== #include "containers/juce_AbstractFifo.cpp" #include "containers/juce_ArrayBase.cpp" +#include "containers/juce_ReferenceCountedArray.cpp" #include "containers/juce_NamedValueSet.cpp" #include "containers/juce_PropertySet.cpp" #include "containers/juce_SparseSet.cpp"