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, offset = 0) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 44
def self.copy_value_to_pointer(value, pointer, offset = 0)
  size = value.size_in_bytes
  pointer.put_bytes(offset, value.to_ptr.read_bytes(size))
end
get_value_from_pointer(pointer, offset) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 40
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 11
def initialize(element_type, size, pointer)
  @element_type = element_type
  @size = size
  @pointer = pointer
end
wrap(element_type, size, pointer) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 49
def self.wrap(element_type, size, pointer)
  new element_type, size, pointer unless pointer.null?
end

Private Class Methods

check_size(expected_size, size) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 104
def check_size(expected_size, size)
  return if expected_size == -1
  return if size == expected_size

  raise ArgumentError, "Expected size #{expected_size}, got #{size}"
end
copy_from(element_type, size, enumerable) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 77
def copy_from(element_type, size, enumerable)
  return unless enumerable

  arr = enumerable.to_a
  case element_type
  when Array
    _main_type, sub_type = *element_type
    arr = arr.map { |it| sub_type.copy_from it }
  end

  from_enumerable element_type, size, arr
end
from(element_type, size, item) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 64
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 97
def from_enumerable(element_type, expected_size, arr)
  size = arr.size
  check_size expected_size, size
  ptr = 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 92
def from_sized_array(size, sized_array)
  check_size size, sized_array.size
  sized_array
end

Public Instance Methods

==(other) click to toggle source
# File lib/gir_ffi/sized_array.rb, line 32
def ==(other)
  to_a.eql? other.to_a
end
each() { |index(idx)| ... } click to toggle source
# File lib/gir_ffi/sized_array.rb, line 26
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 21
def index(idx)
  convertor = ArrayElementConvertor.new element_type, @pointer + idx * element_size
  convertor.to_ruby_value
end
size_in_bytes() click to toggle source
# File lib/gir_ffi/sized_array.rb, line 36
def size_in_bytes
  size * element_size
end
to_ptr() click to toggle source
# File lib/gir_ffi/sized_array.rb, line 17
def to_ptr
  @pointer
end

Private Instance Methods

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