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 19
def self.from(type, arg)
  new type, InPointer.from_array(type, arg)
end
new(elm_t, ptr) click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 10
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 23
def self.wrap(type, arg)
  new type, arg
end

Public Instance Methods

==(other) click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 37
def ==(other)
  to_a == other.to_a
end
each() { |wrap_value(val)| ... } click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 27
def each
  return if @ptr.null?

  offset = 0
  while (val = read_value(offset))
    offset += ffi_type_size
    yield wrap_value(val)
  end
end
to_ptr() click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 15
def to_ptr
  @ptr
end

Private Instance Methods

fetch_value(offset) click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 52
def fetch_value(offset)
  case ffi_type
  when Module
    ffi_type.get_value_from_pointer(@ptr, offset)
  when Symbol
    @ptr.send(getter_method, offset)
  else
    raise NotImplementedError
  end
end
ffi_type() click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 74
def ffi_type
  @ffi_type ||= TypeMap.type_specification_to_ffi_type element_type
end
ffi_type_size() click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 78
def ffi_type_size
  @ffi_type_size ||= FFI.type_size(ffi_type)
end
getter_method() click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 48
def getter_method
  @getter_method ||= "get_#{ffi_type}"
end
null_check_strategy() click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 82
def null_check_strategy
  @null_check_strategy ||=
    if ffi_type == :pointer
      :pointer
    elsif ffi_type.is_a? Symbol
      :numeric
    elsif ffi_type < GirFFI::ClassBase # rubocop:disable Lint/DuplicateBranch
      :pointer
    elsif ffi_type.singleton_class.include? GirFFI::EnumBase
      :enum
    end
end
null_value?(val) click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 95
def null_value?(val)
  case null_check_strategy
  when :pointer
    val.null?
  when :enum
    ffi_type.to_native(val, nil) == 0
  else
    val == 0
  end
end
read_value(offset) click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 43
def read_value(offset)
  val = fetch_value(offset)
  val unless null_value? val
end
wrap_value(val) click to toggle source
# File lib/gir_ffi/zero_terminated.rb, line 63
def wrap_value(val)
  case element_type
  when Array
    element_type.last.wrap val
  when Class
    element_type.wrap val
  else
    val
  end
end