Nathaniel Talbott <nathaniel / NOSPAMtalbott.ws> skrev den Wed, 10 Sep 2003 12:28:47 +0900:

> I some structs that are (conceptually) like this:
>
> typedef struct {int a, b, c;} s1;
> typedef struct {int d, e, f; s1 g;} s2;
>
> I'm trying to use Ruby/DL to declare and access the struct s2 from Ruby, but
> can't figure out how. I'd like to be able to use it like this:
>
> s = S2::new(ptr)
> p s.a
> p s.g.a
>
> I have no trouble declaring and using a simple struct (like s1), it's only
> nested structs that are causing me to scratch my head.
>
> Any help would be appreciated,
>
I haven't found a way either. I asked Ruby/DL author about it
yesterday; if he gets back to me I can post here also.

One way to get around it that seems to work is to "inline" the
defs of the structs on the ruby side. You wont
be able to use them as you want
but C functions seem to be ok with it.

For the C code:

typedef struct S1 {
  int b;
  char c;
} S1;

typedef struct S2 {
  int a;
  S1  s1;
  int d;
} S2;

extern int getb(S2 *s2) {
  return s2->s1.b;
}

this works on my end:

module Ex06
  extend DL::Importable
  dlload './test.so'

  S2 = struct [
    "int a",
    "int b",
    "char c",
    "int d",
  ]

  # We specify that it takes an int and returns an int. Do NOT give
  # the name of the parameter (n in this case), JUST the type.
  extern "int getb(void*)"
end

s2 = Ex06::S2.malloc
s2.a, s2.b, s2.c = 1, 2, 3

p Ex06.getb(s2.to_ptr)

Regards,

Robert Feldt