简体   繁体   English

使用Boost Python和std :: shared_ptr

[英]Using Boost Python & std::shared_ptr

I'm trying to get Boost Python to play nicely with std::shared_ptr. 我试图让Boost Python与std :: shared_ptr很好地配合。 Currently, I'm receiving this error: 目前,我收到此错误:

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    comp.place_annotation(circle.centre())
TypeError: No to_python (by-value) converter found for C++ type: std::shared_ptr<cgl::Anchor>

From calling circle.centre(), which returns an std::shared_ptr. 从调用circle.centre(),它返回一个std :: shared_ptr。 I could change every std::shared_ptr to a boost::shared_ptr (which Boost Python plays nicely with) however the amount of code to change in pretty considerable and I'd like to use the standard library. 我可以将每个std :: shared_ptr更改为boost :: shared_ptr(Boost Python可以很好地使用)但是要改变的代码量非常可观,我想使用标准库。

The circle method is declared like this: circle方法声明如下:

const std::shared_ptr<Anchor> centre() const
{
    return Centre;
}

The anchor class like this: 像这样的锚类:

class Anchor
{
    Point Where;
    Annotation* Parent;
public:

    Anchor(Annotation* parent) :
        Parent(parent)
    {
        // Do nothing.
    }

    void update(const Renderer& renderer)
    {
        if(Parent)
        {
            Parent->update(renderer);
        }
    }

    void set(Point point)
    {
        Where = point;
    }

    Point where() const
    {
        return Where;
    }
};

And the relevant Boost Python code is: 相关的Boost Python代码是:

class_<Circle, bases<Annotation> >("Circle", init<float>())
.def("radius", &Circle::radius)
    .def("set_radius",  &Circle::set_radius)
    .def("diameter", &Circle::diameter)
    .def("top_left", &Circle::top_left)
    .def("centre", &Circle::centre);

// The anchor base class.
class_<Anchor, boost::noncopyable>("Anchor", no_init)
    .def("where", &Anchor::where);

I'm using Boost 1.48.0. 我正在使用Boost 1.48.0。 Any ideas? 有任何想法吗?

Looks like boost::python doesn't support C++ 11 std::shared_ptr. 看起来像boost :: python不支持C ++ 11 std :: shared_ptr。

If you have a look to file boost/python/converter/shared_ptr_to_python.hpp you'll find implementation of template function shared_ptr_to_python(shared_ptr<T> const& x) for boost::shared_ptr (it explain why the code works fine for boost::shared_ptr). 如果您查看文件boost / python / converter / shared_ptr_to_python.hpp,您将找到boost :: shared_ptr的模板函数shared_ptr_to_python(shared_ptr <T> const&x)的实现(它解释了为什么代码适用于boost :: shared_ptr的)。

I think you have several options: 我想你有几个选择:

  • use boost::shared_ptr (which you trying to avoid) 使用boost :: shared_ptr(你试图避免)
  • write your implementation of shared_ptr_to_python for std::shared_ptr (IMHO the best option) 为std :: shared_ptr编写shared_ptr_to_python的实现(恕我直言,最佳选择)
  • send request to boost::python developers to support std::shared_ptr 发送请求到boost :: python开发人员以支持std :: shared_ptr

Unless I've misunderstood, I think this solves your problem: 除非我误解了,否则我认为这解决了你的问题:

boost::python::register_ptr_to_python<std::shared_ptr<Anchor>>();

http://www.boost.org/doc/libs/1_57_0/libs/python/doc/v2/register_ptr_to_python.html http://www.boost.org/doc/libs/1_57_0/libs/python/doc/v2/register_ptr_to_python.html

There's a bug report for this: https://svn.boost.org/trac/boost/ticket/6545 有一个错误报告: https//svn.boost.org/trac/boost/ticket/6545

Looks like someone is working on it. 看起来有人正在努力。

A snippet from http://boost.2283326.n4.nabble.com/No-automatic-upcasting-with-std-shared-ptr-in-function-calls-td4573165.html : 来自http://boost.2283326.n4.nabble.com/No-automatic-upcasting-with-std-shared-ptr-in-function-calls-td4573165.html的摘录:

/* make boost::python understand std::shared_ptr */
namespace boost {
       template<typename T>
       T *get_pointer(std::shared_ptr<T> p)
       {
               return p.get();
       }
}

Worked for me. 为我工作。 You would define your classes like: 你可以定义你的类,如:

class_<foo, std::shared_ptr<foo>>("Foo", ...);

With this, other methods returning std::shared_ptr<foo> will Just Work. 有了这个,返回std::shared_ptr<foo>其他方法将Just Work。

Some magic may be required for virtual functions/polymorphism, that should be covered in the thread I linked to. 虚函数/多态可能需要一些魔法,应该在我链接的线程中介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM