|
| 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 | + |
| 7 | +#include <boost/python/module.hpp> |
| 8 | +#include <boost/python/class.hpp> |
| 9 | +#include <boost/python/extract.hpp> |
| 10 | +#include <boost/python/def.hpp> |
| 11 | +#include "test_class.hpp" |
| 12 | + |
| 13 | +#include <memory> |
| 14 | + |
| 15 | +using namespace boost::python; |
| 16 | + |
| 17 | +typedef test_class<> X; |
| 18 | + |
| 19 | +int look(std::auto_ptr<X> const& x) |
| 20 | +{ |
| 21 | + return (x.get()) ? x->value() : -1; |
| 22 | +} |
| 23 | + |
| 24 | +int steal(std::auto_ptr<X> x) |
| 25 | +{ |
| 26 | + return x->value(); |
| 27 | +} |
| 28 | + |
| 29 | +int maybe_steal(std::auto_ptr<X>& x, bool doit) |
| 30 | +{ |
| 31 | + int n = x->value(); |
| 32 | + if (doit) |
| 33 | + x.release(); |
| 34 | + return n; |
| 35 | +} |
| 36 | + |
| 37 | +std::auto_ptr<X> make() |
| 38 | +{ |
| 39 | + return std::auto_ptr<X>(new X(77)); |
| 40 | +} |
| 41 | + |
| 42 | +std::auto_ptr<X> callback(object f) |
| 43 | +{ |
| 44 | + std::auto_ptr<X> x(new X(77)); |
| 45 | +// call<void>(f.ptr(),x); |
| 46 | +// return std::auto_ptr<X>(new X(77)); |
| 47 | + return call<std::auto_ptr<X> >(f.ptr(), x); |
| 48 | +} |
| 49 | + |
| 50 | +std::auto_ptr<X> extract_(object o) |
| 51 | +{ |
| 52 | + return extract<std::auto_ptr<X> >(o); |
| 53 | +} |
| 54 | + |
| 55 | +BOOST_PYTHON_MODULE(auto_ptr_ext) |
| 56 | +{ |
| 57 | + class_<X, std::auto_ptr<X>, boost::noncopyable>("X", init<int>()) |
| 58 | + .def("value", &X::value) |
| 59 | + ; |
| 60 | + |
| 61 | + def("look", look); |
| 62 | + def("steal", steal); |
| 63 | + def("maybe_steal", maybe_steal); |
| 64 | + def("make", make); |
| 65 | + def("callback", callback); |
| 66 | + def("extract", extract_); |
| 67 | +} |
| 68 | + |
| 69 | +#include "module_tail.cpp" |
| 70 | + |
0 commit comments