存储在struct中的SWIG调用函数指针 [英] SWIG call function pointers stored within struct
问题描述
我的结构如下:
struct power_model {
int64_t (*estimate_energy)(statistics *stats, statistics *scaled_stats, parameters *from, parameters *to, energy_container *energy_container);
int64_t (*estimate_performance)(statistics *stats, parameters *params);
uint32_t (*freq_to_volt)(uint32_t freq);
};
我的代码包含多个电源模型. 我想用SWIG包装这些模型并将它们暴露给Python,以便我可以运行单元测试.
There are multiple power models that my code contains. I would like to wrap these models with SWIG and expose them to Python so that I can run my unit tests.
尽管SWIG文档讨论了公开函数指针,但并未讨论结构中包含的函数指针. 我试图将调用封装在接口文件中
While the SWIG documentation talks about exposing function pointers, it does not talk about function pointers contained within structs. I tried to encapsulate the calls in my interface file
%{
#include "models.h"
%}
%include "models.h"
%extend power_model {
%pythoncallback;
int64_t (*estimate_energy)(statistics *stats, statistics *scaled_stats, parameters *from, parameters *to, energy_container *energy_container);
int64_t (*estimate_performance)(statistics *stats, parameters *params);
uint32_t (*freq_to_volt)(uint32_t freq);
%nopythoncallback;
}
我还尝试使用%constant
为字段名称添加前缀.
I also tried prefixing the field names with %constant
.
使用这些方法,我总是会遇到相同的错误:
With these approaches, I always end up with the same error:
In [3]: model.estimate_energy()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-b2e3ace2fc9b> in <module>()
----> 1 model.estimate_energy()
TypeError: 'SwigPyObject' object is not callable
如何调用struct power_model
中包含的函数指针引用的基础函数?
How can I call the underlying functions referenced by the function pointers contained within struct power_model
?
修改:
为了详细说明我的设置,我还提供了两个附加文件,以更好地说明我正在尝试通过power_model
界面实现的设置.
Edit:
To elaborate on my setup, I am also sources of two additional files to better explain the setup I'm trying to achieve with the power_model
interface.
nexus5.c
static int64_t estimate_energy(statistics *stats, statistics *scaled_stats, parameters *from, parameters *to, energy_container *energy) {
...
}
static int64_t estimate_performance(statistics *stats, parameters *params) {
...
}
static uint32_t freq_to_volt(uint32_t freq) {
...
}
struct power_model nexus5_power_model = {
.estimate_energy = estimate_energy,
.estimate_performance = estimate_performance,
.freq_to_volt = freq_to_volt,
};
galaxy_s.c
static int64_t estimate_energy(statistics *stats, statistics *scaled_stats, parameters *from, parameters *to, energy_container *energy) {
...
}
static int64_t estimate_performance(statistics *stats, parameters *params) {
...
}
static uint32_t freq_to_volt(uint32_t freq) {
...
}
struct power_model galaxy_s_power_model = {
.estimate_energy = estimate_energy,
.estimate_performance = estimate_performance,
.freq_to_volt = freq_to_volt,
};
推荐答案
这对我有用.解决方案5是首选方案.
This works for me. The solution 5 is the preferred one.
test.i
%module test
%{
#include "test.h"
%}
// Solution 5 (right one)
%pythoncallback;
double f5(double);
%nopythoncallback;
%ignore f5;
%include "test.h"
test.h
typedef double (*fptr_t)(double);
// Solution 5
double f5(double x) {
return x*x;
}
typedef struct bla {
fptr_t f;
} bla;
在Python内部
import test
s = test.bla
# Assign function pointer
s.f = test.f5
# Execute
s.f(2)
f是一个以函数指针作为参数的函数
f is a function taking a function pointer as argument
这篇关于存储在struct中的SWIG调用函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!