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()
{
masterReference.clear();
}
private:
WeakReference<MyObject>::Master masterReference;
friend class WeakReference<MyObject>;
};
auto*
n =
new MyObject();
WeakReference<MyObject> myObjectRef =
n;
auto pointer1 = myObjectRef.get();
auto pointer2 = myObjectRef.get();
#define JUCE_DECLARE_WEAK_REFERENCEABLE(Class)
Definition juce_WeakReference.h:234
int n
Definition crypt.c:458
- See also
- WeakReference::Master
@tags{Core}
template<class ObjectType, class ReferenceCountingType = ReferenceCountedObject>
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.