SWIG call function pointers stored within struct

丶灬走出姿态 提交于 2019-12-12 17:40:58

问题


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.

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;
}

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

How can I call the underlying functions referenced by the function pointers contained within 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.

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,
};

回答1:


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;

From within 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



来源:https://stackoverflow.com/questions/32791661/swig-call-function-pointers-stored-within-struct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!