Boost Python官方样例(二)
返回值
使用return_by_value有点像C++ 11的auto关键字,可以让模板自适应返回值类型(返回值类型必须是要拷贝到新的python对象的任意引用或值类型),可以使用return_by_value替换copy_const_reference、copy_non_const_reference、manage_new_object和reference_existing_object
返回常量对象引用
编写C++函数实现
$ vim ref.h
struct Bar { int x; };
struct Foo {
Foo(int x) { b.x = x; }
Bar const& get_bar() const { return b; }
private:
Bar b;
};
编写Boost.Python文件
$ vim ref_wrapper.cpp
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/return_value_policy.hpp>
#include "ref.h"
// Wrapper code
using namespace boost::python;
BOOST_PYTHON_MODULE(ref_ext)
{
class_<Bar>("Bar")
.def_readwrite("x", &Bar::x);
class_<Foo>("Foo", init<int>())
.def("get_bar", &Foo::get_bar
, return_value_policy<copy_const_reference>());
}
运行python测试库文件
$ python
>>> import ref_ext
>>> f = ref_ext.Foo(2)
>>> b = f.get_bar()
>>> b.x
2
返回对象引用
编写C++函数实现
$ vim ref.cpp
struct Bar { int x; };
struct Foo {
Foo(int x) { b.x = x; }
Bar& get_bar() { return b; }
private:
Bar b;
};
编写Boost.Python文件
$ vim ref_wrapper.cpp
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/return_value_policy.hpp>
#include "ref.h"
// Wrapper code
using namespace boost::python;
BOOST_PYTHON_MODULE(ref_ext)
{
class_<Bar>("Bar")
.def_readwrite("x", &Bar::x);
class_<Foo>("Foo", init<int>())
.def("get_bar", &Foo::get_bar
, return_value_policy<copy_non_const_reference>());
}
运行python测试库文件
$ python
>>> import ref_ext
>>> f = ref_ext.Foo(3)
>>> b = f.get_bar()
>>> b.x
3
返回堆对象
编写C++函数实现
$ vim ref.h
#include <iostream>
struct Foo {
Foo(int v) : x(v) {}
~Foo() { std::cout << "Foo destructor" << std::endl; }
int x;
};
Foo* make_foo(int x) { return new Foo(x); }
编写Boost.Python文件
$ vim ref_wrapper.cpp
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/args.hpp>
#include <boost/python/class.hpp>
#include <boost/python/manage_new_object.hpp>
#include <boost/python/return_value_policy.hpp>
#include "ref.h"
// Wrapper code
using namespace boost::python;
BOOST_PYTHON_MODULE(ref_ext)
{
def("make_foo", make_foo, return_value_policy<manage_new_object>());
class_<Foo>("Foo", init<int>())
.def_readwrite("x", &Foo::x);
}
运行python测试库文件
$ python
>>> import ref_ext
>>> f = ref_ext.make_foo(3)
>>> f.x
3
返回静态对象
编写C++函数实现
$ vim ref.h
#include <utility>
struct Singleton
{
Singleton() : x(0) {}
int exchange(int n) // set x and return the old value
{
std::swap(n, x);
return n;
}
int x;
};
Singleton& get_it()
{
static Singleton just_one;
return just_one;
}
编写Boost.Python文件
$ vim ref_wrapper.cpp
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/reference_existing_object.hpp>
#include <boost/python/return_value_policy.hpp>
#include "ref.h"
using namespace boost::python;
BOOST_PYTHON_MODULE(ref_ext)
{
def("get_it", get_it, return_value_policy<reference_existing_object>());
class_<Singleton>("Singleton")
.def("exchange", &Singleton::exchange);
}
运行python测试库文件
$ python
>>> import ref_ext
>>> s1 = ref_ext.get_it()
>>> s2 = ref_ext.get_it()
>>> id(s1) == id(s2)
False
>>> s1.exchange(42)
0
>>> s2.exchange(99)
42
枚举
创建工程目录
$ mkdir Lesson5
$ cd Lesson5
编写C++函数实现
$ vim enum.h
enum color { red = 1, green = 2, blue = 8 };
编写Boost.Python文件
$ vim enum_wrapper.cpp
#include <boost/python.hpp>
#include "enum.h"
BOOST_PYTHON_MODULE(enum_ext)
{
using namespace boost::python;
enum_<color>("color")
.value("red", red)
.value("green", green)
.value("blue", blue);
}
为库编写CMakeLists.txt
$ vim CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(enum)
### 此处的动态库名必须和BOOST_PYTHON_MODULE()中定义的保持一致,即最后生成的库必须名为enum_ext.so
set(enumSRC enum_wrapper.cpp)
add_library(enum_ext SHARED ${enumSRC})
set_target_properties(enum_ext PROPERTIES PREFIX "")
#dependencies
INCLUDE(FindPkgConfig)
pkg_check_modules(PYTHON REQUIRED python)
include_directories(/usr/include ${PYTHON_INCLUDE_DIRS})
target_link_libraries(enum_ext boost_python)
编译库
$ mkdir build
$ cd build
$ cmake ..
$ make
运行python测试库文件
### 在build目录下执行,即enum_ext.so存在的目录(可以将so移至其他目录,这样就可以在其他目录下打开python终端)
$ python
>>> import enum_ext
>>> help(enum_ext)
>>> enum_ext.color
<class 'enum_ext.color'>
>>> int(enum_ext.color.red)
1
>>> int(enum_ext.color.blue)
8
Boost Python官方样例(二)的更多相关文章
- Boost Python官方样例(三)
导出C++类(纯虚函数和虚函数) 大致做法就是为class写一个warp,通过get_override方法检测虚函数是否被重载了,如果被重载了调用重载函数,否则调用自身实现,最后导出的时候直接导出wa ...
- 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 ...
- gtk+3.0的环境配置及基于gtk+3.0的python简单样例
/********************************************************************* * Author : Samson * Date ...
- Android利用Volley异步载入数据完整具体演示样例(二)
MainActivity例如以下: package cc.y; import android.app.Activity; import android.content.Context; import ...
- ShardingSphere 知识库更新 | 官方样例集助你快速上手
Apache ShardingSphere 作为 Apache 顶级项目,是数据库领域最受欢迎的开源项目之一.经过 5 年多的发展,ShardingSphere 已获得超 14K Stars 的关注, ...
- Android清理设备内存具体完整演示样例(二)
版权声明: https://blog.csdn.net/lfdfhl/article/details/27672913 MainActivity例如以下: package cc.c; import j ...
- Boost Python学习笔记(二)
你将学到什么 如何在Python中调用C++代码 如何在C++中调用Python代码 在Python中调用C++代码 首先定义一个动物类(include/animal.h) #pragma once ...
- [b0010] windows 下 eclipse 开发 hdfs程序样例 (二)
目的: 学习windows 开发hadoop程序的配置 相关: [b0007] windows 下 eclipse 开发 hdfs程序样例 环境: 基于以下环境配置好后. [b0008] Window ...
随机推荐
- Tab支持的DHTML Window控件
带有Tab标签支持的DHTML Window控件.它使用cookies来“记忆”窗体大小,位置,哪个Tab选项被选中,window堆叠顺序.代码下载地址:http://www.huiyi8.com/ ...
- 解决编译warning:warning: ‘MeteringUnit::voltage_gain_’ will be initialized after [-Wreorder]
问题: 环境:ubuntu 12.04,g++版本4.6.3,编译目标文件时出现warnings: u1204@u1204-zhw:~/hwsvn/2sw/4prj_mips/UCP_rt5350/s ...
- loj514模拟只会猜题意
果然是道模拟... 一开始想线段树 看了一眼数据范围:“这tm不是前缀和吗” 然后水过 #include<iostream> #include<cstdio> #include ...
- uboot命令(1):mmc命令
版权声明 更新:2017-06-07博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 今天在进行Android分区修改的时候发 ...
- redisCheckMem脚本
最近维护的redis cluster需要扫描每个实例的内存使用率,首先我们需要获取实例已经使用的内存,获取实例的最大内存配额,两个值相比就能获取到内存使用比例. 实例的最大内存获取方法: $REDIS ...
- POJ1422 Air Raid 和 CH6902 Vani和Cl2捉迷藏
Air Raid Language:Default Air Raid Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9547 A ...
- 多版本Python共存时pip给指定版本的python安装package的方法
在Linux安装了多版本Python时(例如python2.7和3.6),pip安装的包不一定是用户想要的位置,此时可以用 -t 选项来指定位置. 例如目标位置是/usr/local/lib/pyth ...
- Django学习(1)——python manage.py startapp app-name新建app报错问题
作为一个刚接触python的小白,开始学习Django注定前路漫漫,记录一下学习过程中的问题和解决方案. 感谢“自强学堂”的无私奉献,根据教程安装了Django 1.9.12后,尝试新建项目,此时使用 ...
- Core Data存储数据出错(This NSPersistentStoreCoordinator has no persistent stores (unknown))
Core Data存储数据的时候崩溃,崩溃信息: reason: 'This NSPersistentStoreCoordinator has no persistent stores (unknow ...
- Poj 1077 eight(BFS+全序列Hash解八数码问题)
一.题意 经典的八数码问题,有人说不做此题人生不完整,哈哈.给出一个含数字1~8和字母x的3 * 3矩阵,如: 1 2 X 3 4 6 7 5 8 ...