使用Boost的Python和放大器;的std :: shared_ptr的 [英] Using Boost Python & std::shared_ptr

查看:108
本文介绍了使用Boost的Python和放大器;的std :: shared_ptr的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让Python的提升与性病:: shared_ptr的发挥很好。目前,我收到此错误:

I'm trying to get Boost Python to play nicely with 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>

从调用circle.centre(),它返回一个std :: shared_ptr的。我可以shared_ptr的改变每的std ::到一个boost :: shared_ptr的(这升压用Python很好地扮演)code的不过量pretty改变相当大的,我想使用标准库。

From calling circle.centre(), which returns an 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.

Circle方法声明如下:

The circle method is declared like this:

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

锚类是这样的:

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;
    }
};

和相关升压Python的code是:

And the relevant Boost Python code is:

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);

我使用升压1.48.0。任何想法?

I'm using Boost 1.48.0. Any ideas?

推荐答案

貌似的boost :: Python不支持C ++ 11的std :: shared_ptr的。

Looks like boost::python doesn't support C++ 11 std::shared_ptr.

如果你看看到文件升压/蟒蛇/转换器/ shared_ptr_to_python.hpp你会发现执行模板函数shared_ptr_to_python的(shared_ptr的&LT; T&GT;常量和放大器; X)升压:: shared_ptr的(它解释了为什么code ++工程,罚款的boost :: 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).

我觉得你有几种选择:


  • 使用升压:: shared_ptr的(你试图避免)

  • 编写shared_ptr_to_python的实施的std :: shared_ptr的(恕我直言最好的选择)

  • 发送请求的boost :: Python开发者支持的std :: shared_ptr的

这篇关于使用Boost的Python和放大器;的std :: shared_ptr的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆