Class: FFI::Pointer

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

Direct Known Subclasses

AutoPointer

Constant Summary collapse

SIZE =

Pointer size

Platform::ADDRESS_SIZE / 8

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sizeInteger

Return the size of a pointer on the current platform, in bytes

Returns:

  • (Integer)


49
50
51
# File 'lib/ffi/pointer.rb', line 49

def self.size
  SIZE
end

Instance Method Details

#read(type) ⇒ Object

Read pointer’s contents as type

Same as:

ptr.get(type, 0)

Parameters:

  • type (Symbol, Type)

    of data to read

Returns:

  • (Object)


152
153
154
# File 'lib/ffi/pointer.rb', line 152

def read(type)
  get(type, 0)
end

#read_array_of_type(type, reader, length) ⇒ Array

Read an array of type of length length.

Examples:

ptr.read_array_of_type(TYPE_UINT8, :read_uint8, 4) # -> [1, 2, 3, 4]

Parameters:

  • type (Type)

    type of data to read from pointer’s contents

  • reader (Symbol)

    method to send to self to read type

  • length (Integer)

Returns:

  • (Array)


114
115
116
117
118
119
120
121
122
123
# File 'lib/ffi/pointer.rb', line 114

def read_array_of_type(type, reader, length)
  ary = []
  size = FFI.type_size(type)
  tmp = self
  length.times { |j|
    ary << tmp.send(reader)
    tmp += size unless j == length-1 # avoid OOB
  }
  ary
end

#read_string(len = nil) ⇒ String

Read pointer’s contents as a string, or the first len bytes of the equivalent string if len is not nil.

Parameters:

  • len (nil, Integer) (defaults to: nil)

    length of string to return

Returns:

  • (String)


57
58
59
60
61
62
63
64
# File 'lib/ffi/pointer.rb', line 57

def read_string(len=nil)
  if len
    return ''.b if len == 0
    get_bytes(0, len)
  else
    get_string(0)
  end
end

#read_string_length(len) ⇒ String

Read the first len bytes of pointer’s contents as a string.

Same as:

ptr.read_string(len)  # with len not nil

Parameters:

  • len (Integer)

    length of string to return

Returns:

  • (String)


72
73
74
# File 'lib/ffi/pointer.rb', line 72

def read_string_length(len)
  get_bytes(0, len)
end

#read_string_to_nullString

Read pointer’s contents as a string.

Same as:

ptr.read_string  # with no len

Returns:

  • (String)


81
82
83
# File 'lib/ffi/pointer.rb', line 81

def read_string_to_null
  get_string(0)
end

#to_ptrself

Returns:

  • (self)


142
143
144
# File 'lib/ffi/pointer.rb', line 142

def to_ptr
  self
end

#write(type, value) ⇒ nil

Write value of type type to pointer’s content

Same as:

ptr.put(type, 0)

Parameters:

  • type (Symbol, Type)

    of data to read

  • value (Object)

    to write

Returns:

  • (nil)


163
164
165
# File 'lib/ffi/pointer.rb', line 163

def write(type, value)
  put(type, 0, value)
end

#write_array_of_type(type, writer, ary) ⇒ self

Write ary in pointer’s contents as type.

Examples:

ptr.write_array_of_type(TYPE_UINT8, :put_uint8, [1, 2, 3 ,4])

Parameters:

  • type (Type)

    type of data to write to pointer’s contents

  • writer (Symbol)

    method to send to self to write type

  • ary (Array)

Returns:

  • (self)


132
133
134
135
136
137
138
139
# File 'lib/ffi/pointer.rb', line 132

def write_array_of_type(type, writer, ary)
  size = FFI.type_size(type)
  ary.each_with_index { |val, i|
    break unless i < self.size
    self.send(writer, i * size, val)
  }
  self
end

#write_string(str, len = nil) ⇒ self

Write str in pointer’s contents, or first len bytes if len is not nil.

Parameters:

  • str (String)

    string to write

  • len (Integer) (defaults to: nil)

    length of string to return

Returns:

  • (self)


101
102
103
104
105
# File 'lib/ffi/pointer.rb', line 101

def write_string(str, len=nil)
  len = str.bytesize unless len
  # Write the string data without NUL termination
  put_bytes(0, str, 0, len)
end

#write_string_length(str, len) ⇒ self

Write len first bytes of str in pointer’s contents.

Same as:

ptr.write_string(str, len)   # with len not nil

Parameters:

  • str (String)

    string to write

  • len (Integer)

    length of string to return

Returns:

  • (self)


92
93
94
# File 'lib/ffi/pointer.rb', line 92

def write_string_length(str, len)
  put_bytes(0, str, 0, len)
end