| AngelScript

weakref object

Path: /sdk/add_on/weakref/

The `weakref` type is a template type for holding weak references to objects, i.e. the references that will not keep the referred object alive.

The type is registered with `RegisterScriptWeakRef(asIScriptEngine*)`.

See Also
  Weak references

# Public C++ interface

class CScriptWeakRef
{
public:
// Constructors
CScriptWeakRef(asIObjectType *type);
CScriptWeakRef(const CScriptWeakRef &other);
CScriptWeakRef(void *ref, asIObjectType *type);
// Memory management
void AddRef() const;
void Release() const;
// Copy the stored value from another weakref object
CScriptWeakRef &operator=(const CScriptWeakRef &other);
// Compare equalness
bool operator==(const CScriptWeakRef &o) const;
bool operator!=(const CScriptWeakRef &o) const;
// Returns the object if it is still alive
void *Get() const;
// Returns the type of the reference held
asIObjectType *GetRefType() const;
};

# Example usage in script

In the scripts it can be used as follows:

      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 );