Python:SWIG:Wrap访问Stru

2024-09-24 06:27:56 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个具有单个字段的简单结构:

typedef struct {
    MY_UNICODE value[512];
} TEST_STRUCTURE

其中MY_UNICODE是一个自定义的unicode实现。 另外,我有两种方法:

^{pr2}$

从和转换为此自定义类型。 现在我可以使用SWIG为此生成一个Python接口。 但是当我尝试在Python中访问TESTSTRUCTURE.value时。我总是指向一个MY_UNICODE对象。在

我的问题是:如何包装对结构成员的访问,以便获取python字符串并使用python字符串设置值?在

我知道SWIG的文档中提到了memberin类型映射。 但我的例子行不通:

%module test
%include "typemaps.i"
// This is the header file, where the structure and the functions are defined
%include "test.h"

%typemap(memberin) MY_UNICODE [512] {
    if(UTF8ToMyUnicode($1, $input) != 0) {
        return NULL;
    }
}

%typemap(memberout) MY_UNICODE [512] {
    if(MyUnicodeToUTF8($1, $input) != 0) {
        return NULL;
    }
}    

在生成的包装文件中,映射尚未应用。在

任何帮助都将不胜感激!谢谢!在

附言:我用的是swig2.0.10


Tags: the字符串test类型inputreturnifinclude
1条回答
网友
1楼 · 发布于 2024-09-24 06:27:56

我自己解决了这个问题。重要的是在接口中定义结构之前需要定义类型映射。文档对此并不清楚(或者我没有看到)。此外,还可以使用“in”和“out”类型映射来转换值。 这个例子对我很有用:

%module test
%include "typemaps.i"

%typemap(in) MY_UNICODE [512] {
    if(UTF8ToMyUnicode($1, $input) != 0) {
        return NULL;
    }
}

%typemap(out) MY_UNICODE [512] {
    if(MyUnicodeToUTF8($1, $result) != 0) {
        return NULL;
    }
}
// Now include the header file
%include "test.h"

相关问题 更多 >