boost.python sample code (Exposing Classes)


/ Published in: C++
Save to your folder(s)

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
  1. // boopyclass.cpp
  2. #include <boost/python.hpp>
  3. //-- Constructors
  4. struct World
  5. {
  6. void set(std::string msg) { this->msg = msg; }
  7. std::string greet() { return msg; }
  8. std::string msg;
  9. };
  10. class PlusOne{
  11. public:
  12. double func(double val) {
  13. return val+1;
  14. }
  15. };
  16. //-- Class Data Members
  17. struct Var
  18. {
  19. Var(std::string name) : name(name), value() {}
  20. std::string const name;
  21. float value;
  22. };
  23. //-- Class Properties
  24. struct Num
  25. {
  26. Num() : value() {}
  27. float value;
  28. float get() const {
  29. return value;
  30. }
  31. void set(float _value){
  32. value = _value;
  33. }
  34. };
  35. //-- Class virtual functions
  36. struct Base1{
  37. virtual ~Base1() {}
  38. virtual int f() =0;
  39. };
  40. struct BaseWrap1 : Base1, boost::python::wrapper<Base1>{
  41. int f(){
  42. return this->get_override("f")();
  43. }
  44. };
  45. //-- Virtual Functions with Default Implementations
  46. struct Base2{
  47. virtual ~Base2() {}
  48. virtual int f() { return 0; }
  49. };
  50. struct BaseWrap2 : Base2, boost::python::wrapper<Base2>{
  51. int f(){
  52. if (boost::python::override f = this->get_override("f"))
  53. return f(); // *note*
  54. return Base2::f();
  55. }
  56. int default_f() { return this->Base2::f(); }
  57. };
  58.  
  59. BOOST_PYTHON_MODULE(libboopyclass)
  60. {
  61. boost::python::class_<World>("World")
  62. .def("greet", &World::greet)
  63. .def("set", &World::set);
  64. boost::python::class_<PlusOne>("PlusOne")
  65. .def("func", &PlusOne::func);
  66.  
  67. boost::python::class_<Var>("Var", boost::python::init<std::string>())
  68. .def_readonly("name", &Var::name)
  69. .def_readwrite("value", &Var::value);
  70.  
  71. boost::python::class_<Num>("Num")
  72. .add_property("rovalue", &Num::get)
  73. .add_property("value", &Num::get, &Num::set);
  74.  
  75. boost::python::class_<BaseWrap1, boost::noncopyable>("Base1")
  76. .def("f", boost::python::pure_virtual(&Base1::f));
  77.  
  78. boost::python::class_<BaseWrap2, boost::noncopyable>("Base2")
  79. .def("f", &Base2::f, &BaseWrap2::default_f);
  80. }
  81.  
  82. /*
  83. // CMakeLists.txt
  84. INCLUDE_DIRECTORIES( /usr/include/python2.5 )
  85. ADD_LIBRARY(boopyclass SHARED boopyclass.cpp )
  86. TARGET_LINK_LIBRARIES(boopyclass boost_python python2.5 )
  87.  
  88. // Compile
  89. $ cmake .
  90. $ make -k
  91.  
  92. // python
  93. In [0]: import libboopyclass
  94. In [0]: p = libboopyclass.World()
  95. In [0]: p.set("howdaadfay")
  96. In [0]: p.greet()
  97. Out[0]: 'howdy'
  98. In [0]: p = libboopyclass.PlusOne()
  99. In [0]: p.func(1)
  100. Out[0]: 2.0
  101. In [0]: p=libboopyclass.Var("pi")
  102. In [0]: p.value=3.14
  103. In [0]: p.name
  104. Out[0]: 'pi'
  105. In [0]: p.value
  106. Out[0]: 3.1400001049041748
  107. In [0]: p.name="aa"
  108. <type 'exceptions.AttributeError'>
  109. In [0]: p.value=2
  110. In [0]: p.value
  111. Out[0]: 2.0
  112. In [0]: x=libboopyclass.Num()
  113. In [0]: x.value = 1
  114. In [0]: x.value
  115. Out[0]: 1.0
  116. In [0]: x.rovalue
  117. Out[0]: 1.0
  118. In [0]: x.rovalue = 1
  119. <type 'exceptions.AttributeError'>
  120. In [0]: import libboopyclass
  121. In [0]: b=libboopyclass.Base2()
  122. In [0]: class D(libboopyclass.Base2):
  123.   ....: def f(self):
  124.   ....: return 42
  125. In [0]: d=D()
  126. In [0]: b.f()
  127. Out[0]: 0
  128. In [0]: d.f()
  129. Out[0]: 42
  130. In [0]: class D(libboopyclass.Base1):
  131.   ....: def f():
  132.   ....: return 1
  133. In [0]: e=D()
  134. In [0]: e.f()
  135. <type 'exceptions.TypeError'>
  136. In [0]: class D(libboopyclass.Base1):
  137.   ....: def f(self):
  138.   ....: return 1
  139. In [0]: e=D()
  140. In [0]: e.f()
  141. Out[0]: 1
  142. */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.