SWIG无符号字符和字节[] [英] SWIG unsigned char and byte[]

查看:81
本文介绍了SWIG无符号字符和字节[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处都是.我已经在此站点上尝试了一些技巧.无济于事.

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

我有一个c ++全局函数

I have a c++ global function

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"

无论我做什么,界面始终是

No matter what I do the interface is always

public static String Squid(String buff, int len)

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

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

这是Swig 2.0.1

this is Swig 2.0.1

推荐答案

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

Your interface is close, but has the following issues:

  1. const%apply
  2. 很重要
  3. 您需要完全匹配buff的带符号/不带符号的限定词(显示的声明中没有限定词.
  4. 您在typemap中需要numinputs=1才能将其压缩为一个Java输入.
  5. 将大小设置为计算指针没有多大意义.
  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.

因此固定接口如下:

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

这篇关于SWIG无符号字符和字节[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆