Class: GLib::Array

Inherits:
Object
  • Object
show all
Extended by:
ContainerClassMethods
Includes:
Enumerable
Defined in:
lib/ffi-glib/array.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ContainerClassMethods

from, wrap

Constructor Details

#initialize(type) ⇒ Array

Returns a new instance of Array.



17
18
19
20
21
# 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

Instance Attribute Details

#element_typeObject (readonly)

Returns the value of attribute element_type.



15
16
17
# File 'lib/ffi-glib/array.rb', line 15

def element_type
  @element_type
end

Class Method Details

.calculated_element_size(type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
32
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



24
25
26
# File 'lib/ffi-glib/array.rb', line 24

def self.from_enumerable(elmtype, arr)
  new(elmtype).tap { |it| it.append_vals arr }
end

Instance Method Details

#==(other) ⇒ Object



57
58
59
# File 'lib/ffi-glib/array.rb', line 57

def ==(other)
  to_a == other.to_a
end

#append_vals(ary) ⇒ Object



35
36
37
38
39
# 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

#eachObject



41
42
43
44
45
# File 'lib/ffi-glib/array.rb', line 41

def each
  length.times do |idx|
    yield index(idx)
  end
end

#get_element_sizeObject Also known as: element_size



51
52
53
# File 'lib/ffi-glib/array.rb', line 51

def get_element_size
  Lib.g_array_get_element_size self
end

#index(idx) ⇒ Object

Re-implementation of the g_array_index macro



73
74
75
76
77
78
79
80
81
# 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

#lengthObject



47
48
49
# File 'lib/ffi-glib/array.rb', line 47

def length
  struct[:len]
end

#reset_typespec(typespec = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



62
63
64
65
66
67
68
69
70
# 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