Usage
var obj0 = $F(1.0), obj1 = $F(2.0);
var r = $(Box, obj0);
show(r);
show(deref(r)); /* 1.0 */
ref(r, obj1);
show(deref(r)); /* 2.0 */
assign(r, obj0);
show(deref(r)); /* 1.0 */
Lifetimes
var quote = $S("Life is long");
with (r in $B(new(String, quote))) {
println("This reference is: %$", r);
println("This string is alive: '%s'", deref(r));
}
print("Now it has been cleared up!\n");
Collection
/* Multiple Types in one Collection */
var x = new(Array, Box,
new(String, $S("Hello")),
new(String, $S("There")),
new(Int, $I(10)));
print(deref(get(x, $I(0)))); /* Hello */
del(x); /* Contents of `x` deleted with it */
Unique Pointer
The Box
type is another wrapper around a C pointer with one additional behaviour as compared to Ref
. When a Box
object is deleted it will also call del
on the object it points to. The means a Box
is considered a pointer type that owns the object it points to, and so is responsible for it's destruction. Due to this Box
s must point to valid Cello objects and so can't be initalised with NULL
or anything else invalid.
While this might not seem that useful when there is Garbage Collection this can be very useful when Garbage Collection is turned off, and when used in conjunction with collections.
struct Box {
var val;
};
$
alloc
dealloc
cast
cmp
eq
neq
gt
lt
ge
le
copy
hash
hash_data
size
swap
assign
name
brief
description
definition
new
del
construct
destruct
ref
deref
show
look
print
scan