使用SWIG在重载的C ++方法

时间:2016-04-19 07:15:00

标签: java android c++ swig

我正在编写C ++库,供不同平台上的应用程序使用,包括Android。 对于Android版本,使用SWIG(我不能改变这个选择,而且我从未使用swig)。 Java-to-C ++的自动类型转换工作正常,直到我被分配到init方法重载的任务。

对于旧的init C ++接口是:

        /**
        * Initialize classifier from file
        * @param modelPath std::string  Full path to file with model
        * @return Classifier            Possible values:
        *                               pointer to Classifier instance in case of success
        *                               nullptr otherwise
        */
        static Classifier* init(const std::string& modelPath);

以下代码正在生成:

  public static Classifier init(String modelPath) {
    long cPtr = CLJNI.Classifier_init__SWIG_0(modelPath);
    return (cPtr == 0) ? null : new Classifier(cPtr, true);
  }

对于新的init C ++是:

        /**
        * Initialize classifier from memory block
        * @param modelMemory char*       Pointer to the memory block where model definition is stored
        * @param modelMemorySize size_t  Size of memory block allocated for model storage
        * @return Classifier             Possible values:
        *                                pointer to Classifier instance in case of success
        *                                nullptr otherwise
        */
        static Classifier* init(char* modelMemory, const size_t modelMemorySize);

和SWIG生成的Java代码是:

  public static Classifier init(String modelMemory, long modelMemorySize) {
    long cPtr = CLJNI.Classifier_init__SWIG_1(modelMemory, modelMemorySize);
    return (cPtr == 0) ? null : new Classifier(cPtr, true);
  }

但我需要Java方法:

Classifier init(byte[] modelMemory, long modelMemorySize);

即。必须将char*转换为byte[]而不是String

我应该在Classifier.i文件中进行哪些更改(现在看起来如下),以便有两个init方法可供Java调用?

%{
#include "Group.h"
#include "Classifier.h"
%}

//Java programmers are afraid of 'delete' method, so we have to rename it.
//Yes, bloody stupid, I know.
%typemap(javadestruct, methodname="dispose", methodmodifiers="public synchronized") CL::Classifier {
    if(swigCPtr != 0 && swigCMemOwn) {
      swigCMemOwn = false;
      $jnicall;
    }
    swigCPtr = 0;
}

%typemap(javafinalize) CL::Classifier %{
    protected void finalize() {
      dispose();
    }
%}

%newobject CL::Classifier::init;

%include "Classifier.h"

1 个答案:

答案 0 :(得分:1)

以下解决方案(基于Flexo's example)检查了 SWIG 2.0.11 SWIG 3.0.8

%module Classifier

%{
#include "Group.h"
#include "Classifier.h"
%}

%typemap(jtype) (signed char* modelMemory, const size_t modelMemorySize) "byte[]"
%typemap(jstype) (signed char* modelMemory, const size_t modelMemorySize) "byte[]"
%typemap(jni) (signed char* modelMemory, const size_t modelMemorySize) "jbyteArray"
%typemap(javain) (signed char* modelMemory, const size_t modelMemorySize) "$javainput"

%typemap(in,numinputs=1) (signed char* modelMemory, const size_t modelMemorySize) {
  $1 = JCALL2(GetByteArrayElements, jenv, $input, NULL);
  $2 = JCALL1(GetArrayLength, jenv, $input);
}

%typemap(freearg) (signed char* modelMemory, const size_t modelMemorySize) {
  // Or use  0 instead of ABORT to keep changes if it was a copy
  JCALL3(ReleaseByteArrayElements, jenv, $input, $1, JNI_ABORT);
}

%typemap(javadestruct, methodname="dispose", methodmodifiers="public synchronized") CL::Classifier {
    if(swigCPtr != 0 && swigCMemOwn) {
      swigCMemOwn = false;
      $jnicall;
    }
    swigCPtr = 0;
}

%typemap(javafinalize) CL::Classifier %{
    protected void finalize() {
      dispose();
    }
%}

%newobject CL::Classifier::init;

%include "Classifier.h"

%include "arrays_java.i"%apply(原始示例中所写)发现不必要。

这很好,并提供了两种init方法:

  public static Classifier init(String modelPath) {
    long cPtr = CLJNI.Classifier_init__SWIG_0(modelPath);
    return (cPtr == 0) ? null : new Classifier(cPtr, true);
  }

  public static Classifier init(byte[] modelMemory) {
    long cPtr = CLJNI.Classifier_init__SWIG_1(modelMemory);
    return (cPtr == 0) ? null : new Classifier(cPtr, true);
  }

但要求使用signed char*类型而不是char*。 否则

 public static Classifier init(String modelMemory, long modelMemorySize)

生产。

此外,我发现对于 SWIG 3.0.8 解决方案只能在一个额外的行中:

 %apply(char *STRING, size_t LENGTH) { (signed char *modelMemory, size_t modelMemorySize) };

即。 Classifier.i是:

%module Classifier

%{
#include "Group.h"
#include "Classifier.h"
%}

%apply(char *STRING, size_t LENGTH) { (signed char *modelMemory, size_t modelMemorySize) };

%typemap(javadestruct, methodname="dispose", methodmodifiers="public synchronized") CL::Classifier {
    if(swigCPtr != 0 && swigCMemOwn) {
      swigCMemOwn = false;
      $jnicall;
    }
    swigCPtr = 0;
}

%typemap(javafinalize) CL::Classifier %{
    protected void finalize() {
      dispose();
    }
%}

%newobject CL::Classifier::init;

%include "Classifier.h"

使用SWIG 2.0.11这种方式会导致编译错误,因为生成错误的包装器(CLJAVA_wrap.cxx):

SWIGEXPORT jlong JNICALL Java_impl_tools_CLJNI_Classifier_1init_1_1SWIG_11(JNIEnv *jenv, jclass jcls, jbyteArray jarg1) {
  jlong jresult = 0 ;
  signed char *arg1 = (signed char *) 0 ;
  size_t arg2 ;
  CL::Classifier *result = 0 ;

  (void)jenv;
  (void)jcls;
  {
    if (jarg1) {
      arg1 = (char *) jenv->GetByteArrayElements(jarg1, 0);     // ERROR HERE: invalid conversion from 'char*' to 'signed char*'
      arg2 = (size_t) jenv->GetArrayLength(jarg1);
    } else {
      arg1 = 0;
      arg2 = 0;
    }
  }
  result = (CL::Classifier *)CL::Classifier::init(arg1,arg2);
  *(CL::Classifier **)&jresult = result; 
  {
    if (jarg1) jenv->ReleaseByteArrayElements(jarg1, (jbyte *)arg1, 0);
  }

  return jresult;
}