OwnedObject

Usage

use OwnedObject;

Owned (along with Shared) manage the deallocation of a class instance. Owned is meant to be used when only one reference to an object needs to manage that object's storage.

To use Owned, allocate a class instance following this pattern:

var myOwnedObject = new Owned(new MyClass(...));

When myOwnedObject goes out of scope, the class instance it refers to will be deleted.

It is an error to copy initialize from myOwnedObject or to assign it to another Owned.

Note

The ways in which Owned may be used are currently limited. Copy-initialization, assignment, and in intent are expected to work. However, it is an error to use a Owned in a way that causes the compiler to add an implicitly copy, such as by returning a Owned that was passed by reference.

Note

Owned arguments with const in intent do not work yet.

record Owned

Owned manages the deletion of a class instance assuming that this Owned is the only thing responsible for managing the lifetime of the class instance.

proc forwarding_expr1_p()
proc Owned(type t)

Default-initialize a Owned.

proc Owned(p, type t = p.type)

Initialize a Owned with a class instance. When this Owned goes out of scope, it will delete whatever class instance it is storing.

It is an error to directly delete the class instance while it is managed by a Owned.

Arguments:p -- the class instance to manage. Must be of class type.
proc Owned(ref src: Owned, type t = src.t)

Copy-initializer. Creates a new Owned that takes over ownership from src. src will refer to nil after this call.

proc deinit()

The deinitializer for Owned will destroy the class instance it manages when the Owned goes out of scope.

proc ref clear()

Empty this Owned so that it stores nil. Deletes the previously managed object, if any.

proc ref retain(newPtr: p.type )

Change the instance managed by this class to newPtr. If this record was already managing a non-nil instance, that instance will be deleted.

proc ref release(): p.type

Empty this Owned so that it manages nil. Returns the instance previously managed by this Owned.

proc borrow()

Return the object managed by this Owned without impacting its lifetime at all. It is an error to use the value returned by this function after the Owned goes out of scope.