Skip to content

Commit

Permalink
Allow using const reference counted object ptrs
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Nov 30, 2024
1 parent 4ef8b0b commit 12cb7e4
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 8 deletions.
18 changes: 10 additions & 8 deletions modules/juce_core/memory/juce_ReferenceCountedObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ class JUCE_API ReferenceCountedObject
This is done automatically by the smart pointer, but is public just
in case it's needed for nefarious purposes.
*/
void incReferenceCount() noexcept
void incReferenceCount() const noexcept
{
++refCount;
}

/** Decreases the object's reference count.
If the count gets to zero, the object will be deleted.
*/
void decReferenceCount() noexcept
void decReferenceCount() const noexcept
{
jassert (getReferenceCount() > 0);

Expand All @@ -108,7 +108,7 @@ class JUCE_API ReferenceCountedObject
If the count gets to zero, the object will not be deleted, but this method
will return true, allowing the caller to take care of deletion.
*/
bool decReferenceCountWithoutDeleting() noexcept
bool decReferenceCountWithoutDeleting() const noexcept
{
jassert (getReferenceCount() > 0);
return --refCount == 0;
Expand Down Expand Up @@ -151,7 +151,8 @@ class JUCE_API ReferenceCountedObject

private:
//==============================================================================
Atomic<int> refCount { 0 };
mutable Atomic<int> refCount { 0 };

friend struct ContainerDeletePolicy<ReferenceCountedObject>;
};

Expand All @@ -177,15 +178,15 @@ class JUCE_API SingleThreadedReferenceCountedObject
This is done automatically by the smart pointer, but is public just
in case it's needed for nefarious purposes.
*/
void incReferenceCount() noexcept
void incReferenceCount() const noexcept
{
++refCount;
}

/** Decreases the object's reference count.
If the count gets to zero, the object will be deleted.
*/
void decReferenceCount() noexcept
void decReferenceCount() const noexcept
{
jassert (getReferenceCount() > 0);

Expand All @@ -197,7 +198,7 @@ class JUCE_API SingleThreadedReferenceCountedObject
If the count gets to zero, the object will not be deleted, but this method
will return true, allowing the caller to take care of deletion.
*/
bool decReferenceCountWithoutDeleting() noexcept
bool decReferenceCountWithoutDeleting() const noexcept
{
jassert (getReferenceCount() > 0);
return --refCount == 0;
Expand Down Expand Up @@ -232,7 +233,8 @@ class JUCE_API SingleThreadedReferenceCountedObject

private:
//==============================================================================
int refCount = 0;
mutable int refCount = 0;

friend struct ContainerDeletePolicy<ReferenceCountedObject>;
};

Expand Down
226 changes: 226 additions & 0 deletions tests/juce_core/juce_ReferenceCountedObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/*
==============================================================================
This file is part of the YUP library.
Copyright (c) 2024 - [email protected]
YUP is an open source library subject to 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.
YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/

#include <gtest/gtest.h>

#include <juce_core/juce_core.h>

using namespace juce;

class TestClass : public ReferenceCountedObject
{
public:
using Ptr = ReferenceCountedObjectPtr<TestClass>;
using ConstPtr = ReferenceCountedObjectPtr<const TestClass>;

void doSomething() const {}
};

class SingleThreadedTestClass : public SingleThreadedReferenceCountedObject
{
public:
using Ptr = ReferenceCountedObjectPtr<SingleThreadedTestClass>;
using ConstPtr = ReferenceCountedObjectPtr<const SingleThreadedTestClass>;

void doSomething() const {}
};

TEST (ReferenceCountedObjectTests, IncDecReferenceCount)
{
TestClass::Ptr obj = new TestClass();
EXPECT_EQ (obj->getReferenceCount(), 1);

obj->incReferenceCount();
EXPECT_EQ (obj->getReferenceCount(), 2);

obj->decReferenceCount();
EXPECT_EQ (obj->getReferenceCount(), 1);

obj = nullptr; // this should decrement the count and delete the object
}

TEST (ReferenceCountedObjectTests, IncDecConstReferenceCount)
{
TestClass::ConstPtr obj = new TestClass();
EXPECT_EQ (obj->getReferenceCount(), 1);

obj->incReferenceCount();
EXPECT_EQ (obj->getReferenceCount(), 2);

obj->decReferenceCount();
EXPECT_EQ (obj->getReferenceCount(), 1);

obj = nullptr; // this should decrement the count and delete the object
}

TEST (ReferenceCountedObjectTests, DecReferenceCountWithoutDeleting)
{
TestClass::Ptr obj = new TestClass();
EXPECT_EQ (obj->getReferenceCount(), 1);

EXPECT_TRUE (obj->decReferenceCountWithoutDeleting());
EXPECT_EQ (obj->getReferenceCount(), 0);

delete obj;
}

TEST (ReferenceCountedObjectTests, DecConstReferenceCountWithoutDeleting)
{
TestClass::ConstPtr obj = new TestClass();
EXPECT_EQ (obj->getReferenceCount(), 1);

EXPECT_TRUE (obj->decReferenceCountWithoutDeleting());
EXPECT_EQ (obj->getReferenceCount(), 0);

delete obj;
}

// SingleThreadedReferenceCountedObject tests
TEST (SingleThreadedReferenceCountedObjectTests, IncDecReferenceCount)
{
SingleThreadedTestClass::Ptr obj = new SingleThreadedTestClass();
EXPECT_EQ (obj->getReferenceCount(), 1);

obj->incReferenceCount();
EXPECT_EQ (obj->getReferenceCount(), 2);

obj->decReferenceCount();
EXPECT_EQ (obj->getReferenceCount(), 1);

obj = nullptr; // this should decrement the count and delete the object
}

TEST (SingleThreadedReferenceCountedObjectTests, IncDecConstReferenceCount)
{
SingleThreadedTestClass::ConstPtr obj = new SingleThreadedTestClass();
EXPECT_EQ (obj->getReferenceCount(), 1);

obj->incReferenceCount();
EXPECT_EQ (obj->getReferenceCount(), 2);

obj->decReferenceCount();
EXPECT_EQ (obj->getReferenceCount(), 1);

obj = nullptr; // this should decrement the count and delete the object
}

TEST (SingleThreadedReferenceCountedObjectTests, DecReferenceCountWithoutDeleting)
{
SingleThreadedTestClass::Ptr obj = new SingleThreadedTestClass();
EXPECT_EQ (obj->getReferenceCount(), 1);

EXPECT_TRUE (obj->decReferenceCountWithoutDeleting());
EXPECT_EQ (obj->getReferenceCount(), 0);

delete obj;
}

TEST (SingleThreadedReferenceCountedObjectTests, DecConstReferenceCountWithoutDeleting)
{
SingleThreadedTestClass::ConstPtr obj = new SingleThreadedTestClass();
EXPECT_EQ (obj->getReferenceCount(), 1);

EXPECT_TRUE (obj->decReferenceCountWithoutDeleting());
EXPECT_EQ (obj->getReferenceCount(), 0);

delete obj;
}

// ReferenceCountedObjectPtr tests
TEST (ReferenceCountedObjectPtrTests, PointerAssignment)
{
TestClass::Ptr obj1 = new TestClass();
TestClass::Ptr obj2 = obj1;

EXPECT_EQ (obj1->getReferenceCount(), 2);
EXPECT_EQ (obj2->getReferenceCount(), 2);

obj1 = nullptr;
EXPECT_EQ (obj2->getReferenceCount(), 1);
obj2 = nullptr; // this should delete the object
}

TEST (ReferenceCountedObjectPtrTests, PointerComparison)
{
TestClass::Ptr obj1 = new TestClass();
TestClass::Ptr obj2 = obj1;

EXPECT_EQ (obj1, obj2);
EXPECT_NE (obj1, nullptr);

obj1 = nullptr;
EXPECT_EQ (obj1, nullptr);
EXPECT_NE (obj2, nullptr);
}

TEST (ReferenceCountedObjectPtrTests, PointerDereference)
{
TestClass::Ptr obj = new TestClass();
EXPECT_NO_THROW (obj->doSomething());
EXPECT_NO_THROW (*obj);
}

TEST (ReferenceCountedObjectPtrTests, Reset)
{
TestClass::Ptr obj = new TestClass();
EXPECT_EQ (obj->getReferenceCount(), 1);

obj.reset();
EXPECT_EQ (obj, nullptr);
}

// SingleThreadedReferenceCountedObjectPtr tests
TEST (SingleThreadedReferenceCountedObjectPtrTests, PointerAssignment)
{
SingleThreadedTestClass::Ptr obj1 = new SingleThreadedTestClass();
SingleThreadedTestClass::Ptr obj2 = obj1;

EXPECT_EQ (obj1->getReferenceCount(), 2);
EXPECT_EQ (obj2->getReferenceCount(), 2);

obj1 = nullptr;
EXPECT_EQ (obj2->getReferenceCount(), 1);
obj2 = nullptr; // this should delete the object
}

TEST (SingleThreadedReferenceCountedObjectPtrTests, PointerComparison)
{
SingleThreadedTestClass::Ptr obj1 = new SingleThreadedTestClass();
SingleThreadedTestClass::Ptr obj2 = obj1;

EXPECT_EQ (obj1, obj2);
EXPECT_NE (obj1, nullptr);

obj1 = nullptr;
EXPECT_EQ (obj1, nullptr);
EXPECT_NE (obj2, nullptr);
}

TEST (SingleThreadedReferenceCountedObjectPtrTests, PointerDereference)
{
SingleThreadedTestClass::Ptr obj = new SingleThreadedTestClass();
EXPECT_NO_THROW (obj->doSomething());
EXPECT_NO_THROW (*obj);
}

TEST (SingleThreadedReferenceCountedObjectPtrTests, Reset)
{
SingleThreadedTestClass::Ptr obj = new SingleThreadedTestClass();
EXPECT_EQ (obj->getReferenceCount(), 1);

obj.reset();
EXPECT_EQ (obj, nullptr);
}

0 comments on commit 12cb7e4

Please sign in to comment.