Swig virtual functions with parameters

Started by
3 comments, last by Njguy 9 years, 6 months ago

I was wondering if anyone has any experience dealing with swig, c++ -> java. Basically I have a class which I am extending in java. It is used as an interface basically. My problem is that the virtual function parameters create a new java object everytime the function is called which sends the garbage collector through the roof. Here is an example:

class b2ContactListener
{
public:
virtual ~b2ContactListener() {}
/// Called when two fixtures begin to touch.
virtual void BeginContact(b2Contact* contact) { B2_NOT_USED(contact); }
/// Called when two fixtures cease to touch.
virtual void EndContact(b2Contact* contact) { B2_NOT_USED(contact); }
};
If you look at b2Contact parameter in BeginContact(), in Java this generates a new Contact object everytime the virtual method is called. Is there any way to get swig to make it so this doesn't happen?
This is my swig file.
%module(directors="1") b2WorldCallbacks;
%include "enums.swg"
%javaconst(1);
%feature("director") b2QueryCallback;
%feature("director") b2ContactListener;
%include "../../../Box2D/Box2D/Dynamics/b2WorldCallbacks.h"

Advertisement

You are the first person I've seen that has done this besides myself. I was wondering if anyone else ever did this.

I have not had any experience extending classes. Every time I ran into something like this I would create something in either the C++ side or the Java side (whichever was easier) that wrapped up the problem or added a level of indirection.

But I think if you are trying to extend a C++ then there will be overhead. I always made a complete interface in C++, wrapped it, swig'd it, and then wrapped that code in Java so no one ever had to know it was there (because it can get really weird passing in "pointers").

Do you have the source code for both parts? Maybe there is another way to approach the problem?

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

You are the first person I've seen that has done this besides myself. I was wondering if anyone else ever did this.

I have not had any experience extending classes. Every time I ran into something like this I would create something in either the C++ side or the Java side (whichever was easier) that wrapped up the problem or added a level of indirection.

But I think if you are trying to extend a C++ then there will be overhead. I always made a complete interface in C++, wrapped it, swig'd it, and then wrapped that code in Java so no one ever had to know it was there (because it can get really weird passing in "pointers").

Do you have the source code for both parts? Maybe there is another way to approach the problem?

That is what I figured had to be done, but I have no idea how to pass in pointers as you say. To be honest I think passing in pointers and having their reference changed on the c++ side is the only way of doing it. If you have any examples I'd love to see it.

It's been a while since I messed with this stuff, but I remember you could mark function parameter in the swig.i file as IN, OUT, or INOUT (or something like that, I don't remember the syntax). If you had a C++ function like void add( double* pValue ) { } then you'd get a Java function that took a double array and converted it to the right stuff.

You see this kind of junk when you use OpenGL in Java.

glVertex2iv(int[] v, int v_offset)

Entry point to C language function: void glVertex2iv(const GLint * v)

Here you are using an array instead of a pointer.

http://www.swig.org/Doc3.0/Java.html#Java_input_output_parameters

I believe this is the stuff I'm talking about.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

It's been a while since I messed with this stuff, but I remember you could mark function parameter in the swig.i file as IN, OUT, or INOUT (or something like that, I don't remember the syntax). If you had a C++ function like void add( double* pValue ) { } then you'd get a Java function that took a double array and converted it to the right stuff.

You see this kind of junk when you use OpenGL in Java.

glVertex2iv(int[] v, int v_offset)

Entry point to C language function: void glVertex2iv(const GLint * v)

Here you are using an array instead of a pointer.

http://www.swig.org/Doc3.0/Java.html#Java_input_output_parameters

I believe this is the stuff I'm talking about.

Thanks a bunch, ill give it a read.

This topic is closed to new replies.

Advertisement