[29] | 1 | // Copyright David Abrahams 2001. |
---|
| 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 | // This module exercises the converters exposed in m1 at a low level |
---|
| 7 | // by exposing raw Python extension functions that use wrap<> and |
---|
| 8 | // unwrap<> objects. |
---|
| 9 | #include <boost/python/module.hpp> |
---|
| 10 | #include <boost/python/def.hpp> |
---|
| 11 | #include <boost/python/copy_non_const_reference.hpp> |
---|
| 12 | #include <boost/python/copy_const_reference.hpp> |
---|
| 13 | #include <boost/python/return_value_policy.hpp> |
---|
| 14 | #include "simple_type.hpp" |
---|
| 15 | |
---|
| 16 | // Get a simple (by value) from the argument, and return the |
---|
| 17 | // string it holds. |
---|
| 18 | PyObject* unwrap_simple(simple x) |
---|
| 19 | { |
---|
| 20 | return PyString_FromString(x.s); |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | // Likewise, but demands that its possible to get a non-const |
---|
| 24 | // reference to the simple. |
---|
| 25 | PyObject* unwrap_simple_ref(simple& x) |
---|
| 26 | { |
---|
| 27 | return PyString_FromString(x.s); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | // Likewise, with a const reference to the simple object. |
---|
| 31 | PyObject* unwrap_simple_const_ref(simple const& x) |
---|
| 32 | { |
---|
| 33 | return PyString_FromString(x.s); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | // Get an int (by value) from the argument, and convert it to a |
---|
| 37 | // Python Int. |
---|
| 38 | PyObject* unwrap_int(int x) |
---|
| 39 | { |
---|
| 40 | return PyInt_FromLong(x); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | // Get a non-const reference to an int from the argument |
---|
| 44 | PyObject* unwrap_int_ref(int& x) |
---|
| 45 | { |
---|
| 46 | return PyInt_FromLong(x); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | // Get a const reference to an int from the argument. |
---|
| 50 | PyObject* unwrap_int_const_ref(int const& x) |
---|
| 51 | { |
---|
| 52 | return PyInt_FromLong(x); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | // rewrap<T> extracts a T from the argument, then converts the T back |
---|
| 56 | // to a PyObject* and returns it. |
---|
| 57 | template <class T> |
---|
| 58 | struct rewrap |
---|
| 59 | { |
---|
| 60 | static T f(T x) { return x; } |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | BOOST_PYTHON_MODULE(m2) |
---|
| 64 | { |
---|
| 65 | using boost::python::return_value_policy; |
---|
| 66 | using boost::python::copy_const_reference; |
---|
| 67 | using boost::python::copy_non_const_reference; |
---|
| 68 | using boost::python::def; |
---|
| 69 | |
---|
| 70 | def("unwrap_int", unwrap_int); |
---|
| 71 | def("unwrap_int_ref", unwrap_int_ref); |
---|
| 72 | def("unwrap_int_const_ref", unwrap_int_const_ref); |
---|
| 73 | def("unwrap_simple", unwrap_simple); |
---|
| 74 | def("unwrap_simple_ref", unwrap_simple_ref); |
---|
| 75 | def("unwrap_simple_const_ref", unwrap_simple_const_ref); |
---|
| 76 | |
---|
| 77 | def("wrap_int", &rewrap<int>::f); |
---|
| 78 | |
---|
| 79 | def("wrap_int_ref", &rewrap<int&>::f |
---|
| 80 | , return_value_policy<copy_non_const_reference>() |
---|
| 81 | ); |
---|
| 82 | |
---|
| 83 | def("wrap_int_const_ref", &rewrap<int const&>::f |
---|
| 84 | , return_value_policy<copy_const_reference>() |
---|
| 85 | ); |
---|
| 86 | |
---|
| 87 | def("wrap_simple", &rewrap<simple>::f); |
---|
| 88 | |
---|
| 89 | def("wrap_simple_ref", &rewrap<simple&>::f |
---|
| 90 | , return_value_policy<copy_non_const_reference>() |
---|
| 91 | ); |
---|
| 92 | |
---|
| 93 | def("wrap_simple_const_ref", &rewrap<simple const&>::f |
---|
| 94 | , return_value_policy<copy_const_reference>() |
---|
| 95 | ); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | #include "module_tail.cpp" |
---|