Boost Python官方样例(三)
导出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官方样例(三)的更多相关文章
- Boost Python官方样例(二)
返回值 使用return_by_value有点像C++ 11的auto关键字,可以让模板自适应返回值类型(返回值类型必须是要拷贝到新的python对象的任意引用或值类型),可以使用return_by_ ...
- Boost Python官方样例(一)
配置环境 $ cat /etc/os-release NAME="Ubuntu" VERSION="16.04 LTS (Xenial Xerus)" ID=u ...
- Python word_cloud 样例 标签云系列(三)
转载地址:https://zhuanlan.zhihu.com/p/20436642word_cloud/examples at master · amueller/word_cloud · GitH ...
- Python代码样例列表
扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│ Python用户推荐系统曼哈顿算法实现.py│ ...
- gtk+3.0的环境配置及基于gtk+3.0的python简单样例
/********************************************************************* * Author : Samson * Date ...
- ShardingSphere 知识库更新 | 官方样例集助你快速上手
Apache ShardingSphere 作为 Apache 顶级项目,是数据库领域最受欢迎的开源项目之一.经过 5 年多的发展,ShardingSphere 已获得超 14K Stars 的关注, ...
- Boost Python学习笔记(三)
你将学到什么 在C++中调用Python代码时的传参问题 基础类型 继续使用前面的项目,但是先修改下Python脚本(zoo.py),添加Add和Str函数,分别针对整数.浮点数和字符串参数的测试 d ...
- [b0011] windows 下 eclipse 开发 hdfs程序样例 (三)
目的: 学习windows 开发hadoop程序的配置. [b0007] windows 下 eclipse 开发 hdfs程序样例 太麻烦 [b0010] windows 下 eclipse 开发 ...
- 维特比算法(Viterbi)及python实现样例
维特比算法(Viterbi) 维特比算法 维特比算法shiyizhong 动态规划算法用于最可能产生观测时间序列的-维特比路径-隐含状态序列,特别是在马尔可夫信息源上下文和隐马尔科夫模型中.术语“维特 ...
随机推荐
- 现有exe转为服务_方式01
1.安装X.exe服务: ...>路径\X.exe /install 2.卸载X.exe服务: ...>路径\X.exe /uninstall 3.开始运行XX(程序是X.exe,服务名是 ...
- 分享知识-快乐自己:Liunx—Maven 部署步骤
第一步: 点我下载 Liunx—Maven Linux命令下载:wget [下载文件存放路径] [下载文件地址] 第二步: 上传 Maven 并解压到 指定的目录:(上传方式 xftp 或 rz ...
- C# 多线程 线程池(ThreadPool) 2 如何控制线程池?
线程池启动了,但是没有方法去控制线程池,如果子线程出现了问题,难道线程池就死了吗? 我们可以设置线程池的线程数量,进行加入任务,线程池会自动分配并且合理的执行,但是控制不了又有啥意思呢. 线程池里线程 ...
- Data URI Scheme,base64
一.从HTTP URI Scheme入手 对于 <a href="http://github.com">HTTP URI Scheme</a> 我想大家都应 ...
- BEC listen and translation exercise 41
Its advantages are that it can be used for outside activities So my recommendation I'm afraid would ...
- codeforces 631C C. Report
C. Report time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- linux命令学习笔记(41):ps命令
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前 那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程 ...
- Struts2 - 值栈(ValueStack)
1. 关于值栈: 1). helloWorld 时, ${productName} 读取 productName 值, 实际上该属性并不在 request 等域对象中, 而是从值栈中获取的. 2). ...
- Arc082_F Sandglass
Description有一个沙漏由两个上下相通玻璃球$A$和$B$构成,这两个玻璃球都含有一定量的沙子,我们暂且假定$A,B$中位于上方的玻璃球的为$U$,下方的玻璃球为$L$,则除非$U$中没有沙子 ...
- 让网站变灰的CSS代码(支持IE、FIREFOX和CHROME)(转)
方法1:支持IE <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xht ...