![](/img/trans.png)
[英]Call a C++ function that takes pointer to an array using boost::python
[英]Get a c++ pointer to a Python instance using Boost::Python
我正在努力将Python嵌入到C ++应用程序中。 当我在Python中创建一个新对象时,我希望能够在我的C ++应用程序中存储对该Object的引用,以便稍后可以调用该对象上的方法。 建议的方法是什么?
例如,我希望能够做到这样的事情:
Entity.py
class Entity:
def getPointer(self)
return pointertoSelf;
Manager.cpp
Py_Initialize();
PyRun_SimpleString("import Entity");
PyRun_SimpleString("entity = Entity.Entity()");
pointerToPythonObj* = somehowGetPointerToObj("entity");
建议的方法是查询创建entity
对象的命名空间,然后将entity
对象的句柄存储为boost::python::object
。 当与C ++中的Python对象进行交互时,最好尽可能使用boost::python::object
,因为它提供了一个与Python变量非常相似的高级表示法。 此外,它还提供适当的引用计数来管理Python对象的生命周期。 例如,存储原始指针(即pointerToPythonObj*
)不会延长Python对象的生命周期; 如果Python对象是从解释器中收集的垃圾,那么pointerToPythonObj
将是一个悬空指针。
这是一个证明这一点的例子:
Entity.py:
class Entity:
def action(self):
print "in Entity::action"
main.cpp中:
#include <boost/python.hpp>
int main()
{
namespace python = boost::python;
try
{
Py_Initialize(); // Start interpreter.
// Create the __main__ module.
python::object main = python::import("__main__");
python::object main_namespace = main.attr("__dict__");
// Import Entity.py, and instantiate an Entity object in the
// global namespace. PyRun_SimpleString could also be used,
// as it will default to running within and creating
// __main__'s namespace.
exec(
"import Entity\n"
"entity = Entity.Entity()\n"
, main_namespace
);
// Obtain a handle to the entity object created from the previous
// exec.
python::object entity = main_namespace["entity"];
// Invoke the action method on the entity.
entity.attr("action")();
}
catch (const python::error_already_set&)
{
PyErr_Print();
}
}
运行以上程序会产生以下输出:
in Entity::action
如果无法导入Entity.py
,则可能需要将其包含的目录添加到PYTHONPATH
环境变量中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.