SWIG C++ Python:通过引用或POINT包装int

2024-06-28 20:06:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图把一些C++函数封装到Python包装器中。 对此,SWIG似乎是一个很好且简单的方法。

包装可以工作,但通过引用或指针传递整数时遇到问题。 由于Python不能处理引用,SWIG在内部将这些转换为指针。

一些简单的示例代码:

布拉特水电站:

#ifndef __BLAAT_HPP__
#define __BLAAT_HPP
class Blaat
{
public:
 int mA;
 float mB;

public:
 Blaat() {}
 void getA(int & fA);
 void setA(const int fA);
 ~Blaat() {}
};

#endif // __BLAAT_HPP__

Blaat.cpp公司

#include "Blaat.hpp"
#include <iostream>

void Blaat::getA(int & fA) {
 std::cout << "[Blaat::getA] fA = " << fA << std::endl;
 fA = mA;
} 

void Blaat::setA(const int fA) {
 std::cout << "[Blaat::setA] fA = " << fA << std::endl;
 mA = fA;
}

布莱特,我:

%module Blaat
%{
/* Includes the header in the wrapper code */
#include "Blaat.hpp"
%}

/* Parse the header file to generate wrappers */
%include "Blaat.hpp"

将代码转换为Python包装:

#!/bin/sh
swig -python -c++ -v $1.i 
gcc -c $1_wrap.cxx -fPIC -I/usr/include/python2.6
gcc -shared $1_wrap.o -o _$1<library_path> so -L. -l$1

一切正常。现在,我启动Python并执行以下操作:

from Blaat import *
a = Blaat()
b = int(1)
a.setA(b) <-- fine, calls setA() function fine
a.getA(b) <-- does not work

在“getA()”调用时,发生以下错误:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "Blaat.py", line 86, in getA
   def getA(self, *args): return _Blaat.Blaat_getA(self, *args)
TypeError: in method 'Blaat_getA', argument 2 of type 'int &'

注意,当通过引用和指针传递参数时,我都会遇到这个问题。 查看生成的“Blaat_wrap.cxx”文件,它会在实际的类型转换时停止:

res2 = SWIG_ConvertPtr(obj1, &argp2, SWIGTYPE_p_int,  0 );
if (!SWIG_IsOK(res2)) {
 SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Blaat_getA" "', argument " "2"" of type '" "int &""'"); 
}

这意味着函数SWIG_ConvertPtr()失败,这很奇怪,因为它检查的类型似乎是SWIG type_p_int。 从“setA()”函数中,我们可以看到类型转换是有效的(如果通过值传递)。

The SWIG documentation tells me

C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :

class Foo { public: double bar(double &a); }

has a low-level accessor

double Foo_bar(Foo *obj, double *a) { obj->bar(*a); }

有人能把我丢的东西扔进去吗?我现在很纠结。。。 Found this post, but this did not help either


Tags: 函数inincludeswigintfastddouble
2条回答

谢谢克里斯,这行! 经过进一步的挖掘,SWIG的文档似乎并不完整。

SWIG type conversion using the typemaps.i library is described here。 我从这个例子中得到的信息是,您必须手动指定要将参数用作输出(这意味着关于“指针和引用”的SWIG文档只对输入参数有效!)。

对于上面的简单示例,只包含.hpp文件并让SWIG自动处理所有内容就足够了。

布莱特,我:

%module Blaat
%include typemaps.i
%apply int &OUTPUT { int & fA };
%{
#include "Blaat.hpp"
%}

%include "Blaat.i"

PS:Blaat.cpp文件可能有错误的值,当然应该是mA而不是fA,因为fA是在cout之后设置的。。。

我不认为python有按引用返回的概念,但这里是我的解决方案:

布莱特,我:

%module Blaat
%include typemaps.i
%apply int &OUTPUT { int & fA };
%{
/* Includes the header in the wrapper code */
#include "Blaat.hpp"
%}

/* Parse the header file to generate wrappers */
class Blaat
{
public:
 Blaat();
 void getA(int & fA);
 void setA(const int fA);
 ~Blaat();
};

b.py公司:

from Blaat import *
a = Blaat()
b = int(1)
a.setA(b)
b = a.getA()

跑步:

python b.py
[Blaat::setA] fA = 1
[Blaat::getA] fA = 63

相关问题 更多 >