<tfoot id='vHZvJ'></tfoot>

    <bdo id='vHZvJ'></bdo><ul id='vHZvJ'></ul>

      <legend id='vHZvJ'><style id='vHZvJ'><dir id='vHZvJ'><q id='vHZvJ'></q></dir></style></legend>

    1. <i id='vHZvJ'><tr id='vHZvJ'><dt id='vHZvJ'><q id='vHZvJ'><span id='vHZvJ'><b id='vHZvJ'><form id='vHZvJ'><ins id='vHZvJ'></ins><ul id='vHZvJ'></ul><sub id='vHZvJ'></sub></form><legend id='vHZvJ'></legend><bdo id='vHZvJ'><pre id='vHZvJ'><center id='vHZvJ'></center></pre></bdo></b><th id='vHZvJ'></th></span></q></dt></tr></i><div id='vHZvJ'><tfoot id='vHZvJ'></tfoot><dl id='vHZvJ'><fieldset id='vHZvJ'></fieldset></dl></div>

        <small id='vHZvJ'></small><noframes id='vHZvJ'>

      1. 将 C++ 类实例暴露给 Python 嵌入式解释器

        时间:2023-07-19
          <tbody id='rvsPm'></tbody>
          <tfoot id='rvsPm'></tfoot>
          <i id='rvsPm'><tr id='rvsPm'><dt id='rvsPm'><q id='rvsPm'><span id='rvsPm'><b id='rvsPm'><form id='rvsPm'><ins id='rvsPm'></ins><ul id='rvsPm'></ul><sub id='rvsPm'></sub></form><legend id='rvsPm'></legend><bdo id='rvsPm'><pre id='rvsPm'><center id='rvsPm'></center></pre></bdo></b><th id='rvsPm'></th></span></q></dt></tr></i><div id='rvsPm'><tfoot id='rvsPm'></tfoot><dl id='rvsPm'><fieldset id='rvsPm'></fieldset></dl></div>

          <small id='rvsPm'></small><noframes id='rvsPm'>

            <legend id='rvsPm'><style id='rvsPm'><dir id='rvsPm'><q id='rvsPm'></q></dir></style></legend>
            • <bdo id='rvsPm'></bdo><ul id='rvsPm'></ul>

                  本文介绍了将 C++ 类实例暴露给 Python 嵌入式解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在寻找一种将 C++ 类实例公开给 Python 嵌入式解释器的简单方法.

                  I am looking for a simple way to expose a C++ class instance to a python embedded interpreter.

                  • 我有一个 C++ 库.这个库是包装好的(暂时使用 swig),我可以从 python 解释器中使用它
                  • 我有一个 C++ 主程序,它从我的库中实例化了一个 Foo 类并嵌入了一个 python 解释器

                  我想将我的 C++ 世界实例 Foo 暴露给 python 世界(并被视为一个 Foo 类).

                  I would like to expose my C++ world instance of Foo to the python world (and seen as a Foo class).

                  这可能吗,如果可能,怎么办?

                  我认为这几乎就像在第一个答案中一样:boost::python::ptr 或PyInstance_新用法

                  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?

                  我唯一的目标是在嵌入式 python 解释器中操作我的 C++ Foo 实例(不确定是否可以用以前的方法完成).

                  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.

                  更新

                  感谢您的回答.事实上,我已经将我的 Foo 类暴露给了 python(使用 swig).

                  Thanks for your answers. In fact, I already have exposed my Foo class to python (with swig).

                  我有什么:

                  我的 Foo 类:

                  class Foo{...};
                  

                  我的包装库(包括 Foo 类)暴露给 python: 所以我可以启动 python 解释器并执行如下操作:

                  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 解释器并操作 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;
                  }
                  

                  现在更清楚了吗?:)

                  推荐答案

                  Boost python 允许您以非常紧密集成的方式将 c++ 类公开给 python - 您甚至可以包装它们,以便您可以从 c++ 类派生 python 类,并将虚拟方法解析为 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.

                  boost python 教程 是一个很好的起点.

                  您可以创建一个 c++ 对象并将对它的引用传递给内部 python 解释器,如下所示:

                  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
                  "
                              "
                  "
                              "def setup(a_foo_from_cxx):
                  "
                              "    print 'setup called with', a_foo_from_cxx
                  "
                              "    global a_foo
                  "
                              "    a_foo = a_foo_from_cxx
                  "
                              "
                  "
                              "def run():
                  "
                              "    a_foo.doSomething()
                  "
                              "
                  "
                              "print 'main module loaded'
                  "
                          );
                  
                          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 嵌入式解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将参数传递给 boost::thread? 下一篇:提升 shared_lock.阅读首选?

                  相关文章

                  <small id='awEmu'></small><noframes id='awEmu'>

                • <i id='awEmu'><tr id='awEmu'><dt id='awEmu'><q id='awEmu'><span id='awEmu'><b id='awEmu'><form id='awEmu'><ins id='awEmu'></ins><ul id='awEmu'></ul><sub id='awEmu'></sub></form><legend id='awEmu'></legend><bdo id='awEmu'><pre id='awEmu'><center id='awEmu'></center></pre></bdo></b><th id='awEmu'></th></span></q></dt></tr></i><div id='awEmu'><tfoot id='awEmu'></tfoot><dl id='awEmu'><fieldset id='awEmu'></fieldset></dl></div>

                • <tfoot id='awEmu'></tfoot>

                    <bdo id='awEmu'></bdo><ul id='awEmu'></ul>

                    1. <legend id='awEmu'><style id='awEmu'><dir id='awEmu'><q id='awEmu'></q></dir></style></legend>