使用 SWIG 将多个 Java 数据类型应用于同一 C 数据类型
我有两个通过 SWIG 向 Java 层公开的 C 函数,它们都有一个带有 const void * 数据类型 ("val) 的输入参数,对于 addCategory 函数需要是 uint8_t,而对于 addAttribute 函数需要是 char我目前在 SWIG 接口文件中使用 %apply 将 const void * C 类型映射到 Java 端的 Short 类型。有没有办法修改 SWIG 接口文件以支持 char 。 (字符串)和一个 uint8_t (短)作为 const void * 输入参数?
来自头文件的 C 函数:
int
addCategory(query_t *query, type_t type, const void *val);
int
addAttribute(query_t *query, type_t type, const void *val);
SWIG 接口文件:
%module Example
%include "stdint.i"
void setPhy_idx(uint32_t value);
%include "arrays_java.i"
void setId(unsigned char *value);
%{
#include "Example.h"
%}
%apply char * { unsigned char * };
%apply char * { void * };
%apply uint8_t { const void * }
%apply int32_t { int32_t * }
%include "Example.h"
I have two C functions that I'm exposing through SWIG to my Java layer and both have an input param with a const void * data type ("val) that needs to be a uint8_t for the addCategory function but a char for the addAttribute function. I'm currently, in the SWIG Interface file, using the %apply to map the const void * C type to a short on the Java side. Is there a way to modify the SWIG interface file to support both a char (String) and a uint8_t (short) for the const void * input parameter?
C Functions from header file:
int
addCategory(query_t *query, type_t type, const void *val);
int
addAttribute(query_t *query, type_t type, const void *val);
SWIG Interface File:
%module Example
%include "stdint.i"
void setPhy_idx(uint32_t value);
%include "arrays_java.i"
void setId(unsigned char *value);
%{
#include "Example.h"
%}
%apply char * { unsigned char * };
%apply char * { void * };
%apply uint8_t { const void * }
%apply int32_t { int32_t * }
%include "Example.h"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能直接这样做——在Java中这个地方会使用什么类型?您需要以某种方式帮助 SWIG 做出决定。
您有(至少)三种可能的解决方案:
%rename
使它们恢复为 Java 中的重载,即使它们在 C 中具有不同的名称)使用
联盟。这将由 SWIG 用
set
和get
函数包装:这会产生一个名为
values
的 Java 类,func()
接受并可以传递联合的成员之一。显然,您希望为union
的成员%apply
适当的类型映射。You can't directly do this - what type would be used in this place in Java? You need to help SWIG decide that in some way.
You have (at least) three possible solutions:
%rename
to make them back into overloads in Java even if they have different names in C)Use a
union
. This will get wrapped withset
andget
functions by SWIG:This results in a Java class called
values
, whichfunc()
takes and can pass one of the members of the union through. Clearly you'd want to%apply
appropriate typemaps for the members of theunion
.