File: call_method.qbk

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (82 lines) | stat: -rw-r--r-- 2,440 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
[section boost/python/call_method.hpp]
[section Introduction]
<boost/python/call_method.hpp> defines the call_method family of overloaded function templates, used to invoke callable attributes of Python objects from C++.
[endsect]
[section Function `call_method`]
``
template <class R, class A1, class A2, ... class An>
R call_method(PyObject* self, char const* method, A1 const&, A2 const&, ... An const&)
``
[variablelist
[[Requires][`R` is a pointer type, reference type, or a complete type with an accessible copy constructor]]
[[Effects][Invokes `self.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.]]
[[Returns][The result of the Python call, converted to the C++ type `R`.]]
[[Rationale][`call_method` is critical to implementing C++ virtual functions which are overridable in Python, as shown by the example below.]]
]
[endsect]
[section Example]
The following C++ illustrates the use of `call_method` in wrapping a class with a virtual function that can be overridden in Python:
C++ Module Definition
``
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/call_method.hpp>
#include <boost/python/def.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, boost::noncopyable>("Base")
        .def("class_name", &Base_callback::Base_name)
        ;

}
``
Python code:
``
>>> 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
``
[endsect]
[endsect]