From: William S F. <ws...@fu...> - 2006-12-30 21:23:51
|
Andras Varga wrote: > I'm wrapping C++ code into Java, and I'm trying to give pointer > ownership to wrapped code (like Petr Novotny recently). This is needed with > insertion methods of wrapped container objects: > > Foo foo = new Foo(); // wrapped object > FooContainer container = new FooContainer(); // wrapped object > container.insert(foo); // here, I want foo's swigMemOwn to be changed to > false > > I am willing to manually annotate FooContainer.insert() and other such > methods in > the SWIG interface file, but so far I found no solution. > > I recognize there isn't a direct way to do it, i.e. there isn't anything like > %disown_argument("1") FooContainer.insert(Foo); > > Currently I would be happy with any means that would let me > inject some custom code into the generated Java insert() method to take care > of that. (I would insert a foo.disown() call or something like that). > > So my question is: > > *** Is there a way to insert custom code into selected Java wrapper > methods? *** > > Typemaps are no good here, because they would apply to all methods taking > Foo, which is not what I need. > Nope. Typemaps will match by the name of the parameter name in addition to the type. So if you have: %typemap(javain) Foo *obj "..." void insert(Foo* obj); void remove(Foo* object); the typemap will only apply to types Foo *obj, ie the parameter in the insert method. Likewise you can match on the return type, using the method name to provide exact matching: %typemap(javaout) void FooContainer::insert { ... } struct FooContainer { void insert(Foo* obj); void remove(Foo* object); }; The first approach is probably more appropriate as the disown needs to be done on the Foo* argument. See the "Memory management for objects passed to the C++ layer" section in the documentation (Java.html) which covers this. William |