module GirFFI::InPointer

The InPointer module handles conversion from ruby types to pointers for arguments with direction :in. This is used for arguments that are arrays, strings, or interfaces.

Public Class Methods

from(type, val) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 26
def self.from(type, val)
  return unless val

  case type
  when :utf8, :filename
    from_utf8 val
  when :gfloat, :gdouble, :gint64, :guint64
    from_basic_type type, val
  when :gint32, :guint32, :gint8, :GType
    FFI::Pointer.new val
  when GirFFI::EnumLikeBase
    FFI::Pointer.new type[val]
  when Module, :void
    val.to_ptr
  else
    raise NotImplementedError, type
  end
end
from_array(type, ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 8
def self.from_array(type, ary)
  return unless ary

  case type
  when Symbol
    from_simple_type_array type, ary
  when Module
    from_module_type_array type, ary
  when Array
    main_type, sub_type = *type
    raise "Unexpected main type #{main_type}" if main_type != :pointer

    from_pointer_array sub_type, ary
  else
    raise NotImplementedError, type
  end
end
from_utf8(str) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 45
def self.from_utf8(str)
  return unless str

  ptr = FFI::MemoryPointer.from_string(str)
  ptr.autorelease = false
  ptr
end

Private Class Methods

from_basic_type(type, value) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 56
def from_basic_type(type, value)
  ffi_type = TypeMap.type_specification_to_ffi_type type
  FFI::MemoryPointer.new(ffi_type).tap do |block|
    block.autorelease = false
    block.send "put_#{ffi_type}", 0, value
  end
end
from_basic_type_array(type, ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 127
def from_basic_type_array(type, ary)
  ffi_type = TypeMap.type_specification_to_ffi_type type
  type_size = FFI.type_size(ffi_type)
  FFI::MemoryPointer.new(type_size * (ary.length + 1)).tap do |block|
    block.autorelease = false
    block.send "put_array_of_#{ffi_type}", 0, ary
  end
end
from_boolean_array(ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 91
def from_boolean_array(ary)
  from_basic_type_array :int, ary.map { |val| val ? 1 : 0 }
end
from_enum_array(type, ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 123
def from_enum_array(type, ary)
  from_basic_type_array :int32, ary.map { |sym| type.to_native sym, nil }
end
from_gvalue_array(type, ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 99
def from_gvalue_array(type, ary)
  ary = ary.map do |it|
    if it.is_a? GObject::Value
      it
    else
      GObject::Value.wrap_ruby_value it
    end
  end
  from_struct_array type, ary
end
from_module_type_array(type, ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 75
def from_module_type_array(type, ary)
  if type == GObject::Value
    from_gvalue_array type, ary
  elsif type < GirFFI::ClassBase
    from_struct_array type, ary
  elsif type.singleton_class.include? GirFFI::EnumBase
    from_enum_array type, ary
  else
    raise NotImplementedError, type
  end
end
from_pointer_array(type, ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 95
def from_pointer_array(type, ary)
  from_basic_type_array :pointer, ary.map { |elem| from type, elem }
end
from_simple_type_array(type, ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 64
def from_simple_type_array(type, ary)
  case type
  when :utf8, :filename
    from_utf8_array ary
  when :gboolean
    from_boolean_array ary
  else
    from_basic_type_array type, ary
  end
end
from_struct_array(type, ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 110
def from_struct_array(type, ary)
  ffi_type = TypeMap.type_specification_to_ffi_type type
  type_size = FFI.type_size(ffi_type)
  length = ary.length

  ptr = FFI::MemoryPointer.new type_size * (length + 1)
  ptr.autorelease = false
  ary.each_with_index do |item, idx|
    type.copy_value_to_pointer item, ptr, idx * type_size
  end
  ptr
end
from_utf8_array(ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 87
def from_utf8_array(ary)
  from_basic_type_array :pointer, ary.map { |str| from_utf8 str }
end