Discussion:
[Swig-user] Java arrays to C pointers
Guillaume Bittoun
2017-04-21 09:52:36 UTC
Permalink
Hi everyone,

I am having hard time understanding how I can pass java arrays to my C++
code using SWIG. The java code I would like to use is the following :

float[] foo = someProcessingFunction(
)

myCppFunction(foo)

So that the prototype `myCppFunction` in my C++ code would be either,

myCppFunction(float * floatArray)

or,

myCppFunction(float * floatArray size_t length)

So far, all my attempts have failed, I thought there would be something
like the `char * STRING` and`char * BYTE` typemaps that can maps
`byte[]` to `String` or `char*` but I still cannot find anything about
mapping float arrays.

There is either something obvious I missed or maybe a typemap to create
but I cannot find any solution so far. Is there a way to map java’s
`float[]` to C++ `float *` ? Moreover, is there a way to return a java
`float[]` using a C++ function wrapped by swig ?

Help is welcome.

Regards,

Guillaume
Guillaume Bittoun
2017-04-21 11:07:48 UTC
Permalink
EDIT: I just realized that it is possible to use jfloatArray both as
input and output parameters. This is an obvious thing I missed, if there
is any other best practice on this point, don’t hesitate to tell me.

Regards,

Guillaume
Post by Guillaume Bittoun
Hi everyone,
I am having hard time understanding how I can pass java arrays to my
float[] foo = someProcessingFunction(
)
myCppFunction(foo)
So that the prototype `myCppFunction` in my C++ code would be either,
myCppFunction(float * floatArray)
or,
myCppFunction(float * floatArray size_t length)
So far, all my attempts have failed, I thought there would be
something like the `char * STRING` and`char * BYTE` typemaps that can
maps `byte[]` to `String` or `char*` but I still cannot find anything
about mapping float arrays.
There is either something obvious I missed or maybe a typemap to
create but I cannot find any solution so far. Is there a way to map
java’s `float[]` to C++ `float *` ? Moreover, is there a way to return
a java `float[]` using a C++ function wrapped by swig ?
Help is welcome.
Regards,
Guillaume
William S Fulton
2017-04-21 17:45:03 UTC
Permalink
Look at arrays_java.i, see http://swig.org/Doc3.0/Java.html#Java_c_arrays.

William
EDIT: I just realized that it is possible to use jfloatArray both as input
and output parameters. This is an obvious thing I missed, if there is any
other best practice on this point, don’t hesitate to tell me.
Regards,
Guillaume
Hi everyone,
I am having hard time understanding how I can pass java arrays to my C++
float[] foo = someProcessingFunction(
)
myCppFunction(foo)
So that the prototype `myCppFunction` in my C++ code would be either,
myCppFunction(float * floatArray)
or,
myCppFunction(float * floatArray size_t length)
So far, all my attempts have failed, I thought there would be something
like the `char * STRING` and`char * BYTE` typemaps that can maps `byte[]`
to `String` or `char*` but I still cannot find anything about mapping float
arrays.
There is either something obvious I missed or maybe a typemap to create
but I cannot find any solution so far. Is there a way to map java’s
`float[]` to C++ `float *` ? Moreover, is there a way to return a java
`float[]` using a C++ function wrapped by swig ?
Help is welcome.
Regards,
Guillaume
------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
https://lists.sourceforge.net/lists/listinfo/swig-user
Guillaume Bittoun
2017-05-23 12:53:55 UTC
Permalink
Thanks,

FYI, there is another option that I’ve chosen : adding the `JEnv *
jniEnv` variable in the C++ functions prototype and ignore it in the
typemap using the following code in the interface file :

%typemap(in, numinputs=0) JNIEnv *jenv %{
$1 = jenv;
%}

I think that this isn’t obviously the best choice but it might help some
of you. In any case, first try the arrays_java.i as William told me :-)

Regards, and sorry for the late answer (I solved this a month ago).

Guillaume
Post by William S Fulton
Look at arrays_java.i, see http://swig.org/Doc3.0/Java.html#Java_c_arrays.
William
On 21 April 2017 at 12:07, Guillaume Bittoun
EDIT: I just realized that it is possible to use jfloatArray both
as input and output parameters. This is an obvious thing I missed,
if there is any other best practice on this point, don’t hesitate
to tell me.
Regards,
Guillaume
Post by Guillaume Bittoun
Hi everyone,
I am having hard time understanding how I can pass java arrays to
my C++ code using SWIG. The java code I would like to use is the
float[] foo = someProcessingFunction(
)
myCppFunction(foo)
So that the prototype `myCppFunction` in my C++ code would be either,
myCppFunction(float * floatArray)
or,
myCppFunction(float * floatArray size_t length)
So far, all my attempts have failed, I thought there would be
something like the `char * STRING` and`char * BYTE` typemaps that
can maps `byte[]` to `String` or `char*` but I still cannot find
anything about mapping float arrays.
There is either something obvious I missed or maybe a typemap to
create but I cannot find any solution so far. Is there a way to
map java’s `float[]` to C++ `float *` ? Moreover, is there a way
to return a java `float[]` using a C++ function wrapped by swig ?
Help is welcome.
Regards,
Guillaume
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
https://lists.sourceforge.net/lists/listinfo/swig-user
<https://lists.sourceforge.net/lists/listinfo/swig-user>
Loading...