This class acts as a pointer which will automatically become null if the object to which it points is deleted. More...
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 | |
| typedef ReferenceCountedObjectPtr < SharedPointer > | SharedRef |
Public Member Functions | |
| WeakReference () noexcept | |
| Creates a null SafePointer. | |
| WeakReference (ObjectType *const object) | |
| Creates a WeakReference that points at the given object. | |
| WeakReference (const WeakReference &other) noexcept | |
| Creates a copy of another WeakReference. | |
| WeakReference & | operator= (const WeakReference &other) |
| Copies another pointer to this one. | |
| WeakReference & | operator= (ObjectType *const newObject) |
| Copies another pointer to this one. | |
| 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-> () noexcept |
| Returns the object that this pointer refers to, or null if the object no longer exists. | |
| const 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. | |
| bool | operator== (ObjectType *const object) const noexcept |
| bool | operator!= (ObjectType *const object) const noexcept |
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.
E.g.
class MyObject { public: MyObject() { // If you're planning on using your WeakReferences in a multi-threaded situation, you may choose // to create a WeakReference to the object here in the constructor, which will pre-initialise the // embedded object, avoiding an (extremely unlikely) race condition that could occur if multiple // threads overlap while creating the first WeakReference to it. } ~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.. MyObject* n = new MyObject(); WeakReference<MyObject> myObjectRef = n; MyObject* pointer1 = myObjectRef; // returns a valid pointer to 'n' delete n; MyObject* pointer2 = myObjectRef; // returns a null pointer
| typedef ReferenceCountedObjectPtr<SharedPointer> WeakReference< ObjectType, ReferenceCountingType >::SharedRef |
| WeakReference< ObjectType, ReferenceCountingType >::WeakReference | ( | ) |
Creates a null SafePointer.
| WeakReference< ObjectType, ReferenceCountingType >::WeakReference | ( | ObjectType *const | object | ) |
Creates a WeakReference that points at the given object.
| WeakReference< ObjectType, ReferenceCountingType >::WeakReference | ( | const WeakReference< ObjectType, ReferenceCountingType > & | other | ) |
Creates a copy of another WeakReference.
| WeakReference& WeakReference< ObjectType, ReferenceCountingType >::operator= | ( | const WeakReference< ObjectType, ReferenceCountingType > & | other | ) |
Copies another pointer to this one.
| WeakReference& WeakReference< ObjectType, ReferenceCountingType >::operator= | ( | ObjectType *const | newObject | ) |
Copies another pointer to this one.
| ObjectType* WeakReference< ObjectType, ReferenceCountingType >::get | ( | ) | const |
Returns the object that this pointer refers to, or null if the object no longer exists.
| WeakReference< ObjectType, ReferenceCountingType >::operator ObjectType * | ( | ) | const |
Returns the object that this pointer refers to, or null if the object no longer exists.
| ObjectType* WeakReference< ObjectType, ReferenceCountingType >::operator-> | ( | ) |
Returns the object that this pointer refers to, or null if the object no longer exists.
| const ObjectType* WeakReference< ObjectType, ReferenceCountingType >::operator-> | ( | ) | const |
Returns the object that this pointer refers to, or null if the object no longer exists.
| bool WeakReference< ObjectType, ReferenceCountingType >::wasObjectDeleted | ( | ) | const |
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.
| bool WeakReference< ObjectType, ReferenceCountingType >::operator== | ( | ObjectType *const | object | ) | const |
| bool WeakReference< ObjectType, ReferenceCountingType >::operator!= | ( | ObjectType *const | object | ) | const |