Loading...
Searching...
No Matches
WeakReference< ObjectType, ReferenceCountingType > Class Template Reference

This class acts as a pointer which will automatically become null if the object to which it points is deleted. More...

#include <juce_WeakReference.h>

Classes

class  Master
 This class is embedded inside an object to which you want to attach WeakReference pointers. More...
 
class  SharedPointer
 This class is used internally by the WeakReference class - don't use it directly in your code! More...
 

Public Types

using SharedRef = ReferenceCountedObjectPtr<SharedPointer>
 

Public Member Functions

 WeakReference ()=default
 Creates a null WeakReference.
 
 WeakReference (ObjectType *object)
 Creates a WeakReference that points at the given object.
 
 WeakReference (const WeakReference &other) noexcept
 Creates a copy of another WeakReference.
 
 WeakReference (WeakReference &&other) noexcept
 Move constructor.
 
WeakReferenceoperator= (const WeakReference &other)
 Copies another pointer to this one.
 
WeakReferenceoperator= (ObjectType *newObject)
 Copies another pointer to this one.
 
WeakReferenceoperator= (WeakReference &&other) noexcept
 Move assignment operator.
 
ObjectType * get () const noexcept
 Returns the object that this pointer refers to, or null if the object no longer exists.
 
 operator ObjectType * () const noexcept
 Returns the object that this pointer refers to, or null if the object no longer exists.
 
ObjectType * operator-> () const noexcept
 Returns the object that this pointer refers to, or null if the object no longer exists.
 
bool wasObjectDeleted () const noexcept
 This returns true if this reference has been pointing at an object, but that object has since been deleted.
 

Detailed Description

template<class ObjectType, class ReferenceCountingType = ReferenceCountedObject>
class WeakReference< ObjectType, ReferenceCountingType >

This class acts as a pointer which will automatically become null if the object to which it points is deleted.

To accomplish this, the source object needs to cooperate by performing a couple of simple tasks. It must embed a WeakReference::Master object, which stores a shared pointer object, and must clear this master pointer in its destructor.

Note that WeakReference is not designed to be thread-safe, so if you're accessing it from different threads, you'll need to do your own locking around all uses of the pointer and the object it refers to.

E.g.

class MyObject
{
public:
MyObject() {}
~MyObject()
{
// This will zero all the references - you need to call this in your destructor.
masterReference.clear();
}
private:
// You need to embed a variable of this type, with the name "masterReference" inside your object. If the
// variable is not public, you should make your class a friend of WeakReference<MyObject> so that the
// WeakReference class can access it.
friend class WeakReference<MyObject>;
};
OR: just use the handy JUCE_DECLARE_WEAK_REFERENCEABLE macro to do all this for you.
// Here's an example of using a pointer..
auto* n = new MyObject();
WeakReference<MyObject> myObjectRef = n;
auto pointer1 = myObjectRef.get(); // returns a valid pointer to 'n'
delete n;
auto pointer2 = myObjectRef.get(); // now returns nullptr
This class is embedded inside an object to which you want to attach WeakReference pointers.
Definition juce_WeakReference.h:162
This class acts as a pointer which will automatically become null if the object to which it points is...
Definition juce_WeakReference.h:93
ObjectType * get() const noexcept
Returns the object that this pointer refers to, or null if the object no longer exists.
Definition juce_WeakReference.h:117
#define JUCE_DECLARE_WEAK_REFERENCEABLE(Class)
Macro to easily allow a class to be made weak-referenceable.
Definition juce_WeakReference.h:246
See also
WeakReference::Master

Member Typedef Documentation

◆ SharedRef

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
using WeakReference< ObjectType, ReferenceCountingType >::SharedRef = ReferenceCountedObjectPtr<SharedPointer>

Constructor & Destructor Documentation

◆ WeakReference() [1/4]

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
WeakReference< ObjectType, ReferenceCountingType >::WeakReference ( )
default

Creates a null WeakReference.

◆ WeakReference() [2/4]

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
WeakReference< ObjectType, ReferenceCountingType >::WeakReference ( ObjectType * object)

Creates a WeakReference that points at the given object.

◆ WeakReference() [3/4]

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
WeakReference< ObjectType, ReferenceCountingType >::WeakReference ( const WeakReference< ObjectType, ReferenceCountingType > & other)
noexcept

Creates a copy of another WeakReference.

◆ WeakReference() [4/4]

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
WeakReference< ObjectType, ReferenceCountingType >::WeakReference ( WeakReference< ObjectType, ReferenceCountingType > && other)
noexcept

Move constructor.

Member Function Documentation

◆ operator=() [1/3]

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
WeakReference & WeakReference< ObjectType, ReferenceCountingType >::operator= ( const WeakReference< ObjectType, ReferenceCountingType > & other)

Copies another pointer to this one.

◆ operator=() [2/3]

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
WeakReference & WeakReference< ObjectType, ReferenceCountingType >::operator= ( ObjectType * newObject)

Copies another pointer to this one.

◆ operator=() [3/3]

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
WeakReference & WeakReference< ObjectType, ReferenceCountingType >::operator= ( WeakReference< ObjectType, ReferenceCountingType > && other)
noexcept

Move assignment operator.

◆ get()

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
ObjectType * WeakReference< ObjectType, ReferenceCountingType >::get ( ) const
noexcept

Returns the object that this pointer refers to, or null if the object no longer exists.

References ReferenceCountedObjectPtr< ObjectType >::get().

Referenced by WeakReference< Component >::operator Component *(), and WeakReference< ObjectType, ReferenceCountingType >::operator->().

◆ operator ObjectType *()

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
WeakReference< ObjectType, ReferenceCountingType >::operator ObjectType * ( ) const
noexcept

Returns the object that this pointer refers to, or null if the object no longer exists.

◆ operator->()

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
ObjectType * WeakReference< ObjectType, ReferenceCountingType >::operator-> ( ) const
noexcept

Returns the object that this pointer refers to, or null if the object no longer exists.

References WeakReference< ObjectType, ReferenceCountingType >::get().

◆ wasObjectDeleted()

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
bool WeakReference< ObjectType, ReferenceCountingType >::wasObjectDeleted ( ) const
noexcept

This returns true if this reference has been pointing at an object, but that object has since been deleted.

If this reference was only ever pointing at a null pointer, this will return false. Using operator=() to make this refer to a different object will reset this flag to match the status of the reference from which you're copying.

References ReferenceCountedObjectPtr< ObjectType >::get().


The documentation for this class was generated from the following file:
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram