Closes all resources contained in this scope, in the reverse order they were added.
Manages a resource.
Manages a resource. Once a resource is managed, to close it explicitly
use the close(Any)
method on the ResourceScope
, not any close on the
managed object.
Optionally, this function can take a list of values to automatically close when the current resource is closed. Those values must be resources managed by this scope. If this value is transferred to another scope, the other resources will be transferred along with it.
if the value is already managed by this scope,
or if any value listed in transitiveClose
is not.
Registers an unmanaged value with this scope.
Registers an unmanaged value with this scope. This isn't very
useful in itself, but it can take a list of values to
transitively close, just like open
. This is useful for things
like iterator wrappers which are not themselves closeable but
which refer to closable things, and which may be transferred to
other scopes.
if the value is already "managed" by this scope,
or if any value listed in transitiveClose
is not.
Transfers a resource's ownership (and that of all its dependencies) between two scopes.
Transfers a resource's ownership (and that of all its dependencies) between two scopes.
rs1.transfer(something).to(rs2)
rs2.transfer(something).from(rs1) // equivalent
If the resource has dependencies, the order in which they are transferred to the other scope is defined only up to what can be inferred by the graph formed by those dependencies. That is:
val d1 = rs1.open(f()) val d2 = rs1.open(f()) val r = rs1.open(g(), transitiveClose = List(d1, d2)) // rs1.close() would be guaranteed to close [r, d2, d1] rs1.transfer(r).to(rs2) rs2.close() // may close either [r, d2, d1], or [r, d1, d2]
if the value is not managed by the source scope
This function is easy to use incorrectly; it depends on the declared dependency graph being correct.
Un-manages a resource.
Un-manages a resource. If this succeeds, the value will no longer be managed at all.
if the value is not managed by this scope
Utility for creating an owned-but-unmanaged object with an associated managed scope.
Utility for creating an owned-but-unmanaged object with an associated managed scope.
using(new ResourceScope("scope")) { rs => val lines: Iterator[Line] = rs.unmanagedWithAssociatedScope("iterator scope") { itScope => val stream = itScope.open(new FileInputStream("/tmp/foo")) linesOfFile(stream) // if this throws, stream will be closed before unmanagedWithAssociatedScope returns } rs.close(lines) // closes the associated FileInputStream }
A (managable) object for managing resources with lifetimes that do not nest nicely.
Resource scopes may themselves be owned by other resource scopes. It is possible to build an object which contains complex sub-resources in this fashion.
All methods on this class are thread-safe up to disposal of the
ResourceScope
itself.