ruby / fiddle

A libffi wrapper for Ruby.
BSD 2-Clause "Simplified" License
154 stars 37 forks source link

If the nested structure is an array of structures #126

Closed kojix2 closed 1 year ago

kojix2 commented 1 year ago

Fiddle supports nested structs.

require 'fiddle/import'

module FFI
  extend Fiddle::Importer

  Coord = struct [
    'float x',
    'float y',
    'float z'
  ]

  Triangle = struct [
    { 'vertices' => Coord },
    { 'normal' => Coord }
  ]
end

But what if the array of structures is nested?

require 'fiddle/import'

module FFI
  extend Fiddle::Importer

  Coord = struct [
    'float x',
    'float y',
    'float z'
  ]

  Triangle = struct [
    { 'vertices' => [Coord, 3] },
    { 'normal' => [Coord, 3] }
  ]
end

original C code:

typedef struct
{
  float x, y, z;
} gr3_coord_t;

typedef struct
{
  gr3_coord_t vertex[3];
  gr3_coord_t normal[3];
} gr3_triangle_t;
kou commented 1 year ago

Could you try the following?


require 'fiddle/import'

module FFI
  extend Fiddle::Importer

  Coord = struct [
    'float x',
    'float y',
    'float z'
  ]

  Triangle = struct [
    { 'vertices[3]' => Coord },
    { 'normal[3]' => Coord }
  ]
end
kojix2 commented 1 year ago

Sorry for the late reply. I will use this code. Thank you very much.