-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp_base_py_derived_ext.cpp
69 lines (52 loc) · 1.62 KB
/
cpp_base_py_derived_ext.cpp
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
#include <boost/python/call_method.hpp>
#include <boost/python/class.hpp>
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>
#include <memory>
namespace {
namespace py = boost::python;
struct base {
base() : base_num(100) {}
virtual int get_num() const { return base_num; }
virtual std::shared_ptr<base> clone() const {
return std::shared_ptr<base>(new base(150));
}
virtual ~base() = default;
private:
explicit base(int num) : base_num(num) {}
int base_num;
};
inline int get_num(std::shared_ptr<base> b) { return b->get_num(); }
inline int clone_get_num(std::shared_ptr<base> b) {
std::shared_ptr<base> c = b->clone();
return (b->get_num() + 3) * 1000 + (c->get_num() + 7);
}
struct base_callback : public base {
PyObject* self;
base_callback(PyObject* self_) : self(self_) {}
base_callback(PyObject* self_, const base&) : self(self_) {}
int get_num() const override {
return py::call_method<int>(self, "get_num");
}
static int default_get_num(std::shared_ptr<base> b) {
return b->base::get_num();
}
std::shared_ptr<base> clone() const override {
return py::call_method<std::shared_ptr<base>>(self, "clone");
}
static std::shared_ptr<base> default_clone(std::shared_ptr<base> b) {
return b->base::clone();
}
};
}
BOOST_PYTHON_MODULE(rwgk_tbx_cpp_base_py_derived_ext)
{
namespace py = boost::python;
py::class_<base, std::shared_ptr<base_callback>>("base")
.def(py::init<>())
.def("get_num", &base_callback::default_get_num)
.def("clone", &base_callback::default_clone)
;
py::def("get_num", get_num);
py::def("clone_get_num", clone_get_num);
}