Class: FFI::Pointer
- Inherits:
-
Object
- Object
- FFI::Pointer
- Defined in:
- lib/ffi/pointer.rb
Direct Known Subclasses
Constant Summary collapse
- SIZE =
Pointer size
Platform::ADDRESS_SIZE / 8
Class Method Summary collapse
-
.size ⇒ Integer
Return the size of a pointer on the current platform, in bytes.
Instance Method Summary collapse
-
#read(type) ⇒ Object
Read pointer’s contents as
type
. -
#read_array_of_type(type, reader, length) ⇒ Array
Read an array of
type
of lengthlength
. -
#read_string(len = nil) ⇒ String
Read pointer’s contents as a string, or the first
len
bytes of the equivalent string iflen
is notnil
. -
#read_string_length(len) ⇒ String
Read the first
len
bytes of pointer’s contents as a string. -
#read_string_to_null ⇒ String
Read pointer’s contents as a string.
- #to_ptr ⇒ self
-
#write(type, value) ⇒ nil
Write
value
of typetype
to pointer’s content. -
#write_array_of_type(type, writer, ary) ⇒ self
Write
ary
in pointer’s contents astype
. -
#write_string(str, len = nil) ⇒ self
Write
str
in pointer’s contents, or firstlen
bytes iflen
is notnil
. -
#write_string_length(str, len) ⇒ self
Write
len
first bytes ofstr
in pointer’s contents.
Class Method Details
.size ⇒ Integer
Return the size of a pointer on the current platform, in bytes
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)
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
.
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
.
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
72 73 74 |
# File 'lib/ffi/pointer.rb', line 72 def read_string_length(len) get_bytes(0, len) end |
#read_string_to_null ⇒ String
Read pointer’s contents as a string.
Same as:
ptr.read_string # with no len
81 82 83 |
# File 'lib/ffi/pointer.rb', line 81 def read_string_to_null get_string(0) end |
#to_ptr ⇒ 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)
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
.
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
.
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
92 93 94 |
# File 'lib/ffi/pointer.rb', line 92 def write_string_length(str, len) put_bytes(0, str, 0, len) end |