Class: FFI::Pointer

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

Overview

We create these 2 methods because of types like ‘size_t`. The type changes by platform, so we don’t know which write/read method to use. So we create a generic read/write that we can pass a type to and it will figure out which method needs to be used.

Instance Method Summary collapse

Instance Method Details

#read_type(type) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
# File 'lib/ffi_pointer.rb', line 6

def read_type(type)
	type = FFI.find_type(type) if type.is_a?(Symbol)
	type = type.native_type if type.is_a?(FFI::Type::Mapped)
	raise ArgumentError, "Can only read built-in types (type=#{type})" unless type.is_a?(FFI::Type::Builtin)
	name = type.inspect.match(/<#{type.class}:(\S+)/)[1].downcase
	method = "read_#{name}".to_sym
	self.send(method)
end

#write_type(type, value) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
# File 'lib/ffi_pointer.rb', line 14

def write_type(type, value)
	type = FFI.find_type(type) if type.is_a?(Symbol)
	type = type.native_type if type.is_a?(FFI::Type::Mapped)
	raise ArgumentError, "Can only write built-in types (type=#{type})" unless type.is_a?(FFI::Type::Builtin)
	name = type.inspect.match(/<#{type.class}:(\S+)/)[1].downcase
	method = "write_#{name}".to_sym
	self.send(method, value)
end