Nested structs with Ruby/DL

N

Nathaniel Talbott

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,


Nathaniel

<:((><
 
R

Robert Feldt

Nathaniel Talbott said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top