i have struct 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 multiple power models code contains. wrap these models swig , expose them python can run unit tests.
while swig documentation talks exposing function pointers, not talk function pointers contained within structs. tried encapsulate calls in 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 tried prefixing field names %constant
.
with these approaches, end 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 not callable
how can call underlying functions referenced function pointers contained within struct power_model
?
edit: elaborate on setup, sources of 2 additional files better explain setup i'm trying achieve 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, };
this works me. solution 5 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;
from within python
import test s = test.bla # assign function pointer s.f = test.f5 # execute s.f(2)
f function taking function pointer argument
Comments
Post a Comment