All,
I’m trying to decide what changes (if any) should be made to the wrapper
code generated by SWIG in light of the new allocation framework in Ruby
1.7. I should preface this by saying that the current SWIG/Ruby
implementation (from SWIG 1.3.12) seems to be working just fine with
Ruby 1.7 and so I’m tempted to just leave it alone. But if there are
some demonstrable benefits of switching over I’d of course do so. (I’ve
found a few threads in the ruby-talk archives about the implications of
the change for extension writers, but I have yet to find any discussion
of why this was changed
The current implementation works as follows. When SWIG sees a C++ class
declaration like so:
class Foo {
public:
Foo();
};
it defines a new class named Foo, with a “new” singleton method and an
"initialize" instance method. The C code for the new singleton method is
pretty boilerplate and looks something like this:
static VALUE
_wrap_Foo_new(int argc, VALUE *argv, VALUE klass) {
VALUE obj = Data_Wrap_Struct(klass, markfunc, freefunc, NULL);
rb_obj_call_init(obj, argc, argv);
return obj;
}
The C code for the initialize instance method looks like something this:
static VALUE
_wrap_Foo_initialize(int argc, VALUE *argv, VALUE self) {
Foo *foo = new Foo();
DATA_PTR(self) = foo
return self;
}
This arrangement works well; users can subclass Foo in their Ruby code
and override its initialize method as usual, e.g.
class MyFoo < Foo
def initialize(my_stuff)
super()
@my_stuff = my_stuff
end
end
My take on the new allocation framework, based on what I’ve seen so far,
boils down to the following change: we shouldn’t override Foo.new
anymore, but instead provide a Foo.allocate singleton method whose C
implementation would look like this:
static VALUE
_wrap_Foo_allocate(VALUE klass) {
return Data_Wrap_Struct(klass, markfunc, freefunc, NULL);
}
It looks like the implementation of Foo#initialize should remain the
same. Is there more to it than that? Is there some benefit (within the
context of SWIG/Ruby) for me changing this or should I just leave things
as-is?
Thanks in advance for any enlightenment on this topic
Lyle