Pass simple java array to primitive c-array swig

I want to pass a simple java array to c.

I am currently doing this with the following .i file.

%module example

%include "arrays_java.i"
%include "example.h"
%{
#include "example.h"
%}

with arrays of headers arrays_java.i was accepted. But it makes a full copy of the array, and it slows me down. I tried to create a sample map using these functions, I can use the GetDoubleArray function to get a pointer to a java array. Before using swig, I create my own wrapper using JNI GetDoubleArrayElements, and it was much faster.

Below you can see my attempt to use Jni functions in a map.

%typemap(jtype) (double *values, int lenght) "double[]"
%typemap(jstype) (double *values, int lenght) "double[]"
%typemap(javain) (double *values, int lenght) "$javainput"
%typemap(jni) (double *values, int lenght) "jdoubleArray"
%typemap(in) (double *values, int lenght) {

    $1 = (double *)JCALL2(GetDoubleArrayElements, jenv, $input, NULL);
    $2 = (int)    JCALL1(GetArrayLength,       jenv, $input);
 }

 %apply (double * DOUBLE, int LENGTH)   { (double * doubleArray, long len) };

But swig only creates me javaclass (SWIGTYPE_p_double) which I should use. I just want to pass a simple java array and use this java array pointer in c.

hope you can help me thanks

EDIT:

.i. .

%module exampleModule
%include "std_string.i"
%include "std_vector.i"
%include "arrays_java.i"
#include <string>
#include <vector>





%template(nativeDoubleVector) std::vector<double>;



%typemap(jtype) (double *values, int length) "double[]"
%typemap(jstype) (double *values, int length) "double[]"
%typemap(javain) (double *values, int length) "$javainput"
%typemap(jni) (double *values, int length) "jdoubleArray"
%typemap(in) (double *values, int length) {

   $1 = (double *)JCALL2(GetDoubleArrayElements, jenv, $input, NULL);
   $2 = (int)    JCALL1(GetArrayLength,       jenv, $input);
}

%apply (double * values, int length)   { (double * doubleArray, long len) };




/* Let just grab the original header file here */
%include "example.h"
%{
  #include "example"
%}

.h

class example
{
public:
    example(int width, int height);
    ~example();
    test(double *values, int length);
 };

-Wall? , android studio

+4
1

.

//java float[] in c float arr
%typemap(jstype) double[] "double[]"
%typemap(jtype) double[] "double[]"
%typemap(jni) double[] "jdoubleArray"
%typemap(javain) double[] "$javainput"
%typemap(in) double[] {
    $1 = JCALL2(GetDoubleArrayElements, jenv, $input, NULL);
}

java . c, java . Java .

. wrap.cpp ReleaseasDoubleArrayElements. wrap.cpp, . wrap.cpp, swig.

SWIGEXPORT void JNICALL      Java_com_example_glm_ndk_1swig_1example_exampleJNI_Circle_1testfunktion(JNIEnv   *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jfloatArray jarg2, jint jarg3) {
   Circle *arg1 = (Circle *) 0 ;
   float *arg2 ;
   int arg3 ;

   (void)jenv;
   (void)jcls;
   (void)jarg1_;
   arg1 = *(Circle **)&jarg1; 
   {
     arg2 = jenv->GetFloatArrayElements(jarg2, NULL);
   }
   arg3 = (int)jarg3; 
   (arg1)->testfunktion(arg2,arg3);
   jenv->ReleaseFloatArrayElements(jarg2, arg2, NULL);//insert manually

}

swig, ?

:

. , . . python arg out % typemap (argout). , .

%typemap(jstype) double[] "double[]"
%typemap(jtype) double[] "double[]"
%typemap(jni) double[] "jdoubleArray"
%typemap(javain) double[] "$javainput"
%typemap(in) double[] {
     $1 = JCALL2(GetDoubleArrayElements, jenv, $input, NULL);
}
%typemap(argout) double[]{
    JCALL3(ReleaseDoubleArrayElements,jenv, $input, $1, 0);
}

, c.

0

All Articles