boost.python Library Learn

The following is an excerpt from learning boost official website

Example 1. The simplest routines HelloWorld

#include <Boost / python.hpp>
 
// preparation derived function char const * the greet () { return "hello, world"; }
// Register PYTHON module hello_ext BOOST_PYTHON_MODULE(hello_ext) {
a using namespace the Boost :: Python;
  // function name is derived from the actual function bindings def(
"greet", greet); }
# Compiler directive 
G ++ -O2 -shared HelloWorld.cpp \ the PYTHONPATH \ python37.dll the -I \ the PYTHONPATH \ the include the -I \ boostPath -L \ boostPath \ lib -lboost_pytho n-xxx -o hello_ext.pyd
#Python call (note hello_ext.pyd to be in the search path PYTHON, as well as the file name and an export API function between the same name) 
Import
hello_ext Print (hello_ext.greet ())

 

Example 2. Export to a structure PYTHON

#include <the Boost / python.hpp>
 a using  namespace the Boost :: Python;
 
// World is a structure derived from (all members are public classes)
struct World { void set(std::string msg) { this->msg = msg; } std::string greet() { return msg; } std::string msg; };
// Register the module name hello PYTHON BOOST_PYTHON_MODULE(hello) {
  // register class World PYTHON class_
<World>("World") .def ( " the greet " , & World :: the greet) // register class function .def("set", &World::set)    //同上 ; }

 Compile the same way as before, calling the new module examples are as follows:

>>> import hello
>>> planet = hello.World()
>>> planet.set('howdy')
>>> planet.greet()
'howdy'

Example 3. adding a non-default initialization function parameter included in the class derivation module

#include <boost/python.hpp>
using namespace boost::python;

struct World
{
    World(std::string msg): msg(msg) {} // added constructor
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }
    std::string msg;
};

BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World", init<std::string>())
        .def("greet", &World::greet)
        .def("set", &World::set)
    ;
}
--------------------------------
the class_ < World > ( "World" , the init < STD :: String > ()) . DEF ( the init < Double , Double > ()) // class initialization function is assumed there are two double precision floating point parameters! . DEF ( "the greet" , & World :: the greet ) . DEF ( "the SET" , & World :: the SET ) ;

Example 3. ROM / writable derived class data members to PYTHON

# Header with the former

struct Where
{
    Var(std::string name) : name(name), value() {}
    std::string const name;
    float value;
};

class_<Var>("Var", init<std::string>())
    .def_readonly ( " name " , & Var :: name) // read-only member variables
    .def_readwrite ( " value " , & Var :: value); // read and write member variables

Example 4. PYTHON Boost.Python member attribute class C / C ++ to achieve

struct Num
{
    On one();
    float get() const;
    void set(float value);
    ...
};

the class_ <the Num> ( " the Num " ) 
  // read-only properties rovalue .add_property (
" rovalue " , & the Num :: GET )
  // property value read-write (read and write respectively calling a class member function get / set) .add_property(
"value", &Num::get, &Num::set);

 

Guess you like

Origin www.cnblogs.com/zbnbu/p/11027741.html