对于通过参数返回一个函数创建类型映射 [英] Create a typemap for a function that returns through arguments
问题描述
我转换C API> Java和我有下面的函数原型。
I am converting C api > Java, and I have the following function prototype.
/*
Retrieves an individual field value from the current Line
\param reader pointer to Text Reader object.
\param field_num relative field [aka column] index: first field has index 0.
\param type on completion this variable will contain the value type.
\param value on completion this variable will contain the current field value.
\return 0 on failure: any other value on success.
*/
extern int gaiaTextReaderFetchField (gaiaTextReaderPtr reader, int field_num, int *type, const char **value);
我想获得返回的状态不如预期,返回类型为int和价值作为一个字符串(不被释放)
I would like to get the status returned as expected, return the "type" as an int and "value" as a string (not to be deallocated)
从技术文档我发现你创建一对夫妇可以保留返回值结构的。
From documentation I have found that you create a couple of structs that can retain the returned values.
可能有人请帮助我这第一个?
Could someone please help make this first one with me?
推荐答案
假设你的函数声明存在于一个名为header.h,你可以这样做:
Assuming your function declaration exists in a file called header.h you can do something like:
%module test
%{
#include "header.h"
%}
%inline %{
%immutable;
struct FieldFetch {
int status;
int type;
char *value;
};
%mutable;
struct FieldFetch gaiaTextReaderFetchField(gaiaTextReaderPtr reader, int field_num) {
struct FieldFetch result;
result.status = gaiaTextReaderFetchField(reader, field_num, &result.type, &result.value);
return result;
}
%}
%ignore gaiaTextReaderFetchField;
%include "header.h"
这隐藏了真正的 gaiaTextReaderFetchField
,而是替代返回两个输出参数,并以(不可修改)结构调用的结果的版本。
This hides the "real" gaiaTextReaderFetchField
and instead substitutes a version that returns both of the output parameters and the result of the call in an (unmodifiable) struct.
(你可以做的返回状态为0会导致异常,而不是被抛出,如果您愿意,使用%javaexception
,而不是将其放置在结构)
(You could make the return status being 0 cause an exception to be thrown instead if you'd rather, using %javaexception
instead of placing it in the struct)
这篇关于对于通过参数返回一个函数创建类型映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!