class GLib::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/ffi-glib/sized_array.rb, line 37
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
new(element_type, size, pointer) click to toggle source
# File lib/ffi-glib/sized_array.rb, line 8
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/ffi-glib/sized_array.rb, line 83
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/ffi-glib/sized_array.rb, line 57
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, size, arr) click to toggle source
# File lib/ffi-glib/sized_array.rb, line 77
def from_enumerable element_type, size, arr
  check_size(size, arr.size)
  ptr = GirFFI::InPointer.from_array element_type, arr
  self.wrap element_type, arr.size, ptr
end
from_sized_array(size, sized_array) click to toggle source
# File lib/ffi-glib/sized_array.rb, line 72
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/ffi-glib/sized_array.rb, line 52
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/ffi-glib/sized_array.rb, line 29
def ==(other)
  self.to_a == other.to_a
end
each() { |index(idx)| ... } click to toggle source
# File lib/ffi-glib/sized_array.rb, line 23
def each &block
  size.times do |idx|
    yield index(idx)
  end
end
index(idx) click to toggle source
# File lib/ffi-glib/sized_array.rb, line 18
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/ffi-glib/sized_array.rb, line 33
def size_in_bytes
  size * element_size
end
to_ptr() click to toggle source
# File lib/ffi-glib/sized_array.rb, line 14
def to_ptr
  @pointer
end

Private Instance Methods

element_ffi_type() click to toggle source
# File lib/ffi-glib/sized_array.rb, line 44
def element_ffi_type
  @element_ffi_type ||= GirFFI::TypeMap.type_specification_to_ffitype element_type
end
element_size() click to toggle source
# File lib/ffi-glib/sized_array.rb, line 48
def element_size
  @element_size ||= FFI.type_size element_ffi_type
end