【问题标题】:Exposing virtual member functions from C++ to Python using boost::python使用 boost::python 将虚拟成员函数从 C++ 公开到 Python
【发布时间】:2015-11-02 11:42:03
【问题描述】:

我尝试向 python 公开两个不同的类,但我没有让它编译。我尝试按照 boost::python 示例进行操作,效果很好。但是,如果我尝试为我的类编写包装类,它就行不通了。我在下面提供了两个最小的示例:

struct Base
{
    virtual ~Base() {}
    virtual std::unique_ptr<std::string> f() = 0;
};

struct BaseWrap : Base, python::wrapper<Base>
{
    std::unique_ptr<std::string> f()
    {
        return this->get_override("f")();
    }
};

struct Base
{
    virtual ~Base() {}
    virtual void f() = 0;
};

struct BaseWrap : Base, python::wrapper<Base>
{
    void f()
    {
        return this->get_override("f")();
    }
};

第一个由于唯一指针而无法编译(我认为 boost::python 不使用唯一指针?),第二个示例抱怨 void 函数内的 return 语句。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 我明白为什么这似乎是重复的。但是我已经红了这个帖子。纯虚函数似乎是我的问题。我不知道如何编写包装类。在可能的副本中有什么关于这个的吗?
  • 再读一遍。您的问题是 unique_ptr,而不是虚函数。
  • 如果你知道怎么做,请帮助我.. 我已经尝试使用该帖子中的解决方案.. 我必须编写我的包装器,get_override 函数不会转换 @ 987654325@ 那么我必须将什么传递给get_override 函数?
  • 你不能使用 unique-ptr,我认为链接的答案很清楚。为什么需要 unique_ptr?为什么不直接按值返回字符串?

标签: python c++ boost boost-python


【解决方案1】:

示例无法编译,因为:

  • 第一个示例尝试将未指定的类型(override::operator() 的返回类型)转换为不兼容的类型。特别是,Boost.Python 目前不支持std::unique_ptr,因此不会转换为它。
  • 当调用函数声明返回void时,第二个示例尝试返回上述未指定类型。

从 Python 的角度来看,字符串是不可变的,尝试将字符串的所有权从 Python 转移到 C++ 违反了语义。但是,可以在 C++ 中创建字符串的副本,并将复制的字符串的所有权传递给 C++。例如:

std::unique_ptr<std::string> BaseWrap::f()
{
  // This could throw if the Python method throws or the Python
  // method returns a value that is not convertible to std::string.
  std::string result = this->get_override("f")();

  // Adapt the result to the return type.
  return std::unique_ptr<std::string>(new std::string(result));
}

this-&gt;get_override("f")() 返回的对象具有未指定的类型,但可用于转换为 C++ 类型。如果 Python 抛出,覆盖的调用将抛出,如果从 Python 返回的对象不能转换为 C++ 类型,则转换为 C++ 类型将抛出。


这里有一个完整的例子demonstrating 两种方法将返回的 Python 对象适配为 C++ 对象。如上所述,可以使用override 转换。或者,可以使用boost::python::extract&lt;&gt;,允许在执行转换之前检查转换是否会失败:

#include <memory> // std::unique_ptr
#include <boost/algorithm/string.hpp> // boost::to_upper_copy
#include <boost/python.hpp>

struct base
{
  virtual ~base() {}
  virtual std::unique_ptr<std::string> perform() = 0;
};

struct base_wrap : base, boost::python::wrapper<base>
{
  std::unique_ptr<std::string> perform()
  {
    namespace python = boost::python;
    // This could throw if the Python method throws or the Python
    // method returns a value that is not convertible to std::string.
    std::string result = this->get_override("perform")();

    // Alternatively, an extract could be used to defer extracting the
    // result.
    python::object method(this->get_override("perform"));
    python::extract<std::string> extractor(method());

    // Check that extractor contains a std::string without throwing.
    assert(extractor.check());

    // extractor() would throw if it did not contain a std::string.
    assert(result == extractor());

    // Adapt the result to the return type.
    return std::unique_ptr<std::string>(new std::string(result));
  }
};

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  python::class_<base_wrap, boost::noncopyable>("Base", python::init<>())
    .def("perform", python::pure_virtual(&base::perform))
    ;

  python::def("make_upper", +[](base* object) {
    auto result = object->perform(); // Force dispatch through base_wrap.
    assert(result);
    return boost::to_upper_copy(*result);
  });
}

互动使用:

>>> import example
>>> class Derived(example.Base):
...     def perform(self):
...         return "abc"
...        
>>> derived = Derived()
>>> assert("ABC" == example.make_upper(derived))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-18
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多