Boost.Python实现Python C/C++混合编程

导出函数

#include<string>
#include<boost/python.hpp>using namespace std;
using namespace boost::python;char const * greet()
{return "hello,world";}BOOST_PYTHON_MODULE(hello_ext)
{def("greet", greet);
}
import hello_ext
print hello_ext.greet()

 

导出默认构造的函数的类

#include<string>
#include<boost/python.hpp>using namespace std;
using namespace boost::python;struct World
{void set(string msg) { this->msg = msg; }string greet() { return msg; }string msg;
};BOOST_PYTHON_MODULE(hello) //导出的module 名字
{class_<World>("World").def("greet", &World::greet).def("set", &World::set);
}
import hello 
planet = hello.World() # 调用默认构造函数,产生类对象
planet.set("howdy") # 调用对象的方法
print planet.greet() # 调用对象的方法

 

构造函数的导出:

#include<string>
#include<boost/python.hpp>using namespace std;
using namespace boost::python;struct World
{World(string msg):msg(msg){} //增加构造函数World(double a, double b):a(a),b(b) {} //另外一个构造函数void set(string msg) { this->msg = msg; }string greet() { return msg; }double sum_s() { return a + b; }string msg;double a;double b;
};BOOST_PYTHON_MODULE(hello) //导出的module 名字
{class_<World>("World",init<string>()) .def(init<double,double>()) // expose another construct.def("greet", &World::greet).def("set", &World::set).def("sum_s", &World::sum_s);
}
import hello
planet = hello.World(5,6)
planet2 = hello.World("hollo world")print planet.sum_s()
print planet2.greet()

 

如果不想导出任何构造函数,则使用no_init:

class_<Abstract>("Abstract",no_init)

 

类的数据成员

#include<string>
#include<boost/python.hpp>using namespace std;
using namespace boost::python;struct Var
{Var(string name):name(name),value(){}string const name;float value;
};BOOST_PYTHON_MODULE(hello_var)
{class_<Var>("Var", init<string>()).def_readonly("name", &Var::name) //只读.def_readwrite("value", &Var::value); //读写
}
import hello_varvar = hello_var.Var("hello_var")
var.value = 3.14
# var.name = 'hello' # error
print var.name

 

类的属性

// 类的属性#include<string>
#include<boost/python.hpp>using namespace std;
using namespace boost::python;struct Num
{Num(){}float get() const { return val; }void set(float val) { this->val = val; }float val;};BOOST_PYTHON_MODULE(hello_num)
{class_<Num>("Num").add_property("rovalue", &Num::get) // 对外:只读.add_property("value", &Num::get, &Num::set);// 对外读写 .value值会改变.rovalue值,存储着同样的数据。}
import hello_num
num = hello_num.Num()
num.value = 10
print num.rovalue # result: 10

 

继承

// 类的继承#include<string>
#include<iostream>
#include<boost/python.hpp>using namespace std;
using namespace boost::python;struct Base {virtual ~Base() {};virtual string getName() { return "Base"; }string str;
};struct Derived : Base {string getName() { return "Derived"; }};void b(Base *base) { cout << base->getName() << endl; };void d(Derived *derived) { cout << derived->getName() << endl; };Base * factory() { return new Derived; }/*下面的额外的代码如果去掉会报错。解决地址:http://stackoverflow.com/questions/38261530/unresolved-external-symbols-since-visual-studio-2015-update-3-boost-python-link/38291152#38291152
*/
namespace boost
{template <>Base const volatile * get_pointer<class Base const volatile >(class Base const volatile *c){return c;}
}BOOST_PYTHON_MODULE(hello_derived)
{class_<Base>("Base").def("getName", &Base::getName).def_readwrite("str", &Base::str);class_<Derived, bases<Base> >("Derived").def("getName", &Derived::getName).def_readwrite("str", &Derived::str);def("b", b);def("d", d);def("factory", factory,return_value_policy<manage_new_object>());//}
import hello_derived
derive = hello_derived.factory()
hello_derived.d(derive)

 

类的虚函数:

/*类的虚函数,实现的功能是:可以编写Python类,来继承C++类
*/
#include<boost/python.hpp>#include<boost/python/wrapper.hpp>
#include<string>
#include<iostream>using namespace boost::python;
using namespace std;struct Base
{virtual ~Base() {}virtual int f() { return 0; };
};struct BaseWrap : Base, wrapper<Base>
{int f(){if (override f = this->get_override("f"))return f(); //如果函数进行重载了,则返回重载的return Base::f(); //否则返回基类}int default_f() { return this->Base::f(); }
};BOOST_PYTHON_MODULE(hello_virtual)
{class_<BaseWrap, boost::noncopyable>("Base").def("f", &Base::f, &BaseWrap::default_f);}
import hello_virtualbase = hello_virtual.Base()
# 定义派生类,继承C++类
class Derived(hello_virtual.Base):def f(self):return 42derived = Derived()print base.f()print derived.f()

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/432125.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

swift UIActivityIndicatorView

// // ActivityIndicatorViewController.swift // UIControlDemo // // Created by on 14/12/1. // Copyright (c) 2014年 马大哈. All rights reserved. // import UIKit class ActivityIndicatorViewController: BaseViewController { var waitActivity:UIActivityIndi…

UE4打包后如何调试

在项目打包后发现有一个数组越界问题&#xff0c;然而无论是 Play in Editor或是 VS选为DebugGame后启动&#xff0c;游戏都没有任何问题&#xff0c;越界问题只在打包后出现。这里记录一下自己的Debug方法。 首先将项目以DebugGame配置打包&#xff1a; 更改打包配置&#xff…

asp.net 使用my97 datepicker实现前后两个日期的范围界定

说明&#xff1a;日期选择后&#xff0c;前面的日期小于等后面的日期&#xff0c;后面的日期大于等于前面的日期。点点看就知道了:) &#xff5e; 这里将周末日期不可选。代码如下&#xff1a; 1 <html xmlns"http://www.w3.org/1999/xhtml">2 <head runat…

james-2.3.2中的配置

james&#xff1a;1、解压缩2、先运行一遍3、修改配置 apps\james\SAR-INF\config.xml(1)postmaster(2)servername localhost --> abclocalhost j08.com --> abcj08.com 自动检查名称和IP设为false(3)注释掉RemoteAddrNotInNetwork(4)添加虚拟域名&#xff0c;指向localh…

Win7下安装配置gVim

本文根据vim官网的《Simple Steps to Help You Install gVim on Windows 7》【1】一文整理而成。 1. 下载gVim 在http://www.vim.org/download.php/pc 下找到“PC: MS-DOS and MS-Windows”&#xff0c;下载“gvim74.exe”【2】。 2. 安装gVim 1. 安装时可以选择更改目录&#…

ActiveReports 报表控件官方中文入门教程 (2)-创建、数据源、浏览以及发布

ActiveReports 报表控件官方中文入门教程 (2)-创建、数据源、浏览以及发布 原文:ActiveReports 报表控件官方中文入门教程 (2)-创建、数据源、浏览以及发布本篇文章将阐述首次使用 ActiveReports 报表控件 的方法&#xff0c;包括添加报表文件、绑定数据源以及如何发布报表等内…

C++ Lambda表达式基本用法

创建一个匿名函数并执行。采用的是配对的方括号[]。实例如下&#xff1a; 1 2 3 4 5 6 7 8 9 #include <iostream> using namespace std; int main() { []{ cout << "Hello,Worldn"; }(); } 我们也可以方便的将这个创建的匿名函数赋…

每日一测4(装箱与拆箱)

1、定义 装箱&#xff1a;用于在垃圾回收堆中存储值类型。装箱是值类型到 object 类型或到此值类型所实现的任何接口类型的隐式转换。 拆箱&#xff1a;从 object 类型到值类型或从接口类型到实现该接口的值类型的显式转换。 2、为什么要装箱&#xff1a; &#xff08;1&#x…

flex 正则表达式匹配规则

正则表达式扫描的优先级 1. 先根据空格&#xff0c;tab&#xff0c;回车分割字符串&#xff0c;作为正则表达式匹配的基础 2. 找到完全匹配的正则表达式&#xff0c;以排列在前面的优先级为高 3. 如果找不到匹配的表达式&#xff0c;那么从头开始截取字符串来查找合…

笔记本多硬盘win7下U盘安装Cnetos7引导问题!

CentOS7出来的时间已经不算短了&#xff0c;前段时间一直在虚拟机上使用&#xff0c;最近闲下来了&#xff0c;就像在自己的笔记本上装一个win7和Centos7的双系统体验以下实体机的效果&#xff0c;几经波折之后终于顺利的装上了WIN7和CentOS7的双系统&#xff01;在这里主要是想…

QString转char*的问题

QString tmp"abc"; char *p tmp.toLatin1().data()); QString tmp"abc"; char *p new char[1strlen(tmp.toLatin1().data())]; strcpy(p, tmp.toLatin1().data()); 运行模式两种方法都可以&#xff0c;调试模式&#xff0c;第一种方式无法得到QString的…

hdu 1870

水题。。。。 AC代码&#xff1a; #include <iostream> #include <queue> using namespace std; int main() {char str[1010];int i,k;while(scanf("%s",&str)!EOF){queue<char>q;for(i0;str[i]!B;i){if(str[i]()q.push(str[i]);if(str[i]))q.…

Linux Apache php MySQL 安装配置(Centos 6.4 yum安装)

一、yum准备 1.Yum&#xff08;全称为 Yellow dog Updater, Modified&#xff09;是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器。 基于RPM包管理&#xff0c;能够从指定的服务器自动下载RPM包并且安装&#xff0c;可以自动处理依赖性关系&#xff0c;并且一次安…

QComboBox 样式设置

QComboBox QAbstractItemView::item:hover {color: black;background-color: lightgreen; } 上面理论上是对鼠标划过的项目的样式设置&#xff0c;实际不管用 QComboBox QAbstractItemView { color:black; selection-background-color:yellow; background-color:white; } 上…

函数IsValid()

函数IsValid() 功能&#xff1a;检查对象变量是否已经实例化&#xff0c;即实例变量的值是否是个有效的对象句柄。 语法&#xff1a;IsValid(objectname) 参数&#xff1a;objectname:要检查的对象名。 返回值&#xff1a;Boolean。如果指定对象已经创建了对此案实例&#xff0…

获取场景中指定类的实例

for (TActorIterator<类名> It(GetWorld()); It; It) {UE_LOG(LogTemp, Warning, TEXT("%s"), *It->GetName()); }

公司用章知识

最近在走公司的财务报销流程&#xff0c;了解公司财务运作过程中的一些用到的章&#xff0c;现在把公司用章之类的知识点搜集起来&#xff0c;汇总在这里&#xff0c;留以备用。 了解总喜欢刨根问底&#xff0c;那就开始刨起来。 印章的历史 印章是融合了雕刻、书法和绘画等多种…

可变参数模板

【导读】&#xff1a;C 可变参数模板对参数进行了高度泛化&#xff0c;它能表示0到任意个数、任意类型的参数。相比C98/03&#xff0c;类模版和函数模版中只能含固定数量的模版参数&#xff0c;可变模版参数无疑是一个巨大的改进。然而由于可变模版参数比较抽象&#xff0c;使用…

职业生涯起步不要去顶级公司

很多人都希望自己能够去世界顶级的公司工作&#xff0c;比如像宝洁、IBM、沃尔玛、微软等等这些可以在全球范围内 形成垄断的巨无霸&#xff0c;按常规来说就是财富500强排名在前100名的公司&#xff0c;这些公司实力台&#xff0c;业务增长都非常惊人&#xff0c;所以很多求职…