将简单java数组传递给原始c数组swig

将简单java数组传递给原始c数组swig,java,android,c++,arrays,swig,Java,Android,C++,Arrays,Swig,我想把一个简单的java数组传递给c 目前,我使用下面的.i文件执行此操作 %module example %include "arrays_java.i" %include "example.h" %{ #include "example.h" %} 对于数组_java.i头,java数组被接受。 但它会生成阵列的完整副本,这对我来说太慢了。 我试图用这些函数构建一个类型映射,我可以使用GetDoubleArray函数来获取java数组的指针。 在使用swig之前,我使用JNI GetDo

我想把一个简单的java数组传递给c

目前,我使用下面的.i文件执行此操作

%module example

%include "arrays_java.i"
%include "example.h"
%{
#include "example.h"
%}
对于数组_java.i头,java数组被接受。 但它会生成阵列的完整副本,这对我来说太慢了。 我试图用这些函数构建一个类型映射,我可以使用GetDoubleArray函数来获取java数组的指针。 在使用swig之前,我使用JNI GetDoubleArrayElements构建了自己的包装器,这要快得多

下面您可以看到我尝试在typemap中使用Jni函数

%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) };
但是swig只为我构建了一个我应该使用的javaclass(SWIGTYPE_p_double)。 我只想传递一个简单的java数组,并在c中使用这个java数组指针

希望你能帮助我

编辑:

在这里您可以看到我的完整.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's just grab the original header file here */
%include "example.h"
%{
  #include "example"
%}

我能看到墙上的输出吗?我尝试了一下,但在android studio终端中没有输出,我得到了上半部分的解决方案

//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中,缺少函数ReleasDoubleArrayElements。 当我将其插入wrap.cpp时,一切正常。下面您可以看到从swig生成的wrap.cpp

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什么才能插入这个函数呢

编辑:

我找到了解决办法。谢谢柔印。你又帮了我一个忙。 我必须使用%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要快得多。

看起来像是拼写“length”这个词时的一个拼写错误,但也有更多的问题。运行SWIG with-Wall参数并注意我确信您将在
%apply
上看到的警告。你能给我们看一下示例.h和你的.i文件以及你的打字图吗?我编辑了我的文章。谢谢你的回答
%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);
}