通过SWIG传递函数指针数组

时间:2016-08-19 19:49:10

标签: python c++ swig

https://stackoverflow.com/a/22965961/353337的帮助下,我能够创建一个如何通过Python将一个函数指针传递给函数的简单示例。具体来说,用

double f(double x) {
  return x*x;
}

double myfun(double (*f)(double x)) {
  fprintf(stdout, "%g\n", f(2.0));
  return -1.0;
}
%module test

%{
#include "test.hpp"
%}

%pythoncallback;
double f(double);
%nopythoncallback;

%ignore f;
%include "test.hpp"

我可以打电话

import test
test.f(13)
test.myfun(test.f)

并获得预期的结果。

现在,我想更改myfun的签名以允许函数指针的数组(都具有相同的签名),例如,

double myfun(std::vector<double (*)(double)>)

如何调整.i文件?

理想情况下,Python调用将通过列表

test.myfun([test.f, test.g])

1 个答案:

答案 0 :(得分:1)

我做了以下测试用例来说明你正在尝试做什么。它有myfun(const std::vector<double(*)(double)>&)的真实实现,可以让生活更有趣:

#include <vector>

double g(double x) {
  return -x;
}

double f(double x) {
  return x*x;
}

typedef double(*pfn_t)(double);

std::vector<double> myfun(const std::vector<pfn_t>& funs, const double d) {
  std::vector<double> ret;
  ret.reserve(funs.size());
  for(auto && fn : funs)
    ret.emplace_back(fn(d));
  return ret;
}

我希望我们需要做的就是使用:

%include <std_vector.i>
%template(FunVec) std::vector<double(*)(double)>;
%template(DoubleVec) std::vector<double>;
%include "test.h"

但是,SWIG 3.0(来自Debian stable)无法正确处理此FunVec,并且生成的模块无法编译。所以我添加了一个类型映射作为解决方法:

%module test

%{
#include "test.h"
%}

%pythoncallback;
double f(double);
double g(double);
%nopythoncallback;

%ignore f;
%ignore g;

%typemap(in) const std::vector<pfn_t>& (std::vector<pfn_t> tmp) {
    // Adapted from: https://docs.python.org/2/c-api/iter.html
    PyObject *iterator = PyObject_GetIter($input);
    PyObject *item;

    if (iterator == NULL) {
      assert(iterator);
      SWIG_fail; // Do this properly
    }

    while ((item = PyIter_Next(iterator))) {
        pfn_t f;
        const int res = SWIG_ConvertFunctionPtr(item, (void**)(&f), $descriptor(double(*)(double)));
        if (!SWIG_IsOK(res)) {
          assert(false);
          SWIG_exception_fail(SWIG_ArgError(res), "in method '" "foobar" "', argument " "1"" of type '" "pfn_t""'");
        }
        Py_DECREF(item);
        tmp.push_back(f);
    }

    Py_DECREF(iterator);
    $1 = &tmp;
}

%include <std_vector.i>
// Doesn't work:
//%template(FunVec) std::vector<double(*)(double)>;
%template(DoubleVec) std::vector<double>;
%include "test.h"

基本上所有这一切都是为函数指针类型的向量添加一个'in'类型映射。该类型映射只是迭代Python提供的输入,并从Python迭代中构建一个临时的std::vector

这足以使以下Python按预期工作:

import test

print test.g
print test.f
print test.g(666)
print test.f(666)

print test.myfun([test.g,test.f],123)