1 [section boost/python/wrapper.hpp]
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.
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`.
10 class override : object
13 unspecified operator() const;
15 unspecified operator(A0) const;
16 template <class A0, class A1>
17 unspecified operator(A0, A1) const;
19 template <class A0, class A1, ...class An>
20 unspecified operator(A0, A1, ...An) const;
25 [section Class `override` observer functions]
27 unspecified operator() const;
29 unspecified operator(A0) const;
30 template <class A0, class A1>
31 unspecified operator(A0, A1) const;
33 template <class A0, class A1, ...class An>
34 unspecified operator(A0, A1, ...An) const;
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].]]
41 [section Class template `wrapper`]
42 Deriving your wrapper class from both `T` and `wrapper<T>` makes writing that derived class easier.
49 override get_override(char const* name) const;
54 [section Class template `wrapper` observer functions]
55 ``override get_override(char const* name) const;``
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`.]]
63 #include <boost/python/module.hpp>
64 #include <boost/python/class.hpp>
65 #include <boost/python/wrapper.hpp>
66 #include <boost/python/call.hpp>
68 using namespace boost::python;
70 // Class with one pure virtual function
74 virtual char const* f() = 0;
75 char const* g() { return "P::g()"; }
78 struct PCallback : P, wrapper<P>
82 return this->get_override("f")();
86 // Class with one non-pure virtual function
90 virtual char const* f() { return "A::f()"; }
93 struct ACallback : A, wrapper<A>
97 if (override f = this->get_override("f"))
102 char const* default_f() { return this->A::f(); }
105 BOOST_PYTHON_MODULE_INIT(polymorphism)
107 class_<PCallback,boost::noncopyable>("P")
108 .def("f", pure_virtual(&P::f))
111 class_<ACallback,boost::noncopyable>("A")
112 .def("f", &A::f, &ACallback::default_f)