【发布时间】:2018-03-05 23:49:10
【问题描述】:
我正在尝试为 C++ 库修改现有的 SWIG Python 接口,为更多功能添加 Python 包装器,非常感谢有 SWIG 经验的人提供的帮助。
具体来说,我正在开发一个具有这样签名的函数:
void execute(int x, double y, ResultType& result1, ResultType& result2);
此函数接受两个空的 ResultType 对象并将它们填充为输出参数。在 Python 中,这必须转换为只接受 x 和 y 的函数,然后返回 result1 和 result2 的元组。
ResultType 是一种在整个库中广泛使用的容器类型。
typemap(in)
从研究中,我想我明白我需要为 result1 和 result2 添加一个类型映射“in”,它会吞下参数并将它们保存到临时变量中。我还发现引用被 SWIG 转换为指针,因此 &temp 而不是 temp。这是我的“输入”类型图:
typemap(in, numinputs=0) ResultType& result1 (ResultType temp) {
$1 = &temp;
}
typemap(in, numinputs=0) ResultType& result2 (ResultType temp) {
$1 = &temp;
}
类型映射(参数)
接下来,我添加了一个类型映射“argout”,将值附加到返回元组:
%typemap(argout) ResultType& result1 {
$result = SWIG_Python_AppendOutput($result, temp$argnum);
}
%typemap(argout) ResultType& result2 {
$result = SWIG_Python_AppendOutput($result, temp$argnum);
}
但是,这显然行不通,因为temp$argnum 将是原始C++ 类型ResultType,而我需要PyObject * 才能附加到元组。 ResultType 已经有一个有效的 SWIG 包装器。因此,在 Python 中,我可以毫无问题地调用 ResultType() 来构造它的实例。假设到目前为止我走在正确的轨道上,如何将原始 C++ ResultType 对象转换为属于 SWIG 生成的 ResultType 包装器的 PyObject *? (对不起,如果太多细节,我试图避免“XY问题”)
【问题讨论】: