Subject:
Re: [ruby-ffi] Struct within a Struct
From:
Wayne Meissner
Date:
3/24/10 2:53 AM
To:
ruby-ffi@googlegroups.com

On 24 March 2010 15:31, Colby Gutierrez-Kraybill <colbygk@gmail.com> wrote:
>
> I'm attempting to make use of a library with methods that return a
> struct by value and that struct contains another struct:
>
> spam_in_a_can_view s = windowShopping(); // note, not returning a
> pointer
>
> typedef struct
> {
>  size_t size;
>  double *data;
> } spam_in_a_can;
>
> typedef struct
> {
>  spam_in_a_can can;
> }  spam_in_a_can_view;
>
> I'm not sure how to go about this (if it's possible) with FFI.  The
> initial layout I would put down as:
>
> class Spam_In_A_Can < FFI::Struct
>  layout :size, :size_t,
>            :data, :pointer
> end
>
> class Spam_In_A_Can_View < FFI::Struct
>  layout :can, :pointer # ???
> end

You simply use the struct subclass name as the type
i.e.

class Spam_In_A_Can_View < FFI::Struct
  layout :can, Spam_In_A_Can
end

The same also works for arrays of structs as members
e.g.

class Many_Cans_Of_Spam < FFI::Struct
  layout :spam_array, [ Spam_In_A_Can, 5 ]
end

To unsubscribe from this group, send email to ruby-ffi+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.