SWIG unsigned char and byte[]

China☆狼群 提交于 2019-12-13 15:10:58

问题


I've looked all over the place. I have tried some of the techniques on this site. To no avail.

I have a c++ global function

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

I create a .i file

%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"

No matter what I do the interface is always

public static String Squid(String buff, int len)

if I remove the unsigned I get illegal conversions in the cxx wrapper

this is Swig 2.0.1


回答1:


Your interface is close, but has the following issues:

  1. const matters for %apply
  2. You need to exactly match the signed/unsigned qualifier for buff (there is no qualifier in the declaration you showed.
  3. Your in typemap needs numinputs=1 to compress it to just one Java input.
  4. Setting the size to be a computed pointer doesn't make much sense.

So the fixed interface looks like:

%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"


来源:https://stackoverflow.com/questions/16673256/swig-unsigned-char-and-byte

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!