【问题标题】:Boost::Python bindings for classes with inheritanceBoost::Python 为具有继承的类绑定
【发布时间】:2023-03-23 12:26:02
【问题描述】:

我试图找出一些自动生成的(使用 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?

【问题讨论】:

  • 这应该用 'pyste' 和 'boost-python' 标记。
  • @Matthew,pyste 以前被用于自动生成代码,但现在已被删除,并且支持不带它的代码。

标签: c++ python boost boost-python


【解决方案1】:

如果你想让 BP 隐式转换你的对象,你必须告诉 BP 可以隐式转换。在你的 bp::code 中添加这样的内容:

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

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

【讨论】:

  • 非常感谢,这确实奏效了。如果我执行implicitly_convertible&lt;DrawableRectangle,Drawable&gt;,它运行良好,但在implicitly_convertible&lt;DrawableBase,Drawable&gt; 上会出现段错误。哦,好吧,复制粘贴所有 Drawable 也可以。
  • 嗯。据我所知,它应该可以工作。也许段错误在包装的库中?或者可能是糟糕的互动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 2019-12-12
  • 1970-01-01
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多