Owning wrapper over dynamically allocated storage with automatic destruction of the owned object while ownership remains in the wrapper.
Ownership boundary
get borrows access while ownership stays in the wrapper.
clear destroys the owned object and leaves the wrapper empty.
release returns one Ref and leaves the wrapper empty. Automatic destruction no longer applies to that object after release.
lower transfers the same owned allocation to one base-typed owner and empties the original owner.
valid? reports whether the wrapper currently owns one object.
Owner
Methods
valid? |
(-- valid) |
Tests whether the owner currently holds an object. |
acquire |
(element --) |
Replaces the current owned object with element. |
Postconditions
- The previous owned object is destroyed if one was present.
valid? becomes TRUE.
clear |
(--) |
Destroys the owned object and leaves the owner empty. |
Postconditions
get |
(-- ref) |
Returns Ref to the owned object. |
Preconditions
lower |
(-- owner) |
Returns an owner lowered to the base element type. |
Preconditions
Postconditions
- The original owner becomes empty.
- The result owner takes over the same storage.
init |
(object --) |
Allocates storage and initializes the owner from object. |
Preconditions
initDerived |
(object --) |
Allocates storage from object and stores the base view in the owner. |
Preconditions
release |
(-- ref) |
Releases the owned object as Ref and leaves the owner empty. |
Preconditions
Postconditions
Fields
OWNER: marker field used by generic ownership helpers.
destructor: destructor routine used when the owned object is destroyed.
elementType: Ref to the owned element schema.
memory: stored Ref to the owned object; empty owners store NIL.
owner (element -- owner)
Constructs Owner from one element and transfers ownership of newly allocated storage to the result.
Compile-time owner and release types
"Owner" use
"control" use
{} 0 {} [
holder: 5 owner;
holder.valid? printStack _:;
holder.get printStack _:;
@holder.release printStack _:;
holder.valid? printStack _:;
] "main" exportFunction
Expected Output During Compilation
Cond
Int32 Cref
Int32 Ref
FALSE
Runtime example
"Owner" use
"String" use
"control" use
{} Int32 {} [
holder: 5 owner;
("value=" @holder.get new LF) printList
released: @holder.release;
("released=" @released new LF) printList
0
] "main" exportFunction
Expected Output
value=5
released=5
Runtime example: clear resets validity
"Owner" use
"String" use
"control" use
{} Int32 {} [
holder: 5 owner;
("valid0=" holder.valid? LF) printList
@holder.clear
("valid1=" holder.valid? LF) printList
0
] "main" exportFunction
Expected Output
valid0=TRUE
valid1=FALSE
ownerDerived (element base destructor -- owner)
Constructs an owner with explicit base type and destructor handling.
getHeapUsedSize (owner -- size)
Returns total heap usage of the owned object as Natx.
- If the owner is empty, the result is
0nx.
- If the owned element participates in
getHeapUsedSize, that result is included recursively.
See also
- Mref: Minimal Ref type with custom semantics.
- Pool: Pooled storage with reusable numeric keys.