From: Martin DeMello Date: 2009-06-16T03:00:53+09:00 Subject: Re: wrapping a C struct[] constant On Mon, Jun 15, 2009 at 11:00 PM, Joel VanderWerf wrote: > Martin DeMello wrote: >> >> another basic thing I missed is the >> ability to define a typemap and then have a ruby class >> serialised/deserialised to a C struct. (Is that what Cgenerator's >> CShadow is all about?). Are these not common use cases? > > Would this be like swig in reverse? Input a ruby class with type > annotations, output C header, accessors, etc? I actually meant like a simplified swig - you have a C struct (arbitrary nesting of structs and arrays, actually), and you have a ruby class that you define a typemap for and then have it serialise/deserialise. Basically you know that you can mirror C structures in ruby with a bit of gruntwork, all you need is a tool to do the gruntwork for you > I'd like to do that for the > special case of BitStruct classes (fixed-length, packed fields, usually > numeric or character, stored in a ruby String using pack/unpack), so that > once you define a network protocol or other binary format in ruby, you can > share your code with C programmers (and via swig with other languages). That > seems feasible, and I did actually do this with a predecessor of BitStruct, > using cgen. But how would you handle a more general case, like a class with > a hash or array attribute? Network protocols were one of the use cases I was thinking of, except that you usually see them defined in C. I wanted the ability to use the structs from ruby, without going through C the way data_wrap_struct makes you do. Not asking for magic, just the ability to make the common case trivial. > Including CShadow in your class lets you define the structure of the T_DATA > object in ruby, in terms of accessors of various types. When your code runs, > it uses cgenerator to build an extension that takes care of all the boiler > plate functions: mark, free, accessors with type checking/conversion, alloc, > init, inspect, marshal, and yaml. Plus it handles inheritance and sets up > introspection methods. [See example below.] Okay, that's most of what I was looking for :) If it can convert an array of structs into a ruby array, that's *all* of what I'm looking for. Need to look through the CGenerator docs some more - great piece of work! martin