[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

SWIG: ruby proc->c function pointer

gga

1/28/2005 6:24:00 PM

Sorry for the slightly off-topic question. I am starting to use swig
to wrap some C++ library for ruby for the first time.
In the process of doing so, I realize I will need to pass proc objects
from ruby and turn them into function pointers in C, so as to provide a
callback mechanism.
I understand this can potentially be done with typemaps, but I am not
sure exactly how. I only need to have a single callback per class, so
I was thinking of storing the ruby callback within the class itself and
then have a common C wrap function that would map the C callback to the
appropiate ruby callbacks (ie. proc object) based on what was stored in
the class. I was also unsure if SWIG already provides some auxiliary
functions for doing something similar.
Does anyone have any pointers?
I'm already familiar with how ruby-fltk handles this, for example, and
I want to basically emulate something akin within the SWIG
meta-language. I'm looking at FXRuby but the code is a tad too complex
for a SWIG newbie.

2 Answers

Kouhei Sutou

1/29/2005 3:29:00 AM

0

gga

2/17/2005 8:52:00 PM

0

I finally got around to trying this. Thanks.
Unfortunately, the latest SWIG (v1.3.22) seems to choke on it, with:

irb(main):005:0> Callback.invoke(Proc.new{|str| p str}, "abc")
TypeError: cannot convert nil into String
from (irb):5:in `invoke'
from (irb):5

Looking at the code, SWIG fails in the StringValuePtr() conversion
here.

/* Get type mangle from class name */
SWIGRUNTIME(char *)
SWIG_Ruby_MangleStr(VALUE obj)
{
VALUE stype = rb_iv_get(obj, "__swigtype__");
return StringValuePtr(stype);
}