JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | List of all members
juce::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.
 
ObjectTypeget () 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.
 
ObjectTypeoperator-> () 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 juce::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.
WeakReference<MyObject>::Master masterReference;
friend class WeakReference<MyObject>;
};
// Here's an example of using a pointer..
auto* n = new MyObject();
auto pointer1 = myObjectRef.get(); // returns a valid pointer to 'n'
delete n;
auto pointer2 = myObjectRef.get(); // now returns nullptr
#define JUCE_DECLARE_WEAK_REFERENCEABLE(Class)
Macro to easily allow a class to be made weak-referenceable.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
See also
WeakReference::Master

@tags{Core}

Definition at line 77 of file juce_WeakReference.h.

Member Typedef Documentation

◆ SharedRef

Definition at line 138 of file juce_WeakReference.h.

Constructor & Destructor Documentation

◆ WeakReference() [1/3]

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

Creates a WeakReference that points at the given object.

Definition at line 84 of file juce_WeakReference.h.

◆ WeakReference() [2/3]

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

Creates a copy of another WeakReference.

Definition at line 87 of file juce_WeakReference.h.

◆ WeakReference() [3/3]

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

Move constructor.

Definition at line 90 of file juce_WeakReference.h.

Member Function Documentation

◆ get()

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

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

Definition at line 102 of file juce_WeakReference.h.

◆ operator ObjectType *()

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

Definition at line 105 of file juce_WeakReference.h.

◆ operator->()

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

Definition at line 108 of file juce_WeakReference.h.

◆ operator=() [1/3]

Copies another pointer to this one.

Definition at line 93 of file juce_WeakReference.h.

◆ operator=() [2/3]

Copies another pointer to this one.

Definition at line 96 of file juce_WeakReference.h.

◆ operator=() [3/3]

Move assignment operator.

Definition at line 99 of file juce_WeakReference.h.

◆ wasObjectDeleted()

template<class ObjectType , class ReferenceCountingType = ReferenceCountedObject>
bool juce::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.

Definition at line 117 of file juce_WeakReference.h.


The documentation for this class was generated from the following file: