Class: GirFFI::InOutPointer

Inherits:
FFI::Pointer
  • Object
show all
Defined in:
lib/gir_ffi/in_out_pointer.rb

Overview

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 extended pointer class and should be renamed.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, ptr = nil) ⇒ InOutPointer

Returns a new instance of InOutPointer.



9
10
11
12
13
14
# 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

Instance Attribute Details

#value_typeObject (readonly)

Returns the value of attribute value_type.



7
8
9
# File 'lib/gir_ffi/in_out_pointer.rb', line 7

def value_type
  @value_type
end

Class Method Details

.for(type) ⇒ Object



58
59
60
# File 'lib/gir_ffi/in_out_pointer.rb', line 58

def self.for(type)
  new(type).tap(&:clear)
end

.from(type, value) ⇒ Object



62
63
64
# File 'lib/gir_ffi/in_out_pointer.rb', line 62

def self.from(type, value)
  new(type).tap { |ptr| ptr.set_value value }
end

Instance Method Details

#clearObject



54
55
56
# File 'lib/gir_ffi/in_out_pointer.rb', line 54

def clear
  put_bytes 0, "\x00" * value_type_size, 0, value_type_size
end

#set_value(value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/gir_ffi/in_out_pointer.rb', line 43

def set_value(value)
  case value_ffi_type
  when Module
    value_ffi_type.copy_value_to_pointer(value, self)
  when Symbol
    send "put_#{value_ffi_type}", 0, value
  else
    raise NotImplementedError, value_ffi_type
  end
end

#to_ruby_valueObject

Convert more fully to a ruby value than #to_value



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gir_ffi/in_out_pointer.rb', line 29

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_valueObject

TODO: Create type classes that extract values from pointers.



17
18
19
20
21
22
23
24
25
26
# File 'lib/gir_ffi/in_out_pointer.rb', line 17

def to_value
  case value_ffi_type
  when Module
    value_ffi_type.get_value_from_pointer(self, 0)
  when Symbol
    send("get_#{value_ffi_type}", 0)
  else
    raise NotImplementedError
  end
end