c++ - Boost python 和虚拟类

标签 c++ virtual boost-python

我正在努力将一些现有的 C++ 代码暴露给 Python。

我有一个虚拟类:

class SomeVirtualClass {
public:
    SomeVirtualClass();
    virtual ~SomeVirtualClass();
    virtual SomeVirtualClass *clone() const = 0;
    virtual SomeVirtualClassType getType() const = 0;
    // SomeVirtualClassType is a descriptive enum
    virtual std::string getSomeString() const = 0;
private:
    //some other things here
}

和一个使用虚拟类型的类:

class A {
    public:
        SomeVirtualClass getThisThing();
    }

我需要能够在 python 中使用 A 中的 getThisThing。该方法可以返回从 SomeVirtualClass 派生的多个类之一。使用 boost 文档和几个类似的 stackoverflow 问题,我想出了:

namespace bp = boost::python
class SomeVirtualClassWrap : public SomeVirtualClass,
                         public bp::wrapper<SomeVirtualClass> {
    public:
    std::string getSomeString() {
        return this->get_override("getSomeString")();
    }

    SomeVirtualClass *clone() {
        return this->get_override("clone")();
    }

    SomeVirtualClassType getType() {
        return this->get_override("getType")();   
    }
};

作为虚拟类的包装器并且:

bp::class_ <SomeVirtualClassWrap, boost::noncopyable>("SomeVirtualClass", no_init)
        .def("GetSomeString", bp::pure_virtual(&SomeVirtualClass::getSomeString))
        .def("Clone", bp::pure_virtual(&SomeVirtualClass::clone))
        .def("GetType", bp::pure_virtual(&SomeVirtualClass::getType));

作为python暴露。

供引用 A 类也暴露了:

bp::class_<A>("A") {
    .def("GetThisThing", &A::getThisThing)
}

当我尝试编译时,我得到:

error: cannot declare field 'boost::python::objects::value_holder<SomeVirtualClassWrap>::m_held' to be of abstract type 'SomeVirtualClassWrap'
src/lib/objects/sub_mtrl/rebarmodule.c:412:7: note:   because the following virtual functions are pure within 'SomeVirtualClassWrap':
ThisIsAHeader.h:42:27: note:     virtual SomeVirtualClass* SomeVirtualClass::clone() const
ThisIsAHeader.h:43:30: note:     virtual SomeVirtualClassType SomeVirtualClass::getType() const
ThisIsAHeader.h:44:25: note:     virtual std::string SomeVirtualClass::getSomeString() const
In file included from tools/boost-for-sds2-2016/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:47:0,
                 from tools/boost-for-sds2-2016/include/boost/python/object/value_holder.hpp:50,
                 from tools/boost-for-sds2-2016/include/boost/python/object/class_metadata.hpp:11,
                 from tools/boost-for-sds2-2016/include/boost/python/class.hpp:23,
                 from tools/boost-for-sds2-2016/include/boost/python.hpp:18

我需要能够从 Python 调用 A.GetThisThing() 并处理该对象。如果可能的话,我宁愿保留现有代码。

编辑:根据评论,我将 no_init 添加到 python 暴露代码中,并且清除了有关纯方法的错误。现在我剩下:

error: no match for call to '(const boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<SomeVirtualClass*>) (SomeVirtualClass*)

编辑:从 Python 暴露中删除 *clone 方法清除了该错误。事实证明我根本不需要那个。只是处理转换错误。

最佳答案

根据 Luka Rahne 的评论,添加 no_init 修复了编译错误:

bp::class_ <SomeVirtualClassWrap, boost::noncopyable>("SomeVirtualClass", no_init)
        .def("GetSomeString", bp::pure_virtual(&SomeVirtualClass::getSomeString))
        .def("Clone", bp::pure_virtual(&SomeVirtualClass::clone))
        .def("GetType", bp::pure_virtual(&SomeVirtualClass::getType));

特别感谢 n.m.以及。

关于c++ - Boost python 和虚拟类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40508667/

相关文章:

c++ - C++中的虚拟变量?

c# - 为什么在 Entity Framework 模型定义中使用 'virtual' 作为类属性?

c++ - Boost python 和 Mac Mavericks

c++ - 从其基类列表中调用重写的函数?

python - 使用 boost python 从 C++ 为 python 中的类成员变量赋值

c++ - 通过引用 : TypeError: No to_python (by-value) converter found for C++ type: 调用 Boost.Python

C++通过继承在具有 protected 构造函数的基类堆上分配对象

c++ - 获取电池设备名称

c++ - 在 linux 中关闭 udp 套接字后 recv 不返回

c++ - 从另一个类 C++ 的构造函数初始化二维数组