Class: GLib::Array

Inherits:
Object
  • Object
show all
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

Instance Attribute Details

#element_typeObject

Returns the value of attribute element_type.



9
10
11
# File 'lib/ffi-glib/array.rb', line 9

def element_type
  @element_type
end

Class Method Details

.from(elmtype, it) ⇒ Object



72
73
74
75
76
77
78
# 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) ⇒ Object



17
18
19
20
# 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) ⇒ Object



66
67
68
69
70
# 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

Instance Method Details

#==(other) ⇒ Object



62
63
64
# File 'lib/ffi-glib/array.rb', line 62

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

#append_vals(ary) ⇒ Object



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

def append_vals ary
  bytes = GirFFI::InPointer.from_array element_type, ary
  len = ary.length
  Lib.g_array_append_vals(self, bytes, len)
  self
end

#dataObject



54
55
56
# File 'lib/ffi-glib/array.rb', line 54

def data
  @struct[:data]
end

#eachObject



44
45
46
47
48
# File 'lib/ffi-glib/array.rb', line 44

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

#get_element_sizeObject



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

def get_element_size
  Lib.g_array_get_element_size self
end

#index(idx) ⇒ Object

Re-implementation of the g_array_index macro



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ffi-glib/array.rb', line 31

def index idx
  # TODO: Check idx < length
  ptr = GirFFI::InOutPointer.new element_type, data + idx * get_element_size
  case element_type
  when :utf8
    GirFFI::ArgHelper.ptr_to_utf8 ptr.to_value
  when Symbol
    ptr.to_value
  else
    element_type.wrap ptr.to_value
  end
end

#lengthObject



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

def length
  @struct[:len]
end