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

from(elmtype, it) click to toggle source
# File lib/ffi-glib/array.rb, line 72
def self.from elmtype, it
  case it
  when self then it
  when FFI::Pointer then wrap elmtype, it
  else self.new(elmtype).tap {|arr| arr.append_vals it }
  end
end
new(type) click to toggle source
# File lib/ffi-glib/array.rb, line 17
def new type
  ptr = Lib.g_array_new(0, 0, calculated_element_size(type))
  wrap type, ptr
end
wrap(elmttype, ptr) click to toggle source
Calls superclass method
# File lib/ffi-glib/array.rb, line 66
def self.wrap elmttype, ptr
  super(ptr).tap do |array|
    array.element_type = elmttype if array
  end
end

Private Class Methods

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

Public Instance Methods

==(other) click to toggle source
# File lib/ffi-glib/array.rb, line 62
def ==(other)
  self.to_a == other.to_a
end
append_vals(ary) click to toggle source
# File lib/ffi-glib/array.rb, line 23
def append_vals ary
  bytes = GirFFI::InPointer.from_array element_type, ary
  Lib.g_array_append_vals(self, bytes, ary.length)
  self
end
data()

@deprecated From 0.7.0, data will return the actual data as a string, as generated by GirFFI.

Alias for: data_ptr
data_ptr() click to toggle source
# File lib/ffi-glib/array.rb, line 48
def data_ptr
  @struct[:data]
end
Also aliased as: data
each() { |index(idx)| ... } click to toggle source
# File lib/ffi-glib/array.rb, line 38
def each
  length.times do |idx|
    yield index(idx)
  end
end
element_type=(val) click to toggle source
# File lib/ffi-glib/array.rb, line 10
def element_type= val
  @element_type = val
  check_element_size_match
end
get_element_size() click to toggle source
# File lib/ffi-glib/array.rb, line 58
def get_element_size
  Lib.g_array_get_element_size self
end
index(idx) click to toggle source

Re-implementation of the g_array_index macro

# File lib/ffi-glib/array.rb, line 30
def index idx
  if idx >= length or idx < 0
    raise IndexError, "Index #{idx} outside of bounds 0..#{length - 1}"
  end
  ptr = GirFFI::InOutPointer.new element_type, data_ptr + idx * get_element_size
  ptr.to_ruby_value
end
length() click to toggle source
# File lib/ffi-glib/array.rb, line 44
def length
  @struct[:len]
end

Private Instance Methods

calculated_element_size() click to toggle source
# File lib/ffi-glib/array.rb, line 87
def calculated_element_size
  self.class.calculated_element_size self.element_type
end
check_element_size_match() click to toggle source
# File lib/ffi-glib/array.rb, line 91
def check_element_size_match
  unless calculated_element_size == self.get_element_size
    warn "WARNING: Element sizes do not match"
  end
end