From: John W Higgins Date: 2009-06-16T03:53:37+09:00 Subject: Re: wrapping a C struct[] constant --001517511008df8646046c6792fe Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Mon, Jun 15, 2009 at 11:11 AM, Martin DeMello wrote: > On Mon, Jun 15, 2009 at 7:13 PM, Jason Roelofs > wrote: > > > > Could you give an example of how this would work? I'm having a hard time > > picturing how such a thing would be possible to have done automagically. > > There has to be something somewhere that defines how a Ruby class would > map > > to said C struct. > > Yes, I was simply looking for something like > > struct foo { > int bar; > char *baz; > } > > cFoo = rb_define_class('cFoo') > rb_map_struct(cFoo, foo) > rb_accessor(cFoo, 'int', 'bar', bar) > rb_accessor(cFoo, 'str', 'bar', bar) > > VALUE rb_function() { > foo* a = some_function_call() > return rb_deserialize_data(foo) > } > > and from ruby > > foo = rb_function > foo.bar > foo.baz > > martin > > You are attacking your problem from the wrong end. The problem is that C doesn't have reflection so there is no way the rb_map_struct concept would ever work. C won't tell you what the members are so you can't map it to anything. However, working in the other direction - you could use the C preprocessor to build both your structs and the appropriate C code to define your ruby class you want for each struct. It's not pretty but it does work - have a look here for a nice defintion of what you are looking for http://en.wikipedia.org/wiki/C_preprocessor#X-Macros John --001517511008df8646046c6792fe--