有人知道如何指示 SWIG 将 C 结构的这些成员视为函数指针并使其可从 Python 调用吗?
完整的故事 我有包含指向函数的指针的 C 结构。这些函数都是 typedefed。我有一个 C 函数,它将为此 C 结构分配内存,并将函数指针设置为指向有效的 C 函数。我的简化头文件看起来像这样
// simplified api.h
typedef void *handle_t;
typedef void sample_t;
typedef error_t comp_close_t(handle_t *h);
typedef error_t comp_process_t(handle_t h,
sample_t *in_ptr,
sample_t *out_ptr,
size_t *nr_samples);
typedef struct
{
comp_close_t *close;
comp_process_t *process;
} audio_comp_t;
// prototype for init
error_t comp_init(handle_t *h, int size);
以及相应的简化源文件:
// simplified api.c
static comp_close_t my_close;
static comp_process_t my_process;
audio_comp_t comp = {
my_close,
my_process
};
error_t comp_init(audio_comp_t **handle) {
*handle = ∁
return 0;
}
error_t my_close(handle_t *h) {
// stuff
*h = NULL;
return 0;
}
error_t my_process(handle_t h,
sample_t *in_ptr,
sample_t *out_ptr,
size_t *nr_samples) {
audio_comp_t *c = (audio_comp_t*) h;
// stuff
printf("doing something useful\n");
}
以及我的界面文件的最新版本:
%module comp_wrapper
%{
#include "api.h"
%}
%include "api.h"
// Take care of the double pointer in comp_init
%ignore comp_init;
%rename(comp_init) comp_init_overload;
%newobject comp_init;
%inline %{
audio_comp_t* comp_init_overload(int size) {
audio_comp_t *result = NULL;
error_t err = comp_init(&result, size);
if (SSS_NO_ERROR == err) {
...
}
return result;
}
%}
我可以通过类似“call_process”函数的方法来解决它,你可以在接口文件中找到它:
call_process(h, in, out, 32)
但这需要我为所有 struct 成员函数添加一个额外的包装器,而这不是必需的,因为 [SWIG 文档指出完全支持函数指针][1]
我假设我应该在接口文件中编写一些代码,以便 SWIG 知道它正在处理一个函数而不是一个 SwigPyObject
相关分类