具有继承的类的Boost :: Python绑定

时间:2010-12-02 06:44:38

标签: c++ python boost boost-python

我试图用一些自动生成的(使用Pyste)boost :: python代码来解决什么问题,但到目前为止还没有运气。

有C ++库,Magick ++,提供两个类Magick::DrawableMagick::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()的参数: https://www.imagemagick.org/subversion/ImageMagick/trunk/Magick++/lib/Magick++/Image.h

// 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 个答案:

答案 0 :(得分:1)

如果你希望BP隐含地转换对象,你必须告诉BP它可以隐式转换。把这样的东西放在你的bp :: code:

boost::python::implicitly_convertible<SourceType,DestType>();

我不知道如何诱导Pyste这样做。