Subject: Re: [ruby-ffi] Pointer to a struct - how to cast? |
From: Wayne Meissner |
Date: 5/20/10 8:13 PM |
To: ruby-ffi@googlegroups.com |
On 21 May 2010 05:04, DA <drew.avis@gmail.com> wrote:
So I guess I need the equivalent of "get_array_of_struct"? I can't find an example for this case (array of struct) on the wiki. Any ideas?
That one is going to be a bit nasty. Try using FFI::Pointer#+ e.g. 0.upto(tis[:len]).each do|i|
s = FM::FTexFtItemT.new(tis[:val] + (i * FM::FTexFtItemT.size)) # do stuff with s here end Alternatively, you can use Pointer.new(FTexFtItemT, tis[:val]) to create a new pointer with a different type_size (the size used to step along it using the [] method). e.g. val_array = FFI::Pointer.new(FM::FTexFtItemT, tis[:val]) 0.upto(tis[:len]).each do|i|
s = FM::FTexFtItemT.new(val_array[i]) # do stuff with s struct here end It wouldn't be that hard to create a bit of a helper class to do this, since its a pretty common thing ... and some enterprising induhvidual might want to do that and submit it for inclusion in say ... NiceFFI.