Boost Python官方样例(三)

Stella981
• 阅读 782

导出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
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
JS 对象数组Array 根据对象object key的值排序sort,很风骚哦
有个js对象数组varary\{id:1,name:"b"},{id:2,name:"b"}\需求是根据name或者id的值来排序,这里有个风骚的函数函数定义:function keysrt(key,desc) {  return function(a,b){    return desc ? ~~(ak
Stella981 Stella981
2年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这