问题
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:
const
matters for%apply
- You need to exactly match the signed/unsigned qualifier for
buff
(there is no qualifier in the declaration you showed. - Your in typemap needs
numinputs=1
to compress it to just one Java input. - 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