From: Axel Etzold Date: 2007-12-25T18:27:44+09:00 Subject: Re: C++ Functors and Ruby extensions -------- Original-Nachricht -------- > Datum: Tue, 25 Dec 2007 04:02:38 +0900 > Von: "Jason Roelofs" > An: ruby-talk@ruby-lang.org > Betreff: C++ Functors and Ruby extensions > I wonder if anyone has tried to do what I'm doing and if they've come up > with an appropriate solution. > > Basic idea: > > struct ruby_func_wrapper > { > VALUE operator()(VALUE self, VALUE args) > { > m_callback(args ...); > } > } > > extern "C" > void Init_functor_test() { > ruby_func_wrapper functor; > [ ... code that sets callback of functor ... ] > > rb_define_global_function("functor_test", [something here &functor], > -2); > } > > Basically, I want to use instances of the ruby_func_wrapper functor as > callback for Ruby methods. I'm using Boost right now, and have tried > Boost.Function and Boost.Bind to no avail (the code compiles but the > actual > call causes a seg fault, I assume the memory location is voided after the > init is done, so trying to pass execution to a bad memory location, but > I'm > not sure). For the record, I am trying my hand at a Boost.Ruby library. > > I'm also wondering if there is another way to add methods to Kernel or > Classes outside of the rb_define_* methods. In Python you can do > PyObject_SetAttr(namespace, name, PyCFunction) which is effectively (" > namespace.name = function"). I've yet to find an equivalent in Ruby. > > I hope this is clear, though if not I can post the full code I've got > right > now. > > Thanks > > Jason Dear Jason, I think SWIG (http://www.swig.org/) may have a solution for what you want to do: http://www.goto.info.waseda.ac.jp/~fukusima/ruby/swig-examples/funcptr/index.html Here's some more information on how to use it in conjunction with Ruby : http://www.swig.org/Doc1.3/Ruby.html Best regards, Axel -- GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS. Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail