weakref
is only available in the scripts if the application registers the support for it.An object handle will keep the object it refers to alive as long as the handle itself exists. A weakref
object can be used in place of the handle where the reference to the object is needed but the object shouldn't be kept alive.
class MyClass {} MyClass @obj1 = MyClass(); // Keep a weakref to the object weakref<MyClass> r1(obj1); // Keep a weakref to a readonly object const_weakref<MyClass> r2(obj1); // As long as there is a strong reference to the object, // the weakref will be able to return a handle to the object MyClass @obj2 = r1.get(); assert( obj2 !is null ); // After all strong references are removed the // weakref will only return null @obj1 = null; @obj2 = null; const MyClass @obj3 = r2.get(); assert( obj3 is null );
weakref<T>()
weakref<T>(T@) explicit
const_weakref<T>()
const_weakref<T>(const T@) explicit
@= handle assignment
The handle assignment operator is used to set the object that the referred to by the ref type.
= value assignment
The value assignment operator is used when one weakref object is copied to another.
is, !is identity operator
The identity operators are used to compare the address of the object referred to by the ref type.
cast<type> implicit cast operator
The implicit cast operator is used to cast the weak ref type to strong reference of the type. If the object referred to by the weakref is already dead this operator will return null.
T@ get() const
This does the exact same thing as the implicit cast operator. It is just a more explicit way of writing it.