class GirFFI::InOutPointer

The InOutPointer class handles conversion between ruby types and pointers for arguments with direction :inout and :out.

TODO: This has now become a more general extende pointer class and should be renamed.

Attributes

value_type[R]

Public Class Methods

for(type) click to toggle source
# File lib/gir_ffi/in_out_pointer.rb, line 71
def self.for type
  self.new(type).tap {|ptr| ptr.clear}
end
from(type, value) click to toggle source
# File lib/gir_ffi/in_out_pointer.rb, line 75
def self.from type, value
  self.new(type).tap {|ptr| ptr.set_value value}
end
new(type, ptr = nil) click to toggle source
Calls superclass method
# File lib/gir_ffi/in_out_pointer.rb, line 9
def initialize type, ptr = nil
  @value_type = type

  ptr ||= AllocationHelper.safe_malloc(value_type_size)
  super ptr
end

Public Instance Methods

clear() click to toggle source
# File lib/gir_ffi/in_out_pointer.rb, line 67
def clear
  set_value nil_value
end
set_value(value) click to toggle source
# File lib/gir_ffi/in_out_pointer.rb, line 51
def set_value value
  case value_ffi_type
  when Class
    value_ffi_type.copy_value_to_pointer(value, self)
  # FIXME: Make SizedArray an FFI DataConverter so it conflates with the code above.
  when :c
    GLib::SizedArray.copy_value_to_pointer(value, self)
  when Symbol
    self.send "put_#{value_ffi_type}", 0, value
  when FFI::Enum
    self.send "put_int32", 0, value_ffi_type.to_native(value, nil)
  else
    raise NotImplementedError, value_ffi_type
  end
end
to_ruby_value() click to toggle source

Convert more fully to a ruby value than to_value

# File lib/gir_ffi/in_out_pointer.rb, line 37
def to_ruby_value
  bare_value = to_value
  case value_type
  when :utf8
    bare_value.to_utf8
  when Array
    value_type[1].wrap bare_value
  when Class
    value_type.wrap bare_value
  else
    bare_value
  end
end
to_value() click to toggle source

TODO: Create type classes that extract values from pointers.

# File lib/gir_ffi/in_out_pointer.rb, line 19
def to_value
  case value_ffi_type
  when Class
    value_ffi_type.get_value_from_pointer(self)
  # FIXME: This is a hack, since :c is not really a FFI type. Make
  # SizedArray a type FFI understands instead.
  when :c
    self
  when Symbol
    self.send("get_#{value_ffi_type}", 0)
  when FFI::Enum
    value_ffi_type[self.get_int32(0)]
  else
    raise NotImplementedError
  end
end

Private Instance Methods

nil_value() click to toggle source
# File lib/gir_ffi/in_out_pointer.rb, line 89
def nil_value
  value_ffi_type == :pointer ? nil : 0
end
value_ffi_type() click to toggle source
# File lib/gir_ffi/in_out_pointer.rb, line 81
def value_ffi_type
  @value_ffi_type ||= TypeMap.type_specification_to_ffitype value_type
end
value_type_size() click to toggle source
# File lib/gir_ffi/in_out_pointer.rb, line 85
def value_type_size
  @value_type_size ||= FFI.type_size value_ffi_type
end