wrapper.qbk 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. [section boost/python/wrapper.hpp]
  2. [section Introduction]
  3. To wrap a class T such that its virtual functions can be "overridden in Python"—so that the corresponding method of a Python derived class will be called when the virtual function is invoked from C++—you must create a C++ wrapper class derived from `T` that overrides those virtual functions so that they call into Python. This header contains classes that can be used to make that job easier.
  4. [endsect]
  5. [section Class `override`]
  6. Encapsulates a Python override of a C++ virtual function. An override object either holds a callable Python object or `None`.
  7. ``
  8. namespace boost
  9. {
  10. class override : object
  11. {
  12. public:
  13. unspecified operator() const;
  14. template <class A0>
  15. unspecified operator(A0) const;
  16. template <class A0, class A1>
  17. unspecified operator(A0, A1) const;
  18. ...
  19. template <class A0, class A1, ...class An>
  20. unspecified operator(A0, A1, ...An) const;
  21. };
  22. };
  23. ``
  24. [endsect]
  25. [section Class `override` observer functions]
  26. ``
  27. unspecified operator() const;
  28. template <class A0>
  29. unspecified operator(A0) const;
  30. template <class A0, class A1>
  31. unspecified operator(A0, A1) const;
  32. ...
  33. template <class A0, class A1, ...class An>
  34. unspecified operator(A0, A1, ...An) const;
  35. ``
  36. [variablelist
  37. [[Effects][If *this holds a callable Python object, it is invoked with the specified arguments in the manner specified here. Otherwise, throws [link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set].]]
  38. [[Returns][An object of unspecified type that holds the Python result of the invocation and, when converted to a C++ type R, attempts to convert that result object to R. If that conversion fails, throws [link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set].]]
  39. ]
  40. [endsect]
  41. [section Class template `wrapper`]
  42. Deriving your wrapper class from both `T` and `wrapper<T>` makes writing that derived class easier.
  43. ``
  44. namespace boost
  45. {
  46. class wrapper
  47. {
  48. protected:
  49. override get_override(char const* name) const;
  50. };
  51. };
  52. ``
  53. [endsect]
  54. [section Class template `wrapper` observer functions]
  55. ``override get_override(char const* name) const;``
  56. [variablelist
  57. [[Requires][name is a [link ntbs].]]
  58. [[Returns][If `*this` is the C++ base class subobject of a Python derived class instance that overrides the named function, returns an override object that delegates to the Python override. Otherwise, returns an override object that holds `None`.]]
  59. ]
  60. [endsect]
  61. [section Example]
  62. ``
  63. #include <boost/python/module.hpp>
  64. #include <boost/python/class.hpp>
  65. #include <boost/python/wrapper.hpp>
  66. #include <boost/python/call.hpp>
  67. using namespace boost::python;
  68. // Class with one pure virtual function
  69. struct P
  70. {
  71. virtual ~P(){}
  72. virtual char const* f() = 0;
  73. char const* g() { return "P::g()"; }
  74. };
  75. struct PCallback : P, wrapper<P>
  76. {
  77. char const* f()
  78. {
  79. return this->get_override("f")();
  80. }
  81. };
  82. // Class with one non-pure virtual function
  83. struct A
  84. {
  85. virtual ~A(){}
  86. virtual char const* f() { return "A::f()"; }
  87. };
  88. struct ACallback : A, wrapper<A>
  89. {
  90. char const* f()
  91. {
  92. if (override f = this->get_override("f"))
  93. return f();
  94. return A::f();
  95. }
  96. char const* default_f() { return this->A::f(); }
  97. };
  98. BOOST_PYTHON_MODULE_INIT(polymorphism)
  99. {
  100. class_<PCallback,boost::noncopyable>("P")
  101. .def("f", pure_virtual(&P::f))
  102. ;
  103. class_<ACallback,boost::noncopyable>("A")
  104. .def("f", &A::f, &ACallback::default_f)
  105. ;
  106. }
  107. ``
  108. [endsect]
  109. [endsect]