Referenced objects' lifetime is controlled by the mean of reference counting.
A Referenced object destroys itself, when its reference count becomes zero, provided that the object is not floating. If the reference count becomes zero, but the object is floating, then it will not be deleted.
After construction, the reference count of an object is set to zero, and the object is marked as floating.
To make an object non-floating, one should grab it by calling the grab() method. Grabbing is a one-time action which marks an object as non-floating and increments its reference count by 1.
If actually controlled by reference counting, an object must be allocated on the heap using the new operator. One is not obliged to allocate a Referenced object on the stack, provided that the unref() method is not called for that object.
See also documentation for MyCpp::Ref class, which provides a convinient way of tracking references to Referenced objects.
All operations with Referenced objects (i.e. all the methods) are atomic.
| virtual void |
This is an important protected method,
which is called for an object just before it is
about to be destroyed (i.e. when its reference count
becomes zero, and the object is not floating). | |||
| void |
Grabs an object. | |||
| void |
Increments the reference count of the object. | |||
| void |
Decrements the reference count of the object. |
This is an important protected method, which is called for an object just before it is about to be destroyed (i.e. when its reference count becomes zero, and the object is not floating).
During processing of the dispose method, object's reference count can be incremented, so that the destruction will be avoided. In this case, the dispose method will be called again, once the reference count reaches zero again. This will be repeated until the object gets actually destroyed.
This method can be called multiple times, but is guaranteed to be called only once at a time (i.e. all calls are synchronous).
virtual void dispose ()
Grabs an object.
Grabbing means marking the object as non-floating, and incrementing its reference count by 1.
This operation is atomic.
void grab ()
Increments the reference count of the object.
This operation is atomic.
void ref ()
Decrements the reference count of the object.
This operation is atomic.
void unref ()