Class AutoScope

java.lang.Object
smile.util.AutoScope
All Implemented Interfaces:
AutoCloseable

public class AutoScope extends Object implements AutoCloseable
AutoScope allows for predictable, deterministic resource deallocation. AutoScope can be closed explicitly or be best used with try-with-resources statement. Closing an AutoScope will cause all the resources associated with that scope to be closed.
  • Constructor Details

    • AutoScope

      public AutoScope(AutoCloseable... resources)
      Constructors.
      Parameters:
      resources - the resources to be added to this scope.
  • Method Details

    • add

      public <T extends AutoCloseable> T add(T resource)
      Adds resource to this scope.
      Type Parameters:
      T - the type of resource.
      Parameters:
      resource - the resource to be added to this scope.
      Returns:
      the resource object.
    • remove

      public void remove(AutoCloseable resource)
      Detaches a resource from this scope by reference identity.

      Must not use Object.equals(Object): Tensor.equals compares native handle addresses, so after a closed tensor's address is reused by a new allocation, List.remove(Object) can detach the wrong entry and leave the live tensor on the scope to be freed by close().

      Parameters:
      resource - the resource to detach.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable