[英]SWIG call function pointers stored within struct
I have a struct as follows:我有一个结构如下:
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);
};
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 包装这些模型并将它们公开给 Python,以便我可以运行我的单元测试。
While the SWIG documentation talks about exposing function pointers, it does not talk about function pointers contained within structs.虽然 SWIG 文档讨论了公开函数指针,但并未讨论包含在结构中的函数指针。 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;
}
I also tried prefixing the field names with %constant
.我还尝试使用%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
How can I call the underlying functions referenced by the function pointers contained within struct power_model
?如何调用struct 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.编辑:为了详细说明我的设置,我也是两个附加文件的来源,以更好地解释我试图使用power_model
接口实现的设置。
nexus5.c 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星系_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,
};
This works for me.这对我有用。 The solution 5 is the preferred one.方案5是首选方案。
test.i测试文件
%module test
%{
#include "test.h"
%}
// Solution 5 (right one)
%pythoncallback;
double f5(double);
%nopythoncallback;
%ignore f5;
%include "test.h"
test.h测试.h
typedef double (*fptr_t)(double);
// Solution 5
double f5(double x) {
return x*x;
}
typedef struct bla {
fptr_t f;
} bla;
From within Python从 Python 内部
import test
s = test.bla
# Assign function pointer
s.f = test.f5
# Execute
s.f(2)
f is a function taking a function pointer as argument f 是一个以函数指针为参数的函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.