Class: HDF5::FileContext

Inherits:
Object
  • Object
show all
Defined in:
lib/hdf5/file_context.rb

Constant Summary collapse

CLOSE_FUNCTIONS =
{
  file: :H5Fclose,
  group: :H5Gclose,
  dataset: :H5Dclose
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_id) ⇒ FileContext

Returns a new instance of FileContext.



9
10
11
12
13
# File 'lib/hdf5/file_context.rb', line 9

def initialize(file_id)
  @file_id = file_id
  @handles = { file_id => :file }
  @closed = false
end

Class Method Details

.guard(*methods) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/hdf5/file_context.rb', line 59

def self.guard(*methods)
  Module.new do
    methods.each do |method|
      define_method(method) do |*args, **kwargs, &block|
        lock = @context || HDF5::FFI::CALL_LOCK
        lock.synchronize { super(*args, **kwargs, &block) }
      end
    end
  end
end

Instance Method Details

#close(id) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hdf5/file_context.rb', line 22

def close(id)
  synchronize do
    type = @handles[id]
    return if type.nil?

    status = HDF5::FFI.public_send(CLOSE_FUNCTIONS.fetch(type), id)
    raise NativeError, "Failed to close HDF5 #{type}" if status < 0

    @handles.delete(id)
    @closed = true if type == :file
    nil
  end
end

#close_fileObject



36
37
38
39
40
41
42
43
44
# File 'lib/hdf5/file_context.rb', line 36

def close_file
  synchronize do
    return if closed?

    children = @handles.keys.reject { |id| id == @file_id }
    children.reverse_each { |id| close(id) }
    close(@file_id)
  end
end

#closed?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/hdf5/file_context.rb', line 51

def closed?
  @closed
end

#ensure_open!(id = nil) ⇒ Object

Raises:



46
47
48
49
# File 'lib/hdf5/file_context.rb', line 46

def ensure_open!(id = nil)
  raise ClosedError, 'HDF5 file is closed' if closed?
  raise ClosedError, 'HDF5 object is closed' if id && !@handles.key?(id)
end

#register(id, type) ⇒ Object

Raises:



15
16
17
18
19
20
# File 'lib/hdf5/file_context.rb', line 15

def register(id, type)
  raise ClosedError, 'HDF5 file is closed' if closed?

  @handles[id] = type
  id
end

#synchronize(&block) ⇒ Object



55
56
57
# File 'lib/hdf5/file_context.rb', line 55

def synchronize(&block)
  HDF5::FFI::CALL_LOCK.synchronize(&block)
end