Jump to content
  • Advertisement
Sign in to follow this  
Njguy

Swig virtual functions with parameters

This topic is 1403 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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"

 

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!