揭露一个C ++类实例为Python嵌入式间preTER [英] Exposing a C++ class instance to a python embedded interpreter
问题描述
我要寻找一个简单的方法来揭露一个C ++类的实例,以嵌入式间preTER蟒蛇。
I am looking for a simple way to expose a C++ class instance to a python embedded interpreter.
- 我有一个C ++库。该库被包裹(使用痛饮的时刻),我能够从蟒蛇间preTER使用它
- 我有一个C ++主程序从我的图书馆instanciates一个Foo类并嵌入一个Python间preTER
我想我的C ++美孚的全球实例,暴露在蟒蛇世界(视为Foo类)。
I would like to expose my C++ world instance of Foo to the python world (and seen as a Foo class).
这是可能的,如果是这样,怎么样?
我觉得这几乎就像在第一个答案:
<一href=\"http://stackoverflow.com/questions/5055443/boost-python-how-to-pass-a-c-class-instance-to-a-python-class\">boost::python::ptr或PyInstance_New使用
I think it's almost like in the first answer of : boost::python::ptr or PyInstance_New usage
我想这意味着我应该使用 Boost.Python的
来总结我的图书馆?
I guess this means I should use boost.Python
to wrap my library?
我唯一的目标就是操纵美孚我的C ++的实例在嵌入式Python间preTER(不知道它可以用previous方法来完成)。
My only goal is to manipulate my C++ instance of Foo in the embedded python interpreter (not sure that it can be done with the previous method).
希望我是清楚的,感谢您的帮助。
Hope I am clear, thanks for your help.
更新的
update
谢谢您的回答。事实上,我已经暴露我的Foo类到Python(与痛饮)。
Thanks for your answers. In fact, I already have exposed my Foo class to python (with swig).
我有什么:
的我的Foo类:的
class Foo{...};
的我的包裹库(包括Foo类)暴露在蟒蛇:的这样我就可以启动巨蟒间preTER,做这样的事情:
my wrapped library (including the Foo class) exposed to python: so I can start the python interpreter and do something like this :
import my_module
foo=my_modulde.Foo()
我要什么:
有一个C ++主程序中嵌入一个python间preTER和操纵C ++世界的变量。
Having a C++ main program which embeds a python interpreter and manipulates C++ world variables.
int main(int argc, char **argv)
{
Foo foo; // instanciates foo
Py_Initialize();
Py_Main(argc, argv); // starts the python interpreter
// and manipulates THE foo instance in it
Py_Finalize();
return 0;
}
现在更清晰? :)
推荐答案
升压蟒蛇一>允许您揭露C ++类到Python以一种非常紧密集成的方式 - 你甚至可以包装它们,这样你可以从你的c ++的人获得Python类,并有决心蟒蛇覆盖虚拟方法
Boost python Allows you to expose c++ classes to python in a very tightly integrated way - you can even wrap them so that you can derive python classes from your c++ ones, and have virtual methods resolved to the python overrides.
借助升压蟒蛇教程是一个良好的开端。
The boost python tutorial is a good place to start.
<子>编辑:
您可以创建一个C ++对象,并传递到内部蟒间preTER这样对它的引用:
You can create a c++ object and pass a reference to it to an internal python interpreter like this:
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/python.hpp>
#include <string>
#include <iostream>
namespace bp = boost::python;
struct Foo{
Foo(){}
Foo(std::string const& s) : m_string(s){}
void doSomething() {
std::cout << "Foo:" << m_string << std::endl;
}
std::string m_string;
};
typedef boost::shared_ptr<Foo> foo_ptr;
BOOST_PYTHON_MODULE(hello)
{
bp::class_<Foo, foo_ptr>("Foo")
.def("doSomething", &Foo::doSomething)
;
};
int main(int argc, char **argv)
{
Py_Initialize();
try {
PyRun_SimpleString(
"a_foo = None\n"
"\n"
"def setup(a_foo_from_cxx):\n"
" print 'setup called with', a_foo_from_cxx\n"
" global a_foo\n"
" a_foo = a_foo_from_cxx\n"
"\n"
"def run():\n"
" a_foo.doSomething()\n"
"\n"
"print 'main module loaded'\n"
);
foo_ptr a_cxx_foo = boost::make_shared<Foo>("c++");
inithello();
bp::object main = bp::object(bp::handle<>(bp::borrowed(
PyImport_AddModule("__main__")
)));
// pass the reference to a_cxx_foo into python:
bp::object setup_func = main.attr("setup");
setup_func(a_cxx_foo);
// now run the python 'main' function
bp::object run_func = main.attr("run");
run_func();
}
catch (bp::error_already_set) {
PyErr_Print();
}
Py_Finalize();
return 0;
}
这篇关于揭露一个C ++类实例为Python嵌入式间preTER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!