class GLib::Array

Overrides for GArray, GLib’s automatically growing array. It should not be necessary to create objects of this class from Ruby directly.

Attributes

element_type[R]

Public Class Methods

calculated_element_size(type) click to toggle source

@api private

# File lib/ffi-glib/array.rb, line 29
def self.calculated_element_size(type)
  ffi_type = GirFFI::TypeMap.type_specification_to_ffi_type(type)
  FFI.type_size(ffi_type)
end
from_enumerable(elmtype, arr) click to toggle source

@api private

# File lib/ffi-glib/array.rb, line 24
def self.from_enumerable(elmtype, arr)
  new(elmtype).tap { |it| it.append_vals arr }
end
new(type) click to toggle source
# File lib/ffi-glib/array.rb, line 17
def initialize(type)
  @element_type = type
  ptr = Lib.g_array_new(0, 0, calculated_element_size)
  store_pointer(ptr)
end

Public Instance Methods

==(other) click to toggle source
# File lib/ffi-glib/array.rb, line 57
def ==(other)
  to_a == other.to_a
end
append_vals(ary) click to toggle source

@override

# File lib/ffi-glib/array.rb, line 35
def append_vals(ary)
  bytes = GirFFI::InPointer.from_array element_type, ary
  Lib.g_array_append_vals(self, bytes, ary.length)
  self
end
each() { |index(idx)| ... } click to toggle source
# File lib/ffi-glib/array.rb, line 41
def each
  length.times do |idx|
    yield index(idx)
  end
end
element_size()
Alias for: get_element_size
get_element_size() click to toggle source
# File lib/ffi-glib/array.rb, line 51
def get_element_size
  Lib.g_array_get_element_size self
end
Also aliased as: element_size
index(idx) click to toggle source

Re-implementation of the g_array_index macro

# File lib/ffi-glib/array.rb, line 73
def index(idx)
  unless (0...length).cover? idx
    raise IndexError, "Index #{idx} outside of bounds 0..#{length - 1}"
  end

  item_ptr = data_ptr + idx * element_size
  convertor = GirFFI::ArrayElementConvertor.new element_type, item_ptr
  convertor.to_ruby_value
end
length() click to toggle source
# File lib/ffi-glib/array.rb, line 47
def length
  struct[:len]
end
reset_typespec(typespec = nil) click to toggle source

@api private

# File lib/ffi-glib/array.rb, line 62
def reset_typespec(typespec = nil)
  if typespec
    @element_type = typespec
    check_element_size_match
  else
    @element_type = guess_element_type
  end
  self
end

Private Instance Methods

calculated_element_size() click to toggle source
# File lib/ffi-glib/array.rb, line 89
def calculated_element_size
  self.class.calculated_element_size element_type
end
check_element_size_match() click to toggle source
# File lib/ffi-glib/array.rb, line 93
def check_element_size_match
  return if calculated_element_size == get_element_size

  warn "WARNING: Element sizes do not match"
end
data_ptr() click to toggle source
# File lib/ffi-glib/array.rb, line 85
def data_ptr
  struct[:data]
end
guess_element_type() click to toggle source
# File lib/ffi-glib/array.rb, line 99
def guess_element_type
  case get_element_size
  when 1 then :uint8
  when 2 then :uint16
  when 4 then :uint32
  when 8 then :uint64
  end
end