Class: FFI::Pointer

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi/extra.rb

Instance Method Summary collapse

Instance Method Details

#read_array_of(type, number) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ffi/extra.rb', line 105

def read_array_of (type, number)
  if type.is_a?(Symbol)
    if respond_to? "read_array_of_#{type}"
      return send "read_array_of_#{type}"
    else
      type = FFI.find_type(type)
    end
  end

  type = type.native_type if type.respond_to? :native_type

  if type.is_a?(Class) && type.ancestors.member?(FFI::Struct) || type.is_a?(FFI::ManagedStruct)
    read_array_of_pointer(number).map {|pointer|
      type.new(pointer)
    }
  else
    begin
      send "read_array_of_#{type.name.downcase}", number
    rescue
      raise ArgumentError, "#{type.name} is not supported"
    end
  end
end

#typecast(type) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ffi/extra.rb', line 85

def typecast (type)
  if type.is_a?(Symbol)
    if respond_to? "read_#{type}"
      return send "read_#{type}"
    else
      type = FFI.find_type(type)
    end
  end

  if type.is_a?(Type::Builtin)
    send "read_#{type.name.downcase}"
  elsif type.is_a?(Class) && type.ancestors.member?(FFI::Struct) && !type.ancestors.member?(FFI::ManagedStruct)
    type.new(self)
  elsif type.respond_to? :from_native
    type.from_native(typecast(type.native_type), nil)
  else
    raise ArgumentError, 'you have to pass a Struct, a Builtin type or a Symbol'
  end
end