SWIG unsigned char和byte []

时间:2013-05-21 15:02:11

标签: swig binary-data

我到处都看了。我在这个网站上尝试了一些技术。无济于事。

我有一个c ++全局函数

char* squid( char* buff, int len );

我创建一个.i文件

%module Crabby

%include "arrays_java.i"

%{
/* headers here are included in the wrapper code */
#include "sponge.h"
%}



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

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

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

%apply (const signed char* arr, size_t sz) { (const unsigned char* buff, int len) }
%apply (const signed char* arr, size_t sz) { (const unsigned char* query, int queryLen) }

%include "sponge.h"

无论我做什么,界面总是

public static String Squid(String buff, int len)

如果我删除未签名的,我会在cxx包装器中获得非法转换

这是Swig 2.0.1

1 个答案:

答案 0 :(得分:2)

您的界面已关闭,但存在以下问题:

  1. const%apply
  2. 很重要
  3. 您需要与buff的签名/无符号限定符完全匹配(您显示的声明中没有限定符。
  4. 您的inmap需要numinputs=1将其压缩为一个Java输入。
  5. 将大小设置为计算指针没有多大意义。
  6. 所以固定界面如下:

    %module Crabby
    
    %include "arrays_java.i"
    
    %{
    /* headers here are included in the wrapper code */
    #include "sponge.h"
    %}
    
    %typemap(jtype) (const signed char *arr, size_t sz) "byte[]"
    %typemap(jstype) (const signed char *arr, size_t sz) "byte[]"
    %typemap(jni) (const signed char *arr, size_t sz) "jbyteArray"
    %typemap(javain) (const signed char *arr, size_t sz) "$javainput"
    
    %typemap(in,numinputs=1) (const signed char* arr, size_t sz) {
      $1 = JCALL2(GetByteArrayElements, jenv, $input, NULL);
      const size_t sz = JCALL1(GetArrayLength, jenv, $input);
      $2 = sz;
    }
    
    %typemap(freearg) (const signed char *arr, size_t sz) {
      // Or use  0 instead of ABORT to keep changes if it was a copy
      JCALL3(ReleaseByteArrayElements, jenv, $input, $1, JNI_ABORT);
    }
    
    %apply (const signed char* arr, size_t sz) { ( char* buff, int len) }
    
    %include "sponge.h"