Class: GLib::PtrArray

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

Overview

Overrides for GPtrArray, GLib’s automatically growing array of pointers.

Constant Summary collapse

POINTER_SIZE =
FFI.type_size(:pointer)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ContainerClassMethods

from, wrap

Constructor Details

#initialize(type) ⇒ PtrArray

Returns a new instance of PtrArray.



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

def initialize(type)
  @element_type = type
  store_pointer Lib.g_ptr_array_new
end

Instance Attribute Details

#element_typeObject (readonly)

Returns the value of attribute element_type.



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

def element_type
  @element_type
end

Class Method Details

.add(array, data) ⇒ Object



33
34
35
# File 'lib/ffi-glib/ptr_array.rb', line 33

def self.add(array, data)
  array.add data
end

.from_enumerable(type, arr) ⇒ Object



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

def self.from_enumerable(type, arr)
  new(type).tap { |it| it.add_array arr }
end

Instance Method Details

#==(other) ⇒ Object



68
69
70
# File 'lib/ffi-glib/ptr_array.rb', line 68

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

#add(data) ⇒ Object



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

def add(data)
  ptr = GirFFI::InPointer.from element_type, data
  Lib.g_ptr_array_add self, ptr
end

#add_array(ary) ⇒ Object



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

def add_array(ary)
  ary.each { |item| add item }
end

#eachObject



58
59
60
61
62
# File 'lib/ffi-glib/ptr_array.rb', line 58

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

#index(idx) ⇒ Object

Re-implementation of the g_ptrarray_index macro



52
53
54
55
56
# File 'lib/ffi-glib/ptr_array.rb', line 52

def index(idx)
  item_ptr = item_pointer(idx)
  convertor = GirFFI::ArrayElementConvertor.new convert_element_type, item_ptr
  convertor.to_ruby_value
end

#lengthObject



64
65
66
# File 'lib/ffi-glib/ptr_array.rb', line 64

def length
  struct[:len]
end

#reset_typespec(typespec) ⇒ Object



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

def reset_typespec(typespec)
  @element_type = typespec
  self
end