如何使python函数作为c ++函数的回调[重复](How to make a python function as callback of a c++ function [duplicate])

编程入门 行业动态 更新时间:2024-10-17 05:32:01
如何使python函数作为c ++函数的回调[重复](How to make a python function as callback of a c++ function [duplicate])

可能重复: 使用boost :: python将回调从python传递给c ++

我必须将python函数作为c ++函数的回调。如何做?在哪里可以找到一些例子? 我想使用boost.python。

Possible Duplicate: pass callback from python to c++ using boost::python

I have to make a python function as callback of a c++ function.How to do?Where can I find some examples?? I want to use boost.python.

最满意答案

我的方法(不是唯一的方法)

在C ++中,我有一个以下列方式提供回调的类:

struct Mesh { template<class Visitor> void visitChildren(Visitor& v) { [...] v.visit([...]) } }

然后当我将类导出到python(仍然是C ++)

struct ChildrenVisitor { ChildrenVisitor(PyObject* self) : self(self) {} void visit( /* .... */ ) { call_method<void>(self, "visit" /*, [...] */ ); } private: PyObject* self; // 1 };

访问者本身导出到python

typedef ChildrenVisitor ClassT; class_<ClassT, boost::noncopyable >("ChildrenVisitor", init<PyObject*>() );

而对于Mesh的出口,你做的

.def("visitChildren", &Mesh::template visitChildren<ChildrenVisitor> )

我总是用...你可以在哪里插入任何参数。

在python中你做这样的事情

class MyVisitor(ChildrenVisitor): def __init__(self): ChildrenVisitor.__init__(self,self) def visit(self): # do whatever you want

我还想创建一个接受lambda函数的ChildrenVisitor的子类,这使得访问者可以用python单行编写。


哦,顺便问一下。 如果你想稍后调用该函数,那么你需要将C ++实现改为这样的

struct ChildrenVisitorBase { virtual void visit( /* .... */ ) = 0; }; struct Mesh { void registerCallback(shared_ptr<ChildrenVisitorBase> v) { visitors.push_back(v) } void doSomeWork() { // ... for(int i=0; i < visitors.size(); ++i) visitors[i]->visit( /* ... */ ) } std::vector< shared_ptr<ChildrenVisitorBase> > visitors; }

并使ChildrenVisitor实现ChildrenVisitorBase 。

My approach to this (not the only way)

In C++ I have a class that provides a callback in the following way:

struct Mesh { template<class Visitor> void visitChildren(Visitor& v) { [...] v.visit([...]) } }

and then when I export the class to python (still C++)

struct ChildrenVisitor { ChildrenVisitor(PyObject* self) : self(self) {} void visit( /* .... */ ) { call_method<void>(self, "visit" /*, [...] */ ); } private: PyObject* self; // 1 };

The export of the visitor itself to python

typedef ChildrenVisitor ClassT; class_<ClassT, boost::noncopyable >("ChildrenVisitor", init<PyObject*>() );

And for the export of Mesh you do

.def("visitChildren", &Mesh::template visitChildren<ChildrenVisitor> )

I always used ... where you can insert any of your arguments.

In python you do something like this

class MyVisitor(ChildrenVisitor): def __init__(self): ChildrenVisitor.__init__(self,self) def visit(self): # do whatever you want

I also like to make a subclass of ChildrenVisitor that accepts lambda functions, that makes writing visitors in python single lines.


Oh, btw. if you want to call the function at some later point then you would need to change the C++ implementation to something like this

struct ChildrenVisitorBase { virtual void visit( /* .... */ ) = 0; }; struct Mesh { void registerCallback(shared_ptr<ChildrenVisitorBase> v) { visitors.push_back(v) } void doSomeWork() { // ... for(int i=0; i < visitors.size(); ++i) visitors[i]->visit( /* ... */ ) } std::vector< shared_ptr<ChildrenVisitorBase> > visitors; }

And make ChildrenVisitor implement ChildrenVisitorBase.

更多推荐

本文发布于:2023-08-07 12:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464498.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   回调   python   duplicate   callback

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!