class GirFFI::InPointer

The InPointer class 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 30
def self.from type, val
  return if !val
  case type
  when :utf8, :filename
    from_utf8 val
  when :gint32, :guint32, :gint8
    self.new val
  when Module
    self.new type[val]
  when :void
    from_object val
  else
    raise NotImplementedError, type
  end
end
from_array(type, ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 6
def self.from_array type, ary
  return if !ary
  case type
  when :utf8, :filename
    from_utf8_array ary
  when :gboolean
    from_boolean_array ary
  when Symbol
    from_basic_type_array type, ary
  when Class
    if type == GObject::Value
      from_gvalue_array type, ary
    else
      from_struct_array type, ary
    end
  when Module
    from_enum_array type, ary
  when Array
    from_interface_pointer_array ary
  else
    raise NotImplementedError, type
  end
end
from_object(obj) click to toggle source

FIXME: Hideous

# File lib/gir_ffi/in_pointer.rb, line 48
def from_object obj
  return nil if obj.nil?
  return obj.to_ptr if obj.respond_to? :to_ptr

  FFI::Pointer.new(obj.object_id).tap {|ptr|
    ArgHelper::OBJECT_STORE[ptr.address] = obj }
end

Private Class Methods

from_basic_type_array(type, ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 102
def from_basic_type_array type, ary
  ffi_type = TypeMap.map_basic_type type
  length = ary.length
  size = FFI.type_size ffi_type

  block = AllocationHelper.safe_malloc size * (length + 1)
  block.send "put_array_of_#{ffi_type}", 0, ary
  block.send("put_#{ffi_type}",
             length * size,
             (ffi_type == :pointer ? nil : 0))

  new block
end
from_boolean_array(ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 62
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 92
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 70
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_interface_pointer_array(ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 66
def from_interface_pointer_array ary
  from_basic_type_array :pointer, ary.map {|ifc| ifc.to_ptr}
end
from_struct_array(type, ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 81
def from_struct_array type, ary
  type_size = type::Struct.size
  length = ary.length

  ptr = AllocationHelper.safe_malloc length * type_size
  ary.each_with_index do |item, idx|
    type.copy_value_to_pointer item, ptr, idx * type_size
  end
  new ptr
end
from_utf8(str) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 96
def from_utf8 str
  len = str.bytesize
  ptr = AllocationHelper.safe_malloc(len + 1).write_string(str).put_char(len, 0)
  new ptr
end
from_utf8_array(ary) click to toggle source
# File lib/gir_ffi/in_pointer.rb, line 58
def from_utf8_array ary
  from_basic_type_array :pointer, ary.map {|str| from_utf8 str}
end