class GirFFI::ZeroTerminated

Represents a null-terminated array.

Attributes

element_type[R]

Public Class Methods

from(type, arg) click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 17
def self.from type, arg
  self.new type, InPointer.from_array(type, arg)
end
new(elm_t, ptr) click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 8
def initialize elm_t, ptr
  @element_type = elm_t
  @ptr = ptr
end
wrap(type, arg) click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 21
def self.wrap type, arg
  self.new type, arg
end

Public Instance Methods

==(other) click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 37
def ==(other)
  self.to_a == other.to_a
end
each() { |val| ... } click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 25
def each
  return if @ptr.null?
  offset = 0
  while val = read_value(offset)
    offset += FFI.type_size(ffi_type)
    if complex_element_type?
      val = element_class.wrap val
    end
    yield val
  end
end
to_ptr() click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 13
def to_ptr
  @ptr
end

Private Instance Methods

basic_element_type() click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 56
def basic_element_type
  if complex_element_type?
    element_type.first
  else
    element_type
  end
end
complex_element_type?() click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 52
def complex_element_type?
  Array === element_type
end
element_class() click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 72
def element_class
  if complex_element_type?
    element_type.last
  end
end
ffi_type() click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 48
def ffi_type
  @ffi_type ||= TypeMap.type_specification_to_ffitype basic_element_type
end
is_null_value(value) click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 64
def is_null_value value
  if basic_element_type == :pointer
    value.null?
  else
    value == 0
  end
end
read_value(offset) click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 43
def read_value offset
  val = @ptr.send("get_#{ffi_type}", offset)
  return val unless is_null_value(val)
end