[英]Boost::Python bindings for classes with inheritance
I'm trying to figure whats wrong with some autogenerated (with Pyste) boost::python code, but have no luck so far. 我试图弄清楚一些自动生成的(使用Pyste)boost :: python代码出了什么问题,但是到目前为止还没有运气。
There is C++ library, Magick++, which provides two classes, Magick::Drawable
and Magick::DrawableRectangle
: 有C ++库Magick ++,它提供了两个类Magick::Drawable
和Magick::DrawableRectangle
:
https://www.imagemagick.org/subversion/ImageMagick/trunk/Magick++/lib/Magick++/Drawable.h https://www.imagemagick.org/subversion/ImageMagick/trunk/Magick++/lib/Magick++/Drawable.h
class MagickDLLDecl DrawableBase:
public std::unary_function<MagickCore::DrawingWand,void>
{...}
class MagickDLLDecl Drawable
{
public:
// Constructor
Drawable ( void );
// Construct from DrawableBase
Drawable ( const DrawableBase& original_ );
...
}
class MagickDLLDecl DrawableRectangle : public DrawableBase
{ ... }
These are used as arguments for Image.draw()
: https://www.imagemagick.org/subversion/ImageMagick/trunk/Magick++/lib/Magick++/Image.h 这些用作Image.draw()
参数: https : Image.draw()
// Draw on image using a single drawable
void draw ( const Drawable &drawable_ );
// Draw on image using a drawable list
void draw ( const std::list<Magick::Drawable> &drawable_ );
I'm trying to make python bindings for it, there are autogenned wrappers for all the classes, 我正在尝试为其制作python绑定,所有类都有自动生成的包装器,
http://bitbucket.org/dan.kluev/pythonmagick/src/65d45c998ef3/src/_Drawable.cpp http://bitbucket.org/dan.kluev/pythonmagick/src/65d45c998ef3/src/_Drawable.cpp
http://bitbucket.org/dan.kluev/pythonmagick/src/65d45c998ef3/src/_DrawableRectangle.cpp http://bitbucket.org/dan.kluev/pythonmagick/src/65d45c998ef3/src/_DrawableRectangle.cpp
http://bitbucket.org/dan.kluev/pythonmagick/src/65d45c998ef3/src/_Image.cpp http://bitbucket.org/dan.kluev/pythonmagick/src/65d45c998ef3/src/_Image.cpp
Problem is, due to indirect class casts from DrawableBase to Drawable, these wrappers do not work: 问题是,由于从DrawableBase到Drawable的间接类转换,这些包装器不起作用:
>>> import PythonMagick
>>> image = PythonMagick.Image()
>>> square = PythonMagick._PythonMagick.DrawableRectangle(0,0,200,200)
>>> image.draw(square)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
Image.draw(Image, DrawableRectangle)
did not match C++ signature:
draw(Magick::Image {lvalue}, std::list<Magick::Drawable, std::allocator<Magick::Drawable> >)
draw(Magick::Image {lvalue}, Magick::Drawable)
# But abstract Drawable() does work:
>>> image.draw(PythonMagick._PythonMagick.Drawable())
>>>
Is there any better way to handle this than write my own draw() wrapper in C++, which would cast PyObject into Drawable? 除了用C ++编写自己的draw()包装器(将PyObject转换为Drawable)外,还有什么更好的方法来处理此问题?
If you want BP to implicitly convert objects foy you , you must tell BP that thay are implicitly convertable. 如果要BP隐式转换对象,则必须告诉BP Thay可以隐式转换。 put something like this in your bp::code: 在您的bp :: code中放入以下内容:
boost::python::implicitly_convertible<SourceType,DestType>();
I do not know how to induce Pyste to do this. 我不知道如何诱使Pyste这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.