tkf on 05/24/08

# Statistics

Viewed 410 times
Favorited by 2 user(s)

# boost.python sample code (Exposing Classes)

/ Published in: C++

Ref:
1. http://www.boost.org/doc/libs/1_35_0/libs/python/doc/tutorial/doc/html/python/exposing.html
-- code from here
2. http://d.hatena.ne.jp/niitsuma/20080108
-- ã‚³ãƒ³ãƒ‘ã‚¤ãƒ«ã®æ–¹æ³•

Copy this code and paste it in your HTML
`// boopyclass.cpp#include <boost/python.hpp>//-- Constructorsstruct World{  void set(std::string msg) { this->msg = msg; }  std::string greet() { return msg; }  std::string msg;};class PlusOne{public:  double func(double val) {    return val+1;  }};//-- Class Data Membersstruct Var{  Var(std::string name) : name(name), value() {}  std::string const name;  float value;};//-- Class Propertiesstruct Num{  Num() : value() {}  float value;  float get() const {    return value;  }  void set(float _value){    value = _value;  }};//-- Class virtual functionsstruct Base1{  virtual ~Base1() {}  virtual int f() =0;};struct BaseWrap1 : Base1, boost::python::wrapper<Base1>{  int f(){    return this->get_override("f")();  }};//-- Virtual Functions with Default Implementationsstruct Base2{  virtual ~Base2() {}  virtual int f() { return 0; }};struct BaseWrap2 : Base2, boost::python::wrapper<Base2>{  int f(){    if (boost::python::override f = this->get_override("f"))      return f(); // *note*    return Base2::f();  }  int default_f() { return this->Base2::f(); }}; BOOST_PYTHON_MODULE(libboopyclass){  boost::python::class_<World>("World")    .def("greet", &World::greet)    .def("set", &World::set);  boost::python::class_<PlusOne>("PlusOne")    .def("func", &PlusOne::func);   boost::python::class_<Var>("Var", boost::python::init<std::string>())    .def_readonly("name", &Var::name)    .def_readwrite("value", &Var::value);   boost::python::class_<Num>("Num")    .add_property("rovalue", &Num::get)    .add_property("value", &Num::get, &Num::set);   boost::python::class_<BaseWrap1, boost::noncopyable>("Base1")    .def("f", boost::python::pure_virtual(&Base1::f));   boost::python::class_<BaseWrap2, boost::noncopyable>("Base2")    .def("f", &Base2::f, &BaseWrap2::default_f);} /*// CMakeLists.txtINCLUDE_DIRECTORIES(  /usr/include/python2.5       )ADD_LIBRARY(boopyclass  SHARED boopyclass.cpp )TARGET_LINK_LIBRARIES(boopyclass boost_python python2.5 ) // Compile\$ cmake .\$ make -k // pythonIn [0]: import libboopyclassIn [0]: p = libboopyclass.World()In [0]: p.set("howdaadfay")In [0]: p.greet()Out[0]: 'howdy'In [0]: p = libboopyclass.PlusOne()In [0]: p.func(1)Out[0]: 2.0In [0]: p=libboopyclass.Var("pi")In [0]: p.value=3.14In [0]: p.nameOut[0]: 'pi'In [0]: p.valueOut[0]: 3.1400001049041748In [0]: p.name="aa"<type 'exceptions.AttributeError'>In [0]: p.value=2In [0]: p.valueOut[0]: 2.0In [0]: x=libboopyclass.Num()In [0]: x.value = 1In [0]: x.valueOut[0]: 1.0In [0]: x.rovalueOut[0]: 1.0In [0]: x.rovalue = 1<type 'exceptions.AttributeError'>In [0]: import libboopyclassIn [0]: b=libboopyclass.Base2()In [0]: class D(libboopyclass.Base2):   ....:     def f(self):   ....:         return 42In [0]: d=D()In [0]: b.f()Out[0]: 0In [0]: d.f()Out[0]: 42In [0]: class D(libboopyclass.Base1):   ....:     def f():   ....:         return 1In [0]: e=D()In [0]: e.f()<type 'exceptions.TypeError'>In [0]: class D(libboopyclass.Base1):   ....:     def f(self):   ....:         return 1In [0]: e=D()In [0]: e.f()Out[0]: 1*/`