Nested structs with Ruby/DL

Discussion in 'Ruby' started by Nathaniel Talbott, Sep 10, 2003.

  1. 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

    <:((><
     
    Nathaniel Talbott, Sep 10, 2003
    #1
    1. Advertising

  2. Nathaniel Talbott

    Robert Feldt Guest

    Nathaniel Talbott <> 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
     
    Robert Feldt, Sep 10, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    660
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    578
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    654
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,439
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    503
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page