[英]Using Boost Python & std::shared_ptr
我試圖讓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>
從調用circle.centre(),它返回一個std :: shared_ptr。 我可以將每個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。 有任何想法嗎?
看起來像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::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
有一個錯誤報告: https : //svn.boost.org/trac/boost/ticket/6545
看起來有人正在努力。
/* 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>
其他方法將Just Work。
虛函數/多態可能需要一些魔法,應該在我鏈接的線程中介紹。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.