Juce in the WeakReference design is more ingenious, clever is to use the delete after you can inform the WeakReference, the principle is also very single, in fact, is in the object added a sub-object masterreference, The object is actively called Masterreference.clear () at the time of the destructor, so that the object that is notified of the weak pointer has been destroyed and can be set to null. feel Juce finally call a clear or feel a bit stiff, the outer layer is best or again nested a layer, when the destruction of the automatic call clear can be, the object declaration is also written as a macro, so it is more concise.
The way to use it is simple:
/==============================================================================/** This class acts as a pointer which Would 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. @code class MyObject {public:myobject () {//If you ' re planning on using your Weakrefe Rences in a multi-threaded situation, you could choose//To create a weakreference to the object here in the con Structor, which would pre-initialise the//embedded object, avoiding an (extremely unlikely) race condition tha T could occur if multiple//threads overlap while creating the first weakreference to it. } ~myobject () {//This would zero all the refErences-you need to the 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 isn't public, you should make your class A friend of Weakreference<myobject> so 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 @endcode @see weakreference::master*/
#ifndef juce_weakreference_h_included#define juce_weakreference_h_included/template <class ObjectType, class Referencecountingtype = Referencecountedobject>class weakreference{public:/** Creates a null SafePointer. */INL Ine WeakReference () noexcept {}/** creates a weakreference that points at the given object. */WeakReference (objecttype* const object): Holder (GetRef (object)) {}/** creates a copy of another Weakreferen Ce. */weakreference (const weakreference& Other) Noexcept:holder (Other.holder) {}/** Copies another poi Nter to this one. */weakreference& operator= (const weakreference& Other) {holder = Other.holder; return *this;} /** Copies Another pointer to this one. */weakreference& operator= (objecttype* const newObject) {holder = GetRef (newObject); return *this;} #if juce_compiler_supports_move_semantics WeakReference (weakreference&& other) Noexcept:holder (StatIc_cast<sharedref&&> (Other.holder)) {} weakreference& operator= (weakreference&& Other) noexcept {holder = static_cast<sharedref&&> (other.holder); return *this;} #endif/** Returns The object, this is pointer refers to, or null if the object no longer exists. */objecttype* get () const NOEXCEPT {return holder! = nullptr? Holder->get (): nullptr; }/** Returns The object, this pointer refers to, or null if the object no longer exists. */Operator objecttype* () const noexcept {return get ();} /** Returns The object, this is pointer refers to, or null if the object no longer exists. */objecttype* operator-> () noexcept {return get ();} /** Returns The object, this is pointer refers to, or null if the object no longer exists. */const objecttype* operator-> () const noexcept {return get ();} /** this returNS True if this reference have been pointing at a object, but the object has since been deleted. If This reference is only ever pointing at a null pointer, this would return false. Using operator= () to make this refer to a different object would reset this flag to match the status of the R Eference from which you ' re copying. */BOOL Wasobjectdeleted () const NOEXCEPT {return holder! = nullptr && holder->get () = = nullptr; } bool operator== (objecttype* const object) const NOEXCEPT {return get () = = object;} BOOL Operator!= (objecttype* const object) const NOEXCEPT {return get ()! = object;} ==============================================================================/** This class was used internally by The WeakReference Class-don ' t use it directly in your code! @see WeakReference, */class Sharedpointer:public Referencecountingtype {public:explicit SHAREDPO InteR (objecttype* const obj) noexcept:owner (obj) {} inline objecttype* get () const Noexcept {return owner;} void Clearpointer () noexcept {owner = nullptr;} private:objecttype* volatile owner; Juce_declare_non_copyable (Sharedpointer)}; typedef referencecountedobjectptr<sharedpointer> SHAREDREF; ==============================================================================/** This class is embedded Insi De an object to which want to attach weakreference pointers. See the WeakReference class notes for a example of how to use this class. @see WeakReference */class Master {Public:master () noexcept {} ~master () noexcept { You must remember to call clear () in your source object ' s destructor! See the Notes//for the WeakReference class for a example of how to does this. Jassert (Sharedpointer = = Nullptr | | sharedpointer-≫get () = = nullptr); }/** The first call to this method would create an internal object which is a shared by all weak references to the object. */sharedpointer* Getsharedpointer (objecttype* const object) {if (Sharedpointer = = nullptr) {//Wrap with smart pointer first sharedpointer = new Sharedpointer (object); } else {//You ' re trying-to-create a weak reference to an object-have already be En deleted!! Jassert (Sharedpointer->get ()! = nullptr); } return sharedpointer; }/** The object that is owns this master pointer should call this before it gets destroyed, to zero all th E references to this object, May is out there. See the WeakReference class notes for a example of how to does this. */void Clear () noexcept {if (sharedpointer! = nullptr) sharedPointer->clearpointer (); } private:sharedref Sharedpointer; Juce_declare_non_copyable (Master)};p rivate:sharedref Holder;
//See the Masterreference in object is obtained, so the object to be used must include this member, Getsharedpointer returns a Sharedpointer smart pointer, And sharedpointer the pointer of the real object, because Sharedpointer is wrapped by a smart pointer, so the weak reference object is deleted, this
Sharedpointer still exists, but the pointer object inside it is set to null because clear, and here again call get nature to know whether the object is deleted
Static inline sharedpointer* getRef (objecttype* const o) {return (o! = nullptr)? o->masterreference.getsharedpointer (o): nullptr; } }; #endif//juce_weakreference_h_included
Analysis of WeakReference in Juce