class FFI::Pointer

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.

Public Instance Methods

read_type(type) click to toggle source
# 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) click to toggle source
# 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