How to config SWIG interface to map a c++ data type in JNI to multiple data types

30 Views Asked by At

I'm new to SWIG, I'm using SWIG to generate a wrapper for a C++ library in Java. I have a C++ function float sum(aVector2). In my Java code, I want to pass two float values (x, y) to generated wrapper instead of creating an aVector2 object. I want achieve this becaue I can't use aVector2 later in Java code. I have to use lets say Vector2 java class, then if map Vector2 to aVector2 I need to make calls to FindClass(), GetFieldID(), GetMethodId(), etc. I just want to avoid that to take better performance or at least I think I would gain some performance from that, or If I'm wrong please tell me, thank you.

I've tried defining %typemap in the SWIG interface file, but I couldn't achieve that. I spent hours searching for solution but no result. I want something like this but obviously this is not working.

%typemap(jni) aVector2 "jfloat" "jfloat" "jfloat"
%typemap(jtype) aVector2 "float" "float" "float"
%typemap(jstype) aVector2 "float" "float" "float"

%typemap(in) aVector2 %{
    $1.x = $input$1;
    $1.y = $input$2;
    $1.z = $input$1
%}

Here is the C++ header file:

struct aVector2 {
    float x, y;
};

float sum(aVector2 v2) {
    return v2.x + v2.y;
}

and this is how I want use the wrapper in Java:

float x = 1.0f;
float y = 2.0f;

float result = exampleJNI.sum(x, y);

I would appreciate any guidance or examples on how to do this even though I'm not sure this is achievable in SWIG but I would like to know. Thank you for taking your time to read my question!

0

There are 0 best solutions below