免责声明,我是swig和python noob
我有自己的c ++库,并且将其包装为在swig的python中使用。
我的c ++类是这样的:
public MyCppClass()
{
public:
void MyFunction(char* outCharPtr, string& outStr, int& outInt, long& outLong)
{
outCharPtr = new char[2];
outCharPtr[0] = "o";
outCharPtr[1] = "k";
outStr = "This is a result";
outInt = 1;
outLong = (long)12345;
}
}
现在,我使用swig包装此类,并说该模块称为MyClass。
我要在python中实现的是以下代码(或说PSEUDO CODE,因为如果是这样,它将起作用)并输出:
import module MyClass
from MyClass import MyCppClass
obj = MyCppClass();
outCharPtr = "";
outStr = "";
outInt = 0;
outLong = 0;
obj.MyFunction(outCharPtr, outStr, outInt, outLong);
print(outCharPtr);
print(outStr);
print(outInt);
print(outLong);
我想要的输出是:
>>>Ok
>>>This is a result
>>>1
>>>12345
我正在使用python 3.4
如果这是基本操作,我真的很抱歉,但是我已经花了大约8个小时来解决这个问题,并且什么都没想出来。
任何帮助将不胜感激。
谢谢。
这是一种方法。我对您的无效示例进行了一些修改:
%module example
%include <std_string.i>
%include <cstring.i>
%cstring_bounded_output(char* outCharPtr, 256)
%apply std::string& OUTPUT {std::string& outStr};
%apply int& OUTPUT {int& outInt};
%apply long& OUTPUT {long& outLong};
%inline %{
class MyCppClass
{
public:
void MyFunction(char* outCharPtr, std::string& outStr, int& outInt, long& outLong)
{
outCharPtr[0] = 'o';
outCharPtr[1] = 'k';
outCharPtr[2] = '\0';
outStr = "This is a result";
outInt = 1;
outLong = (long)12345;
}
};
%}
使用示例:
>>> import example
>>> c=example.MyCppClass()
>>> c.MyFunction()
['ok', 'This is a result', 1, 12345]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句