我正在尝试将类型为signed char * 的结构成员转换为Java 中的字节数组。我有以下结构:
typedef struct {
signed char * content;
int contentLength;
} Foo;
我试过这个:
%typemap(jni) signed char *content [ANY] "jbyteArray"
%typemap(jtype) signed char *content [ANY] "byte[]"
%typemap(jstype) signed char *content [ANY] "byte[]"
%typemap(javaout) signed char *content [ANY] {
return $jnicall;
}
%typemap(memberin) int contentLength [ANY] {
int length=0;
$1 = &length;
}
%typemap(out) signed char * content [ANY] {
$result = JCALL1(NewByteArray, jenv, length);
JCALL4(SetByteArrayRegion, jenv, $result, 0, length, $1);
}
但没有结果。 Foo 的 getContent 方法具有以下签名:
SWIGTYPE_p_signed_char getContent();
我希望这个方法返回 byte[]。有解决办法吗?
这非常接近你想要的。你不想要[ANY]
因为数组的大小在 C 中不是“固定的”(它由一个int
,但这不是其类型的一部分)。
您可以使您的类型映射适用于:
%module test
%typemap(jni) signed char *content "jbyteArray"
%typemap(jtype) signed char *content "byte[]"
%typemap(jstype) signed char *content "byte[]"
%typemap(javaout) signed char *content {
return $jnicall;
}
%typemap(out) signed char * content {
$result = JCALL1(NewByteArray, jenv, arg1->contentLength);
JCALL4(SetByteArrayRegion, jenv, $result, 0, arg1->contentLength, $1);
}
// Optional: ignore contentLength;
%ignore contentLength;
%inline %{
typedef struct {
signed char * content;
int contentLength;
} Foo;
%}
我可能在这里遗漏了一些东西,但我看不到比这更好的从输出类型映射中获取“self”指针的方法 -arg$argnum
不起作用,也不起作用$self
。没有任何其他类型映射应用于此函数会有所帮助。
(请注意,您可能还想为signed char * content
或使其不可变。我很想%ignore
the contentLength
也完全是成员)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)