From: John L. <jl...@ma...> - 2007-06-04 21:29:16
|
On 05/22/2007 12:41 AM, Dan Muresan wrote: > Hi, > > 1. I have wrapped a C library using SWIG; soon I discovered that I > sometimes need access to the underlying C pointers from the target > language. I've also discovered that > > (define swig-underlying-ptr > (foreign-lambda* c-pointer ((c-pointer swig_ptr)) > "C_return (swig_ptr);" > )) > > always does the trick. I find this puzzling, since after reading some > past messages I expected the correct accessor to be C_block_item > (swig_ptr, 0). More importantly, this means that I can't access the type > information which was supposed to be in C_block_item (swig_ptr, 1). Can > anyone shed some light? > I would have to look closer into this, doesn't seem correct. But I would actually try a typemap instead. For example, %typemap(in, numinputs=0) void **out (void *temp) { $1 = &temp; } %typemap(argout) void **out { $result = C_unsigned_long_to_num( (unsigned long) *($1)); or $result = C_make_tagged_ptr(...) or whatever } %inline %{ void getSwigPtr(YourType *a, void **out) { *out = (void *) a; } %} This way you do not depend on how SWIG represents the data underneath.... Hasn't been tested, but I hope you get the idea... If you want the type information, I suggest you look up "External Access to runtime" at http://www.swig.org/Doc1.3/Modules.html#Modules. Then write some C functions that lookup the type and for example return the name as a string or something. > > 2. Is there a way to use typemaps to avoid wrapping certain pointers? > I.e. I have a structure > > struct request { > char *data; > int size; > } > > where really I need to access "data" as a C pointer from Chicken. I > don't want the pointer to be wrapped; I *especially* don't want data to > be treated as a null-terminated string (and thus copied back and forth). > Sure, as seen above, typemaps can match based on argument names http://www.swig.org/Doc1.3/Typemaps.html#Typemaps_nn16 so you could write a typemap that only is used on arguments struct request *custom where not only the type, but also the argument name is taken into account. Note, the main reason above I used an argout typemap is so I could match on the argument name... John |