|
Boost.PythonHeader <call_method.hpp> |
<boost/python/call_method.hpp>
defines the call_method
family of overloaded
function templates, used to invoke callable attributes of Python objects
from C++.
template <class R, class A1, class A2, ... class An> R call_method(PyObject* self, char const* method, A1 const&, A2 const&, ... An const&)
R
is a pointer type, reference type,
or a complete type with an accessible copy constructorself.method(a1, a2, ...an)
in
Python, where a1
...an
are the
arguments to call_method()
, converted to Python objects.
For a complete semantic description, see this
page.R
.call_method
is critical to
implementing C++ virtual functions which are overridable in Python, as
shown by the example below.call_method
in
wrapping a class with a virtual function that can be overridden in
Python:
#include <boost/python/module.hpp> #include <boost/python/class.hpp> #include <boost/utility.hpp> #include <cstring> // class to be wrapped class Base { public: virtual char const* class_name() const { return "Base"; } virtual ~Base(); }; bool is_base(Base* b) { return !std::strcmp(b->class_name(), "Base"); } // Wrapper code begins here using namespace boost::python; // Callback class class Base_callback : public Base { public: Base_callback(PyObject* self) : m_self(self) {} char const* class_name() const { return call_method<char const*>(m_self, "class_name"); } char const* Base_name() const { return Base::class_name(); } private: PyObject* const m_self; }; using namespace boost::python; BOOST_PYTHON_MODULE(my_module) { def("is_base", is_base); class_<Base,Base_callback, noncopyable>("Base") .def("class_name", &Base_callback::Base_name) ; }
>>> from my_module import * >>> class Derived(Base): ... def __init__(self): ... Base.__init__(self) ... def class_name(self): ... return self.__class__.__name__ ... >>> is_base(Base()) # calls the class_name() method from C++ 1 >>> is_base(Derived()) 0
Revised 13 November, 2002
© Copyright Dave Abrahams 2002.