Boost::Python继承类的绑定
我正在尝试找出一些自动生成的(使用Pyste)boost::python代码的问题,但到目前为止没有找到解决办法。
这里有一个C++库,叫做Magick++,它提供了两个类,Magick::Drawable
和Magick::DrawableRectangle
:
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
{ ... }
这些类被用作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_ );
我正在尝试为它制作Python的绑定,所有类都有自动生成的包装器。
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/_Image.cpp
问题是,由于从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())
>>>
有没有比在C++中写我自己的draw()包装器更好的方法,这样可以将PyObject转换为Drawable?
1 个回答
1
如果你想让BP自动帮你转换对象,你需要告诉BP这些对象是可以自动转换的。你可以在你的bp::code里加上类似下面的内容:
boost::python::implicitly_convertible<SourceType,DestType>();
我不知道怎么让Pyste做到这一点。