|
| 1 | +// Copyright David Abrahams 2002. Permission to copy, use, |
| 2 | +// modify, sell and distribute this software is granted provided this |
| 3 | +// copyright notice appears in all copies. This software is provided |
| 4 | +// "as is" without express or implied warranty, and with no claim as |
| 5 | +// to its suitability for any purpose. |
| 6 | +#include <boost/python/module.hpp> |
| 7 | +#include <boost/python/returning.hpp> |
| 8 | +#include <boost/python/class.hpp> |
| 9 | +#include <boost/ref.hpp> |
| 10 | + |
| 11 | +using namespace boost::python; |
| 12 | + |
| 13 | +int apply_int_int(PyObject* f, int x) |
| 14 | +{ |
| 15 | + return returning<int>::call(f, x); |
| 16 | +} |
| 17 | + |
| 18 | +void apply_void_int(PyObject* f, int x) |
| 19 | +{ |
| 20 | + returning<void>::call(f, x); |
| 21 | +} |
| 22 | + |
| 23 | +struct X |
| 24 | +{ |
| 25 | + explicit X(int x) : x(x), magic(7654321) { ++counter; } |
| 26 | + X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; } |
| 27 | + ~X() { assert(magic == 7654321); magic = 6666666; x = 9999; --counter; } |
| 28 | + |
| 29 | + void set(int x) { assert(magic == 7654321); this->x = x; } |
| 30 | + int value() const { assert(magic == 7654321); return x; } |
| 31 | + static int count() { return counter; } |
| 32 | + private: |
| 33 | + void operator=(X const&); |
| 34 | + private: |
| 35 | + int x; |
| 36 | + long magic; |
| 37 | + static int counter; |
| 38 | +}; |
| 39 | + |
| 40 | +X apply_X_X(PyObject* f, X x) |
| 41 | +{ |
| 42 | + return returning<X>::call(f, x); |
| 43 | +} |
| 44 | + |
| 45 | +void apply_void_X_ref(PyObject* f, X x) |
| 46 | +{ |
| 47 | + returning<X>::call(f, boost::ref(x)); |
| 48 | +} |
| 49 | + |
| 50 | +int X::counter; |
| 51 | + |
| 52 | +BOOST_PYTHON_MODULE_INIT(callbacks_ext) |
| 53 | +{ |
| 54 | + boost::python::module("callbacks_ext") |
| 55 | + .def("apply_int_int", apply_int_int) |
| 56 | + .def("apply_void_int", apply_void_int) |
| 57 | + .def("apply_X_X", apply_X_X) |
| 58 | + .def("apply_void_X_ref", apply_void_X_ref) |
| 59 | + .add( |
| 60 | + class_<X>("X") |
| 61 | + .def_init(args<int>()) |
| 62 | + .def_init(args<X const&>()) |
| 63 | + .def("value", &X::value) |
| 64 | + .def("set", &X::set) |
| 65 | + ) |
| 66 | + .def("x_count", &X::count) |
| 67 | + ; |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | + |
0 commit comments