mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-26 02:14:22 +00:00
Fixed a bug adding ReferenceCountedArrays
This commit is contained in:
parent
0535d1b222
commit
4bab8cc1d5
4 changed files with 85 additions and 7 deletions
|
|
@ -230,7 +230,7 @@ public:
|
|||
}
|
||||
|
||||
template <class OtherArrayType>
|
||||
typename std::enable_if<! std::is_pointer<OtherArrayType>::value, void>::type
|
||||
typename std::enable_if<! std::is_pointer<OtherArrayType>::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;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
|
|
|
|||
76
modules/juce_core/containers/juce_ReferenceCountedArray.cpp
Normal file
76
modules/juce_core/containers/juce_ReferenceCountedArray.cpp
Normal file
|
|
@ -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<TestDerivedObj*> (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<TestDerivedObj> derivedObjects;
|
||||
|
||||
ReferenceCountedArray<TestBaseObj> getObjects() const
|
||||
{
|
||||
ReferenceCountedArray<TestBaseObj> objects;
|
||||
objects.addArray (derivedObjects);
|
||||
return objects;
|
||||
}
|
||||
};
|
||||
|
||||
static ReferenceCountedArrayTests referenceCountedArrayTests;
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
||||
|
|
@ -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<OtherObjectClass, OtherCriticalSection>& other) noexcept
|
||||
{
|
||||
const typename ReferenceCountedArray<OtherObjectClass, OtherCriticalSection>::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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue