Boost Python official sample (II)

Source: Internet
Author: User
Tags wrapper

return value

Using return_by_value a bit like the auto keyword for C + + 11, you can make the template adaptive return value type (the return value type must be any reference or value type to be copied to the new Python object), and you can use the Return_by_value Replace copy_const_reference,copy_non_const_reference,manage_new_object and Reference_existing_object

Returns a constant object reference

Writing C + + function implementations

$ vim ref.hstruct Bar { int x; };struct Foo {   Foo(int x) { b.x = x; }   Bar const& get_bar() const { return b; } private:   Bar b;};

Writing Boost.python files

$ 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 codeusing 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>());}

Run the Python Test library file

$ python>>> import ref_ext>>> f = ref_ext.Foo(2)>>> b = f.get_bar()>>> b.x2
Return object reference

Writing C + + function implementations

$ vim ref.cppstruct Bar { int x; };struct Foo {   Foo(int x) { b.x = x; }   Bar& get_bar() { return b; } private:   Bar b;};

Writing Boost.python files

$ 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 codeusing 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>());}

Run the Python Test library file

$ python>>> import ref_ext>>> f = ref_ext.Foo(3)>>> b = f.get_bar()>>> b.x3
Returns the Heap object

Writing C + + function implementations

$ 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); }

Writing Boost.python files

$ 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 codeusing 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);}

Run the Python Test library file

$ python>>> import ref_ext>>> f = ref_ext.make_foo(3)>>> f.x3
Returning a static object

Writing C + + function implementations

$ 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;}

Writing Boost.python files

$ 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);}

Run the Python Test library file

$ 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
Enumeration

Create a Project Catalog

$ mkdir Lesson5$ cd Lesson5

Writing C + + function implementations

$ vim enum.henum color { red = 1, green = 2, blue = 8 };

Writing Boost.python files

$ 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);}

Writing CMakeLists.txt for libraries

$ vim CMakeLists.txtcmake_minimum_required(VERSION 2.8)project(enum)### 此处的动态库名必须和BOOST_PYTHON_MODULE()中定义的保持一致,即最后生成的库必须名为enum_ext.soset(enumSRC enum_wrapper.cpp)add_library(enum_ext SHARED ${enumSRC})set_target_properties(enum_ext PROPERTIES PREFIX "")#dependenciesINCLUDE(FindPkgConfig)pkg_check_modules(PYTHON REQUIRED python)include_directories(/usr/include ${PYTHON_INCLUDE_DIRS})target_link_libraries(enum_ext boost_python)

Compiling libraries

$ mkdir build$ cd build$ cmake ..$ make

Run the Python Test library file

### 在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 official sample (II)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.