Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Nested structs with Ruby/DL

Reply
Thread Tools

Nested structs with Ruby/DL

 
 
Nathaniel Talbott
Guest
Posts: n/a
 
      09-10-2003
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

<(><


 
Reply With Quote
 
 
 
 
Robert Feldt
Guest
Posts: n/a
 
      09-10-2003
Nathaniel Talbott <(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Packed structs vs. unpacked structs: what's the difference? Daniel Rudy C Programming 15 04-10-2006 08:10 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments