导出C++类(纯虚函数和虚函数)

大致做法就是为class写一个warp,通过get_override方法检测虚函数是否被重载了,如果被重载了调用重载函数,否则调用自身实现,最后导出的时候直接导出warp类,但是类名使用class,析构函数不需要导出,因为它会被自动调用

纯虚函数

编写C++函数实现

$ vim virt.h
#include <iostream>
#include <boost/python/wrapper.hpp> // 用class会出现编译问题, 不知道是不是和boost::python里面的class产生了冲突
struct Base
{
virtual ~Base() { std::cout << "Base destructor" << std::endl; };
virtual int f() = 0;
}; struct BaseWrap : Base, boost::python::wrapper<Base>
{
int f()
{
return this->get_override("f")();
}
};

编写Boost.Python文件

$ vim virt_wrapper.cpp
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/pure_virtual.hpp>
#include "virt.h" using namespace boost::python;
using namespace boost::python::detail; BOOST_PYTHON_MODULE_INIT(virt_ext)
{
class_<BaseWrap, boost::noncopyable>("Base")
.def("f", pure_virtual(&Base::f));
}

运行python测试库文件

$ python
>>> import virt_ext
>>> def f():
... o = virt_ext.Base()
...
>>> f()
Base destructor

虚函数

编写C++函数实现

$ vim virt.h
#include <boost/python/wrapper.hpp>
#include <boost/python/call.hpp> struct Base
{
virtual ~Base() { std::cout << "Base destructor" << std::endl; };
virtual int f() = 0;
}; struct BaseWrap : Base, boost::python::wrapper<Base>
{
int f()
{
return this->get_override("f")();
}
}; struct Derived: Base
{
virtual ~Derived() { std::cout << "Derived destructor" << std::endl; }
virtual int f() { std::cout << "Override by Derived" << std::endl; return 0; }
}; struct DerivedWrap : Derived, boost::python::wrapper<Derived>
{
int f()
{
if (boost::python::override func = this->get_override("f"))
return func();
return Derived::f();
}
};

编写Boost.Python文件

$ vim virt_wrapper.cpp
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/pure_virtual.hpp>
#include "virt.h" using namespace boost::python;
using namespace boost::python::detail; BOOST_PYTHON_MODULE_INIT(virt_ext)
{
// 可以导出Base也可以不导出Base
// class_<BaseWrap, boost::noncopyable>("Base")
// .def("f", pure_virtual(&Base::f));
class_<DerivedWrap, boost::noncopyable>("Derived")
.def("f", &Derived::f);
}

运行python测试库文件

>>> import virt_ext
>>> b = virt_ext.Base()
>>> d = virt_ext.Derived()
>>> d.f()
Override by Derived
0
>>> b.f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Pure virtual function called
>>> exit()
Base destructor
Derived destructor
Base destructor

最新文章

  1. poj1002-487-3279(字符串处理)
  2. PC使用网线上网的条件下,通过PC的Wifi共享提供手机上网教程
  3. QT常用资料
  4. 初识ASP.NET 5中的Sake与KoreBuild
  5. [JSOI2008] 完美的对称
  6. bzoj4009
  7. 微信公众号PHP简单开发流程
  8. ajax2016/4/15 post与get
  9. 一篇文章搞定mongodb
  10. Python_序列化和反序列化模块
  11. [Python设计模式] 第25章 联合国维护世界和平——中介者模式
  12. Determine YARN and MapReduce Memory Configuration Settings
  13. day11 匿名函数
  14. memcache的下载与安装
  15. HDU2220 Eddy&#39;s AC难题
  16. export DataTable To Excel(C)
  17. Python类之魔术方法
  18. 小强 ROS 机器人教程
  19. MySQL练习-主外键多表查询
  20. Chrome浏览器清除页面js文件缓存的方法

热门文章

  1. ubuntu commands mysql
  2. BZOJ 3296 [USACO2011 Open] Learning Languages:并查集
  3. Jackson的用法实例分析
  4. 分享知识-快乐自己:揭秘HBase
  5. php 微信公众平台开发之微信群发信息
  6. POJ 2309 BST(二叉搜索树)
  7. 201621123014《Java程序设计》第四周学习总结
  8. mfc设置鼠标状态OnSetCursor响应函数
  9. cocos2dx &amp; cocostudio 实现模态对话框
  10. 如何实现1080P延迟低于500ms的实时超清直播传输技术