class GirFFI::SizedArray

Class representing an array with a determined size

Attributes

element_type[R]
size[R]

Public Class Methods

copy_value_to_pointer(value, pointer) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 40
def self.copy_value_to_pointer(value, pointer)
  size = value.size_in_bytes
  pointer.put_bytes(0, value.to_ptr.read_bytes(size), 0, size)
end
get_value_from_pointer(pointer, offset) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 36
def self.get_value_from_pointer(pointer, offset)
  pointer + offset
end
new(element_type, size, pointer) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 7
def initialize(element_type, size, pointer)
  @element_type = element_type
  @size = size
  @pointer = pointer
end

Private Class Methods

check_size(expected_size, size) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 87
def check_size(expected_size, size)
  if expected_size > 0 && size != expected_size
    raise ArgumentError, "Expected size #{expected_size}, got #{size}"
  end
end
from(element_type, size, item) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 60
def from(element_type, size, item)
  return unless item

  case item
  when FFI::Pointer
    wrap element_type, size, item
  when self
    from_sized_array size, item
  else
    from_enumerable element_type, size, item
  end
end
from_enumerable(element_type, expected_size, arr) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 80
def from_enumerable(element_type, expected_size, arr)
  size = arr.size
  check_size expected_size, size
  ptr = GirFFI::InPointer.from_array element_type, arr
  wrap element_type, size, ptr
end
from_sized_array(size, sized_array) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 75
def from_sized_array(size, sized_array)
  check_size size, sized_array.size
  sized_array
end
wrap(element_type, size, pointer) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 55
def self.wrap(element_type, size, pointer)
  new element_type, size, pointer unless pointer.null?
end

Public Instance Methods

==(other) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 28
def ==(other)
  to_a == other.to_a
end
each() { |index(idx)| ... } click to toggle source
# File lib/gir_ffi/sized_array.rb, line 22
def each
  size.times do |idx|
    yield index(idx)
  end
end
index(idx) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 17
def index(idx)
  ptr = GirFFI::InOutPointer.new element_type, @pointer + idx * element_size
  ptr.to_ruby_value
end
size_in_bytes() click to toggle source
# File lib/gir_ffi/sized_array.rb, line 32
def size_in_bytes
  size * element_size
end
to_ptr() click to toggle source
# File lib/gir_ffi/sized_array.rb, line 13
def to_ptr
  @pointer
end

Private Instance Methods

element_ffi_type() click to toggle source
# File lib/gir_ffi/sized_array.rb, line 47
def element_ffi_type
  @element_ffi_type ||= GirFFI::TypeMap.type_specification_to_ffi_type element_type
end
element_size() click to toggle source
# File lib/gir_ffi/sized_array.rb, line 51
def element_size
  @element_size ||= FFI.type_size element_ffi_type
end