This class acts as a pointer which will automatically become null if the object to which it points is deleted.
More...
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()
{
masterReference.clear();
}
private:
};
auto* n = new MyObject();
auto pointer1 = myObjectRef.
get();
delete n;
auto pointer2 = myObjectRef.
get();
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
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().