Return to Snippet

Revision: 6496
at May 24, 2008 16:04 by tkf


Initial Code
// boopyclass.cpp
#include <boost/python.hpp>
//-- Constructors
struct 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 Members
struct Var
{
  Var(std::string name) : name(name), value() {}
  std::string const name;
  float value;
};
//-- Class Properties
struct Num
{
  Num() : value() {}
  float value;
  float get() const {
    return value;
  }
  void set(float _value){
    value = _value;
  }
};
//-- Class virtual functions
struct 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 Implementations
struct 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.txt
INCLUDE_DIRECTORIES(  /usr/include/python2.5       )
ADD_LIBRARY(boopyclass  SHARED boopyclass.cpp )
TARGET_LINK_LIBRARIES(boopyclass boost_python python2.5 )

// Compile
$ cmake .
$ make -k

// python
In [0]: import libboopyclass
In [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.0
In [0]: p=libboopyclass.Var("pi")
In [0]: p.value=3.14
In [0]: p.name
Out[0]: 'pi'
In [0]: p.value
Out[0]: 3.1400001049041748
In [0]: p.name="aa"
<type 'exceptions.AttributeError'>
In [0]: p.value=2
In [0]: p.value
Out[0]: 2.0
In [0]: x=libboopyclass.Num()
In [0]: x.value = 1
In [0]: x.value
Out[0]: 1.0
In [0]: x.rovalue
Out[0]: 1.0
In [0]: x.rovalue = 1
<type 'exceptions.AttributeError'>
In [0]: import libboopyclass
In [0]: b=libboopyclass.Base2()
In [0]: class D(libboopyclass.Base2):
   ....:     def f(self):
   ....:         return 42
In [0]: d=D()
In [0]: b.f()
Out[0]: 0
In [0]: d.f()
Out[0]: 42
In [0]: class D(libboopyclass.Base1):
   ....:     def f():
   ....:         return 1
In [0]: e=D()
In [0]: e.f()
<type 'exceptions.TypeError'>
In [0]: class D(libboopyclass.Base1):
   ....:     def f(self):
   ....:         return 1
In [0]: e=D()
In [0]: e.f()
Out[0]: 1
*/

Initial URL

                                

Initial Description
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
-- コンパイルの方法

Initial Title
boost.python sample code (Exposing Classes)

Initial Tags

                                

Initial Language
C++