Class: GLib::Array

Inherits:
Object
  • Object
show all
Extended by:
ContainerClassMethods
Includes:
Enumerable, ArrayMethods
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

Methods included from ArrayMethods

#index

Constructor Details

#initialize(type) ⇒ Array

Returns a new instance of Array.



18
19
20
21
22
# File 'lib/ffi-glib/array.rb', line 18

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.



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

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.



30
31
32
33
# File 'lib/ffi-glib/array.rb', line 30

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.



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

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

Instance Method Details

#==(other) ⇒ Object



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

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

#append_vals(ary) ⇒ Object



36
37
38
39
40
# File 'lib/ffi-glib/array.rb', line 36

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

#eachObject



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

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

#get_element_sizeObject Also known as: element_size



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

def get_element_size
  Lib.g_array_get_element_size self
end

#lengthObject



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

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.



63
64
65
66
67
68
69
70
71
# File 'lib/ffi-glib/array.rb', line 63

def reset_typespec(typespec = nil)
  if typespec
    @element_type = typespec
    check_element_size_match
  else
    @element_type = guess_element_type
  end
  self
end