About
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.
Pointer
maxon::Pointer is handling an ordinary C++ pointer.
UniqueRef
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.
- Note
- A maxon::UniqueRef object can be moved (using std::move()) but not copied.
-
The const-ness of the maxon::UniqueRef object can be used to control access to the referenced object.
{
ref->_data = 123;
// when the scope is left, UniqueRef and the allocated element are destroyed
}
#define MAXON_SCOPE
Definition: apibase.h:2886
static auto Create(ARGS &&... args)
Definition: apibase.h:2818
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:170
#define iferr_return
Definition: resultbase.h:1524
{
}
std::conditional< Bool(HANDLER::KIND &VALUEKIND::COW_MASK), typename std::remove_const< T >::type, T >::type * Disconnect()
Definition: baseref.h:505
Definition: pointerarray.h:43
ResultPtr< T > AppendPtr(T *x)
Definition: pointerarray.h:224
return OK
Definition: apibase.h:2735
#define NewObj(T,...)
Definition: newobj.h:108
#define iferr_scope
Definition: resultbase.h:1389
maxon::AutoMem is an alias for maxon::UniqueRef handling raw memory maxon::RawMem. See also Memory Allocation.
{
}
Py_ssize_t i
Definition: abstract.h:645
Py_ssize_t count
Definition: abstract.h:640
#define NewMemClear(T, cnt)
Definition: defaultallocator.h:204
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:202
StrongRef, StrongCOWRef and WeakRef
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.
{
}
PyObject * obj
Definition: complexobject.h:60
{
original->_data = 123;
{
const SimpleRef copy = original;
original->_data = 456;
}
}
A maxon::StrongRef can also manage raw memory:
{
}
#define NewMem(T, cnt)
Definition: defaultallocator.h:195
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.
{
data->_data = 100;
const SimpleCOWRef original(data);
{
SimpleCOWRef copy = original;
data->_data = 200;
copyData._data = 300;
}
}
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.
{
const SimpleRef strongRef(
element);
weakRef = strongRef;
if (weakRef)
{
const SimpleRef data = weakRef;
}
}
if (!weakRef)
Definition: weakref.h:164
PyObject * element
Definition: unicodeobject.h:1016
Classes
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:
- debugging reference handling
- reference counting with alien memory management
- synchronization of a reference with a background object
class ReferencedElement
{
public:
{
_data = 0;
}
~ReferencedElement()
{
_data = 0;
}
void AddReference() const
{
}
void RemoveReference() const
{
{
}
}
};
Bool Inc()
Definition: atomictypes.h:2062
static StrongReferenceCounter & GetReferenceCounter(const void *object)
Returns the reference counter for classes which do not implement custom AddReference/RemoveReference ...
Definition: system.h:232
#define MAXON_IMPLICIT
Definition: apibase.h:186
MAXON_ATTRIBUTE_FORCE_INLINE void DeleteConstPtrObj(T *o)
Definition: newobj.h:190
Further Reading