将参考从Python传递到用swig包裹的c ++函数以获取返回值

塔哈汗

免责声明,我是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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何获取递归python函数以返回值?

来自分类Dev

Python:编写函数以检查列表并返回值或无

来自分类Dev

如何获得在另一个函数中调用的函数以返回值?PYTHON

来自分类Dev

从Go调用Python函数并获取函数返回值

来自分类Dev

无法从Lua函数获取C函数返回值

来自分类Dev

可以声明C ++函数以使返回值不被忽略吗?

来自分类Dev

获取作为变量传递的函数的返回值

来自分类Dev

从开关盒到函数或方法获取返回值

来自分类Dev

如何获取python函数的所有可能的返回值?

来自分类Dev

如何仅获取c中函数的返回值?

来自分类Dev

C ++稍后从线程函数获取返回值

来自分类Dev

执行一个函数以在循环上返回值,直到该函数返回False-Python

来自分类Dev

在两次调用一个函数以及将返回值存储在变量之间选择哪个?

来自分类Dev

将表单值传递给函数以获取数组值

来自分类Dev

如何将JavaScript函数的返回值获取到Python变量

来自分类Dev

Python将返回值传递给函数

来自分类Dev

将lambda绑定到函数以获取返回结果

来自分类Dev

获取从Controller到javascript的返回值

来自分类Dev

传递函数的返回值作为参考

来自分类Dev

传递函数的返回值作为参考

来自分类Dev

pthread_create(),如何从传递的函数中获取返回值

来自分类Dev

pthread_create(),如何从传递的函数中获取返回值

来自分类Dev

简单赋值帮助:调用函数向其传递参数并获取其返回值

来自分类Dev

Python-从函数返回值

来自分类Dev

在python中打印函数的返回值?

来自分类Dev

从python中的修饰函数返回值

来自分类Dev

Python-返回值的函数

来自分类Dev

Python:从线程函数收集返回值

来自分类Dev

Python 中递归函数的返回值

Related 相关文章

热门标签

归档