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.



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

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.



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

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.



28
29
30
31
# File 'lib/ffi-glib/array.rb', line 28

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, it) ⇒ 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.



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

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

Instance Method Details

#==(other) ⇒ Object



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

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

#append_vals(ary) ⇒ Object



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

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

#eachObject



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

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

#get_element_sizeObject Also known as: element_size



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

def get_element_size
  Lib.g_array_get_element_size self
end

#lengthObject



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

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.



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

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