1 | // Copyright David Abrahams 2002. |
---|
2 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
3 | // accompanying file LICENSE_1_0.txt or copy at |
---|
4 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | #include <boost/python/module.hpp> |
---|
7 | #include <boost/python/def.hpp> |
---|
8 | #include <boost/python/class.hpp> |
---|
9 | #include <boost/ref.hpp> |
---|
10 | #include <boost/python/ptr.hpp> |
---|
11 | #include <boost/python/return_value_policy.hpp> |
---|
12 | #include <boost/python/reference_existing_object.hpp> |
---|
13 | #include <boost/python/call.hpp> |
---|
14 | #include <boost/python/object.hpp> |
---|
15 | #include <boost/assert.hpp> |
---|
16 | |
---|
17 | using namespace boost::python; |
---|
18 | BOOST_STATIC_ASSERT(converter::is_object_manager<handle<> >::value); |
---|
19 | |
---|
20 | int apply_int_int(PyObject* f, int x) |
---|
21 | { |
---|
22 | return call<int>(f, x); |
---|
23 | } |
---|
24 | |
---|
25 | void apply_void_int(PyObject* f, int x) |
---|
26 | { |
---|
27 | call<void>(f, x); |
---|
28 | } |
---|
29 | |
---|
30 | struct X |
---|
31 | { |
---|
32 | explicit X(int x) : x(x), magic(7654321) { ++counter; } |
---|
33 | X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; } |
---|
34 | ~X() { BOOST_ASSERT(magic == 7654321); magic = 6666666; x = 9999; --counter; } |
---|
35 | |
---|
36 | void set(int x) { BOOST_ASSERT(magic == 7654321); this->x = x; } |
---|
37 | int value() const { BOOST_ASSERT(magic == 7654321); return x; } |
---|
38 | static int count() { return counter; } |
---|
39 | private: |
---|
40 | void operator=(X const&); |
---|
41 | private: |
---|
42 | int x; |
---|
43 | long magic; |
---|
44 | static int counter; |
---|
45 | }; |
---|
46 | |
---|
47 | X apply_X_X(PyObject* f, X x) |
---|
48 | { |
---|
49 | return call<X>(f, x); |
---|
50 | } |
---|
51 | |
---|
52 | void apply_void_X_ref(PyObject* f, X& x) |
---|
53 | { |
---|
54 | call<void>(f, boost::ref(x)); |
---|
55 | } |
---|
56 | |
---|
57 | X& apply_X_ref_handle(PyObject* f, handle<> obj) |
---|
58 | { |
---|
59 | return call<X&>(f, obj); |
---|
60 | } |
---|
61 | |
---|
62 | X* apply_X_ptr_handle_cref(PyObject* f, handle<> const& obj) |
---|
63 | { |
---|
64 | return call<X*>(f, obj); |
---|
65 | } |
---|
66 | |
---|
67 | void apply_void_X_cref(PyObject* f, X const& x) |
---|
68 | { |
---|
69 | call<void>(f, boost::cref(x)); |
---|
70 | } |
---|
71 | |
---|
72 | void apply_void_X_ptr(PyObject* f, X* x) |
---|
73 | { |
---|
74 | call<void>(f, ptr(x)); |
---|
75 | } |
---|
76 | |
---|
77 | void apply_void_X_deep_ptr(PyObject* f, X* x) |
---|
78 | { |
---|
79 | call<void>(f, x); |
---|
80 | } |
---|
81 | |
---|
82 | char const* apply_cstring_cstring(PyObject* f, char const* s) |
---|
83 | { |
---|
84 | return call<char const*>(f, s); |
---|
85 | } |
---|
86 | |
---|
87 | char const* apply_cstring_pyobject(PyObject* f, PyObject* s) |
---|
88 | { |
---|
89 | return call<char const*>(f, borrowed(s)); |
---|
90 | } |
---|
91 | |
---|
92 | char apply_char_char(PyObject* f, char c) |
---|
93 | { |
---|
94 | return call<char>(f, c); |
---|
95 | } |
---|
96 | |
---|
97 | char const* apply_to_string_literal(PyObject* f) |
---|
98 | { |
---|
99 | return call<char const*>(f, "hello, world"); |
---|
100 | } |
---|
101 | |
---|
102 | handle<> apply_to_own_type(handle<> x) |
---|
103 | { |
---|
104 | // Tests that we can return handle<> from a callback and that we |
---|
105 | // can pass arbitrary handle<T>. |
---|
106 | return call<handle<> >(x.get(), type_handle(borrowed(x->ob_type))); |
---|
107 | } |
---|
108 | |
---|
109 | object apply_object_object(PyObject* f, object x) |
---|
110 | { |
---|
111 | return call<object>(f, x); |
---|
112 | } |
---|
113 | |
---|
114 | int X::counter; |
---|
115 | |
---|
116 | BOOST_PYTHON_MODULE(callbacks_ext) |
---|
117 | { |
---|
118 | def("apply_object_object", apply_object_object); |
---|
119 | def("apply_to_own_type", apply_to_own_type); |
---|
120 | def("apply_int_int", apply_int_int); |
---|
121 | def("apply_void_int", apply_void_int); |
---|
122 | def("apply_X_X", apply_X_X); |
---|
123 | def("apply_void_X_ref", apply_void_X_ref); |
---|
124 | def("apply_void_X_cref", apply_void_X_cref); |
---|
125 | def("apply_void_X_ptr", apply_void_X_ptr); |
---|
126 | def("apply_void_X_deep_ptr", apply_void_X_deep_ptr); |
---|
127 | |
---|
128 | def("apply_X_ptr_handle_cref", apply_X_ptr_handle_cref |
---|
129 | , return_value_policy<reference_existing_object>()); |
---|
130 | |
---|
131 | def("apply_X_ref_handle", apply_X_ref_handle |
---|
132 | , return_value_policy<reference_existing_object>()); |
---|
133 | |
---|
134 | def("apply_cstring_cstring", apply_cstring_cstring); |
---|
135 | def("apply_cstring_pyobject", apply_cstring_pyobject); |
---|
136 | def("apply_char_char", apply_char_char); |
---|
137 | def("apply_to_string_literal", apply_to_string_literal); |
---|
138 | |
---|
139 | |
---|
140 | class_<X>("X", init<int>()) |
---|
141 | .def(init<X const&>()) |
---|
142 | .def("value", &X::value) |
---|
143 | .def("set", &X::set) |
---|
144 | ; |
---|
145 | |
---|
146 | def("x_count", &X::count); |
---|
147 | } |
---|
148 | |
---|
149 | #include "module_tail.cpp" |
---|