We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
read_array_of_struct
Hi! Question and Feature request.
I searched Internet and found this answer. How to use ruby FFI to read array of static structures
There's no particularly nice way, I'm afraid. read_array_of_type doesn't work for structs.
You can achieve this using simple pointer arithmetics:
def self.readDataArray pointer = myData array_of_structs = 3.times.map { |idx| MyStruct.new(pointer + idx * MyStruct.size) } # Do your business end
Yes. It works well. But, I think it would be nice to have methods like
module FFI class Pointer def read_array_of_struct(type, length) ary = [] size = type.size tmp = self length.times { |j| ary << type.new(tmp) tmp += size unless j == length-1 # avoid OOB } ary end unless method_defined?(:read_array_of_struct) end end
Usage
array = memory.read_array_of_struct(Foo::FFI::Bar, 3) # => [#<BAR>, #<BAR>, #<BAR>]
We have read_array_of_type. But it is not what I am looking for.
read_array_of_type
ffi/lib/ffi/pointer.rb
Lines 107 to 123 in 2cc0e53
Perhaps I missed a useful feature of ruby-ffi. If so, I'd be glad to know.
Thank you.
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Hi!
Question and Feature request.
I searched Internet and found this answer.
How to use ruby FFI to read array of static structures
Yes. It works well. But, I think it would be nice to have methods like
Usage
We have
read_array_of_type
. But it is not what I am looking for.ffi/lib/ffi/pointer.rb
Lines 107 to 123 in 2cc0e53
Perhaps I missed a useful feature of ruby-ffi. If so, I'd be glad to know.
Thank you.
The text was updated successfully, but these errors were encountered: