我的SWIG接口文件(包含在头文件中)中有一个C函数,它当前返回一个char *,SWIG用来为fetchFromRow方法生成一个String Java返回类型。我想知道,如果我在C方面将返回更改为void *,那么SWIG会在Java方面做什么? C fetchFromRow函数返回一个结构体(sockaddr_in),String或int。我如何设置我的SWIG接口文件来支持这个?有没有办法让生成的Java fetchFromRow具有返回类型的Object,以便我可以在Java端进行投票?SWIG对void * C返回类型的支持
C代码:
extern char *
fetchFromRow(struct row_t *r_row,
type_t type);
extern void *
fetchFromRow(struct row_t *r_row,
type_t type);
当我生成使用在头文件的空隙*(包含在SWIG接口文件)的方法中,我得到一个SWIGTYPE_p_void返回类型的Java方法。任何想法如何处理?
痛饮文件:
%module Example
%include "typemaps.i"
%include "stdint.i"
%include "arrays_java.i"
void setPhy_idx(uint32_t value);
%include "arrays_java.i"
void setId(unsigned char *value);
%{
#include "Example1.h"
#include "Example2.h"
#include "Example3.h"
#include "Example4.h"
%}
%rename setLogFile setLogFileAsString;
%inline %{
void setLogFileAsString(const char *fn) {
FILE *f = fopen(fn, "w");
setLogFile(f);
}
%}
%typemap(jstype) void* "java.lang.Object"
%typemap(javaout) void* {
long cPtr = $jnicall;
Object result = null;
if (type == type_t.TYPE_CATEGORY) {
result = void2int8(cPtr);
}
return result;
}
%newobject fetch(struct result_row_t *result_row, type_t type, int32_t *length);
%inline %{
uint8_t void2int8(jlong v) {
return (intptr_t)v;
}
%}
%apply char * { unsigned char * };
%apply char * { const void * }
%apply int32_t { int32_t * }
int createKey(const void* secret, int secret_len, const sdr_id_t *id, unsigned char key[20], int key_len);
session_t* createSession(const char* addr, int ort, const char *ddr, int rt, const der_id_t *id, unsigned char key[20], int key_len);
%apply int32_t *OUTPUT { int32_t *length }
%ignore createKey;
%ignore setLogFile;
%ignore ecreateSession;
%include "Example1.h"
%include "Example2.h"
%include "Example3.h"
%include "Example4.h"
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("Example");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
%}
的Java代码来调用取:我正在试图与SWIG来代替
{
//only type_t param shown here for simplicity
Object category = Example.fetch(result_row, type_t.TYPE_CATEGORY, length);
System.out.println("category=" + aLevel.toString());
System.out.println("category=" + ((Short)aLevel).intValue());
System.out.println("category=" + ((Short)aLevel).toString());
//all 3 Sys outs show same value but when called again each show same value but different than the first execution
}
C代码包装。这就要用到从Java调用,但现在我想调用获取直接(伪代码):
char *
wrapFetch(struct row_t *result_row, type_t type)
{
int8_t *int8_valp = NULL;
switch (attribute) {
case TYPE_CATEGORY:
int8_valp = fetch(
result_row, attribute, &length);
if (length > 0 && int8_valp != NULL) {
snprintf(smallbuf, sizeof(smallbuf), "%u", *int8_valp);
return strdup(smallbuf);
} else {
return NULL;
}
}
我觉得这是 http://stackoverflow.com/questions/2345354/accessing-void-pointers-in-python-using-swig-or-something-else –
这是略有不同的欺骗。它关于支持无效的C返回类型 – c12
这很棘手,但我认为可以。基本上你需要在'type_t's和他们所代表的“真实”类型之间建立一个映射关系,以便使Java在Java上可行。我有一个类似于Python的例子,我希望这个周末能够转换成Java。 – Flexo