16

Boost Python が std::shared_ptr でうまく動作するようにしようとしています。現在、次のエラーが表示されます。

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>

std::shared_ptr を返す circle.centre() の呼び出しから。すべての std::shared_ptr を boost::shared_ptr (Boost Python がうまく機能する) に変更できますが、変更するコードの量はかなり多く、標準ライブラリを使用したいと考えています。

circle メソッドは次のように宣言されます。

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

関連する 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);

Boost 1.48.0 を使用しています。何か案は?

4

4 に答える 4

16

boost::python は C++ 11 std::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)。

いくつかのオプションがあると思います:

  • boost::shared_ptr を使用します (回避しようとしています)。
  • std::shared_ptr の shared_ptr_to_python の実装を記述します (IMHO が最適なオプションです)。
  • std::shared_ptr をサポートするために boost::python 開発者にリクエストを送信します
于 2012-12-26T20:08:37.587 に答える
6

私が誤解していない限り、これであなたの問題は解決すると思います:

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

于 2014-12-11T13:14:27.543 に答える
1

これに関するバグレポートがあります: https://svn.boost.org/trac/boost/ticket/6545

誰かが取り組んでいるようです。

于 2013-05-31T22:33:31.450 に答える
1

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

私のために働いた。クラスを次のように定義します。

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

これにより、他のメソッドが戻るstd::shared_ptr<foo>だけで機能します。

仮想関数/ポリモーフィズムにはいくつかの魔法が必要になる場合があります。これについては、リンク先のスレッドで説明する必要があります。

于 2014-10-26T11:41:13.410 に答える