导出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

Boost Python官方样例(三)的更多相关文章

  1. Boost Python官方样例(二)

    返回值 使用return_by_value有点像C++ 11的auto关键字,可以让模板自适应返回值类型(返回值类型必须是要拷贝到新的python对象的任意引用或值类型),可以使用return_by_ ...

  2. Boost Python官方样例(一)

    配置环境 $ cat /etc/os-release NAME="Ubuntu" VERSION="16.04 LTS (Xenial Xerus)" ID=u ...

  3. Python word_cloud 样例 标签云系列(三)

    转载地址:https://zhuanlan.zhihu.com/p/20436642word_cloud/examples at master · amueller/word_cloud · GitH ...

  4. Python代码样例列表

    扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│       Python用户推荐系统曼哈顿算法实现.py│    ...

  5. gtk+3.0的环境配置及基于gtk+3.0的python简单样例

    /*********************************************************************  * Author  : Samson  * Date   ...

  6. ShardingSphere 知识库更新 | 官方样例集助你快速上手

    Apache ShardingSphere 作为 Apache 顶级项目,是数据库领域最受欢迎的开源项目之一.经过 5 年多的发展,ShardingSphere 已获得超 14K Stars 的关注, ...

  7. Boost Python学习笔记(三)

    你将学到什么 在C++中调用Python代码时的传参问题 基础类型 继续使用前面的项目,但是先修改下Python脚本(zoo.py),添加Add和Str函数,分别针对整数.浮点数和字符串参数的测试 d ...

  8. [b0011] windows 下 eclipse 开发 hdfs程序样例 (三)

    目的: 学习windows 开发hadoop程序的配置. [b0007] windows 下 eclipse 开发 hdfs程序样例 太麻烦 [b0010] windows 下 eclipse 开发 ...

  9. 维特比算法(Viterbi)及python实现样例

    维特比算法(Viterbi) 维特比算法 维特比算法shiyizhong 动态规划算法用于最可能产生观测时间序列的-维特比路径-隐含状态序列,特别是在马尔可夫信息源上下文和隐马尔科夫模型中.术语“维特 ...

随机推荐

  1. 一个用 vue 写的树层级组件 vue-ztree

    最近看了大神的关于vue-ztree的博客,感觉很赞,于是摘抄下来,方便自己学习,机智girl,哈哈哈O(∩_∩)O 最近由于后台管理项目的需要,页面需要制作一个无限树的需求,我第一感就想到了插件 z ...

  2. Python基础-处理时间模块

    import datetime, time # print(time.time()) # 获取当前时间戳,从unix元年开始到现在过了多少秒# print(time.sleep(19)) # 休息几s ...

  3. jmeter 多压力机并发测试过程

    要实现多台压力机并发,就必须有多台服务器上安装了jmeter程序包,首先把一台服务器作为主压力机,测试脚本放到这台机器上,主压力机与从压力机在同一局域网中,服务器之间可以ping通 第一步:在linu ...

  4. Saiku_00_资源帖

    一.精选 1.李秋 随笔分类 - pentaho 二.概述 1.Saiku + Kylin 多维分析平台探索 三.Saiku+Kylin 1.使用Saiku+Kylin构建多维分析OLAP平台 2.使 ...

  5. php获取客户端IP地址的几种方法(转)

    [php] view plain copy php获取客户端IP地址的几种方法 方法一 <?php $iipp=$_SERVER["REMOTE_ADDR"]; echo $ ...

  6. python suds 调用webservice 缓存

    在linux系统中 如果webservice更新了字段 suds调用有可能缓存以前的字段或方法,对新的字段报找不到类型 TypeNotFound,或者对 新加的方法找不到该方法的错误. 当更新或添加w ...

  7. rtmp与hls流媒体服务器搭建:ubuntu下Nginx搭建初探与rtmp-module的添加

    关键词:Nignx(http服务器):rtmp,hls(流媒体服务) 前言:感谢开源,感谢战斗民族.现在在做流媒体服务的一些工作,流媒体服务器搭建的网上教程多入牛毛,但是细细查看,发现很多同志贴上来的 ...

  8. Java Modifiers

    Private means this could only be seen within this class. Protected means "package private" ...

  9. 【JSON解析】JSON解析

    前三篇博客分别介绍了xml的三种解析方法,分别是SAX,DOM,PULL解析XML,兴趣的朋友可以去看一下这[XML解析(一)]SAX解析XML,[XML解析(二)]DOM解析XML,[XML解析(三 ...

  10. BZOJ2212:[POI2011]Tree Rotation

    浅谈线段树合并:https://www.cnblogs.com/AKMer/p/10251001.html 题目传送门:https://lydsy.com/JudgeOnline/problem.ph ...