2012-10-09 32 views
1

我正在轉換C api> Java,並且我有以下函數原型。爲通過參數返回的函數創建一個類型圖

/* 
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和「價值」作爲字符串(不被釋放)

從技術文檔我發現你創建一些可以保留返回值的結構。

請問有人能幫我做第一個嗎?

回答

0

假設你的函數聲明存在於一個名爲header.h,你可以這樣做文件:

%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,而是替代返回兩個輸出參數和一個版本(不可修改)結構中的調用結果。

(你可以做的返回狀態爲0會導致異常被拋出,而不是如果你寧願使用%javaexception,而不是將其放置在結構中的)

+0

嗨@Flexo, 我纔剛剛返回這最近和我有gaiaTextReaderPtr結構的另一個問題。我希望也許你能再次幫助我,這是另一個難題。我爲此創建了一個單獨的問題: http://stackoverflow.com/questions/14132617/create-a-typemap-for-a-function-part-2 – Hank

相關問題