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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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



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

def self.for type
  self.new(type).tap {|ptr| ptr.set_value nil}
end

.from(type, value) ⇒ Object



66
67
68
# File 'lib/gir_ffi/in_out_pointer.rb', line 66

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

Instance Method Details

#set_value(value) ⇒ Object



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

def set_value value
  value = adjust_value_in value
  case value_ffi_type
  when Class
    self.put_bytes 0, value.to_ptr.read_bytes(value_type_size), 0, value_type_size
  when Symbol
    self.send "put_#{value_ffi_type}", 0, value
  else
    raise NotImplementedError
  end
end

#to_valueObject

TODO: Rename to get_value



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

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

#value_ffi_typeObject



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

def value_ffi_type
  @value_ffi_type ||= case value_type
                      when :gboolean
                        :int
                      else
                        TypeMap.type_specification_to_ffitype value_type
                      end
end

#value_type_sizeObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/gir_ffi/in_out_pointer.rb', line 51

def value_type_size
  @value_type_size ||= case value_ffi_type
                       when Class
                         value_ffi_type.size
                       when Symbol
                         FFI.type_size value_ffi_type
                       else
                         raise NotImplementedError
                       end
end