c python boost.python_在boost python中暴露C接口

抽象C类不能以这种方式暴露给Boost.Python. Boost.Python

tutorial提供了如何公开纯虚拟函数的示例.简而言之,当使用boost :: python :: pure_virtual装饰方法时,需要创建一个包装类型,以允许C多态解析虚拟函数,虚拟函数实现将委托Python对象的层次结构中的多项解析函数.

struct BaseWrap : Base,boost::python::wrapper

{

int foo()

{

return this->get_override("foo")();

}

};

...

boost::python::class_("Base",...)

.def("foo",boost::python::pure_virtual(&Base::foo))

;

有关详细信息,当通过boost :: python :: class_公开类型时,HeldType默认为要暴露的类型,并且HeldType在Python对象中构建. class_文档规定:

Template Parameter:

T: The class being wrapped

HeldType: Specifies the type that is actually embedded in a Python object wrapping a T instance […]. Defaults to T.

因此,boost :: python :: class_< Base>将失败,因为T = Base和HeldType = Base,Boost.Python将尝试将HeldType的对象实例化为表示Base实例的Python对象.这个实例化将失败,因为Base是一个抽象类.

这是一个完整的示例,显示了使用BaseWrap类.

#include

struct Base

{

virtual int foo() = 0;

virtual ~Base() {}

};

struct Derived : public Base

{

virtual int foo()

{

return 42;

}

};

Base* get_base()

{

return new Derived;

}

namespace python = boost::python;

/// @brief Wrapper that will provide a non-abstract type for Base.

struct BaseWrap : Base,python::wrapper

{

BaseWrap() {}

BaseWrap(const Base& rhs)

: Base(rhs)

{}

int foo()

{

return this->get_override("foo")();

}

};

BOOST_PYTHON_MODULE(example)

{

python::class_("Base")

.def("foo",python::pure_virtual(&Base::foo));

;

python::def("get_base",&get_base,python::return_value_policy());

}

及其用途:

>>> import example

>>> class Spam(example.Base):

... pass

...

>>> Spam().foo()

Traceback (most recent call last):

File "",line 1,in

RuntimeError: Pure virtual function called

>>> class Egg(example.Base):

... def foo(self):

... return 100

...

>>> e = Egg()

>>> e.foo()

100

>>> d = example.get_base()

>>> d.foo()

42

在Boost.Python中暴露一个抽象类是可能的,它没有默认的初始化器(boost :: python :: no_init)和不可复制(boost :: noncopyable).缺少一个初始化器可以防止Python类型从中有效地避免重写.另外,Base :: foo()在C中由Derived实现的实现细节是无关紧要的.如果Python根本不知道一个foo()方法,那么通过def()来忽略它.

#include

struct Base

{

virtual int foo() = 0;

virtual ~Base() {}

};

struct Derived

: public Base

{

virtual int foo()

{

return 42;

}

};

struct OtherDerived

: public Base

{

virtual int foo()

{

return 24;

}

};

Base* get_base()

{

return new Derived;

}

Base* get_other_base()

{

return new OtherDerived;

}

BOOST_PYTHON_MODULE(example)

{

namespace python = boost::python;

python::class_("Base",python::no_init)

;

python::class_ >("Derived",python::no_init)

.def("foo",&Base::foo)

;

python::class_ >(

"OtherDerived",python::no_init)

;

python::def("get_base",python::return_value_policy());

python::def("get_other_base",&get_other_base,python::return_value_policy());

}

互动用途:

>>> import example

>>> b = example.get_base()

>>> b.foo()

42

>>> b = example.get_other_base()

>>> b.foo()

Traceback (most recent call last):

File "",in

AttributeError: 'OtherDerived' object has no attribute 'foo'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值