References are used to manage allocated objects and to reference-count these objects. For example, when all references to a given object are deleted, the object itself will be deleted. References are based on the maxon::BaseRef template.
The following reference types are available:
Using these reference templates, non-intrusive reference counting can be applied to any allocated object. A reference based alias implements a "Create()" function that allows the easy allocation of a new, reference counted instance.
For safe access to a reference from multiple threads see maxon::ThreadSafeRef.
maxon::Pointer is handling an ordinary C++ pointer.
A maxon::UniqueRef stores a pointer to a given object. It will delete the object when itself is destroyed. Only one maxon::UniqueRef can have the ownership of a given object at a time.
maxon::AutoMem is an alias for maxon::UniqueRef handling raw memory maxon::RawMem. See also Memory Allocation.
One or many maxon::StrongRef objects can reference a given object in memory. When all strong references to that object are destroyed, the object will be deleted.
A maxon::StrongRef can also manage raw memory:
A maxon::StrongCOWRef is the same as maxon::StrongRef but it supports copy-on-write techniques. As long as no reference is trying to modify the referenced object, all references point to the same object. When a reference wants to modify the object, the object is copied and the reference used for modification has ownership of that new object.
A maxon::WeakRef points to an object owned by a maxon::StrongRef. When there are no more strong references, the object is deleted and the maxon::WeakRef returns a null reference.
Instances of custom classes can be managed with maxon::BaseRef based reference types. If it is needed to modify the reference-counting behaviour one can implement the following functions in the custom class:
AddReference()
: Is called when a reference to the object is added.RemoveReference()
: Is called when a reference to the object is removed.CreateStrongReference()
: Is called when a strong reference is added.AddWeakReference()
: Is called when a weak reference to the object is added.InitialReference()
: Sets the initial reference to a newly allocated object.This is typically used for: