Browse Source

Fixed a bug adding ReferenceCountedArrays

tags/2021-05-28
Tom Poole 6 years ago
parent
commit
4bab8cc1d5
4 changed files with 85 additions and 7 deletions
  1. +3
    -1
      modules/juce_core/containers/juce_ArrayBase.h
  2. +76
    -0
      modules/juce_core/containers/juce_ReferenceCountedArray.cpp
  3. +5
    -6
      modules/juce_core/containers/juce_ReferenceCountedArray.h
  4. +1
    -0
      modules/juce_core/juce_core.cpp

+ 3
- 1
modules/juce_core/containers/juce_ArrayBase.h View File

@@ -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
- 0
modules/juce_core/containers/juce_ReferenceCountedArray.cpp View 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

+ 5
- 6
modules/juce_core/containers/juce_ReferenceCountedArray.h View File

@@ -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();
}
}


+ 1
- 0
modules/juce_core/juce_core.cpp View File

@@ -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…
Cancel
Save