Loading...
Searching...
No Matches
juce_WeakReference.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
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
{
//==============================================================================
template <class ObjectType, class ReferenceCountingType = ReferenceCountedObject>
class WeakReference
{
public:
inline WeakReference() = default;
WeakReference (ObjectType* object) : holder (getRef (object)) {}
WeakReference (const WeakReference& other) noexcept : holder (other.holder) {}
WeakReference (WeakReference&& other) noexcept : holder (std::move (other.holder)) {}
WeakReference& operator= (const WeakReference& other) { holder = other.holder; return *this; }
WeakReference& operator= (ObjectType* newObject) { holder = getRef (newObject); return *this; }
WeakReference& operator= (WeakReference&& other) noexcept { holder = std::move (other.holder); return *this; }
ObjectType* get() const noexcept { return holder != nullptr ? holder->get() : nullptr; }
operator ObjectType*() const noexcept { return get(); }
ObjectType* operator->() const noexcept { return get(); }
bool wasObjectDeleted() const noexcept { return holder != nullptr && holder->get() == nullptr; }
//==============================================================================
class SharedPointer : public ReferenceCountingType
{
public:
explicit SharedPointer (ObjectType* obj) noexcept : owner (obj) {}
inline ObjectType* get() const noexcept { return owner; }
void clearPointer() noexcept { owner = nullptr; }
private:
ObjectType* owner;
JUCE_DECLARE_NON_COPYABLE (SharedPointer)
};
using SharedRef = ReferenceCountedObjectPtr<SharedPointer>;
//==============================================================================
class Master
{
public:
Master() = default;
~Master() noexcept
{
// You must remember to call clear() in your source object's destructor! See the notes
// for the WeakReference class for an example of how to do this.
jassert (sharedPointer == nullptr || sharedPointer->get() == nullptr);
}
SharedRef getSharedPointer (ObjectType* object)
{
if (sharedPointer == nullptr)
{
sharedPointer = *new SharedPointer (object);
}
else
{
// You're trying to create a weak reference to an object that has already been deleted!!
jassert (sharedPointer->get() != nullptr);
}
return sharedPointer;
}
void clear() noexcept
{
if (sharedPointer != nullptr)
sharedPointer->clearPointer();
}
int getNumActiveWeakReferences() const noexcept
{
return sharedPointer == nullptr ? 0 : (sharedPointer->getReferenceCount() - 1);
}
private:
SharedRef sharedPointer;
JUCE_DECLARE_NON_COPYABLE (Master)
};
private:
SharedRef holder;
static SharedRef getRef (ObjectType* o)
{
if (o != nullptr)
return o->masterReference.getSharedPointer (o);
return {};
}
};
//==============================================================================
#define JUCE_DECLARE_WEAK_REFERENCEABLE(Class) \
struct WeakRefMaster : public juce::WeakReference<Class>::Master { ~WeakRefMaster() { this->clear(); } }; \
WeakRefMaster masterReference; \
friend class juce::WeakReference<Class>; \
} // namespace juce
juce::ReferenceCountedObjectPtr
Definition juce_ReferenceCountedObject.h:247
juce::WeakReference::Master
Definition juce_WeakReference.h:147
juce::WeakReference::Master::getNumActiveWeakReferences
int getNumActiveWeakReferences() const noexcept
Definition juce_WeakReference.h:187
juce::WeakReference::Master::clear
void clear() noexcept
Definition juce_WeakReference.h:180
juce::WeakReference::Master::getSharedPointer
SharedRef getSharedPointer(ObjectType *object)
Definition juce_WeakReference.h:161
juce::WeakReference::SharedPointer
Definition juce_WeakReference.h:125
juce::WeakReference::operator->
ObjectType * operator->() const noexcept
Definition juce_WeakReference.h:108
juce::WeakReference::WeakReference
WeakReference()=default
juce::WeakReference::get
ObjectType * get() const noexcept
Definition juce_WeakReference.h:102
juce::WeakReference::WeakReference
WeakReference(WeakReference &&other) noexcept
Definition juce_WeakReference.h:90
juce::WeakReference::operator=
WeakReference & operator=(const WeakReference &other)
Definition juce_WeakReference.h:93
juce::WeakReference::WeakReference
WeakReference(const WeakReference &other) noexcept
Definition juce_WeakReference.h:87
juce::WeakReference::wasObjectDeleted
bool wasObjectDeleted() const noexcept
Definition juce_WeakReference.h:117
juce::WeakReference::WeakReference
WeakReference(ObjectType *object)
Definition juce_WeakReference.h:84