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