Class: GLib::PtrArray

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

Overview

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

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/ptr_array.rb', line 9

def element_type
  @element_type
end

Class Method Details

.add(array, data) ⇒ Object



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

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

.from(type, it) ⇒ Object



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

def self.from type, it
  case it
  when self then it
  when FFI::Pointer then wrap type, it
  else self.new(type).tap {|arr| arr.add_array it}
  end
end

.new(type) ⇒ Object



17
18
19
# File 'lib/ffi-glib/ptr_array.rb', line 17

def self.new type
  wrap(type, Lib.g_ptr_array_new)
end

.wrap(type, ptr) ⇒ Object



21
22
23
24
# File 'lib/ffi-glib/ptr_array.rb', line 21

def self.wrap type, ptr
  super(ptr).tap {|it|
    it.element_type = type}
end

Instance Method Details

#==(other) ⇒ Object



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

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

#add(data) ⇒ Object



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

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

#add_array(ary) ⇒ Object



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

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

#eachObject



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

def each
  @struct[:len].times.each do |idx|
    yield index(idx)
  end
end

#index(idx) ⇒ Object

Re-implementation of the g_ptr_array_index macro



48
49
50
51
52
# File 'lib/ffi-glib/ptr_array.rb', line 48

def index idx
  sz = FFI.type_size :pointer
  ptr = @struct[:pdata].get_pointer(idx * sz)
  GirFFI::ArgHelper.cast_from_pointer(element_type, ptr)
end